让玩家在 pygame 中的每个时间间隔移动的最佳方法是什么? [英] What is the best way to make a player move at every interval in pygame?

查看:169
本文介绍了让玩家在 pygame 中的每个时间间隔移动的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个库或一种简单的方法可以每 0.5 秒只循环一次而不中断程序的其余部分?

我刚刚开始使用 pygame,到目前为止已经制作了一个简单的平台游戏和 Pong 副本.我决定尝试制作一个 Snake 复制品(我目前只有头部),我需要蛇每 0.5 秒移动一次,而输入可以以 30 fps 的速度注册,我让游戏的其余部分运行.这是我目前的解决方法:

while running: #this is tabbed back in my code# 保持循环以正确的速度运行时钟滴答(FPS)# 每次迭代获取时间currentTime = str(time.time()).split(".")[0]游戏时间 = int (currentTime) - int (startTime)# 这用于每 0.5 秒(500 毫秒)检查一次currentTimeMs = str(time.time()).split(".")[1]# 蛇每 0.5 秒向一个方向移动一次如果 currentTimeMs[0] 在 ["5","0"] 和 moveDone == False:移动完成 = 真player1.move(方向)elif currentTimeMs[0] 不在 ["5","0"] 中:移动完成 = 假

while running: 循环中有更多代码来获取方向并显示精灵,但这不是必需的.我当前的代码工作正常,并且每次 mm:ss:x 中的 x 为 0 或 5(相隔 0.5 秒)时都会为我的 player1 重复移动函数,并且如果在几帧内多次重复,则不会重复.

此代码需要在运行循环内工作,而不是停止程序,因此 time.sleep() 不起作用.我也尝试过使用 schedule 库,但它不起作用,因为它似乎不允许在将方向变量传递给函数时改变它.

因此我的问题是;是否有图书馆或更短的方法来完成我的需要?

提前致谢,如果您需要,我可以给您发送完整代码.

解决方案

我建议使用 pygames 事件机制和 pygame.time.set_timer()(参见 此处 文档).

你会做这样的事情:

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

并在事件循环中查找事件类型.

if event.type == pygame.USEREVENT:

如果这是您在程序中使用的唯一用户定义事件,您可以使用 USEREVENT.

当您检测到事件时,计时器已过期,您可以移动蛇或其他任何东西.新的计时器可以再设置 1/2 秒.如果您需要更高的准确性,您可以密切关注时间并将计时器设置为适当的时间,但对于您的情况,每次将其设置为 1/2 秒就可以了.

如果您需要多个计时器运行并需要将它们区分开来,您可以创建一个具有属性的事件,您可以将其设置为不同的值来跟踪它们.像这样的东西(虽然我没有运行这个特定的代码片段,所以可能有一个错字):

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

Is there a library or a simple way to only loop something every 0.5 seconds without interrupting the rest of the program?

I have just started using pygame and have made a simple platformer and a Pong replica so far. 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. 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. 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.

This code needs to work within the running loop and not stop the program so time.sleep() doesn't work. 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.

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.

解决方案

I suggest using pygames event mechanics and pygame.time.set_timer() (see here for docs).

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.

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. 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.

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)

这篇关于让玩家在 pygame 中的每个时间间隔移动的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