I'm trying to program a traffic simulation and need the pygame collision detection. However as soon as i run the program (it isn't finished yet) it tells me i have an attribute error on car_up because it hasn't got the attribute rect. Can someone tell me why it is so and what i can do to improve it? And yes i have been trying figuring it out on my own but with no success.
import pygame
import random
import threading
from pygame.locals import (QUIT, KEYDOWN, K_ESCAPE)
# Define constants for the screen width and height
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 400
grey=[90 , 90 , 90]
green =[125, 255, 50]
spawnrate_up=1500
spawnrate_down=800
dimentions=[75,35]
#CLASSES
#create car
class Car_up (pygame.sprite.Sprite):
def __init__(self, green, dimentions):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface(dimentions)
self.image.fill(green)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rect.x = random.randint(-100, -20)
self.rect.y = 150
self.speedx= 10
self.speedy= 0
# move car and delete once out of screen
def update(self):
merge=random.randint(750,1000)
if self.rect.left > SCREEN_WIDTH:
self.kill()
if self.rect.colliderect(car_down.rect):
car_up.speed.x=car_up.speed.y=0
elif self.rect.centery<=266 and self.rect.centerx>merge:
self.speedx=10
self.speedy=3
else:
self.speedx=10
self.speedy=0
self.rect.move_ip(self.speedx, self.speedy)
class Car_down(pygame.sprite.Sprite):
def __init__(self, green, dimentions):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface(dimentions)
self.image.fill(green)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rect.x = random.randint(-100,-20)
self.rect.y = 250
self.speedx= 10
self.speedy= 0
# move car and delete once out of screen
def update(self):
self.speedx=10
self.speedy=0
self.rect.move_ip(self.speedx, self.speedy)
if self.rect.left > SCREEN_WIDTH:
self.kill()
pygame.init()
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock=pygame.time.Clock()
#create custom event
ADDCAR_down= pygame.USEREVENT + 1
ADDCAR_up= pygame.USEREVENT + 2
pygame.time.set_timer(ADDCAR_up, spawnrate_up )
pygame.time.set_timer(ADDCAR_down, spawnrate_down )
#create car
car_up= pygame.sprite.Group()
car_down= pygame.sprite.Group()
car_down.add(Car_down(green,dimentions))
car_up.add(Car_up(green,dimentions))
#infinite loop
run = True
#MAIN
while run:
# Look at every event in the queue
for event in pygame.event.get():
# Did the user hit a key?
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
run = False
# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
run = False
elif event.type == ADDCAR_down:
# Create the new CAR and add it to sprite groups
new_car_down = Car_down(green,dimentions)
car_down.add(new_car_down)
elif event.type == ADDCAR_up:
new_car_up = Car_up(green,dimentions)
car_up.add(new_car_up)
#grey screen
screen.fill(grey)
# Draw the car on the screen
car_up.draw(screen)
car_up.update()
car_down.draw(screen)
car_down.update()
pygame.display.flip()
clock.tick(25)