使用海龟的贪吃蛇游戏 (Python)
Snake Game (Python) using Turtle
所以我一直在做我的项目,就是在 Python 中制作贪吃蛇游戏,我的蛇基本上吃圆形食物,它也可以穿过墙壁它玩起来更容易,我想我可能打错了东西或者可能是版本问题。我也看过很多 Youtube 视频,但没有得到任何帮助。谁能告诉我我在下面的代码中犯的错误?
Python 使用 Turtle 的贪吃蛇游戏
import random
import time
import turtle
delay=0.1
score=0
highestscore=0
bodies=[]
s=turtle.Screen()
s.bgcolor("black")
s.setup(width=600, height=600)
s.title("Snake Game")
s.tracer(0)
head=turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("white")
head.fillcolor("red")
head.goto(0,0)
head.penup()
head.direction="stop"
food=turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("white")
food.fillcolor("yellow")
food.penup()
food.ht()
food.goto(0,150)
food.st()
sb=turtle.Turtle()
sb.shape("square")
sb.fillcolor("blue")
sb.penup()
sb.ht()
sb.goto(0,280)
sb.write("Score: 0 Highest Score: 0")
def moveup():
if head.direction!="down":
head.direction="up"
def movedown():
if head.direction!="up":
head.direction="down"
def moveleft():
if head.direction!="right":
head.direction="left"
def moveright():
if head.direction!="left":
head.direction="right"
def movestop():
head.direction="stop"
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)
s.listen()
s.onkey(moveup,"Up")
s.onkey(movedown,"Down")
s.onkey(moveleft,"Left")
s.onkey(moveright,"Right")
s.onkey(movestop,"space")
while True:
s.update()
if head.xcor()>290:
head.setx(-290)
if head.ycor()>290:
head.sety(-290)
if head.ycor()<-290:
head.sety(290)
if head.xcor()<-290:
head.setx(290)
if head.distance(food)<20:
x=random.randint(290,-290)
y=random.randint(290,-290)
food.goto(x,y)
body=turtle.Turtle()
body.speed(0)
body.shape("square")
body.color("white")
body.fillcolor("green")
body.penup()
bodies.append(body)
score+=10
delay+=0.004
if score>highestscore:
highestscore=score
sb.clear()
sb.write("Score:{} Highest Score: {}".format(score,highestscore))
for index in range(len(bodies)-1,0,-1):
x=bodies[index-1].xcor()
y=bodies[index-1].ycor()
bodies[index].goto(x,y)
if len(bodies)>0:
x=head.xcor()
y=head.ycor()
bodies[0].goto(x,y)
move()
for body in bodies:
if body.distance(head)<20:
time.sleep(1)
head.goto(0,0)
head.direction="stop"
for body in bodies:
body.goto(1000,1000)
bodies.clear()
score=0
delay=0.1
sb.clear()
sb.write("Score:{} Highest Score: {}".format(score,highestscore))
time.sleep(delay)
s.mainloop()
让你的游戏无法运行(吃东西)的是这两行:
x=random.randint(290,-290)
y=random.randint(290,-290)
参数需要反过来:
x=random.randint(-290, 290)
y=random.randint(-290, 290)
所以我一直在做我的项目,就是在 Python 中制作贪吃蛇游戏,我的蛇基本上吃圆形食物,它也可以穿过墙壁它玩起来更容易,我想我可能打错了东西或者可能是版本问题。我也看过很多 Youtube 视频,但没有得到任何帮助。谁能告诉我我在下面的代码中犯的错误?
Python 使用 Turtle 的贪吃蛇游戏
import random
import time
import turtle
delay=0.1
score=0
highestscore=0
bodies=[]
s=turtle.Screen()
s.bgcolor("black")
s.setup(width=600, height=600)
s.title("Snake Game")
s.tracer(0)
head=turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("white")
head.fillcolor("red")
head.goto(0,0)
head.penup()
head.direction="stop"
food=turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("white")
food.fillcolor("yellow")
food.penup()
food.ht()
food.goto(0,150)
food.st()
sb=turtle.Turtle()
sb.shape("square")
sb.fillcolor("blue")
sb.penup()
sb.ht()
sb.goto(0,280)
sb.write("Score: 0 Highest Score: 0")
def moveup():
if head.direction!="down":
head.direction="up"
def movedown():
if head.direction!="up":
head.direction="down"
def moveleft():
if head.direction!="right":
head.direction="left"
def moveright():
if head.direction!="left":
head.direction="right"
def movestop():
head.direction="stop"
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)
s.listen()
s.onkey(moveup,"Up")
s.onkey(movedown,"Down")
s.onkey(moveleft,"Left")
s.onkey(moveright,"Right")
s.onkey(movestop,"space")
while True:
s.update()
if head.xcor()>290:
head.setx(-290)
if head.ycor()>290:
head.sety(-290)
if head.ycor()<-290:
head.sety(290)
if head.xcor()<-290:
head.setx(290)
if head.distance(food)<20:
x=random.randint(290,-290)
y=random.randint(290,-290)
food.goto(x,y)
body=turtle.Turtle()
body.speed(0)
body.shape("square")
body.color("white")
body.fillcolor("green")
body.penup()
bodies.append(body)
score+=10
delay+=0.004
if score>highestscore:
highestscore=score
sb.clear()
sb.write("Score:{} Highest Score: {}".format(score,highestscore))
for index in range(len(bodies)-1,0,-1):
x=bodies[index-1].xcor()
y=bodies[index-1].ycor()
bodies[index].goto(x,y)
if len(bodies)>0:
x=head.xcor()
y=head.ycor()
bodies[0].goto(x,y)
move()
for body in bodies:
if body.distance(head)<20:
time.sleep(1)
head.goto(0,0)
head.direction="stop"
for body in bodies:
body.goto(1000,1000)
bodies.clear()
score=0
delay=0.1
sb.clear()
sb.write("Score:{} Highest Score: {}".format(score,highestscore))
time.sleep(delay)
s.mainloop()
让你的游戏无法运行(吃东西)的是这两行:
x=random.randint(290,-290)
y=random.randint(290,-290)
参数需要反过来:
x=random.randint(-290, 290)
y=random.randint(-290, 290)