gpt4 book ai didi

python - 按下拍摄按钮时控制拍摄频率?

转载 作者:太空宇宙 更新时间:2023-11-03 15:30:23 25 4
gpt4 key购买 nike

当按下按钮时,子弹会立即发射,但如果按下发射按钮,我只希望它们每 0.5 秒发射一次。

我试图导入时间并延迟子弹,但它只是延迟了 pygame 本身。

有什么方法可以调节角色射击的频率吗?

import pygame

pygame.init()

win = pygame.display.set_mode((700, 480))
pygame.display.set_caption("First project")
run = True
red = (255, 0, 0)
green = (0, 255, 0)


def drawbg():
pygame.display.update()
win.fill((255, 255, 255))
for bullet in bullets:
bullet.draw(win)


class person(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.IsJump = False
self.jumpCount = 10
self.left = False
self.right = False

class projectile(object):
def __init__(self, x, y, radius, colour, facing):
self.x = x
self.y = y
self.radius = radius
self.colour = colour
self.facing = facing
self.vel = 7 * facing

def draw(self, win):
pygame.draw.circle(win, self.colour, (self.x, self.y), self.radius)

man = person(100, 400, 50, 60)
man2 = person(500, 400, 50, 60)
bullets = []

while run:
pygame.time.delay(25)

for bullet in bullets:
if bullet.x < 700 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))


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

keys = pygame.key.get_pressed()

if keys[pygame.K_g]:
if man.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(projectile(man.x + man.width//2, round(man.y + man.height/2), 6, (100, 100, 100), facing))


if keys[pygame.K_p]:
if man2.left:
facing = -1
else:
facing = 1
if len(bullets) < 50:
bullets.append(projectile(man2.x + man2.width//2, round(man2.y + man2.height/2), 6, (0, 0, 0), facing))

if keys[pygame.K_LEFT] and man2.x > man2.vel:
man2.x -= man2.vel
man2.left = True
man2.right = False

if keys[pygame.K_RIGHT] and man2.x < 700 - man2.width - man2.vel:
man2.x += man2.vel
man2.right = True
man2.left = False

if keys[pygame.K_a] and man.x > man.vel:
man.x -= man.vel
man.left = True
man.right = False

if keys[pygame.K_d] and man.x < 700 - man.width - man.vel:
man.x += man.vel
man.right = True
man.left = False


if not man.IsJump and keys[pygame.K_w]:
man.IsJump = True
man.JumpCount = 10

if man.IsJump:
if man.JumpCount >= -10:
neg = 1
if man.JumpCount < 0:
neg = -1
man.y -= (man.JumpCount ** 2) / 2 * neg
man.JumpCount -= 1
else:
man.IsJump = False
man.JumpCount = 10

if not man2.IsJump and keys[pygame.K_UP]:
man2.IsJump = True
man2.JumpCount = 10

if man2.IsJump:
if man2.JumpCount >= -10:
neg = 1
if man2.JumpCount < 0:
neg = -1
man2.y -= (man2.JumpCount ** 2) / 2 * neg
man2.JumpCount -= 1
else:
man2.IsJump = False
man2.JumpCount = 10

pygame.draw.rect(win, red, (man.x, man.y, man.width, man.height))
pygame.draw.rect(win, green, (man2.x, man2.y, man2.width, man2.height))
drawbg()

pygame.quit()

最佳答案

您可以查看最后一颗子弹发射的时间。

通过以下方式启动变量:

import time

last_bullet = time.time()

然后将您的代码更改为:

if len(bullets) < 5:
if time.time() - last_bullet > 0.5:
last_bullet = time.time()
bullets.append(projectile(man.x + man.width//2, round(man.y + man.height/2), 6, (100, 100, 100), facing))

关于python - 按下拍摄按钮时控制拍摄频率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com