簡體   English   中英

嘗試延遲特定的 function 在一定時間后生成敵人

[英]Trying to delay a specific function for spawning enemy after a certain amount of time

我正在使用 pygame 制作鼴鼠射擊游戲。 我希望我的鼴鼠在每 1 秒后隨機生成 position。 我曾嘗試使用 time.sleep(1.0) 但這會延遲我的整個代碼,因此由於響應延遲,游戲無法正確執行 function。 我正在使用鼠標移動一個目標(它也會因為 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()

在 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_timer時間參數來停止計時器事件。

另請參閱在 python 中同時生成相同 object 的多個實例


創建一個moles列表並將隨機 position 添加到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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM