My link to my AutoPilot Github just incase I did't explain myself correctly: https://github.com/Enoc-Mena99/AutoPilot
I am trying to make an AI simulation where the car drives by itself and swerves either left or right to avoid incoming debris using Pygame and other future modules. I am having trouble getting the output I need from my debris class (draw_debris) function. I want to spawn in a bunch of cement block sprites I created from the top of my Pygame screen. The Pygame window opens it just loads my car sprite but not the random spawning cement sprites that I want to have spawns at the top of the Pygame window. I'll figure out how to make them fall later. Would appreciate the help.
My code:
"""
BUGS LIST:
-------------------------------------------
10-27-21 = Having problems with the debris class (def draw_debris) function. The game outputs to the
screen but does not spawn in the cement block sprites from the top as I intended.
---------------------------------------------------------------------------------------------------------
"""
import pygame
import random
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 class
class Car(pygame.sprite.Sprite):
def __init__(self, scale, speed):
pygame.sprite.Sprite.__init__(self)
#car speed
self.speed = speed
#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()
#draw car to screen
def draw_car(self):
screen.blit(self.image,(WIDTH/2 - 10, HEIGHT/2 - 10))
#check if car is hitting a block
#check if car is in range of a block
#debris class
class Debris(pygame.sprite.Sprite):
def __init__(self, scale, speed):
pygame.sprite.Sprite.__init__(self)
self.speed = speed
#load debris
self.images = []
img = pygame.image.load('debris/cement.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()
# draw debris to screen
def draw_debris(self):
sides = ['top']
width_height = [WIDTH]
y = 0
x = random.randrange(WIDTH - 4)
side = random.choices(sides, width_height)[0]
if sides == 'top':
screen.blit(self.image)
######################CAR/DEBRIS##########################
car = Car(1,5)
debris = Debris(1,5)
##########################################################
#groups
car_group = pygame.sprite.Group()
car_group.add(car)
debris_group = pygame.sprite.Group()
debris_group.add(debris)
#simulation runs here
run = True
while run:
#draw street
screen.blit(bg, [0,0])
#draw car
car.draw_car()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# draw debris
debris.draw_debris()
#coordinates
x = 4
y = 4
#update the display
pygame.display.update()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()