initial commit

main
Tylan Tyson 2 years ago
commit eec19ec229

2
.gitignore vendored

@ -0,0 +1,2 @@
.DS_STORE
venv/

Binary file not shown.

@ -0,0 +1,182 @@
# Imports
import pygame
import random
# Pygame Setup
pygame.init()
WINDOW_WIDTH = 1000
WINDOW_HEIGHT = 500
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
clock = pygame.time.Clock()
# Assets
pygame.mixer.music.load("assets/music.mp3")
# Render Background
def render_backround(screen):
# Grass
screen.fill((22,135,0))
# Roads
for y in range(5):
pygame.draw.rect(screen, (102, 92, 92), (0, 20 + y * 100, 1000, 60))
# Yellow Lines
for y in range(5):
for x in range(10):
pygame.draw.rect(screen, (204, 204, 0), (10 + x * 100, (20 + y * 100) + 25, 75, 10))
# Car
class Car():
# Class Variables
cars = []
# Constructor Method
def __init__(self, top_speed, acceleration, color):
# Variable Checks
if type(top_speed) != type(0):
raise TypeError("TypeError - top_speed must be an integer")
elif top_speed <= 0:
raise ValueError("ValueError - top_speed must be positive")
if type(acceleration) != type(0):
raise TypeError("TypeError - acceleration must be an integer")
elif acceleration <= 0:
raise ValueError("ValueError - acceleration must be positive")
if type(color) != type(()):
raise TypeError("TypeError - color must be a tuple")
elif len(color) != 3:
raise ValueError("ValueError - color must contain three values")
elif color[0] < 0 or color[0] > 255 or color[1] < 0 or color[1] > 255 or color[2] < 0 or color[2] > 255:
raise ValueError("ValueError - color value must be between 0 and 255")
# Variables
self.__top_speed = top_speed
self.__acceleration = acceleration / 160
self.__color = color
self.__speed = 0
self.__distance = 0
# Add Car
self.cars.append(self)
# Drive Method
def drive(self):
# Update Acceleration
if self.__speed < self.__top_speed:
self.__speed += self.acceleration
if self.__speed > self.__top_speed:
self.__speed = self.__top_speed
# Update Distance
self.__distance += self.__speed / 64
# Class Methods
@classmethod
def number_of_cars(cls):
return len(cls.cars)
# Static Methods
@staticmethod
def i_am_a_car():
print("I am a Car!")
# Getters
@property
def top_speed(self):
return self.__top_speed
@property
def acceleration(self):
return self.__acceleration
@property
def color(self):
return self.__color
@property
def speed(self):
return self.__speed
@property
def distance(self):
return self.__distance
# Setters
@top_speed.setter
def top_speed(self, value):
if type(value) != type(0):
raise TypeError("TypeError - top_speed must be an integer")
elif not value > 1:
raise ValueError("ValueError - top_speed must be positive")
else:
self.__top_speed = value
@acceleration.setter
def acceleration(self, value):
if type(value) != type(0):
raise TypeError("TypeError - acceleration must be an integer")
elif not value > 1:
raise ValueError("ValueError - acceleration must be positive")
else:
self.__acceleration = value
@color.setter
def color(self, value):
if type(value) != type(()):
raise TypeError("TypeError - color must be a tuple")
elif len(value) != 3:
raise ValueError("ValueError - color must contain three values")
elif value[0] < 0 or value[0] > 255 or value[1] < 0 or value[1] > 255 or value[2] < 0 or value[2] > 255:
raise ValueError("ValueError - color value must be between 0 and 255")
else:
self.__color = value
@speed.setter
def speed(self, value):
if type(value) != type(0) or type(value) != type(0.0):
raise TypeError("TypeError - speed must be a number")
else:
self.__speed = value
@distance.setter
def distance(self, value):
if type(value) != type(0) or type(value) != type(0.0):
raise TypeError("TypeError - distance must be a number")
else:
self.__distance = value
# Add Cars
def __add__(self, car):
Car.cars.remove(self)
Car.cars.remove(car)
return Car(int(self.__top_speed + car.top_speed), int(self.acceleration * 160 + car.acceleration * 160), (255, 255, 255))
# Sports Car
class SportsCar(Car):
# Constructor Method
def __init__(self, top_speed, acceleration, color):
# Variables
top_speed = top_speed * 2
acceleration = acceleration * 2
color = color
# Create Object
super().__init__(top_speed, acceleration, color)
# Create Cars
for car in range(5):
type_of_car = random.randint(1, 100)
if type_of_car == 1:
SportsCar(random.randint(10, 40), random.randint(10, 100), (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))) + SportsCar(random.randint(10, 40), random.randint(1, 100), (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)))
elif type_of_car > 60:
Car(random.randint(10, 40), random.randint(10, 100), (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)))
else:
SportsCar(random.randint(10, 40), random.randint(10, 100), (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)))
# Music
pygame.mixer.music.play(-1)
# Game Loop
winner = False
run = True
while run:
# Tick
clock.tick(60)
# Race
if winner == False:
# Background
render_backround(screen)
# Cars
for car_number, car in enumerate(Car.cars):
pygame.draw.rect(screen, car.color, (car.distance, 40 + car_number * 100, 50, 20))
car.drive()
if car.distance >= 1000:
print(f"Car {car_number + 1} won! It was a {type(car).__name__}!")
winner = True
# Event Handler
for event in pygame.event.get():
# Quit
if event.type == pygame.QUIT:
run = False
# Update Display
pygame.display.update()

@ -0,0 +1 @@
pygame==2.3.0
Loading…
Cancel
Save