在Pygame中如何使用Python创建多个子弹?

1 投票
1 回答
2995 浏览
提问于 2025-04-17 09:14

我刚开始学习Python,才两天,所以我的代码写得很乱……不过我只是想让它能运行起来。我正在做一个简单的射击游戏。我想通过按住Z键来发射子弹……现在我遇到的问题是,想让多个子弹正常发射。无论我怎么做,发射了五发之后,就再也发不出了。当我只有一发子弹的时候,如果我在物体状态改变并移除它时还按着Z键,我也发不出那一发。以下是我的代码……

#!/usr/bin/python

import sys, pygame
pygame.init() 

windowSize = width, height = 640, 480
screen = pygame.display.set_mode((windowSize))
pygame.display.set_caption("rzrscm")
clock = pygame.time.Clock()

background = pygame.Surface(screen.get_size())
background = background.convert()

image = pygame.image.load("image.png")
Font = pygame.font.Font("font.ttf",12)
text = Font.render("PIXELFUCKER",1,(255,255,255))
textpos = text.get_rect(centerx=background.get_width()/2)

pygame.mixer.music.load("music.xm")
pygame.mixer.init(44100, -16, 1, 1024)
pygame.mixer.music.play(-1,0.0)

quit = False

eX = 0
eY = 50
dotState = 1

bullet1 = 0
bullet2 = 0
bullet3 = 0
bullet4 = 0
bullet5 = 0

shot = 0
wait = 0

x = 300
y = 300
pX = 0
pY = 0

while quit == False:

    background.fill((0,0,0))

    x += pX
    if x < 0 or x > 640:
        x -= pX
    if x == eX and y == eY:
    x -= pX
    y += pY
    if y < 0 or y > 480:
        y -= pY

    wait = wait + 1

    if shot == 1:     
        if bullet1 == 0 and bullet5 == 0:
            bullet1 = 1
            wait = 0
        if bullet1 == 1 and bullet2 == 0 and wait == 25:
            bullet2 = 1
            wait = 0
        if bullet2 == 1 and bullet3 == 0 and wait == 25:
            bullet3 = 1
            wait = 0
        if bullet3 == 1 and bullet4 == 0 and wait == 25:
            bullet4 = 1
            wait = 0
        if bullet4 == 1 and bullet5 == 0 and wait == 25:
            bullet5 = 1
            wait = 0

    if dotState != 3:  
        background.set_at((eX, eY),(255,255,255))
    if eX == 640:
        dotState = 2
    if eX == 0:
        dotState = 1
    if dotState == 1:
        eX = eX + 1
    if eX == x and eY == y:
    eX = eX - 1
    if dotState == 2:       
        eX = eX - 1
    if eX == x and eY == y:
    eX = eX + 1  

    if bullet1 == 0:
        bX = x
        bY = y
    if bullet1 == 1:
    bY = bY - 5
    background.set_at((bX, bY),(255,255,255)) 
    if bY == 0:
    bullet1 = 0
    if bY == eY and bX == eX:
        bullet1 = 0
        dotState = 3
    if bullet2 == 0:
        bX2 = x
        bY2 = y
    if bullet2 == 1:
    bY2 = bY2 - 5
    background.set_at((bX2, bY2),(255,255,255)) 
    if bY2 == 0:
    bullet2 = 0
    if bY2 == eY and bX2 == eX:
        bullet2 = 0
    if bullet3 == 0:
        bX3 = x
        bY3 = y
    if bullet3 == 1:
    bY3 = bY3 - 5
    background.set_at((bX3, bY3),(255,255,255)) 
    if bY3 == 0:
    bullet3 = 0
    if bY3 == eY and bX3 == eX:
        bullet3 = 0
        dotState = 3
    if bullet4 == 0:
        bX4 = x
        bY4 = y
    if bullet4 == 1:
    bY4 = bY4 - 5
    background.set_at((bX4, bY4),(255,255,255)) 
    if bY4 == 0:
    bullet4 = 0
    if bY4 == eY and bX4 == eX:
        bullet4 = 0
        dotState = 3
    if bullet5 == 0:
        bX5 = x
        bY5 = y
    if bullet5 == 1:
    bY5 = bY5 - 5
    background.set_at((bX5, bY5),(255,255,255)) 
    if bY5 == 0:
    bullet5 = 0
    if bY5 == eY and bX5 == eX:
        bullet5 = 0
        dotState = 3

background.set_at((x, y),(255,255,255))
background.blit(text,textpos)
screen.blit(background,(0,0))  

pygame.display.flip()
clock.tick(250) 

for event in pygame.event.get(): 
    if event.type == pygame.QUIT: 
        sys.exit(0)

    if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
        pX -= 2
    if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
        pX += 2
    if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
        pY -= 2
    if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
        pY += 2       

    if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
        pX += 2
    if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
        pX -= 2
    if event.type == pygame.KEYUP and event.key == pygame.K_UP:
        pY += 2
    if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
        pY -= 2       

    if event.type == pygame.KEYDOWN and event.key == pygame.K_z:
        shot = 1
    if event.type == pygame.KEYUP and event.key == pygame.K_z:
        shot = 0

1 个回答

2

你的代码对我来说有点难懂——我自己也刚开始学编程。不过,我有几个建议,可能会对你有帮助。

首先,你似乎是把子弹的位置(比如 bullet1bullet2,还有 bX1bY1 等)写死在代码里。因为你似乎只能发射5颗子弹,我猜是因为你没有在子弹离开屏幕后,把它们的 x 和 y 坐标以及其他相关的变量重置为零。

另外,试着把子弹做成一个类,并把所有的子弹放在一个列表里,这样你就可以发射超过5颗子弹了。这样做可以把每颗子弹需要的数据整齐地封装在一起,你就可以像这样操作:bullet1.x = 3 或者 bullets_array[1].y = 3,而不是 bX1 = 3

(顺便说一下,我看到你最近开始学习Python。我强烈建议你学习列表、对象和面向对象编程(通常简称为OOP),还有字典。这些会成为你新的好朋友。OOP一开始可能有点难理解(至少我觉得是这样),但学会了会很有用的。)

例如,

# snip initialization, etc.

class Bullet():
    def __init__(self, surface, x_coord, y_coord):
        self.surface = surface
        self.x = x_coord
        self.y = y_coord
        return

    def update(self, y_amount=5):
        self.y += y_amount
        self.surface.set_at((self.x, self.y),(255,255,255))
        return

bullets_array = []

# snip 

while quit == false:    # Inside the main loop...
    for event in pygame.event.get():
        #snip
        if event.type == pygame.KEYDOWN and event.key == pygame.K_z:
            bullets_array.append(background, player_x, player_y)

    #snip

    for bullet in bullets_array:
        bullet.update()
        # ...and check if the bullet is off the screen.
        # If so, remove it from the array.

如果你想做一些更复杂的事情,可以试试使用Pygame的Sprite和Group类。

http://pygame.org/docs/ref/sprite.html

基本上,你可以不自己创建 Bullet 类,而是基于 pygame.sprite.Sprite 来实现,添加你需要的方法,然后把它放到一个组里(pygame.sprite.Group)。这样做可能会更灵活。

撰写回答