Simple Analog Clock in Python 3 Part 3: Drawing the Hands

Поделиться
HTML-код
  • Опубликовано: 20 окт 2024

Комментарии • 37

  • @wildpat03
    @wildpat03 5 лет назад +1

    Very nice and fun exercise, loved it! I'm learning Python (JS dude here) and this is a great help, thank you Christian.
    The only point which could be mentioned is the code for drawing the hands could be DRYer. Here's what I refactored:
    ```
    # Draw the hands
    # Each tuple in list hands describes the color, the length
    # and the divisor for the angle
    hands = [("white", 80, 12), ("blue", 150, 60), ("red", 110, 60)]
    time_set = (hr, mn, sec)
    for hand in hands:
    time_part = time_set[hands.index(hand)]
    angle = (time_part/hand[2])*360
    pen.penup()
    pen.goto(0, 0)
    pen.color(hand[0])
    pen.setheading(90)
    pen.rt(angle)
    pen.pendown()
    pen.fd(hand[1])
    ```

    • @wildpat03
      @wildpat03 5 лет назад

      @@TokyoEdTech Understood. Although one could say that it's never too early to learn good coding practice, as refactoring and DRY code are essential skills in real world. Plus it's another place to apply loops, another pillar of any coding language.

  • @jiexiangshen5839
    @jiexiangshen5839 6 лет назад +1

    very good channel! thank you make these great tutorial!

  • @teacherinthailan6441
    @teacherinthailan6441 3 года назад

    Excellent lesson. Thank you so much.

  • @ltlbruh
    @ltlbruh Год назад +1

    Hey, just asking is this code still up to date?

    • @TokyoEdTech
      @TokyoEdTech  Год назад

      I think it will still work - there haven't been that many changes to the turtle module. If you run into any problems let me know.

  • @thechronics4380
    @thechronics4380 4 года назад +2

    Bring him to 100k subs

  • @joneso.p3045
    @joneso.p3045 6 лет назад +1

    Hello!
    Im really enjoying all ur tutorials, and im enjoy making simple python games. I was wondering if u could make a tutorial on how to make the classic «snake» game :-)

  • @shilpajalli8431
    @shilpajalli8431 6 лет назад +1

    When it is 2.45PM the hour hand should not be on 2 o’clock but somewhere between 2 and 3 o’clock. How to make this to happen, can u help me?

  • @thechronics4380
    @thechronics4380 4 года назад

    What does pen.rt (angle) do

  • @இயற்கை-வ1ல
    @இயற்கை-வ1ல 2 года назад

    hi
    tanx for the tutorial but in the lines *angle = (h / 12)*360* it shows* "h" is not defined*

    • @TokyoEdTech
      @TokyoEdTech  2 года назад

      It really depends on your code - you'd have to share it for me to take a look and help you.

  • @Mr123fairlane
    @Mr123fairlane 5 лет назад

    That's right man get those bugs out

  • @thechronics4380
    @thechronics4380 4 года назад

    Hey do u know the best way to start learning python for eventually using it in robotics and not games?

    • @thechronics4380
      @thechronics4380 4 года назад

      Or just learning python for robotics

    • @thechronics4380
      @thechronics4380 4 года назад

      Christian Thompson thanks. I never had a RUclipsd actually reply to my comments. This comment is really important because I want to end up using python for robotics and I was struggling on the right path to go towards it. So thanks

    • @thechronics4380
      @thechronics4380 4 года назад

      Christian Thompson good thing math is my best subject and favorite. But I’m just a freshman in highschool that likes to code so I’m not quite ready for that yet.

  • @wolfcraftianmusic4316
    @wolfcraftianmusic4316 3 года назад

    It is shwoing the h,m,s has an undefined variable plzz helppp

    • @TokyoEdTech
      @TokyoEdTech  3 года назад

      I’m happy to help, but I need to see the actual code. Please share your code via pastebin.com link so I can download and test it. To learn how, please watch this video and then get back to me: ruclips.net/video/L6AwVuu6O3Y/видео.html Keep on codin’!

  • @lunashi_e
    @lunashi_e 4 года назад

    Can you make custom hands?

    • @lunashi_e
      @lunashi_e 4 года назад

      @@TokyoEdTech how?

  • @Stellarspace95
    @Stellarspace95 5 лет назад +1

    I'm terrible at math, I couldn't understand the equation angle = (h / 12) * 360.. and how it splits the clock in 12 parts but I understand when it's written backwards like angle = (360 / 12) * h. for some reason I was stumped for a good 15 minutes trying to understand the method I almost wanted to just memorize it instead
    edit: just realized I didn't thank you for the instructional, the video was very helpful

  • @siddharthbathri6929
    @siddharthbathri6929 5 лет назад

    Please give source

  • @shilpajalli8431
    @shilpajalli8431 6 лет назад

    How to use numbers in clock? can anybody help

  • @qashrina4402
    @qashrina4402 5 лет назад

    it keeps telling me that h is not defined

    • @qashrina4402
      @qashrina4402 5 лет назад

      @@TokyoEdTech #Analog clock
      import turtle
      import time
      #Window
      wn = turtle.Screen()
      wn.setup(width=750, height=750)
      wn.bgcolor("light blue")
      wn.title("Analog Clock")
      wn.tracer(0)

      #Clock Face
      myTurtle = turtle.Turtle()
      myTurtle.speed(0)
      myTurtle.hideturtle()
      def draw_clock(h, m, s, myTurtle):
      myTurtle.penup()
      myTurtle.goto(0,360)
      myTurtle.setheading(180)
      myTurtle.color("black")
      myTurtle.pensize(3)
      myTurtle.pendown()
      myTurtle.circle(360)
      #Lines on clock for hours
      myTurtle.penup()
      myTurtle.goto(0,0)
      myTurtle.setheading(90)
      for _ in range(12): #This is just the centre of the clockface
      myTurtle.fd(2)
      myTurtle.pendown()
      myTurtle.fd(20)
      myTurtle.penup()
      myTurtle.goto(0,0)
      myTurtle.rt(30)
      for _ in range(12): #This adds the no. lines to the clockface
      myTurtle.fd(340)
      myTurtle.pendown()
      myTurtle.fd(20)
      myTurtle.penup()
      myTurtle.goto(0,0)
      myTurtle.rt(30)
      #Clock hands Hours
      myTurtle.color("pink")
      myTurtle.pensize(3)
      def Draw_HourHand(myTurtle):
      myTurtle.penup()
      myTurtle.goto(0,0)
      myTurtle.setheading(90)
      myTurtle.pendown()
      myTurtle.fd(200)
      angle = (h / 12) * 360
      myTurtle.rt(angle)
      myTurtle.penup()
      myTurtle.goto(0,0)
      myTurtle.clear()
      wn.update()
      myTurtle.pendown()
      myTurtle.fd(200)

      Draw_HourHand(myTurtle)
      #Clock hands Minutes
      myTurtle.color("purple")
      def Draw_MinuteHand(myTurtle):
      myTurtle.penup()
      myTurtle.goto(0,0)
      myTurtle.setheading(180)
      myTurtle.pendown()
      myTurtle.fd(250)

      Draw_MinuteHand(myTurtle)
      #Clock Hands Seconds
      myTurtle.color("dark blue")
      def Draw_SecondsHand(myTurtle):
      myTurtle.penup()
      myTurtle.goto(0,0)
      myTurtle.setheading(210)
      myTurtle.pendown()
      myTurtle.fd(150)
      Draw_SecondsHand(myTurtle)
      while True:
      h = int(time.strftime("%I"))
      m = int(time.strftime("%M"))
      s = int(time.strftime("%S"))
      draw_clock(h, m, s, myTurtle)
      wn.update()
      time.sleep(1)


      wn.mainloop()
      error: name h is not defined

    • @qashrina4402
      @qashrina4402 5 лет назад

      @@TokyoEdTech Don't worry I changed my code completely so this is no longer an issue.

  • @coobypop5532
    @coobypop5532 4 года назад

    Please help him to reach 100k