尝试在一定时间后延迟生成敌人的特定功能 [英] Trying to delay a specific function for spawning enemy after a certain amount of time

查看:81
本文介绍了尝试在一定时间后延迟生成敌人的特定功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a mole shooter game using pygame. I want my mole to spawn at a random position after every 1 second. I have tried using time.sleep(1.0) but that delays my whole code and thus the game doesn't function properly because of delayed responses. I am moving an aim using the mouse(which also gets affected because of time.sleep) to which i will be adding a click to shoot. I need help with delaying and spawning my mole. I would also like some opinions on how to organize my code to provide various levels of difficulty and a main menu later on.

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()

推荐答案

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

In pygame exists a timer event. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue.. The time has to be set in milliseconds:

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

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

Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case the value of pygame.USEREVENT is the event id for the timer event.

在事件循环中接收事件:

Receive the event in the event loop:

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 参数来停止计时器事件.

The timer event can be stopped by passing 0 to the time argument of pygame.time.set_timer.

另见 生成多个实例同一对象在python中并发.

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

Create a list of moles and add a random position to the list in mole_spawn_easy:

moles = []

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

在主应用程序循环中绘制moles:

Draw the moles in the main application loop:

while run:
    # [...]

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


看例子:


See the example:

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)

这篇关于尝试在一定时间后延迟生成敌人的特定功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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