在 pygame 中每 x (milli) 秒做一些事情 [英] Do something every x (milli)seconds in pygame

查看:45
本文介绍了在 pygame 中每 x (milli) 秒做一些事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Python 和 Pygame,我制作的第一件事是一个简单的 Snake 游戏.我试图让蛇每 0.25 秒移动一次.这是我循环的代码部分:

I'm learing Python and Pygame, and my first thing I'm making is a simple Snake game. I'm trying to make it so that the snake moves once every 0.25 seconds. Here is the part of my code that loops:

while True:
    check_for_quit()

    clear_screen()

    draw_snake()
    draw_food()

    check_for_direction_change()

    move_snake() #How do I make it so that this loop runs at normal speed, but move_snake() only executes once every 0.25 seconds?

    pygame.display.update()

我希望所有其他函数都能正常运行,但 move_snake() 每 0.25 秒只发生一次.我查了一下,找到了一些答案,但对于那些正在制作他们的第一个 Python 脚本的人来说,它们似乎都太复杂了.

I want all of the other function to run normally, but move_snake() to only occur once every 0.25 seconds. I've looked it up and found a few answers but they all seem too complicated for someone who's making their first ever Python script.

是否有可能实际获得一个示例来说明我的代码应该是什么样子,而不仅仅是告诉我需要使用哪个函数?谢谢!

Would it be possible to actually get an example of what my code should look like rather than just telling me which function I need to use? Thanks!

推荐答案

有几种方法,例如跟踪系统时间或使用 Clock 和计数滴答.

There are several approaches, like keeping track of the system time or using a Clock and counting ticks.

但最简单的方法是使用事件队列并使用 pygame.time.set_timer():

But the simplest way is to use the event queue and creating an event every x ms, using pygame.time.set_timer():

pygame.time.set_timer()

在事件队列上重复创建一个事件

set_timer(eventid, 毫秒) ->无

将事件类型设置为每隔给定的毫秒数出现在事件队列中.第一个事件在经过一段时间后才会出现.

Set an event type to appear on the event queue every given number of milliseconds. The first event will not appear until the amount of time has passed.

每种事件类型都可以附加一个单独的计时器.最好使用 pygame.USEREVENT 和 pygame.NUMEVENTS 之间的值.

Every event type can have a separate timer attached to it. It is best to use the value between pygame.USEREVENT and pygame.NUMEVENTS.

要禁用事件的计时器,请将毫秒参数设置为 0.

To disable the timer for an event, set the milliseconds argument to 0.

这是一个运行中的小例子,其中蛇每 250 毫秒移动一次:

Here's a small, running example where the snake moves every 250 ms:

import pygame
pygame.init()
screen = pygame.display.set_mode((300, 300))
player, dir, size = pygame.Rect(100,100,20,20), (0, 0), 20
MOVEEVENT, t, trail = pygame.USEREVENT+1, 250, []
pygame.time.set_timer(MOVEEVENT, t)
while True:
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]: dir = 0, -1
    if keys[pygame.K_a]: dir = -1, 0
    if keys[pygame.K_s]: dir = 0, 1
    if keys[pygame.K_d]: dir = 1, 0

    if pygame.event.get(pygame.QUIT): break
    for e in pygame.event.get():
        if e.type == MOVEEVENT: # is called every 't' milliseconds
            trail.append(player.inflate((-10, -10)))
            trail = trail[-5:]
            player.move_ip(*[v*size for v in dir])

    screen.fill((0,120,0))
    for t in trail:
        pygame.draw.rect(screen, (255,0,0), t)
    pygame.draw.rect(screen, (255,0,0), player)
    pygame.display.flip()

这篇关于在 pygame 中每 x (milli) 秒做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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