简体   繁体   English

简单的游戏很卡

[英]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.

相关问题 在非常简单的Python游戏中创建战斗系统 - Creating fighting system in very simple Python game QTableView horizo​​ntalHeader使其很滞后吗? - QTableView horizontalHeader makes it very laggy? 非常简单的数学游戏的答案总是错误的-Python - Answer for very simple maths game is always Wrong - Python MacOS 上的 Spyder。 打字很慢 - Spyder on MacOS. Typing is very laggy Python Tkinter:倒数计时器非常滞后且不准确 - Python Tkinter: Countdown Timer is very laggy and not accurate 如何通过利用NumPy的功能来修复和优化这个非常简单的“生命游戏”代码? - How can I fix and optimize this very simple piece of “Game of Life” code by taking advantage of NumPy's functionality? 在 python 中使用 pynput 和 playsound 模块时鼠标冻结/非常滞后 - Mouse Freezes/Very Laggy when using pynput and playsound module in python 我的程序非常滞后。 我使用 Python/Pygame - My Program Is Very Laggy. I use Python/Pygame 非常简单的PyGame非常慢 - Very simple PyGame very slow 我做了一个非常简单的 Pygame 零游戏,但是我的 Actor 图像和 screen.clear() 不起作用。 你能帮我解决这个问题吗? - I made a very simple Pygame Zero game, but my Actor image and the screen.clear() is not working. Can you help me fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM