首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python Turtle Graphics游戏落后

Python Turtle Graphics游戏落后
EN

Stack Overflow用户
提问于 2018-04-25 16:41:24
回答 1查看 1.3K关注 0票数 -3

我的Python游戏落后了很多。我开始制作一个新的Python游戏,每次测试它时,尽管我没有使用任何图像,但它总是会延迟。

代码语言:javascript
复制
import turtle
import random

#head orientation
h = [0]

#score
a = [0]
b = [0]

#food coord
fcoord = [0,0,0]

#position
pos = []


def home(x,y):
    x = 0
    y = 0
    a[0] = 0
    b[0] = 0
    h[0] = 0
    fcoord[2] = 0
    pos[:] = []
    turtle.hideturtle()
    turtle.clear()
    turtle.pu()
    turtle.color("lime")
    turtle.goto(0,0)
    turtle.write("PLAY", align="center",font="Calibri")
    turtle.title("Snake Game")
    turtle.onscreenclick(start)
    turtle.mainloop()

def level_1():
    turtle.clear()
    turtle.pu()
    turtle.speed(0)
    turtle.pensize(20)
    turtle.color("grey")
    turtle.goto(-220,220)
    turtle.pd()
    turtle.goto(220,220)
    turtle.goto(220,-220)
    turtle.goto(-220,-220)
    turtle.goto(-220,220)
    turtle.pu()
    turtle.goto(0,0)

def start(x,y):
    turtle.onscreenclick(None)

    level_1()

    tfood = turtle.Turtle()
    tfood.hideturtle()
    tfood.pu()
    tfood.speed(0)
    tfood.shape("square")
    tfood.color("red")

    tscore = turtle.Turtle()
    tscore.hideturtle()
    tscore.pu()
    tscore.speed(0)
    tscore.goto(100,-250)
    tscore.write("Score:" + str(a[0]), align="center",font=(10))

    while x > -210 and x < 210 and y > -210 and y <210:
        if fcoord[2] == 0:
            food(tfood)
            fcoord[2] = 1
        turtle.onkey(u,"Up")
        turtle.onkey(l,"Left")
        turtle.onkey(r,"Right")
        turtle.onkey(d,"Down")
        turtle.listen()
        move()
        x = turtle.xcor()
        y = turtle.ycor()        
        if x > fcoord[0]*20-5 and x < fcoord[0]*20+5 and y > fcoord[1]*20-5 and y < fcoord[1]*20+5:
            fcoord[2] = 0
            tfood.clear()
            a[0] += 1
            tscore.clear()
            tscore.write("Score:" + str(a[0]), align="center",font=(10))

        if len(pos) > 1:
            for i in range(1,len(pos)):
                if x < pos[i][0]+5 and x > pos[i][0]-5 and y < pos[i][1]+5 and y > pos[i][1]-5:
                        tscore.clear()
                        tfood.clear()
                        gameover()
    tscore.clear()
    tfood.clear()
    gameover()


#Food
def food(tfood):
    x = random.randrange(-8,8,1)
    y = random.randrange(-8,8,1)
    fcoord[0] = x
    fcoord[1] = y
    tfood.hideturtle()
    tfood.pu()
    tfood.shape("circle")
    tfood.color("red")
    tfood.goto(x*20,y*20)
    tfood.stamp()

#Up   
def u():
    if h[0] == 270:
        pass
    else:
        h[0] = 90
#Down
def d():
    if h[0] == 90:
        pass
    else:
        h[0] = 270
#Left
def l():
    if h[0] == 0:
        pass
    else:
        h[0] = 180
#Right
def r():
    if h[0] == 180:
        pass
    else:
        h[0] = 0

def move():
    turtle.pensize(1)
    turtle.color("green")
    turtle.pu()
    turtle.speed(3)
    turtle.setheading(h[0])
    turtle.shape("square")
    turtle.stamp()
    turtle.fd(20)
    x = turtle.xcor()
    y = turtle.ycor()
    if b[0] > a[0]:     
        turtle.clearstamps(1)
        pos.insert(0,[round(x),round(y)])
        pos.pop(-1)
    else:
        pos.insert(0,[round(x),round(y)])       
        b[0] += 1    

def gameover():
    turtle.onscreenclick(None)
    turtle.speed(0)
    turtle.pu()
    turtle.goto(0,150)
    turtle.color("red")
    turtle.write("Game Over",align="center", font=(10))
    turtle.goto(0,50)
    turtle.write("Score:" + str(a[0]),align="center",font=(10))
    turtle.goto(200,-200)
    turtle.write("(Click anywhere to return to the main menu)",align="right",font=(0.0000001))
    turtle.onscreenclick(home)
    turtle.mainloop()


# # # # # # # # # # # # # # # # # # # # # #
# Main                                    #
# # # # # # # # # # # # # # # # # # # # # #
if __name__ == '__main__':
    home(0,0)
