【问题标题】:Trying to delay a specific function for spawning enemy after a certain amount of time尝试在一定时间后延迟生成敌人的特定功能
【发布时间】:2020-08-08 02:31:28
【问题描述】:

我正在使用 pygame 制作鼹鼠射击游戏。我希望我的鼹鼠每 1 秒后在随机位置生成。我曾尝试使用 time.sleep(1.0) 但这会延迟我的整个代码,因此由于响应延迟,游戏无法正常运行。我正在使用鼠标移动一个目标(它也会因为 time.sleep 而受到影响),我将添加一个点击进行射击。我需要帮助延迟和产卵我的痣。我还想就如何组织我的代码以提供各种难度级别和稍后提供主菜单的一些意见。

import pygame
import random
import time
from threading import Timer

pygame.font.init()



win_width = 1000
win_height = 710

FPS = 60


screen = pygame.display.set_mode((win_width, win_height))

pygame.display.set_caption("Mole Shooter")



white = (255,255,255)
red = (255, 0, 0)



counter, text = 30, 'Time Left: 30'.rjust(3)
pygame.time.set_timer(pygame.USEREVENT, 1000)

font = pygame.font.Font('freesansbold.ttf', 32)



run = True
clock = pygame.time.Clock()
background = pygame.transform.scale(pygame.image.load('back_land.png'), (win_width, win_height))

aim = pygame.image.load("aim.png")
mole = pygame.image.load("mole.png")


def mole_spawn_easy():

    molex = random.randint(50, 950)
    moley = random.randint(450, 682)

    screen.blit(mole, (molex, moley))


while run:
    screen.blit(background, [0,0])
    ax, ay = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.USEREVENT:
            counter -= 1
            text = ("Time Left: " + str(counter)).rjust(3)
            if counter > 0:
                time.sleep(1.0);mole_spawn_easy()

            else:
                print("game over")
                break



    screen.blit(aim, ((ax - 32 ),(ay - 32)))



    screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))

    clock.tick(FPS)

    pygame.display.flip()

【问题讨论】:

标签: python python-3.x pygame


【解决方案1】:

在 pygame 中存在一个计时器事件。使用pygame.time.set_timer()在事件队列中重复创建USEREVENT。时间必须以毫秒为单位:

pygame.time.set_timer(pygame.USEREVENT, 1000) # 1 second

注意,在 pygame 中可以定义客户事件。每个事件都需要一个唯一的 ID。用户事件的 id 必须介于 pygame.USEREVENT (24) 和 pygame.NUMEVENTS (32) 之间。在这种情况下,pygame.USEREVENT 的值是计时器事件的事件 ID。

在事件循环中接收事件:

running = True
while run:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

         elif event.type == pygame.USEREVENT:
             # [...]

可以通过将 0 传递给 pygame.time.set_timertime 参数来停止计时器事件。

另见Spawning multiple instances of the same object concurrently in python


创建moles 的列表,并在mole_spawn_easy 中的列表中添加一个随机位置:

moles = []

def mole_spawn_easy():
    molex = random.randint(50, 950)
    moley = random.randint(450, 682)
    moles.append((molex, moley))

在主应用循环中绘制moles

while run:
    # [...]

    for pos in moles:
        screen.blit(mole, pos)

看例子:

moles = []

def mole_spawn_easy():
    molex = random.randint(50, 950)
    moley = random.randint(450, 682)
    moles.append((molex, moley))

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

while run:
    
    ax, ay = pygame.mouse.get_pos()
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.USEREVENT:
            counter -= 1
            text = ("Time Left: " + str(counter)).rjust(3)
            if counter > 0:
                mole_spawn_easy()
            else:
                print("game over")

    screen.blit(background, [0,0])
    
    for pos in moles:
        screen.blit(mole, pos)
    screen.blit(aim, ((ax - 32 ),(ay - 32)))
    screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
    
    pygame.display.flip()
    clock.tick(FPS)

【讨论】:

  • 谢谢,我只需要添加 del(moles[0]) 即可从屏幕上删除以前的痣。现在产卵工作正常
猜你喜欢
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多