Can anyone help me figure out the rest of the code for spawning and shooting bullets

Joined
Jul 25, 2024
Messages
18
Reaction score
1
Code:
import pygame, sys
from pygame.locals import *
pygame.init()

#Colours
BACKGROUND = (255, 255, 255)

CHARACTER = (255, 30, 70)

bullets = []

# Game Setup
FPS = 60
fpsClock = pygame.time.Clock()
WINDOW_WIDTH = 1500
WINDOW_HEIGHT = 750

WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("My Game!")

# The Main function that controls the game\
def main():
    looping = True
    
    characterHeight = 70
    characterWidth = 50
    characterX = 750
    characterY = 325

    # The main game loop
    while looping:
        # Get inputs
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        
        pressed = pygame.key.get_pressed()
        if (pressed[K_RIGHT]):
            characterX = characterX + 5
        if (pressed[K_LEFT]):
            characterX = characterX - 5
        if (pressed[K_DOWN]):
            characterY = characterY + 5
        if (pressed[K_UP]):
            characterY = characterY - 5
        if (pressed[K_SPACE]):
            bullets.append(bullet)
            bullet = bullet + 10


        # Processing
        # This section will be built out later

        
        if (characterX + characterWidth > WINDOW_WIDTH):
            characterX = 0
        if (characterX < 0):
            characterX = WINDOW_WIDTH - characterWidth
        if (characterY + characterHeight > WINDOW_HEIGHT):
            characterY = 0
        if (characterY < 0):
            characterY = WINDOW_HEIGHT - characterHeight
        bullet = pygame.Rect(characterX, characterY, 10, 10)
        character = pygame.Rect(characterX, characterY, characterWidth, characterHeight)

        # Render elements of the game
        WINDOW.fill(BACKGROUND)
        pygame.draw.rect(WINDOW, CHARACTER, character)
        pygame.draw.rect(WINDOW, CHARACTER, bullet)
        pygame.display.update()
        fpsClock.tick(FPS)
        
main()
 
Joined
Dec 10, 2022
Messages
106
Reaction score
26
You should think about using classes. Would make thinks a lot simpler.
Check out some of the games I made and you will see what I'm talking about.
My Scripts
As it stands now with this code you will need to keep the space bar pressed for the bullet to move.
When the player moves so does the bullet.

Python:
import pygame, sys
from pygame.locals import *
pygame.init()

#Colours
BACKGROUND = (255, 255, 255)

CHARACTER = (255, 30, 70)

# bullets = []

# Game Setup
FPS = 60
fpsClock = pygame.time.Clock()
WINDOW_WIDTH = 1500
WINDOW_HEIGHT = 750

WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("My Game!")



# The Main function that controls the game\
def main():
    looping = True
   
    characterHeight = 70
    characterWidth = 50
    characterX = 750
    characterY = 325

    yspeed = 0

    # The main game loop
    while looping:
        # Get inputs
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
           
       
        pressed = pygame.key.get_pressed()
        if (pressed[K_RIGHT]):
            characterX = characterX + 5
        if (pressed[K_LEFT]):
            characterX = characterX - 5
        if (pressed[K_DOWN]):
            characterY = characterY + 5
        if (pressed[K_UP]):
            characterY = characterY - 5
        if pressed[K_SPACE]:
            yspeed -= 5
   


        # Processing
        # This section will be built out later

       
        if (characterX + characterWidth > WINDOW_WIDTH):
            characterX = 0
        if (characterX < 0):
            characterX = WINDOW_WIDTH - characterWidth
        if (characterY + characterHeight > WINDOW_HEIGHT):
            characterY = 0
        if (characterY < 0):
            characterY = WINDOW_HEIGHT - characterHeight
       
       
        character = pygame.Rect(characterX, characterY, characterWidth, characterHeight)
        bullet = pygame.Rect(character.centerx-5, character.centery-5, 10, 10)
         
       
        bullet.y += yspeed
       
        if bullet.bottom <= 0:
            bullet.centery = character.centery
            yspeed = 0
             
        # Render elements of the game
        WINDOW.fill(BACKGROUND)
        pygame.draw.rect(WINDOW, 'blue', bullet)
        pygame.draw.rect(WINDOW, CHARACTER, character)
        pygame.display.update()
        fpsClock.tick(FPS)
       
main()
 
Last edited:
Joined
Dec 10, 2022
Messages
106
Reaction score
26
Here is a better way. Uses sprites and sprite groups. To detect sprite collisions you will need to create more groups.
Example: you have enemies you would need one for each, player, enemy, and bullet.

