Noob.Gamer101 Noob.Gamer101 - 4 months ago 14
Python Question

Pygame (A bit racey) Objects are there, but not visible

I have a couple of objects defined/entered into a variable

car1 = pygame.image.load('C:\Users\itzrb_000\Downloads\download (3).png')
car2 = pygame.image.load('C:\Users\itzrb_000\Downloads\download (2).png')
car3 = pygame.image.load('C:\Users\itzrb_000\Downloads\images.png')
rock = pygame.image.load('C:\Users\itzrb_000\Downloads\Rock.png')

Then created a function called
which takes the x and y start position as arguments

def cars(thingx, thingy):
objects = [car1, car2, car3, rock]
num_of_objects = random.randint(1,4)
for x in range(num_of_objects):
y = random.choice(objects)
gameDisplay.blit(y,(random.randrange(130, 625),-300))

inside the

thing_startx = random.randrange(130,625)
thing_starty = -600
thing_speed = 10

Then I call all the objects

things(thing_startx, thing_starty, thing_width, thing_height, black)
cars(thing_startx, thing_starty)
thing_starty += thing_speed

is the car that the player plays with,
is a function that draws a black square that later on is the thing that I shouldn't crash into

def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

I'm trying to display the car images instead of just a black block, I commented out the
function and its call in
. When I tested there's nothing visible, however I noticed that my car at some point crashes, so the conclusion is that the car image is there, but it's not visible.

Any idea on how to solve this ?


You're blitting the objects off screen. gameDisplay.blit(y,(random.randrange(130, 625),-300)) will blit the image at x between 130 to 625 and y at -300 (which means 300 to the left of the screen).

But is the function doing what you want it to do? Every loop you're blitting a random number of random objects at random positions. I strongly suggest finding a better tutorial which teaches basics as code structure, good naming, how to use docstrings and object-oriented programming, like this.