我正在尝试创建一个游戏,其中乌龟四处走动并吃点。应该出现一个点。当乌龟越过圆点时,已经吃过的点应该消失,并且应该出现一个新的随机点。此外,海龟的大小每次都会略有增长。到目前为止,我有这个:
from turtle import *
from random import randrange
FRAMES_PER_SECOND = 10
bgcolor('black')
def turnRight():
global turtle
turtle.right(15)
def turnLeft():
global turtle
turtle.left(15)
def dotGood():
x = randrange(-250, 250)
y = randrange(-250, 250)
pen1 = Pen()
pen1.hideturtle()
pen1.penup()
pen1.goto(x, y)
pen1.dot(10, "red")
def eaten(z):
global turtle
global moving
if turtle == dotGood():
z = z + .25
def move():
global turtle
global moving
if moving:
for i in range(1):
turtle.penup()
turtle.shape('turtle')
size = .5
eaten(size)
turtle.shapesize(size, size, size)
turtle.color('green')
turtle.forward(10)
ontimer(move, 10 // FRAMES_PER_SECOND)
def start():
global moving
moving = True
move()
def stop():
global moving
moving = False
def border():
pen1 = Pen()
pen1.color("yellow")
pen1.pensize(3)
pen1.hideturtle()
pen1.speed(0)
pen1.penup()
pen1.goto(-275, -275)
pen1.pendown()
for i in range(4):
pen1.fd(550)
pen1.left(90)
def main():
border()
dotGood()
global turtle
turtle = Turtle()
onkey(turnRight, "Right")
onkey(turnLeft, "Left")
onkey(start, "Up")
onkey(stop, "Down")
listen()
if __name__ == "__main__":
main()
现在,所有的点不断地产生而不是一次产卵,当它越过一个点时,龟不会生长,当龟越过它时,点不会消失。我该怎么做才能解决这个问题?
答案 0 :(得分:0)
答案 1 :(得分:0)
您的实施问题:
通过制作dot()
食物,您无法彻底清除它 - 而是使用圆形龟并stamp()
,因为它会返回一个可以干净利落的邮票ID已通过clearstamp()
删除。
为了更好的转弯,您的事件处理程序应该在处理当前事件时阻止新事件。
你不明白global
的目的 - 你需要它来完成你的一些功能,但不像你宣布的那样频繁。
你的乌龟移动太快而无法控制。如果您没有代码将其保留在屏幕边界内,则很容易丢失。
以下不是完成的代码,但实现了基本的功能:你可以移动乌龟,食物在食用时消失,新的食物出现,乌龟在吃食物时明显变胖:
from turtle import Turtle, Screen
from random import randrange
FRAMES_PER_SECOND = 10
WIDTH, HEIGHT = 500, 500
STAMP_SIZE = 20
DOT_SIZE = 10
def turnRight():
screen.onkey(None, 'Right')
turtle.right(15)
screen.onkey(turnRight, 'Right')
def turnLeft():
screen.onkey(None, 'Left')
turtle.left(15)
screen.onkey(turnLeft, 'Left')
def dotGood(food):
x = randrange(DOT_SIZE - WIDTH//2, WIDTH//2 - DOT_SIZE)
y = randrange(DOT_SIZE - HEIGHT//2, HEIGHT//2 - DOT_SIZE)
food.goto(x, y)
return ((x, y), food.stamp())
def eaten(food):
global dot
((x, y), stamp_id) = dot
if abs(turtle.xcor() - x) < DOT_SIZE and abs(turtle.ycor() - y) < DOT_SIZE:
food.clearstamp(stamp_id)
dot = dotGood(food)
return True
return False
def move():
global score
if not moving:
return
if eaten(food):
score += 1
turtle.shapesize((STAMP_SIZE + 5 * score) / STAMP_SIZE)
turtle.forward(1)
screen.ontimer(move, 10 // FRAMES_PER_SECOND)
def start():
global moving
moving = True
move()
def stop():
global moving
moving = False
screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.bgcolor('black')
turtle = Turtle(shape='turtle')
turtle.speed('slowest')
turtle.color('green')
turtle.penup()
food = Turtle(shape='circle', visible=False)
food.speed('fastest')
food.turtlesize(DOT_SIZE / STAMP_SIZE)
food.color('red')
food.penup()
# globals
moving = False
dot = dotGood(food)
score = 0
screen.onkey(turnRight, 'Right')
screen.onkey(turnLeft, 'Left')
screen.onkey(start, 'Up')
screen.onkey(stop, 'Down')
screen.listen()
screen.mainloop()
我抛出边框代码只是为了让示例更简单。