简体   繁体   English

在 pygame 中让玩家在每个间隔移动的最佳方法是什么?

[英]What is the best way to make a player move at every interval in pygame?

Is there a library or a simple way to only loop something every 0.5 seconds without interrupting the rest of the program?是否有一个库或一种简单的方法可以每 0.5 秒循环一次而不中断程序的 rest?

I have just started using pygame and have made a simple platformer and a Pong replica so far.我刚刚开始使用 pygame 并且到目前为止已经制作了一个简单的平台游戏和 Pong 副本。 I decided to try and make a Snake replica (I only currently have the head) and I need the snake to only move every 0.5 seconds while inputs can be registered at the 30 fps which I have the rest of the game running at.我决定尝试制作一个 Snake 副本(我目前只有头部),我需要蛇每 0.5 秒移动一次,而输入可以以 30 fps 的速度注册,我有游戏运行的 rest。 This is my current workaround:这是我目前的解决方法:

while running: #this is tabbed back in my code

# keep loop running at the right speed
clock.tick(FPS)

# get time at each iteration
currentTime = str(time.time()).split(".")[0]
gameTime = int (currentTime) - int (startTime)
# this is used to check for something every 0.5 second (500 ms)
currentTimeMs = str(time.time()).split(".")[1]

# snake will move evry 0.5 second in a direction
if currentTimeMs[0] in ["5","0"] and moveDone == False:
    moveDone = True
    player1.move(direction)
elif currentTimeMs[0] not in ["5","0"]:
    moveDone = False

There is more code within the while running: loop to get the direction and display the sprites but its not necessary for this.while running:循环中有更多代码来获取方向并显示精灵,但这不是必需的。 My current code works fine and will repeat the move function for my player1 every time that x in mm:ss:x is 0 or 5 (0.5 seconds apart) and will not repeat if it is that multiple times over a few frames.我当前的代码工作正常,每次 mm:ss:x 中的 x 为 0 或 5(相隔 0.5 秒)时,都会为我的 player1 重复移动 function,如果在几帧内多次重复,则不会重复。

This code needs to work within the running loop and not stop the program so time.sleep() doesn't work.此代码需要在运行循环中工作,而不是停止程序,因此 time.sleep() 不起作用。 I have also tried using the schedule library but it will not work as it cannot seem to allow the direction variable to change when passing it into the function.我也尝试过使用schedule库,但它不起作用,因为当将方向变量传递到 function 时,它似乎不允许改变方向变量。

My question therefore is;因此,我的问题是; Is there a library or a shorter way to accomplish what I need?是否有图书馆或更短的方法来完成我的需要?

Thanks in advance and I can message you the whole code if you need.在此先感谢您,如果您需要,我可以将整个代码发送给您。

You can store the moment of last move and then compare to actual time.您可以存储最后一步的时刻,然后与实际时间进行比较。 To do it you can use perf_counter from time .为此,您可以使用perf_counter from time Here is an example这是一个例子

last_move_time = perf_counter()

while running:
    if perf_counter() - last_move_time > 0.5:
        # move player
        last_move_time = perf_counter()

I suggest using pygames event mechanics and pygame.time.set_timer() (seehere for docs).我建议使用 pygames 事件机制和pygame.time.set_timer() (有关文档,请参见此处)。

You would do something like this:你会做这样的事情:

pygame.time.set_timer(pygame.USEREVENT, 500)

and in the event loop look for the event type.并在事件循环中查找事件类型。

if event.type == pygame.USEREVENT:

If this is the only user defined event that you are using in your program you can just use USEREVENT .如果这是您在程序中使用的唯一用户定义事件,则可以使用USEREVENT

When you detect the event the timer has expired and you move your snake or whatever.当您检测到该事件时,计时器已过期并且您移动了蛇或其他任何东西。 A new timer can be set for another 1/2 second.一个新的定时器可以再设置 1/2 秒。 If you need more accuracy you can keep tabs on the time and set the timer for the right amount of time, but for you situation just setting it for 1/2 sec each time is okay.如果您需要更高的准确性,您可以密切关注时间并将计时器设置为正确的时间,但对于您的情况,每次将其设置为 1/2 秒就可以了。

If you need multiple timers going and need to tell them apart, you can create an event with an attribute that you can set to different values to track them.如果您需要多个计时器并需要将它们区分开来,您可以创建一个带有属性的事件,您可以将其设置为不同的值来跟踪它们。 Something like this (though I have not run this particular code snippet, so there could be a typo):像这样的东西(虽然我没有运行这个特定的代码片段,所以可能有错字):

my_event = pygame.event.Event(pygame.USEREVENT, {"tracker": something})
pygame.time.set_timer(my_event , 500)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM