首页 > 解决方案 > 我正在使用乌龟制作蛇游戏,但在制作程序功能时遇到问题,因为它没有移动

问题描述

#importing 
import turtle
import random
import time

def snake_game():
      #making head global so it can be used in other functions
      global head
      #scores
      score = 0
      high_score = 0

      #setting up screen
      wn = turtle.Screen()
      wn.title("Snake Game")
      wn.bgcolor('yellow')
      wn.setup(width=600, height=600)
      wn.tracer(0)

      # making snake head
      head = turtle.Turtle()
      head.speed(0)
      head.shape("square")
      head.color("black")
      head.penup()
      head.goto(0,0)
      head.direction = "stop"

      #snake food
      food= turtle.Turtle()
      food.speed(0)
      food.shape("square")
      food.color("red")
      food.penup()
      food.goto(0,100)

      segments = []

      #scoreboards
      sc = turtle.Turtle()
      sc.speed(0)
      sc.shape("square")
      sc.color("black")
      sc.penup()
      sc.hideturtle()
      sc.goto(0,260)
      sc.write("score: 0  High score: 0", align = "center", font=("ds-digital", 24, "normal"))
        
      #keyboard bindings
      wn.listen()
      wn.onkeypress(go_up, "w")
      wn.onkeypress(go_down, "s")
      wn.onkeypress(go_left, "a")
      wn.onkeypress(go_right, "d")

      #MainLoop
      while True:
            wn.update()
            #check collision with border area
            if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:
                   time.sleep(1)
                   head.goto(0,0)
                   head.direction = "stop"
                   #hide the segments of body
                   for segment in segments:
                           segment.goto(1000,1000) #out of range
                           #clear the segments
                           segments.clear()

                           #reset score
                           score = 0

                           #reset delay
                           delay = 0.1

                           sc.clear()
                           sc.write("score: {}  High score: {}".format(score, high_score), align="center", font=("ds-digital", 24, "normal"))


            #check collision
            if head.distance(food) <20:
                     # move the food to random place
                     x = random.randint(-290,290)
                     y = random.randint(-290,290)
                     food.goto(x,y)

                      #add a new segment to the head
                     new_segment = turtle.Turtle()
                     new_segment.speed(0)
                     new_segment.shape("square")
                     new_segment.color("black")
                     new_segment.penup()
                     segments.append(new_segment)
                     #shorten the delay
                     delay -= 0.001
                     #increase the score
                     score += 10
                     if score > high_score:
                           high_score = score
                      sc.clear()
                      sc.write("score: {}  High score: {}".format(score,high_score), align="center", font=("ds-digital", 24, "normal")) 
             
           #move the segments in reverse order
           for index in range(len(segments)-1,0,-1):
                    x = segments[index-1].xcor()
                    y = segments[index-1].ycor()
                    segments[index].goto(x,y)
            #move segment 0 to head
           if len(segments)>0:
               x = head.xcor()
               y = head.ycor()
               segments[0].goto(x,y)
           
            move()
            for segment in segments:
                  if segment.distance(head)<20:
                        time.sleep(1)
                        head.goto(0,0)
                        head.direction = "stop"

                       #hide segments
                       for segment in segments:
                             segment.goto(1000,1000)
                        segments.clear()
                        score = 0
                        delay = 0.1

        #update the score     
                        sc.clear()
                        sc.write("score: {}  High score: {}".format(score,high_score), align="center", font=("ds-digital", 24, "normal"))
       

           time.sleep(delay)
     wun.mainloop()
def go_up():
     if head.direction != "down":
            head.direction = "up"
def go_down():
     if head.direction != "up":
            head.direction = "down"
def go_left():
     if head.direction != "right":
            head.direction = "left"
def go_right():
     if head.direction != "left":
            head.direction = "right"
def move():
     if head.direction == "up":
            y = head.ycor()
            head.sety(y+20)
     if head.direction == "down":
            y = head.ycor()
            head.sety(y-20)
     if head.direction == "left":
            x = head.xcor()
            head.setx(x-20)
     if head.direction == "right":
            x = head.xcor()
           head.setx(x+20)

好吧,只要函数snake_game中的代码是它正在移动的主程序,它就可以作为普通程序正常工作,但是我必须将此游戏与其他游戏一起附加。我必须使用函数来调用这个游戏,所以当我将它转换为函数时它没有移动。代码正在运行,方点也出现了,但在按键时蛇没有移动。我不知道哪里出错了。

标签: pythontkinterpython-turtle

解决方案


推荐阅读