我两天前才刚开始研究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
最佳答案
对于我来说,您的代码有点难以理解-我对自己编程还很陌生。但是,我确实有一些建议可能会有所帮助。
首先,您似乎在硬编码项目符号的坐标(即bullet1
,bullet2
和bX1
,bY1
等)。由于您似乎无法发射5枚以上的子弹,因此我假设您没有将子弹的x和y坐标以及与它们相关的其他变量从屏幕上移开后重置为零。
另外,尝试将您的项目符号作为一个类来实现,并将所有项目符号放在列表中,这样您就可以拥有更多的项目符号。这样,您就可以整齐地封装单个项目符号所需的所有数据,从而可以执行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
)中。这样可能会更灵活。关于python - 使用Pygame在Python中创建多个项目符号的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8675268/