无法在 pygame 内发射子弹

Not able to shoot bullet in pygame

所以最近我一直在尝试制作我的第一款大型游戏,但我停留在拍摄阶段。基本上,我的问题是我想做到这一点,这样我就可以发射多发子弹并在这样做的同时四处移动,但我似乎无法弄清楚。

代码:

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("Shooter Game!")
#Variables
playerX = 5
playerY = 5
black = (0,0,0)
blue = (0,0,255)
red = (255,0,0)
clicking = False
bulletX = playerX
bulletY = playerY
#End Of Variables#
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
    screen.fill(blue)
    player = pygame.image.load("Young_Link_2D.png").convert_alpha()
    screen.fill(blue)
    player1 = pygame.transform.scale(player, (100,100))
    screen.blit(player1,(playerX,playerY))
    keyPressed = pygame.key.get_pressed()
    #Controls
    if keyPressed[pygame.K_a]:
        playerX -= 1
        bulletX = playerX
    if keyPressed[pygame.K_d]:
        playerX += 1
        bulletX = playerX
    if keyPressed[pygame.K_w]:
        playerY -= 1
        bulletY = playerY
    if keyPressed[pygame.K_s]:
        playerY += 1
        bulletY = playerY
    #End of Controls
    
    #Shooting
    if event.type == pygame.MOUSEBUTTONUP:
        if event.button == 1:
            Bullet = pygame.draw.rect(screen, red, (bulletX+35,bulletY + 60,10,25))
            bulletY = bulletY + 1
    
    pygame.display.update()

我的意思是 classes:

import pygame
import Bullet
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Shooter Game!")
# Variables
playerX = 5
playerY = 5
black = (0, 0, 0)
blue = (0, 0, 255)
red = (255, 0, 0)
clicking = False
bulletX = playerX
bulletY = playerY
bullets = []
newBullet = False
# End Of Variables#
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
    screen.fill(blue)
    player = pygame.image.load("100x100_logo.png").convert_alpha()
    screen.fill(blue)
    player1 = pygame.transform.scale(player, (100, 100))
    screen.blit(player1, (playerX, playerY))
    keyPressed = pygame.key.get_pressed()
    # Controls
    if keyPressed[pygame.K_a]:
        playerX -= 1
        bulletX = playerX
    if keyPressed[pygame.K_d]:
        playerX += 1
        bulletX = playerX
    if keyPressed[pygame.K_w]:
        playerY -= 1
        bulletY = playerY
    if keyPressed[pygame.K_s]:
        playerY += 1
        bulletY = playerY
    # End of Controls

    # Shooting
    if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 1:
            bullets.append(Bullet.Bullet(bulletX + 35, bulletY + 60))

    for i in range(len(bullets)):
        bullets[i].draw(screen, (255, 0, 0))
        bullets[i].move(1)
    print(len(bullets))

    pygame.display.update()

这就是你的主要代码。仍然存在没有冷却时间的错误,因此按住鼠标按钮时会连续创建多个子弹。

import pygame

class Bullet:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def draw(self, win, colour):
        pygame.draw.rect(win, colour, (self.x, self.y, 10, 25))

    def move(self, speed):
        self.y += speed

这就是创建多个实例的要点class

and

您需要管理列表中的项目符号:

bullets = []

当检测到鼠标单击时,将新的项目符号添加到列表中。使用 pygame.Rect 个对象来表示子弹。事件必须在事件循环中处理:

for event in pygame.event.get():
    # [...]

    if event.type == pygame.MOUSEBUTTONUP:
        if event.button == 1:
            bullets.append(pygame.Rect(playerX, playerY, 35, 60))

循环移动子弹。从列表中删除项目符号,当它们离开屏幕时。见 How to remove items from a list while iterating?:

for bullet in bullets[:]:
    bullet.y += 5
    if bullet.top > 600:
        bullets.remove(bullet)

在另一个循环中绘制列表中的所有项目符号:

for bullet in bullets:
    pygame.draw.rect(screen, red, bullet)

完整示例:

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("Shooter Game!")
#Variables
player = pygame.image.load("Young_Link_2D.png").convert_alpha()
playerX, playerY = 5, 5
black = (0,0,0)
blue = (0,0,255)
red = (255,0,0)
clicking = False
bullets = []
#End Of Variables#
clock = pygame.time.Clock()
running = True
while running:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                bullets.append(pygame.Rect(playerX, playerY, 35, 60))
    
    keyPressed = pygame.key.get_pressed()
    playerX += keyPressed[pygame.K_d] - keyPressed[pygame.K_a]
    playerY += keyPressed[pygame.K_s] - keyPressed[pygame.K_w]

    for bullet in bullets[:]:
        bullet.y += 5
        if bullet.top > 600:
            bullets.remove(bullet)

    screen.fill(blue)
    player1 = pygame.transform.scale(player, (100,100))
    screen.blit(player1,(playerX,playerY))
    for bullet in bullets:
        pygame.draw.rect(screen, red, bullet)
    pygame.display.update()

pygame.quit()
exit()