├── README.md ├── finished_project ├── box_truck.png ├── oop_practice.py ├── police_car.png ├── sedan_blue.png ├── sedan_green.png ├── sedan_red.png └── truck_tractor.png └── starter_files ├── box_truck.png ├── oop_practice.py ├── police_car.png ├── sedan_blue.png ├── sedan_green.png ├── sedan_red.png └── truck_tractor.png /README.md: -------------------------------------------------------------------------------- 1 | # practiceOOP_Pygame 2 | Practice Object Oriented Programming with Classes of Cars and Pygame 3 | 4 | ## Introduction 5 | 6 | This repository stores the code of an OOP exercise featured in my Youtube video. 7 | 8 | 9 | 10 | ## Requirements 11 | pygame: https://www.pygame.org/docs/ 12 | 13 | ### Recommended Environment Setup 14 | ``` 15 | >> conda create env -n your_name python=3.11 16 | >> conda activate your_name 17 | >> pip install pygame 18 | ``` 19 | 20 | ## Connect with me 21 | ⭐ YouTube 22 |
23 | https://youtube.com/@pythonsimplified 24 |
25 | ⭐ Discord 26 |
27 | https://discord.com/invite/wgTTmsWmXA 28 |
29 | ⭐ LinkedIn 30 |
31 | https://ca.linkedin.com/in/mariyasha888 32 |
33 | ⭐ Twitter 34 |
35 | https://twitter.com/mariyasha888 36 |
37 | ⭐ Blog 38 |
39 | https://www.pythonsimplified.org 40 | 41 | 42 | -------------------------------------------------------------------------------- /finished_project/box_truck.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/finished_project/box_truck.png -------------------------------------------------------------------------------- /finished_project/oop_practice.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import random 3 | import os 4 | 5 | # optional: place pygame window at fixed location 6 | os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50,50) 7 | 8 | # parent class 9 | class Vehicle: 10 | def __init__(self, colour="red", x=400, y=400): 11 | # parent attributes 12 | self.img_path = "sedan_" + colour + ".png" 13 | self.location = x, y 14 | self.draw() 15 | 16 | def draw(self): 17 | # load image and set its location 18 | self.img = pygame.image.load(self.img_path) 19 | self.img_location = self.img.get_rect() 20 | self.img_location.center = self.location 21 | 22 | # child class of vehicle 23 | class Truck(Vehicle): 24 | def __init__(self, x, y, kind="truck_tractor"): 25 | # initialize parent 26 | super().__init__() 27 | # override parent attributes 28 | self.img_path = kind + ".png" 29 | self.location = x, y 30 | # call inherited method 31 | self.draw() 32 | 33 | # child class of vehicle 34 | class Police(Vehicle): 35 | def __init__(self, x, y): 36 | # initialize parent 37 | super().__init__() 38 | # override parent attributes 39 | self.img_path = "police_car.png" 40 | self.location = x, y 41 | # call inherited method 42 | self.draw() 43 | 44 | # pygame settings 45 | pygame.init() 46 | screen = pygame.display.set_mode((800, 800)) 47 | running = True 48 | 49 | # list to store car objects 50 | cars = [] 51 | 52 | # generate 10 car objects 53 | for i in range(10): 54 | # location settings 55 | x = random.randint(0, 800) 56 | y = random.randint(0, 800) 57 | # choose a class of vehicles 58 | vehicle_class = random.choice(["sedan", "truck", "police"]) 59 | 60 | # if the chosen class is sedan 61 | if vehicle_class == "sedan": 62 | # choose a random colour and store a sedan object in cars 63 | c = random.choice(["red", "green", "blue"]) 64 | cars.append(Vehicle(c, x, y)) 65 | # if the chosen class is truck 66 | elif vehicle_class == "truck": 67 | # choose a random kind and store a truck object in cars 68 | k = random.choice(["truck_tractor", "box_truck"]) 69 | cars.append(Truck(x, y, k)) 70 | # if the chosen class is a police vehicle 71 | elif vehicle_class == "police": 72 | # store a police vehicle in cars 73 | cars.append(Police(x, y)) 74 | 75 | # set background colour 76 | screen.fill("white") 77 | # place image on the screen 78 | for car in cars: 79 | screen.blit(car.img, car.img_location) 80 | 81 | # start game loop 82 | while running: 83 | # if we click on the "exit" button 84 | for event in pygame.event.get(): 85 | if event.type == pygame.QUIT: 86 | # stop game loop 87 | running = False 88 | 89 | # flip() the display to put your work on screen 90 | pygame.display.flip() 91 | 92 | pygame.quit() 93 | 94 | -------------------------------------------------------------------------------- /finished_project/police_car.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/finished_project/police_car.png -------------------------------------------------------------------------------- /finished_project/sedan_blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/finished_project/sedan_blue.png -------------------------------------------------------------------------------- /finished_project/sedan_green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/finished_project/sedan_green.png -------------------------------------------------------------------------------- /finished_project/sedan_red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/finished_project/sedan_red.png -------------------------------------------------------------------------------- /finished_project/truck_tractor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/finished_project/truck_tractor.png -------------------------------------------------------------------------------- /starter_files/box_truck.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/starter_files/box_truck.png -------------------------------------------------------------------------------- /starter_files/oop_practice.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import random 3 | import os 4 | 5 | # place Pygame window at a specific location 6 | os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50,50) 7 | 8 | # pygame settings 9 | pygame.init() 10 | screen = pygame.display.set_mode((800, 800)) 11 | running = True 12 | 13 | # load image and set its location 14 | img = pygame.image.load("sedan_red.png") 15 | img_location = img.get_rect() 16 | img_location.center = 400, 400 17 | 18 | # set background colour 19 | screen.fill("white") 20 | # place image on the screen 21 | screen.blit(img, img_location) 22 | 23 | # start game loop 24 | while running: 25 | # if we click on the "exit" button 26 | for event in pygame.event.get(): 27 | if event.type == pygame.QUIT: 28 | # stop game loop 29 | running = False 30 | 31 | # flip() the display to put your work on screen 32 | pygame.display.flip() 33 | 34 | pygame.quit() 35 | 36 | -------------------------------------------------------------------------------- /starter_files/police_car.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/starter_files/police_car.png -------------------------------------------------------------------------------- /starter_files/sedan_blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/starter_files/sedan_blue.png -------------------------------------------------------------------------------- /starter_files/sedan_green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/starter_files/sedan_green.png -------------------------------------------------------------------------------- /starter_files/sedan_red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/starter_files/sedan_red.png -------------------------------------------------------------------------------- /starter_files/truck_tractor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MariyaSha/practiceOOP_Pygame/0abb7ae531e7d6c2a31ad6bc42cfe21bf01367dc/starter_files/truck_tractor.png --------------------------------------------------------------------------------