More stuff to racing game

added the ability to move the player’s car left/right using the arrow keys.

if event.type == KEYDOWN:
            
            if event.key == K_LEFT and player.rect.center[0] > left_lane:
                player.rect.x -= 100
            elif event.key == K_RIGHT and player.rect.center[0] < right_lane:
                player.rect.x += 100

result:

left lane

and right lane (also added other cars. i’ll get to that later)

Made the crash image pop up determining where the player crashed into the npc car

# check if there's a side swipe collision after changing lanes
            for vehicle in vehicle_group:
                if pygame.sprite.collide_rect(player, vehicle):
                    
                    gameover = True
                    
                    # place the player's car next to other vehicle
                    # and determine where to position the crash image
                    if event.key == K_LEFT:
                        player.rect.left = vehicle.rect.right
                        crash_rect.center = [player.rect.left, (player.rect.center[1] + vehicle.rect.center[1]) / 2]
                    elif event.key == K_RIGHT:
                        player.rect.right = vehicle.rect.left
                        crash_rect.center = [player.rect.right, (player.rect.center[1] + vehicle.rect.center[1]) / 2]

crashing into the left side of a car

crashing into the right side

and crashing into the car from behind

nice

list for the different car images:

image_filenames = ['pickup_truck.png', 'semi_trailer.png', 'taxi.png', 'van.png']

*FINISH LATER (or when you get home)

Author: Zende_

From PA. EHS 2025. Does computer programming and such. That's really it.

Leave a Reply

Your email address will not be published. Required fields are marked *