0

I am trying to make a racing game and I need to know how to check collision between two sprites. I have tried using colors to do this, but to no avail. Also, how can I make the 2nd car (enemy) spawn only on 16 pixel multiples but still have a random spawn? Here is my code:

import pygame, random

pygame.init()
screen = pygame.display.set_mode((300,208))
pygame.display.set_caption("TinyRacer")
car = pygame.image.load("car.png")
car2 = pygame.image.load("car2.png")
bg = pygame.image.load("bg.png")
run = True
y = 64
e_y = random.randint(32,160)
e_x = 0

while run:
    for event in pygame.event.get():
        clock = pygame.time.Clock()  
        if event.type == pygame.QUIT:
            run = False
    key = pygame.key.get_pressed()
    if key[pygame.K_UP] and y > 32:
      y -= 16
    if key[pygame.K_DOWN] and y < 160:
      y += 16
    e_x = e_x - 18
    if e_x < 0:
      e_x = 300
      e_y = random.randint(32,160)

    screen.fill((255,255,255))
    screen.blit(car, (0,y))
    screen.blit(car2, (e_x,e_y))
    screen.blit(bg,(0,0))   
    pygame.display.update()
    clock.tick(16) 
    
pygame.quit()

0 Answers0