首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何在我的第一个游戏中减少延迟?Python

我如何在我的第一个游戏中减少延迟?Python
EN

Stack Overflow用户
提问于 2020-08-25 09:48:35
回答 1查看 32关注 0票数 0

我是一个编码的初学者,但我想做我自己的游戏,没有其他已经制作的游戏的灵感。我知道python的基础知识,并且已经熟悉它好几个月了。我目前正在开发的游戏甚至还没有完成。我只是想知道为什么在我做了一扇闪烁的门之后,我还是落后了这么多?你可以烤我的代码,如果你愿意,我只是需要帮助,并没有发现任何可以帮助我的问题。我也是Stack Overflow的新手

代码:

代码语言:javascript
复制
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 - ##
EN

回答 1

Stack Overflow用户

发布于 2020-08-25 10:24:13

游戏延迟是因为您正在使用sleep暂停游戏

代码语言:javascript
复制
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

当循环处于休眠状态时,整个游戏都会冻结。要闪门,你应该用计时器。

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

https://stackoverflow.com/questions/63570926

复制
相关文章

相似问题

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