Python:
import pygame

pygame.init()

clock = pygame.time.Clock()
fps = 60

screen_size = (1280, 740)
screen = pygame.display.set_mode(screen_size)

# Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)


# Create sprite group
allsprites = pygame.sprite.Group()


class Player(pygame.sprite.Sprite):
    ''' Player class create the player '''
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.rect.centerx = screen_size[0]/2 - self.rect.width/2
        self.rect.bottom = screen_size[1]-5
        self.speed = 0

    def update(self):
        ''' Method updates player movement '''
        self.speed = 0

        state = pygame.key.get_pressed()
        if state[pygame.K_LEFT]:
            self.speed = -8
        if state[pygame.K_RIGHT]:
            self.speed = 8

        self.rect.x += self.speed

        if self.rect.left <= 0:
            self.rect.left = 0
        if self.rect.right >= screen_size[0]:
            self.rect.right = screen_size[0]

    def fire(self):
        ''' Method creates the bullet from Bullet class'''
        bullet = Bullet(self.rect.centerx, self.rect.top)

        # Add bullet to sprite group
        allsprites.add(bullet)


class Bullet(pygame.sprite.Sprite):
    ''' Class creates the bullet '''
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        print('here')
        self.image = pygame.Surface((5,20))
        self.image.fill(green)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.speed = 10

    def update(self):
        ''' Method update bullet movement '''
        self.rect.y -= self.speed

        # If bullet goes of the top of screen, kill the sprite
        if self.rect.bottom <= 0:
            self.kill()


# Create player and add to sprite group
player = Player()
allsprites.add(player)


running = True
while running:
    screen.fill(black)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.fire()


    # Update and draw all sprites
    allsprites.update()
    allsprites.draw(screen)
    
    pygame.display.update()
    clock.tick(fps)

pygame.quit()
 
Joined
Jul 25, 2024
Messages
18
Reaction score
1
while your helping could you help me with the asteroids being destroyed when hit by bullet and adding text on screen to keep track of score
Code:
import pygame
from pygame.locals import *
import random

pygame.init()

clock = pygame.time.Clock()
fps = 60

screen_size = (1280, 740)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Asteroids")

# Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
cyan =  (0,255,255)


# Create sprite group
allsprites = pygame.sprite.Group()


class Player(pygame.sprite.Sprite):
    ''' Player class create the player '''
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.rect.centerx = screen_size[0]/2 - self.rect.width/2
        self.rect.bottom = screen_size[1]-5
        self.speed = 0

    def update(self):
        ''' Method updates player movement '''
        self.speed = 0

        state = pygame.key.get_pressed()
        if state[pygame.K_LEFT]:
            self.speed = -8
        if state[pygame.K_RIGHT]:
            self.speed = 8

        self.rect.x += self.speed

        if self.rect.left <= 0:
            self.rect.left = 0
        if self.rect.right >= screen_size[0]:
            self.rect.right = screen_size[0]

    def fire(self):
        ''' Method creates the bullet from Bullet class'''
        bullet = Bullet(self.rect.centerx, self.rect.top)

        # Add bullet to sprite group
        allsprites.add(bullet)


class Bullet(pygame.sprite.Sprite):
    ''' Class creates the bullet '''
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        print('here')
        self.image = pygame.Surface((5,20))
        self.image.fill(green)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.speed = 10

    def update(self):
        ''' Method update bullet movement '''
        self.rect.y -= self.speed

        # If bullet goes of the top of screen, kill the sprite
        if self.rect.bottom <= 0:
            self.kill()

