0

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()
J.R. BEATS
  • 111
  • 6
  • btw for clarity and expansion when inheriting you probably want to name those methods simply `draw`, because since they are instance methods they will always have to be used with the instance so it should be pretty clear what the `.draw` actually, so there is no point to call the method `draw_debris` – Matiiss Oct 28 '21 at 12:39
  • also the issue seems to be that you haven't provided the `dest`ination argument for the `.blit` function that is supposed to draw the image, in fact you should get an error because that is a required positional argument: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit – Matiiss Oct 28 '21 at 12:43
  • Hi thanks for the clarification. I changed my code now and I can spawn my cement block a top the pygame screen now. Only problem I have now is that I only spawn one cement block and that one cement block just changes position instead of spawning in different cement blocks. I want to be able to spawn multiple cement blocks at different positions not just one cement block that changes position. – J.R. BEATS Oct 28 '21 at 16:41
  • well what you probably want to do is instead of creating one debris, create multiple and add those to the list but also set a limit to how many there are allowed to be added, then just iterate over that list and call the draw method on each of those objects, also there are already similar questions on how to for example shoot an exact amount of bullets which is pretty much what you need – Matiiss Oct 28 '21 at 16:45

0 Answers0