1

I have created bullets for my car sprite to shoot but when I press space bar the bullet sprite comes out but disappears.When I press the space bar the bullet comes out but disappears right there instead of traveling all the way up to the top of the pygame screen like I want it to. How can I fix this?? I have tried a lot of different things now but I'm stuck.

autopilot.py code:

import pygame
import debris
import car

pygame.init()

WIDTH = 1000
HEIGHT = 400

screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("AutoPilot")
screen.fill((255,255,255))

#fps
FPS = 120
clock = pygame.time.Clock()

#background img
bg = pygame.image.load('background/street.png').convert_alpha()


#define variables


######################CAR/DEBRIS##########################

car = car.Car(1,5)
debris = debris.Debris(1,5)

##########################################################

#groups
car_group = pygame.sprite.Group()
car_group.add(car)

debris_group = pygame.sprite.Group()
debris_group.add(debris)

#game runs here
run = True
while run:

    #draw street
    screen.blit(bg,[0,0])

    #update groups
    car_group.update()
    #car_group.draw(screen)

    #draw debris
    debris.draw()

    #draw car
    car.draw()
    car.move()

    #update bullets
    car.bullet_update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        #check if key is down
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
            if event.key == pygame.K_a:
                car.movingLeft = True
            if event.key == pygame.K_d:
                car.movingRight = True
            #shoot bullets
            if event.key == pygame.K_SPACE:
                car.shoot()

        #check if key is up
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                car.movingLeft = False
            if event.key == pygame.K_d:
                car.movingRight = False


    #update the display
    pygame.display.update()
    pygame.display.flip()
    clock.tick(FPS)


pygame.quit()

car.py code:

import pygame

#screen height & width
WIDTH = 1000
HEIGHT = 400

screen = pygame.display.set_mode((WIDTH,HEIGHT))

#car class
class Car(pygame.sprite.Sprite):
    def __init__(self, scale, speed):
        pygame.sprite.Sprite.__init__(self)

        #load bullets
        self.vel = 10
        self.bullet = pygame.image.load('car/bullet.png').convert_alpha()
        self.rect1 = self.bullet.get_rect()
        self.y = float(self.rect1.y)

        self.speed = speed
        #self.x = x
        #self.y = y
        self.moving = True
        self.frame = 0
        self.flip = False
        self.direction = 0

        #load car
        self.images = []
        img = pygame.image.load('car/car.png').convert_alpha()
        img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
        self.images.append(img)
        self.image = self.images[0]
        self.rect = self.image.get_rect()
        self.update_time = pygame.time.get_ticks()
        self.movingLeft = False
        self.movingRight = False
        self.rect.x = 465
        self.rect.y = 325

    #draw car to screen
    def draw(self):
        screen.blit(self.image,(self.rect.centerx, self.rect.centery))

    #move car
    def move(self):
        # reset the movement variables
        dx = 0
        dy = 0

        # moving variables
        if self.movingLeft and self.rect.x > 33:
            dx -= self.speed
            self.flip = True
            self.direction = -1
        if self.movingRight and self.rect.x < 900:
            dx += self.speed
            self.flip = False
            self.direction = 1

        # update rectangle position
        self.rect.x += dx
        self.rect.y += dy

    #shoot bullets
    def shoot(self):
        bullets = [self.bullet]
        for _ in bullets:
            screen.blit(self.bullet,(self.rect.x + 32, self.rect.y))

    #update bullet travel
    def bullet_update(self):
        self.y += self.vel
        self.rect1 = self.y
J.R. BEATS
  • 93
  • 6

1 Answers1

1

Add a bullet list to the Car class:

class Car(pygame.sprite.Sprite):
    def __init__(self, scale, speed):
        # [...]

        self.bullet_list = []

Add a the position of the bullet to the list when SPACE is pressed. The starting position of the bullet is the position of the car:

class Car(pygame.sprite.Sprite):v
    # [...]

    def shoot(self):
        self.bullet_list.append([self.rect.x, self.rect.y])

Move the bullets:

class Car(pygame.sprite.Sprite):
    # [...]

    def bullet_update(self):
        for bullet_pos in self.bullet_list[:]:
            bullet_pos[0] += self.vel
            if bullet_pos[0] > 1000:
                self.bullet_list.remove(bullet_pos)

Draw the bullets with the car:

class Car(pygame.sprite.Sprite):
    # [...]

    def draw(self):
        for bullet_pos in self.bullet_list:
            screen.blit(self.bullet, bullet_pos)
        screen.blit(self.image, self.rect.center)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174