[英]Simple game is very laggy
I've just started coding a little game using turtle, but my very first prototype is already very laggy.我刚刚开始使用海龟编写一个小游戏,但我的第一个原型已经非常滞后。
import turtle
import keyboard
# Player 1 x and y cords
p1x = -350
p1y = 250
wn = turtle.Screen()
wn.title("Actua")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)
# Player 1 Setup
player_1 = turtle.Turtle()
player_1.speed(0)
player_1.shape("square")
player_1.color("red")
player_1.penup()
player_1.goto(p1x, p1y)
player_1.shapesize(1, 1)
win = turtle.Screen()
while True:
win.update()
# Controlls
if keyboard.read_key:
if keyboard.read_key() == "esc":
quit()
if keyboard.read_key() == "d" or keyboard.read_key() == "D":
p1x = p1x+10
if keyboard.read_key() == "a" or keyboard.read_key() == "A":
p1x = p1x-10
if keyboard.read_key() == "w" or keyboard.read_key() == "W":
p1y = p1y+10
if keyboard.read_key() == "s" or keyboard.read_key() == "S":
p1y = p1y-10
player_1.goto(p1x, p1y)
It's probably lagging because of the "while True:" but I don't know how to improve it.它可能因为“while True:”而滞后,但我不知道如何改进它。 Are there just too many if-statements or do I first have to set specific FPS?是否有太多的 if 语句或者我首先必须设置特定的 FPS?
Further information: I'm using VSCode, my operating system is Windows 10, my PC should normally be able to handle such a short code.更多信息:我正在使用 VSCode,我的操作系统是 Windows 10,我的 PC 通常应该能够处理这么短的代码。
Oh, and please go easy on me with the technical terms, I'm kinda new to coding.哦,请用技术术语对我放轻松,我对编码有点陌生。
Edit: I just tested it again, it's definitely due to the if-statements themselves.编辑:我刚刚再次测试它,这绝对是由于 if 语句本身。 However I still don't know how I could fix it.但是我仍然不知道如何解决它。
Your code doesn't run at all on my system.您的代码根本无法在我的系统上运行。 My recommendation is to toss the keyboard module and use turtle's own built-in key event handler, as we should never have while True:
in an event-driven world like turtle:我的建议是扔掉键盘模块并使用turtle自己的内置键事件处理程序,因为我们永远不应该拥有while True:
在像turtle这样的事件驱动的世界中:
from turtle import Screen, Turtle
from functools import partial
# Players X and Y coordinates
p1x, p1y = -350, 250
p2x, p2y = 350, -250
def right(player):
player.setx(player.xcor() + 10)
screen.update()
def left(player):
player.setx(player.xcor() - 10)
screen.update()
def up(player):
player.sety(player.ycor() + 10)
screen.update()
def down(player):
player.sety(player.ycor() - 10)
screen.update()
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor('black')
screen.tracer(False)
player_1 = Turtle()
player_1.shape('square')
player_1.color('red')
player_1.penup()
player_1.goto(p1x, p1y)
player_2 = player_1.clone()
player_2.color('green')
player_2.goto(p2x, p2y)
screen.onkey(partial(left, player_1), 'a')
screen.onkey(partial(right, player_1), 'd')
screen.onkey(partial(up, player_1), 'w')
screen.onkey(partial(down, player_1), 's')
screen.onkey(partial(left, player_2), 'Left')
screen.onkey(partial(right, player_2), 'Right')
screen.onkey(partial(up, player_2), 'Up')
screen.onkey(partial(down, player_2), 'Down')
screen.onkey(screen.bye, 'Escape')
screen.listen()
screen.update()
screen.mainloop()
I've added a second player on the arrow keys to make sure my code is compatible with two players.我在箭头键上添加了第二个播放器,以确保我的代码与两个播放器兼容。
hi you are using too many keyboard.read_kry() fun: means every if statment the program should call the fun again and again:嗨,您使用了太多的 keyboard.read_kry() fun: 意味着每个 if 语句程序都应该一次又一次地调用 fun:
i think now its not laggy:我认为现在它并不落后:
import turtle
import keyboard
# Player 1 x and y cords
p1x = -350
p1y = 250
wn = turtle.Screen()
wn.title("Actua")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)
# Player 1 Setup
player_1 = turtle.Turtle()
player_1.speed(0)
player_1.shape("square")
player_1.color("red")
player_1.penup()
player_1.goto(p1x, p1y)
player_1.shapesize(1, 1)
win = turtle.Screen()
win.update()
while True:
key = keyboard.read_key()
if key == "esc":
quit()
elif key == "d" or key == "D":
p1x = p1x + 10
win.update()
elif key == "a" or key == "A":
p1x = p1x - 10
win.update()
elif key == "w" or key == "W":
p1y = p1y + 10
win.update()
elif key == "s" or key == "S":
p1y = p1y - 10
win.update()
player_1.goto(p1x, p1y)
Not quite sure if that would help, but try changing wn.tracer(0)
parameter to something bigger (ex.10/20).不太确定这是否有帮助,但尝试将wn.tracer(0)
参数更改为更大的参数(例如 10/20)。 The number inside means (if I understand correctly) the amount of milliseconds before another refresh takes place.里面的数字意味着(如果我理解正确的话)在另一次刷新发生之前的毫秒数。 I had the same problem just today, as I tried to make a simple "pong" game and it seemed to help.我今天遇到了同样的问题,因为我试图制作一个简单的“乒乓”游戏,它似乎有帮助。
Ps.附言。 on my machine it wouldn't even work with 0 as an argument PPs.在我的机器上,它甚至不能使用 0 作为参数 PPs。 - I was wrong - see the comment below -我错了 - 请参阅下面的评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.