Hyperdrive Enthusiast
Hyperdrive Enthusiast

Reputation: 133

Bullets Not Appearing in Simple Space Invader Game

I'm a new programmer and am trying to figure out why my bullets aren't showing up. It seems that the Y coordinate changes, but for some reason the bullets are not showing up. This is my code in Python:

#Importing necessary modules
import random
import pygame
import sys
#Setting up pygame
pygame.init()
shooting = False
n = 0
keys = [False,False,False,False]
clock = pygame.time.Clock()
screen = pygame.display.set_mode([500,500])
font = pygame.font.Font(None,50)
#Creating class for player
class Player:
    def __init__(self,x,y,width,height):
        self.x = x 
        self.y = y
        self.width = width
        self.height = height
    def draw(self):
        pygame.draw.rect(screen,[0,255,0],
[int(self.x),int(self.y),int(self.width),int(self.height)],0)
    def move(self):
        if keys[1] == True:
            self.x -= 1
        elif keys[3] == True:
            self.x += 1

        if self.x < 0:
            print(self.x)
            self.x = 0
        if self.x > 500 - self.width:
            print(self.x)
            self.x = 500 - self.width
    def shoot(self):
        return
class Bullet:
   def __init__(self,x,y):
     self.x = x 
     self.y = y
   def update(self,y_amount = 5):
      self.y += y_amount
      return
   def draw(self):
      pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),10,30],0)

 bullets = []        



#Creating a player
player = Player(200,450,40,20)
#Main Loop
while True:
    clock.tick(60)
    #Background
    screen.fill([0,0,0])
    #Letting Player move
    player.move()
    #Drawing Player
    player.draw()
    #Updating screen
    pygame.display.flip()
    #Checking for events
    for event in pygame.event.get():
        #Checking for quit
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            #Checking for keys
            if event.key == pygame.K_w:
                keys[0] = True
            elif event.key == pygame.K_a:
                keys[1]=True
            elif event.key == pygame.K_s:
                keys[2]=True
            elif event.key == pygame.K_d:
                keys[3]=True
            elif event.key == pygame.K_SPACE:
                shooting = True


        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                keys[0]=False
            elif event.key == pygame.K_a:
                keys[1]=False
            elif event.key == pygame.K_s:
                keys[2]=False
            elif event.key == pygame.K_d:
                keys[3]=False
            elif event.key == pygame.K_SPACE:
                shooting = False





    if shooting == True:
        bullets.append(Bullet(player.x, player.y))

    for bullet in bullets:
        bullet.update()
        bullet.draw()

Upvotes: 1

Views: 222

Answers (2)

Ajax1234
Ajax1234

Reputation: 71461

I believe that you need to update your screen at the end of the while-loop, not at the beginning:

while True:
   #fill screen
    for event in pygame.event.get():
       #get user input

    pygame.display().flip()

Upvotes: 2

Glenn Rogers
Glenn Rogers

Reputation: 351

Rule 1: Check your coordinate system.

Pygame has (0,0) at the top left, your player is at (x, 450) - at the bottom. When you create a bullet, you do so at the player coordinate and then update the position to increase Y, i.e. move downwards rather than upwards.

Upvotes: 3

Related Questions