Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图在一定时间后延迟产生敌人的特定功能_Python_Python 3.x_Pygame - Fatal编程技术网

Python 试图在一定时间后延迟产生敌人的特定功能

Python 试图在一定时间后延迟产生敌人的特定功能,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在用pygame制作一个鼹鼠射击游戏。我希望我的鼹鼠每1秒在一个随机的位置产卵。我尝试过使用time.sleep(1.0),但这会延迟我的整个代码,因此游戏无法正常运行,因为响应延迟。我正在使用鼠标移动目标(这也会因为time.sleep而受到影响),我将在其中添加一个单击来射击。我需要帮助延迟和繁殖我的鼹鼠。我还想了解一些关于如何组织代码以提供不同难度和主菜单的意见 import pygame import random import time from threading import

我正在用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()

pygame中存在一个计时器事件。用于在事件队列中重复创建。。必须以毫秒为单位设置时间:

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

在事件循环中接收事件:

running=True
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
elif event.type==pygame.USEREVENT:
# [...]
通过将0传递给pygame.time.set\u timer的time参数,可以停止计时器事件

另见


创建一个
鼹鼠列表
,并在
鼹鼠产卵\u轻松
中的列表中添加一个随机位置:

moles=[]
def mole_spawn_easy():
摩尔=random.randint(50950)
摩尔=random.randint(450682)
摩尔。附加((摩尔,摩尔))
在主应用循环中绘制
moles

运行时:
# [...]
对于以摩尔为单位的pos:
筛网布利特(摩尔,位置)

请参见示例:

moles=[]
def mole_spawn_easy():
摩尔=random.randint(50950)
摩尔=random.randint(450682)
摩尔。附加((摩尔,摩尔))
pygame.time.set_计时器(pygame.USEREVENT,1000)
运行时:
ax,ay=pygame.mouse.get_pos()
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.USEREVENT:
计数器-=1
text=(“剩余时间:”+str(计数器)).rjust(3)
如果计数器>0:
鼹鼠产卵容易
其他:
打印(“游戏结束”)
screen.blit(背景,[0,0])
对于以摩尔为单位的pos:
筛网布利特(摩尔,位置)
blit(aim,((ax-32),(ay-32)))
blit(font.render(text,True,(0,0,0)),(32,48))
pygame.display.flip()
时钟滴答声(FPS)

谢谢,我只需添加del(摩尔[0])就可以从屏幕上删除以前的摩尔。现在产卵工作非常顺利