Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7017371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:54:19+00:00 2026-05-27T22:54:19+00:00

I just started studying Python two days ago, so my code is sloppy…But I’m

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:54:20+00:00Added an answer on May 27, 2026 at 10:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started working with Mercurial a few days ago and there's something I don't
I download asp.net mvc2 preview 2 days back. I've just started studying MVC. I
Just started using log4net and trying to get my head around the config and
Just started doing some code porting from .Net CF to Blackberry JDE 4.6.1. But
I have just started studying Python . I am referring to the tutorials at
I have just started studying Android, I have limited java knowledge but am semi
I just started studying programming about 6 months ago and I have really been
I'm new to using vectors...I just started studying them today. I'm trying to create
Just started to get my feet wet with C# and .NET, liking it so
I just started studying Drupal 7 and learned how powerful CCK and Views are.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.