gpt4 book ai didi

Python Pygame 按两个方向键和另一个键射击没有子弹

转载 作者:行者123 更新时间:2023-12-01 06:50:58 25 4
gpt4 key购买 nike

我正在尝试让一艘船发射子弹,一切都按预期工作,除了当我同时按下 UPLEFTSPACE 时kbd>。然后就没有子弹发射了。 RIGHT + DOWN + SPACEUP + LEFT 没有发射子弹+ 空格 任一。

但是,当我同时按 UPRIGHT,然后按 SPACE 时,它就会起作用。事实上,每隔一个方向键 LEFT + DOWNUP + RIGHTUP> + DOWNSPACE 键配合使用。

代码如下:

 #!/usr/bin/python
import sys
import pygame

class Setting:
def __init__(self, width, height):
self.w = width
self.h = height
self.flag = pygame.RESIZABLE
self.color = (255, 255, 255)
self.speed = 1.5
self.screen = pygame.display.set_mode((self.w, self.h), self.flag)
pygame.display.set_caption("Muhaha")
self.bullet_s = 1.5
self.bullet_w = 5
self.bullet_h = 20
self.bullet_c = (0, 0, 0)

class Bullet(pygame.sprite.Sprite):
def __init__(self, setting, char):
super().__init__()
self.rect = pygame.Rect(0, 0, setting.bullet_w, setting.bullet_h)
self.rect.centerx = char.ship_rect.centerx
self.rect.top = char.ship_rect.top
self.color = setting.bullet_c
self.speed = setting.bullet_s
self.y = float(self.rect.centery)

def move_bullet(self):
self.y -= self.speed
self.rect.y = self.y

def draw_bullet(self, setting):
pygame.draw.rect(setting.screen, self.color, self.rect)

def blit_bullet(self, bullets, setting):
for bullet in bullets.sprites():
pygame.draw.rect(setting.screen, bullet.color, bullet.rect)
bullet.y -= bullet.speed
bullet.rect.y = bullet.y


class Char:
def __init__(self, screen, setting):
bk = pygame.image.load("image/muha.png").convert()
self.bkg = pygame.transform.smoothscale(bk, (setting.w, setting.h))
temp = pygame.image.load("image/title.jpg").convert_alpha()
self.ship = pygame.transform.smoothscale(temp, (200, 200))
self.screen = screen
self.screen_rect = screen.get_rect()
self.ship_rect = self.ship.get_rect()

self.ship_rect.center = self.screen_rect.center
self.ship_rect.bottom = self.screen_rect.bottom

self.x = float(self.ship_rect.centerx)
self.y = float(self.ship_rect.centery)

def blit_screen(self):
self.screen.blit(self.bkg, (0, 0))

def blit_ship(self):
self.screen.blit(self.ship, self.ship_rect)


class Check_event:
def __init__(self):
self.moving_up = False
self.moving_down = False
self.moving_left = False
self.moving_right = False

def event(self, bullets, setting, char):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullets.add(Bullet(setting, char))
elif event.key == pygame.K_UP:
self.moving_up = True
elif event.key == pygame.K_DOWN:
self.moving_down = True
elif event.key == pygame.K_LEFT:
self.moving_left = True
elif event.key == pygame.K_RIGHT:
self.moving_right = True

elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
self.moving_up = False
elif event.key == pygame.K_DOWN:
self.moving_down = False
elif event.key == pygame.K_LEFT:
self.moving_left = False
elif event.key == pygame.K_RIGHT:
self.moving_right = False

def move(self, setting, char):
if self.moving_up == True and char.ship_rect.top >= 0:
char.y -= setting.speed
if self.moving_down == True and char.ship_rect.bottom <= setting.h:
char.y += setting.speed
if self.moving_left == True and char.ship_rect.left >= 0:
char.x -= setting.speed
if self.moving_right == True and char.ship_rect.right <= setting.w:
char.x += setting.speed
# press both "up" &"left" then press "space"
char.ship_rect.centerx = char.x
char.ship_rect.centery = char.y

def game():
pygame.init()
setting = Setting(1200, 800)
screen = setting.screen
char = Char(screen, setting)
check_event = Check_event()
bullet = Bullet(setting, char)
bullets = pygame.sprite.Group()


while True:
check_event.event(bullets, setting, char)

char.blit_screen()
check_event.event(bullets, setting, char)
check_event.move(setting, char)
bullet.blit_bullet(bullets, setting)

char.blit_ship()
pygame.display.flip()
game()

最佳答案

如果您按 UP + LEFT + SPACESPACE 键似乎不会被按下立即地。您必须释放 UPLEFT 键才能获取 SPACEpygame.KEYDOWN 事件。

这是 pygame 中的一个已知错误,似乎尚未解决:Incorrect handling of pressed keys #235

可悲的是pygame.key.get_pressed()在这种情况下没有声明 SPACE 键,所以我什至想不出解决方法。

关于Python Pygame 按两个方向键和另一个键射击没有子弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59004524/

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