简单的游戏很卡

Simple game is very laggy

我刚开始使用 turtle 编写一个小游戏,但我的第一个原型已经很慢了。

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)

它可能因为“while True:”而滞后,但我不知道如何改进它。是否有太多 if 语句,还是我必须先设置特定的 FPS?

更多信息: 我正在使用 VSCode, 我的操作系统是 Windows 10, 我的电脑正常应该可以处理这么短的代码。

哦,请对我的技术术语放轻松,我对编码有点陌生。

编辑:我刚刚又测试了一下,肯定是 if 语句本身的问题。 但是我仍然不知道如何修复它。

我的系统上 运行 根本没有您的代码。我的建议是扔掉 keyboard 模块并使用 turtle 自己的 built-in 键事件处理程序,因为我们不应该在 event-driven 的世界中使用 while True:乌龟:

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

我在箭头键上添加了第二个播放器,以确保我的代码与两个播放器兼容。

您好,您使用了太多 keyboard.read_kry() 乐趣:意味着程序应该一次又一次地调用每个 if 语句:

我觉得现在不卡了:

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)

不太确定这是否有帮助,但请尝试将 wn.tracer(0) 参数更改为更大的值(例如 10/20)。里面的数字意味着(如果我理解正确的话)发生另一次刷新之前的毫秒数。就在今天,我遇到了同样的问题,因为我试图制作一个简单的“乒乓球”游戏,它似乎有所帮助。

Ps。在我的机器上它甚至不能用 0 作为参数 PPs。 - 我错了 - 请参阅下面的评论