gpt4 book ai didi

python - 只要按住拍摄按钮,如何继续拍摄

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

我最近在我的代码中加入了射速,但是,我必须连续按下开火按钮才能射击。

有没有一种方法可以处理 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)

如有任何帮助,我们将不胜感激!

最佳答案

如果您对按下某个键这一事实不感兴趣,而是对某个键是否被按下感兴趣,请使用 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]))

一些进一步的说明:

您可以将 sprite 添加到多个组,将所有 sprite 添加到一个包含所有 sprite 的组以保持主循环简单通常是有意义的。

你已经有一个名为 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)
...

关于python - 只要按住拍摄按钮,如何继续拍摄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006196/

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