我最近在编码中使用了射击率,但是我必须连续按下射击按钮才能进行射击。
有没有一种方法来处理KEYDOWN事件,以便在按下点火按钮时,子弹会定期发射,而不是在我按下点火按钮时发射一颗子弹?
工作代码如下;
import pygame
from constants import *
from player import Player
from Projectile import Projectile
from pygame.math import Vector2
pygame.init()
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption('Labyrinth')
# Spawn player
player = Player(50, 50)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
projectiles = pygame.sprite.Group()
clock = pygame.time.Clock()
previous_time = pygame.time.get_ticks()
speed = 12
done = False
# ----- Event Loop
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
vel = Vector2(-speed, 0)
elif event.key == pygame.K_RIGHT:
vel = Vector2(speed, 0)
elif event.key == pygame.K_UP:
vel = Vector2(0, -speed)
elif event.key == pygame.K_DOWN:
vel = Vector2(0, speed)
if event.key in (pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN):
current_time = pygame.time.get_ticks()
if current_time - previous_time > 500:
previous_time = current_time
projectiles.add(Projectile(player.rect.x, player.rect.y, vel))
# ----- Game Logic
all_sprites_list.update()
projectiles.update()
screen.fill(GREEN)
all_sprites_list.draw(screen)
projectiles.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
from constants import *
import pygame
import time
from datetime import datetime, timedelta
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([15, 15])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.fire_rate = 1
self.change_x = 0
self.change_y = 0
def changespeed(self, x, y):
self.change_x += x
self.change_y += y
def update(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
import pygame
from constants import *
from pygame.math import Vector2
BULLET_IMG = pygame.Surface((4, 4))
BULLET_IMG.fill(RED)
class Projectile(pygame.sprite.Sprite):
def __init__(self, x, y, vel):
super().__init__()
self.image = BULLET_IMG
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.vel = Vector2(vel)
def update(self):
self.rect.move_ip(self.vel)
任何帮助将不胜感激!
答案 0 :(得分:3)
如果您对按下按键而不是按下按键的事实不感兴趣,请使用pygame.key.get_pressed
来获取键盘状态。
解决问题的一种方法是创建一个dict
,将每个箭头键映射到一个矢量,然后简单地迭代该dict
并检查是否按下了特定的键,如下所示:
keymap = {
pygame.K_LEFT: Vector2(-speed, 0),
pygame.K_RIGHT: Vector2(speed, 0),
pygame.K_UP: Vector2(0, -speed),
pygame.K_DOWN: Vector2(0, speed)
}
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
current_time = pygame.time.get_ticks()
pressed = pygame.key.get_pressed()
for key in keymap:
if pressed[key]:
if current_time - previous_time > 500:
previous_time = current_time
projectiles.add(Projectile(player.rect.x, player.rect.y, keymap[key]))
更多注意事项:
您可以将子画面添加到多个组中,通常将所有子画面添加到包含所有子画面的组中,以使主循环保持简单。
您已经有一个名为all_sprites_list
的群组,因此请像这样使用它。也只需将所有新的射弹添加到该组中,然后仅在该组中调用update
/ draw
:
...
if current_time - previous_time > 500:
previous_time = current_time
Projectile(player.rect.x, player.rect.y, keymap[key], all_sprites_list, projectiles)
# ----- Game Logic
all_sprites_list.update()
screen.fill(GREEN)
all_sprites_list.draw(screen)
pygame.display.flip()
...
和Projectile.py中的
...
class Projectile(pygame.sprite.Sprite):
def __init__(self, x, y, vel, *groups):
super().__init__(groups)
...