Python Report 2
Python Report 2
1. Project Abstraction
Key Features:
1. Game Initialization:
The game initializes with the necessary components such as
display settings, car images, enemy car attributes, background
settings, etc.
2. Game Loop:
The game loop (run_car method) continuously updates the game
state, handling player input, updating car and enemy car positions,
detecting collisions, and rendering graphics.
3. Player Control:
Player control is implemented using keyboard inputs. The player
can move the car left or right to avoid collisions.
2
5. Collision Detection:
Collision detection is implemented to detect when the player's car
collides with an enemy car or hits the road boundaries. Upon
collision, the game ends, and a game over message is displayed.
6. Scoring:
The player's score increases as they survive longer in the game
without colliding with enemy cars. The score is displayed on the
game window.
7. Background Scrolling:
The background image scrolls downwards, creating the illusion of
movement. This adds dynamism to the game environment.
9. Credits Display:
3
2. Code
import random
from time import sleep
import pygame
class CarRacing:
def __init__(self):
pygame.init()
self.display_width = 800
self.display_height = 600
self.black = (0, 0, 0)
self.white = (255, 255, 255)
self.clock = pygame.time.Clock()
self.gameDisplay = None
self.initialize()
def initialize(self):
self.crashed = False
self.carImg = pygame.image.load('.\\img\\car.png')
self.car_x_coordinate = (self.display_width * 0.45)
self.car_y_coordinate = (self.display_height * 0.8)
self.car_width = 49
# enemy_car
self.enemy_car = pygame.image.load('.\\img\\enemy_car_1.png')
self.enemy_car_startx = random.randrange(310, 450)
self.enemy_car_starty = -600
self.enemy_car_speed = 5
self.enemy_car_width = 49
self.enemy_car_height = 100
# Background
5
self.bgImg = pygame.image.load(".\\img\\back_ground.jpg")
self.bg_x1 = (self.display_width / 2) - (360 / 2)
self.bg_x2 = (self.display_width / 2) - (360 / 2)
self.bg_y1 = 0
self.bg_y2 = -600
self.bg_speed = 3
self.count = 0
def racing_window(self):
self.gameDisplay = pygame.display.set_mode((self.display_width,
self.display_height))
pygame.display.set_caption('Car Dodge')
self.run_car()
def run_car(self):
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_LEFT):
self.car_x_coordinate -= 50
print ("CAR X COORDINATES: %s" %
self.car_x_coordinate)
if (event.key == pygame.K_RIGHT):
self.car_x_coordinate += 50
print ("CAR X COORDINATES: %s" %
self.car_x_coordinate)
print ("x: {x}, y:
{y}".format(x=self.car_x_coordinate, y=self.car_y_coordinate))
self.gameDisplay.fill(self.black)
self.back_ground_raod()
6
self.run_enemy_car(self.enemy_car_startx,
self.enemy_car_starty)
self.enemy_car_starty += self.enemy_car_speed
self.car(self.car_x_coordinate, self.car_y_coordinate)
self.highscore(self.count)
self.count += 1
if (self.count % 100 == 0):
self.enemy_car_speed += 1
self.bg_speed += 1
if self.car_y_coordinate < self.enemy_car_starty +
self.enemy_car_height:
if self.car_x_coordinate > self.enemy_car_startx and
self.car_x_coordinate < self.enemy_car_startx + self.enemy_car_width or
self.car_x_coordinate + self.car_width > self.enemy_car_startx and
self.car_x_coordinate + self.car_width < self.enemy_car_startx +
self.enemy_car_width:
self.crashed = True
self.display_message("Game Over !!!")
pygame.display.update()
self.clock.tick(60)
def back_ground_raod(self):
self.gameDisplay.blit(self.bgImg, (self.bg_x1, self.bg_y1))
self.gameDisplay.blit(self.bgImg, (self.bg_x2, self.bg_y2))
self.bg_y1 += self.bg_speed
self.bg_y2 += self.bg_speed
def display_credit(self):
font = pygame.font.SysFont("lucidaconsole", 14)
text = font.render("Thanks for playing!", True, self.white)
self.gameDisplay.blit(text, (600, 520))
if __name__ == '__main__':
car_racing = CarRacing()
car_racing.racing_window()
8
3. Explanation
1. Importing Libraries
import random
from time import sleep
import pygame
2. CarRacing Class
class CarRacing:
def __init__(self):
# Initialize Pygame
9
pygame.init()
# Screen dimensions
self.display_width = 800
self.display_height = 600
# Colors
self.black = (0, 0, 0)
self.white = (255, 255, 255)
self.clock = pygame.time.Clock()
self.gameDisplay = None
3. Initialization Function
10
def initialize(self):
# Car properties
self.enemy_car = pygame.image.load('.\\img\\enemy_car_1.png')
self.enemy_car_startx = random.randrange(310, 450)
self.enemy_car_starty = -600
self.enemy_car_speed = 5
self.enemy_car_width = 49
self.enemy_car_height = 100
# Background properties
self.bgImg = pygame.image.load(".\\img\\back_ground.jpg")
self.bg_x1 = (self.display_width / 2) - (360 / 2)
self.bg_x2 = (self.display_width / 2) - (360 / 2)
self.bg_y1 = 0
self.bg_y2 = -600
self.bg_speed = 3
self.count = 0
11
def racing_window(self):
self.gameDisplay = pygame.display.set_mode((self.display_width,
self.display_height))
pygame.display.set_caption('Car Dodge')
self.run_car()
if (event.type == pygame.KEYDOWN):
12
if (event.key == pygame.K_LEFT):
self.car_x_coordinate -= 50
print ("CAR X COORDINATES: %s" %
self.car_x_coordinate)
if (event.key == pygame.K_RIGHT):
self.car_x_coordinate += 50
6. Game Logic
self.gameDisplay.fill(self.black)
self.back_ground_raod()
self.run_enemy_car(self.enemy_car_startx, self.enemy_car_starty)
7. Collision Detection
13
def display_credit(self):
9. Main Function
if __name__ == '__main__':
car_racing = CarRacing()
car_racing.racing_window()
4. Interface
In this picture enemy car is come from front side and it seems too close
to our car.
17
In this picture car is is crashed with enemy car. And game is over.
5. Technology used
6. Summary
7. Conclusion
In this mini project a basic car racing game using Pygame in Python
is implemented. The game features include player control of a car
using left and right arrow keys, avoidance of randomly moving
enemy cars, scoring based on the duration of survival, collision
detection, and a "Game Over" message displayed upon crashing.
The game window displays the score and a message of
appreciation after the game ends. Overall, it's a simple yet
functional implementation suitable for further customization and
expansion to create more complex gameplay features and
enhancements.