Reputation: 98
I just started studying Python two days ago, so my code is sloppy...But I'm just trying to get something that works. I'm working on a simple shmup. I'm trying to get bullets to fire by holding down on the Z key...The problem I'm facing at the moment is getting multiple bullets to work correctly. No matter what I do, after five are shot, I can't shoot anymore. When I only had one bullet, I couldn't shoot it anymore if I was still holding down the Z key when the object state changed to remove it. Here's my code...
#!/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
Upvotes: 1
Views: 3021
Reputation: 64068
Your code was a little hard to follow for me -- I'm fairly new to programming myself. However, I do have a few suggestions that might help a little.
First, you seem to be hardcoding in the coordinates of your bullets (ie bullet1
, bullet2
, and bX1
, bY1
etc.). Because you seem to be unable to fire more then 5 bullets, I'm assuming that you're not resetting your bullets' x and y coordinates, along with other variables related to them to zero after it moves off the screen or something.
Also, try implementing your bullets as a class and put all your bullets in a list so you can have more then 5. This way, you can neatly encapsulate all the data you need for a single bullet so you can do things like bullet1.x = 3
or bullets_array[1].y = 3
instead of bX1 = 3
.
(btw, I see you started learning Python just recently. I highly suggest you learn about lists, objects, and object-oriented programming (often abbreviated OOP). And dictionaries. They'll be your new best friends. OOP might be a little difficult to grok at first (at least, it was for me), but it'll be worth it.)
For example,
# 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.
If you want something a bit more complicated, try using Pygame's Sprite and Group classes.
http://pygame.org/docs/ref/sprite.html
Basically, instead of creating the Bullet
class yourself, you base it on pygame.sprite.Sprite
, implement whatever methods you need, then add it to a group (pygame.sprite.Group
). That way would probably be more flexible.
Upvotes: 2