如何减少我的第一场比赛的滞后?蟒蛇

问题描述

我是编写代码的初学者,但我想在没有其他已开发游戏启发的情况下制作自己的游戏。我了解python的基础知识,并且已经熟悉了几个月。我目前正在开发的游戏甚至还没有完成。我只是想知道为什么我开了眨眼的门后就这么落后?您可以根据需要烘烤我的代码,我只是需要帮助,而没有喜欢任何有助于解决我的问题的方法。我也是Stack Overflow的新手

代码

import turtle,os,time
wn = turtle.Screen()
wn.title("The Last Fortresses")
wn.bgcolor("black")
wn.setup(width=1000,height=1000)

health_h = 100
health_e = 100
c = 1

door = turtle.Turtle()
door.speed(0)
door.shape("square")
door.color("orange")
door.shapesize(stretch_wid=5,stretch_len=1)
door.penup()
door.goto(470,-40)

Fortress = turtle.Turtle()
Fortress.speed(0)
Fortress.shape("square")
Fortress.color("grey")
Fortress.shapesize(stretch_wid=100000,stretch_len=1)
Fortress.penup()
Fortress.right(90)
Fortress.forward(100)
Fortress.goto(-400,-100)

Fortressr = turtle.Turtle()
Fortressr.speed(0)
Fortressr.shape("square")
Fortressr.color("grey")
Fortressr.shapesize(stretch_wid=100000,stretch_len=1)
Fortressr.penup()
Fortressr.right(90)
Fortressr.forward(100)
Fortressr.goto(-400,150)

Fortressw = turtle.Turtle()
Fortressw.speed(0)
Fortressw.shape("square")
Fortressw.color("silver")
Fortressw.shapesize(stretch_wid=10000000,stretch_len=12)
Fortressw.penup()
Fortressw.right(90)
Fortressw.forward(100)
Fortressw.goto(-400,25)

hero = turtle.Turtle()
hero.speed(0)
hero.shape("square")
hero.color("blue")
hero.shapesize(stretch_wid=5,stretch_len=1)
hero.penup()
hero.goto(-450,-40)

ok = 0

playerspeed = 15

def hero_jump():
    ok = 1
    hero.shapesize(stretch_wid=1,stretch_len=5)
    hero.left(90)
    hero.forward(50)
    time.sleep(.5)
    hero.backward(50)
    hero.right(90)
    hero.shapesize(stretch_wid=5,stretch_len=1)
    time.sleep(1)
    ok = 0

def hero_right():
    if ok == 0:
        hero.forward(15)
    
def hero_left(): 
    if ok == 0:
        hero.right(180)
        hero.forward(15)
        hero.right(180)
    
wn.listen()
wn.onkeypress(hero_right,"w")
wn.onkeypress(hero_left,"s")
wn.onkeypress(hero_jump,"j")

while c > 0:
    c = 0
    door.color("blue")
    time.sleep(1)
    door.color("orange") ## - right here is making lag - ##
    time.sleep(1)
    c = 1


def is_collided_with(a,b):
    return abs(a.xcor() - b.xcor()) < 10 and abs(a.ycor() - b.ycor()) < 10 ## - not finished part - ##

解决方法

游戏滞后是因为您使用sleep暂停了游戏:

while c > 0:
    c = 0
    door.color("blue")
    time.sleep(1)  # pause game 1 second
    door.color("orange") ## - right here is making lag - ##
    time.sleep(1) # pause game 1 second
    c = 1

当循环处于休眠状态时,整个游戏将冻结。要擦门,您应该使用计时器。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...