EN

回答 1

Stack Overflow用户

发布于 2018-04-26 07:56:32

我不确定你说的滞后是什么意思,但我在下面的重写中已经解决了几个问题。首先,海龟是全局实体,在正常情况下不会被垃圾收集,所以不要重新创建它们,而是重新使用它们。其次,当您有自己的while循环来控制游戏时,您可能会锁定事件。我已经消除了您的循环,并修改了游戏以处理ontimer事件,以便乌龟的移动应该由处理用户输入的相同事件循环处理,使其对用户的响应稍微更快一些:

代码语言:javascript
复制
from turtle import Turtle, Screen
from random import randint

FONT = ('Arial', 18, 'bold')

# Up
def u():
    if h[0] != 270:
        h[0] = 90
# Down
def d():
    if h[0] != 90:
        h[0] = 270
# Left
def l():
    if h[0] != 0:
         h[0] = 180
# Right
def r():
    if h[0] != 180:
            h[0] = 0

def gameover():

    screen.onkey(None, "Up")
    screen.onkey(None, "Left")
    screen.onkey(None, "Right")
    screen.onkey(None, "Down")

    tscore.clear()
    tfood.clear()
    tplayer.clear()

    tfood.hideturtle()
    tplayer.hideturtle()

    tscore.color("red")

    tscore.goto(0, 150)
    tscore.write("Game Over", align="center", font=FONT)

    tscore.goto(0, 50)
    tscore.write("Score:" + str(a[0]), align="center", font=FONT)

    tscore.goto(0, -200)
    tscore.write("(Click anywhere to return to the main menu)", align="center", font=FONT)

    screen.onscreenclick(home)

def food(tfood):

    x = randint(-160, 160)
    y = randint(-160, 160)

    tfood.goto(x, y)
    tfood.showturtle()

def move():
    x, y = tplayer.position()

    if -210 < x < 210 and -210 < y < 210:
        if not tfood.isvisible():
            food(tfood)

        tplayer.setheading(h[0])
        tplayer.stamp()
        tplayer.forward(20)


        if b[0] > a[0]:
            tplayer.clearstamps(1)
            pos.insert(0, [round(x), round(y)])
            pos.pop(-1)
        else:
            pos.insert(0, [round(x), round(y)])
            b[0] += 1

        if tplayer.distance(tfood) < 15:
            tfood.hideturtle()
            tfood.clear()
            a[0] += 1
            tscore.clear()
            tscore.write("Score:" + str(a[0]), align="center", font=FONT)

        flag = True
        x, y = tplayer.position()

        if len(pos) > 1:
            for i in range(1, len(pos)):
                if pos[i][0] - 5 < x < pos[i][0] + 5 and pos[i][1] - 5 < y < pos[i][1] + 5:
                    flag = False
                    break

        if flag:
            screen.ontimer(move, 25)
    else:
        screen.ontimer(gameover, 100)

def level_1():

    tmarker.penup()
    tmarker.goto(-220, 220)
    tmarker.pendown()
    tmarker.goto(220, 220)
    tmarker.goto(220, -220)
    tmarker.goto(-220, -220)
    tmarker.goto(-220, 220)
    tmarker.penup()

def start(x, y):
    screen.onscreenclick(None)

    tscore.clear()

    level_1()

    tplayer.home()
    tplayer.setheading(h[0])

    tscore.goto(100, -250)
    tscore.write("Score:" + str(a[0]), align="center", font=FONT)

    screen.onkey(u, "Up")
    screen.onkey(l, "Left")
    screen.onkey(r, "Right")
    screen.onkey(d, "Down")

    move()

def home(x=0, y=0):
    screen.onscreenclick(None)

    a[0] = 0
    b[0] = 0
    h[0] = 0

    pos[:] = []

    tscore.clear()
    tscore.home()
    tscore.color('lime')
    tscore.write("PLAY", align="center", font=FONT)

    screen.onscreenclick(start)

# head orientation
h = [0]

# score
a = [0]
b = [0]

# position
pos = []

# turtles
tfood = Turtle('circle', visible=False)
tfood.speed('fastest')
tfood.color('red')
tfood.penup()

tscore = Turtle(visible=False)
tscore.speed('fastest')
tscore.penup()

tplayer = Turtle("square", visible=False)
tplayer.speed('slow')
tplayer.color("green")
tplayer.penup()

tmarker = Turtle(visible=False)
tmarker.speed('fastest')
tmarker.pensize(20)
tmarker.color("grey")

# # # # # # # # # # # # # # # # # # # # # #
# Main                                    #
# # # # # # # # # # # # # # # # # # # # # #
if __name__ == '__main__':
    screen = Screen()
    screen.title("Snake Game")
    screen.listen()

    home()

    screen.mainloop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50017721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档