class Asteroid(pygame.sprite.Sprite):
    ''' Class creates the asteroid '''
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill(cyan)
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(0, screen_size[0]-self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.speed = random.randrange(2, 6)

    def update(self):
        ''' Method updates asteroid movement '''
        self.rect.y += self.speed

        # If asteroid goes off the bottom of the screen, reset it
        if self.rect.top > screen_size[1]:
            self.rect.x = random.randrange(0, screen_size[0]-self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.speed = random.randrange(2, 6)
            # Create background stars
            for i in range(100):
                star = pygame.Surface((2,2))
                star.fill(white)
                star_x = random.randrange(0, screen_size[0])
                star_y = random.randrange(0, screen_size[1])
                screen.blit(star, (star_x, star_y))

def points(self):
    ''' Method calculates the points for the player '''
    points = 0
    for asteroid in allsprites:
        if isinstance(asteroid, Asteroid):
            points += 10
    return points

def bullet_hits_asteroid(self):
    ''' Method checks if bullet hits asteroid '''
    for asteroid in allsprites:
        if isinstance(asteroid, Asteroid):
            if pygame.sprite.collide_rect(self, asteroid):
                self.kill(asteroid)
                asteroid.kill()
                return True
    return False
# Create asteroids
for i in range(10):
    asteroid = Asteroid()
    allsprites.add(asteroid)

# Create player and add to sprite group
player = Player()
allsprites.add(player)


running = True
while running:
    screen.fill(black)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.fire()
    bullet_hits_asteroid(player)
    points(player)


    # Update and draw all sprites
    allsprites.update()
    allsprites.draw(screen)
    
    pygame.display.update()
    clock.tick(fps)

pygame.quit()
 
Joined
Dec 10, 2022
Messages
106
Reaction score
26
All right here is something for you to work with. Note when asteroid hits the player, game exits and score is lost.

Python:
import pygame
from random import randrange, choice

pygame.init()
pygame.font.init()

clock = pygame.time.Clock()
fps = 60

screen_size = (1280, 740)
screen = pygame.display.set_mode(screen_size)

# Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)


# Create sprite group
allsprites = pygame.sprite.Group()
playersprite = pygame.sprite.Group()
asteroidsprite = pygame.sprite.Group()
bulletsprite = pygame.sprite.Group()


class Player(pygame.sprite.Sprite):
    ''' Player class create the player '''
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.rect.centerx = screen_size[0]/2 - self.rect.width/2
        self.rect.bottom = screen_size[1]-5
        self.speed = 0
        self.score = 0

        playersprite.add(self)
        allsprites.add(playersprite)

    def update(self):
        ''' Method updates player movement '''
        self.speed = 0

        state = pygame.key.get_pressed()
        if state[pygame.K_LEFT]:
            self.speed = -8
        if state[pygame.K_RIGHT]:
            self.speed = 8

        self.rect.x += self.speed

        if self.rect.left <= 0:
            self.rect.left = 0
        if self.rect.right >= screen_size[0]:
            self.rect.right = screen_size[0]

    def fire(self):
        ''' Method creates the bullet from Bullet class'''
        bullet = Bullet(self.rect.centerx, self.rect.top)   


class Bullet(pygame.sprite.Sprite):
    ''' Class creates the bullet '''
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((5,20))
        self.image.fill(green)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.speed = 10
        bulletsprite.add(self)
        allsprites.add(bulletsprite)

    def update(self):
        ''' Method update bullet movement '''
        self.rect.y -= self.speed

        # If bullet goes of the top of screen, kill the sprite
        if self.rect.bottom <= 0:
            self.kill()


class asteroid(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((randrange(25,50), randrange(25,50)))
        self.image.fill(choice(['brown', 'gold','purple', 'slateblue', 'tomato', 'teal', 'orange']))
        self.rect = self.image.get_rect()
        self.rect.x = randrange(20, screen_size[0]-20)
        self.rect.y = randrange(-20, -5)
        self.speedx = randrange(-5, 8)
        self.speedy = self.speedy = randrange(5, 8)

        asteroidsprite.add(self)
        allsprites.add(asteroidsprite)

    def update(self):
        self.rect.x += self.speedx
        self.rect.y += self.speedy

        if self.rect.right <= -5:
            self.kill()
            asteroid()

        if self.rect.left >= screen_size[0]+10:
            self.kill()
            asteroid()

        if self.rect.top >= screen_size[1]+10:
            self.rect.y = randrange(-30, -10)
            self.rect.x = randrange(20, screen_size[0]-20)


# Create player and add to sprite group
player = Player()
allsprites.add(player)

# Create random range of asteroids
n = randrange(5, 8)
for i in range(n ):
    asteroid()


running = True
while running:
    screen.fill(black)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.fire()


    # Detect collisions
    bullet_collision = pygame.sprite.groupcollide(bulletsprite, asteroidsprite, True, True)
    asteroid_collision = pygame.sprite.groupcollide(asteroidsprite, playersprite, True, True)
    
    # If a bullet hits asteroid create another asteroid
    if bullet_collision:
        player.score += 1
        asteroid()

    # If asteroid hits player game over
    if asteroid_collision:
        running = False

    # Create font, create font surface blit to screen
    font = pygame.font.SysFont(None, 45)
    text_surface = font.render(f'Score: {player.score}', True, white)
    screen.blit(text_surface, (10,10))

    # Update and draw all sprites
    allsprites.update()
    allsprites.draw(screen)
    
    pygame.display.update()
    clock.tick(fps)

pygame.quit()
 
Joined
Dec 10, 2022
Messages
106
Reaction score
26
One more. This gives a choice of play again or exit. Also gave the asteroids a rotation as the are falling.

Python:
import pygame
from random import randrange, choice

pygame.init()
pygame.font.init()

clock = pygame.time.Clock()
fps = 60

screen_size = (1280, 740)
screen = pygame.display.set_mode(screen_size)

# Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)


# Create sprite group
allsprites = pygame.sprite.Group()
playersprite = pygame.sprite.Group()
asteroidsprite = pygame.sprite.Group()
bulletsprite = pygame.sprite.Group()

def create_color():
    color = ()
    for i in range(3):
        n = randrange(50,255)
        color = color + (n,)
    return color

class Player(pygame.sprite.Sprite):
    ''' Player class create the player '''
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.rect.centerx = screen_size[0]/2 - self.rect.width/2
        self.rect.bottom = screen_size[1]-5
        self.speed = 0
        self.score = 0

        playersprite.add(self)
        allsprites.add(playersprite)

    def update(self):
        ''' Method updates player movement '''
        self.speed = 0

        state = pygame.key.get_pressed()
        if state[pygame.K_LEFT]:
            self.speed = -8
        if state[pygame.K_RIGHT]:
            self.speed = 8

        self.rect.x += self.speed

        if self.rect.left <= 0:
            self.rect.left = 0
        if self.rect.right >= screen_size[0]:
            self.rect.right = screen_size[0]

    def fire(self):
        ''' Method creates the bullet from Bullet class'''
        bullet = Bullet(self.rect.centerx, self.rect.top)   


class Bullet(pygame.sprite.Sprite):
    ''' Class creates the bullet '''
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((5,20))
        self.image.fill(green)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.speed = 10
        bulletsprite.add(self)
        allsprites.add(bulletsprite)

    def update(self):
        ''' Method update bullet movement '''
        self.rect.y -= self.speed

        # If bullet goes of the top of screen, kill the sprite
        if self.rect.bottom <= 0:
            self.kill()


class Asteroid(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image0 = pygame.Surface((randrange(25,50), randrange(25,50)), pygame.SRCALPHA)
        self.image0.fill(create_color())
        
        # Create copy of image for use of rotating animations
        self.image = self.image0.copy()
        self.rect = self.image.get_rect()
        self.rect.x = randrange(20, screen_size[0]-20)
        self.rect.y = randrange(-20, -5)
        self.speedx = randrange(-5, 8)
        self.speedy = self.speedy = randrange(3, 8)

        # Below is used for rotation
        self.rot = 0
        self.rot_speed = choice((randrange(-8,-4), randrange(4,8)))
        self.last_update = pygame.time.get_ticks()

 
        asteroidsprite.add(self)
        allsprites.add(asteroidsprite)

    def update(self):
        # Get current pygame time
        now = pygame.time.get_ticks()
        # If greater then 100, rotate and update
        if now - self.last_update > 100:
            self.last_update = now
            self.rot = (self.rot + self.rot_speed)%360
            new_image = pygame.transform.rotate(self.image0, self.rot)

            # To keep center of image
            old_center = self.rect.center
            self.image = new_image
            self.rect = self.image.get_rect()
            self.rect.center = old_center
            
        self.rect.x += self.speedx
        self.rect.y += self.speedy


        if self.rect.right <= -5:
            self.kill()
            Asteroid()

        if self.rect.left >= screen_size[0]+10:
            self.kill()
            Asteroid()

        if self.rect.top >= screen_size[1]+10:
            self.rect.y = randrange(-30, -10)
            self.rect.x = randrange(20, screen_size[0]-20)


# Create player and add to sprite group
player = Player()


# Create random range of asteroids
n = randrange(5, 8)
for i in range(n ):
    Asteroid()

stop = False

running = True
while running:
    screen.fill(black)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not stop:
                player.fire()

            if stop and event.key == pygame.K_ESCAPE:
                running = False

            if stop and event.key == pygame.K_RETURN:
                player.score = 0
                stop = False
                player = Player()
                for i in range(randrange(5,8)):
                    Asteroid()


    # Detect collisions
    bullet_collision = pygame.sprite.groupcollide(bulletsprite, asteroidsprite, True, True)
    asteroid_collision = pygame.sprite.groupcollide(asteroidsprite, playersprite, True, True)
    
    # If a bullet hits asteroid create another asteroid
    if bullet_collision:
        player.score += 1
        Asteroid()

    # If asteroid hits player game over
    if asteroid_collision:
        stop = True
        allsprites.empty()
        playersprite.empty()
        asteroidsprite.empty()
        bulletsprite.empty()

    if stop:
        font = pygame.font.SysFont(None, 45)
        text = 'Press esc to exit'
        text_surface = font.render(text, True, white)
        screen.blit(text_surface, (screen_size[0]/2-font.size(text)[0]/2, screen_size[1]/2))

        text = 'Press return/enter to play again'
        text_surface = font.render(text, True, green)
        screen.blit(text_surface, (screen_size[0]/2-font.size(text)[0]/2, screen_size[1]/2+screen_size[1]*0.15))

    # Create font, create font surface blit to screen
    font = pygame.font.SysFont(None, 45)
    text_surface = font.render(f'Score: {player.score}', True, white)
    screen.blit(text_surface, (10,10))

    # Update and draw all sprites
    allsprites.update()
    allsprites.draw(screen)
    
    pygame.display.update()
    clock.tick(fps)

pygame.quit()
 
Last edited:
Joined
Dec 10, 2022
Messages
106
Reaction score
26
Here is one that is more of the asteroid type game from days past.

Python:
import pygame

vec = pygame.math.Vector2
import math

pygame.init()

clock = pygame.time.Clock()
fps = 60

screen_size = (800, 800)
screen = pygame.display.set_mode(screen_size)

# Colors
black = (0,0,0)
white = (255,255,255)

allsprites = pygame.sprite.Group()
playersprite = pygame.sprite.Group()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image0 = pygame.Surface((40,40))
        width, height = self.image0.get_size()
        pygame.draw.polygon(self.image0, white, [(width/2, 2), (width-2, height-2), (2, width-2)], 2)
        self.image = self.image0.copy()
        self.rect = self.image.get_rect()
        self.pos = vec(screen_size[0]/2, screen_size[1]/2)
        self.radius = width/2
        self.vel = vec(0, 0)
        self.direction = vec(0, -8.0)
        self.angle = 0
        self.angle_speed = 0
        self.speed = 1.5
        
        playersprite.add(self)
        allsprites.add(playersprite)

    def update(self):
        self.vel = vec(0, 0)
        self.angle_speed = 0

        state = pygame.key.get_pressed()
        if state[pygame.K_LEFT]:
            self.angle_speed = 5
            self.rotate()

        if state[pygame.K_RIGHT]:
            self.angle_speed = -5
            self.rotate()

        if state[pygame.K_UP]:
            self.vel += self.direction * self.speed

        if self.pos.y < 0:
            self.pos.y = screen_size[1]

        if self.pos.y > screen_size[1]:
            self.pos.y = 0

        if self.pos.x < 0:
            self.pos.x = screen_size[0]
        
        if self.pos.x > screen_size[0]:
            self.pos.x = 0

        self.pos += self.vel
        self.rect.center = self.pos

    def rotate(self):
        self.direction.rotate_ip(-self.angle_speed)
        self.angle += self.angle_speed
        if self.angle > 360:
            self.angle -= 360
        elif self.angle < 0:
            self.angle += 360

        image = pygame.transform.rotozoom(self.image0,self.angle, 1)
        old_center = self.rect.center
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.center = old_center

    def fire(self):
        Bullet(self.rect.center, self.direction, self.angle)

class Bullet(pygame.sprite.Sprite):
    def __init__(self, pos, direction, angle):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((5,15))
        self.image.fill('white')
        self.rect = self.image.get_rect()
        self.image = pygame.transform.rotozoom(self.image, angle, 1)
        self.position = vec(pos)
        self.velocity = direction * 5

        allsprites.add(self)

    def update(self):
        self.position += self.velocity
        self.rect.center = self.position
        if self.rect.x < 0 or self.rect.x > screen_size[0] or self.rect.y < 0 or self.rect.y > screen_size[1]:
            self.kill()
        

player = Player()

running = True
while running:
    screen.fill(black)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.fire()
                


    allsprites.update()
    allsprites.draw(screen)

    pygame.display.update()
    clock.tick(fps)

pygame.quit()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,075
Messages
2,570,542
Members
47,197
Latest member
NDTShavonn

Latest Threads

Top