使用Pygame在Python中创建多个项目符号的最佳方法是什么?

时间:2011-12-30 01:23:32

标签: python pygame

我刚刚开始学习Python两天前,所以我的代码很草率......但我只是想尝试一些有用的东西。我正在做一个简单的shmup。我试图通过按住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 个答案:

答案 0 :(得分:2)

你的代码对我来说有点难以理解 - 我对自己编程很新。但是,我确实有一些建议可能会有所帮助。

首先,您似乎在子弹的坐标中进行了硬编码(即bullet1bullet2bX1bY1等。因为你似乎无法发射超过5个子弹,我假设你没有将子弹的x和y坐标以及与它们相关的其他变量在移出屏幕之后重置为零。 / p>

另外,尝试将您的项目符号作为一个类实现,并将所有项目符号放在一个列表中,这样您就可以拥有5个以上的项目符号。这样,您就可以整齐地封装单个项目符号所需的所有数据,这样您就可以执行以下操作: bullet1.x = 3bullets_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)。这种方式可能更灵活。

相关问题