简体   繁体   English

使用TURTLE的Python游戏

[英]Python game using TURTLE

I am attempting to create a game where a turtle goes around and eats dots. 我正在尝试创建一个游戏,其中一只乌龟到处乱吃。 One dot should appear. 应该出现一个点。 When the turtle goes over the dot, the dot that has been eaten should disappear and a new random dot should appear. 当乌龟越过该点时,已吃掉的点应消失,并应出现一个新的随机点。 Additionally, the turtles size should grow slightly each time. 此外,海龟的大小每次都会略有增加。 So far, I have this: 到目前为止,我有这个:

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()

Right now, all dots continually spawn instead of one at a time, the turtle doesn't grow when it goes over a dot, and the dot doesn't disappear when the turtle goes over it. 现在,所有点持续不断地产生而不是一次出现,当龟越过一个点时,乌龟不会增长,当龟越过它时,点也不会消失。 What can I do to fix this? 我该怎么做才能解决此问题?

  1. z in eaten function is local, you miss it every time you call it. Eated函数中的z是本地的,您每次调用它都会错过它。
  2. dotGood doesn't return anything to compare with turtle. dotGood不返回任何东西来与乌龟进行比较。 I guess you need to keep the last x,y can compare it with current turtle position. 我想您需要保留最后一个x,y才能将其与当前乌龟位置进行比较。 turtle will not equal to a drawing. 乌龟不等于图纸。

Problems with your implementation: 实施中的问题:

By making food a dot() , you've no way to cleanly remove it -- instead use a circle-shaped turtle and stamp() it as it returns a stamp ID that can be cleanly removed via clearstamp() . 通过将食物变成dot() ,您将无法干净地删除它-而是使用圆形的turtle和stamp() ,因为它返回可以通过clearstamp()干净删除的图章ID。

For nicer turning, your event handlers should block out new events while they are handling the current event. 为了更好地进行转换,您的事件处理程序应在处理当前事件时阻止新事件。

You don't understand the purpose of global -- you need it for some of your functions but not nearly as often as you declare it. 您不了解global的目的-您需要对某些功能使用它,但并不像声明它那样频繁。

Your turtle moves too fast to control. 您的乌龟动作太快而无法控制。 Given you've no code to keep it within the boundaries of the screen, it's easily lost. 由于您没有代码可将其限制在屏幕范围内,因此很容易丢失。

The following isn't finished code but implements basic functionality: you can move the turtle, food disappears when eaten and new food appears, the turtle grows visibly fatter as he eats food: 以下代码不是完成的代码,但实现了基本功能:您可以移动乌龟,食用时食物会消失,新食物出现后,食用时乌龟会变得明显发胖:

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()

I tossed the border code just to keep the example simpler. 我扔了边界代码只是为了使示例更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM