Snake via Python Turtle

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ธ.ค. 2024

ความคิดเห็น • 1

  • @zhengli2589
    @zhengli2589  2 ปีที่แล้ว

    I strongly suggest everyone to try this optional Task for fun: Moving a black square around.
    Please refer to the demo in the lecture video and try to move the black (or different color) square along different directions.
    You may imagine this optional task in the context of this game Snake. But you are developing a never-growing snake.
    Moving a black square is actually changing colors of different squares on a map. As the first step, you may try to make the same square “flash”, i.e. changing the color of the same square.
    Please read and copy the following code of flashing a square to play a bit. Then try to “move” the square by yourself.
    # Flashing a square
    import time
    import turtle
    turtle.TurtleScreen._RUNNING = True
    turtle.tracer(0, 0)
    myPen=turtle.Turtle()
    int_edge = 20
    # Ten times flashing
    for i in range(10):
    # Start drawing a square in black
    myPen.begin_fill()
    myPen.penup()
    myPen.goto(0,0) # Go to a particular position as the bottom left corner of the square
    myPen.fillcolor("black")
    myPen.pendown()
    # Drawing four edges of the square
    for k in range(4):
    myPen.forward(int_edge)
    myPen.left(90)
    myPen.end_fill()
    turtle.update()
    # Finish drawing the square in black
    time.sleep(0.5)
    # Start drawing a square in white
    myPen.begin_fill()
    myPen.penup()
    myPen.goto(0,0) # Go to a particular position as the bottom left corner of the square
    myPen.fillcolor("white")
    myPen.pendown()
    # Drawing four edges of the square
    for k in range(4):
    myPen.forward(int_edge)
    myPen.left(90)
    myPen.end_fill()
    turtle.update()
    # Finish drawing the square in white
    time.sleep(0.5)
    myScreen = turtle.Screen()
    myScreen.exitonclick()