import pygame
from pygame.locals import *
import random
pygame.init()
# create the window
width = 500
height = 500
screen_size = (width, height)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('Car Game')
# colors
gray = (100, 100, 100)
green = (76, 208, 56)
red = (200, 0, 0)
white = (255, 255, 255)
yellow = (255, 232, 0)
# road and marker sizes
road_width = 300
marker_width = 10
marker_height = 50
# lane coordinates
left_lane = 150
center_lane = 250
right_lane = 350
lanes = [left_lane, center_lane, right_lane]
# road and edge markers
road = (100, 0, road_width, height)
left_edge_marker = (95, 0, marker_width, height)
right_edge_marker = (395, 0, marker_width, height)
# for animating movement of the lane markers
lane_marker_move_y = 0
# player's starting coordinates
player_x = 250
player_y = 400
# frame settings
clock = pygame.time.Clock()
fps = 120
# game settings
gameover = False
speed = 2
score = 0
class Vehicle(pygame.sprite.Sprite):
def __init__(self, image, x, y):
pygame.sprite.Sprite.__init__(self)
# scale the image down so it's not wider than the lane
image_scale = 45 / image.get_rect().width
new_width = image.get_rect().width * image_scale
new_height = image.get_rect().height * image_scale
self.image = pygame.transform.scale(image, (new_width, new_height))
self.rect = self.image.get_rect()
self.rect.center = [x, y]
class PlayerVehicle(Vehicle):
def __init__(self, x, y):
image = pygame.image.load('images/car.png')
super().__init__(image, x, y)
# sprite groups
player_group = pygame.sprite.Group()
vehicle_group = pygame.sprite.Group()
# create the player's car
player = PlayerVehicle(player_x, player_y)
player_group.add(player)
# load the vehicle images
image_filenames = ['pickup_truck.png', 'semi_trailer.png', 'taxi.png', 'van.png']
vehicle_images = []
for image_filename in image_filenames:
image = pygame.image.load('images/' + image_filename)
vehicle_images.append(image)
# load the crash image
crash = pygame.image.load('images/crash.png')
crash_rect = crash.get_rect()
# game loop
running = True
while running:
clock.tick(fps)
for event in pygame.event.get():
if event.type == QUIT:
running = False
# move the player's car using the left/right arrow keys
if event.type == KEYDOWN:
if event.key == K_LEFT and player.rect.center[0] > left_lane:
player.rect.x -= 100
elif event.key == K_RIGHT and player.rect.center[0] < right_lane:
player.rect.x += 100
# check if there's a side swipe collision after changing lanes
for vehicle in vehicle_group:
if pygame.sprite.collide_rect(player, vehicle):
gameover = True
# place the player's car next to other vehicle
# and determine where to position the crash image
if event.key == K_LEFT:
player.rect.left = vehicle.rect.right
crash_rect.center = [player.rect.left, (player.rect.center[1] + vehicle.rect.center[1]) / 2]
elif event.key == K_RIGHT:
player.rect.right = vehicle.rect.left
crash_rect.center = [player.rect.right, (player.rect.center[1] + vehicle.rect.center[1]) / 2]
# draw the grass
screen.fill(green)
# draw the road
pygame.draw.rect(screen, gray, road)
# draw the edge markers
pygame.draw.rect(screen, yellow, left_edge_marker)
pygame.draw.rect(screen, yellow, right_edge_marker)
# draw the lane markers
lane_marker_move_y += speed * 2
if lane_marker_move_y >= marker_height * 2:
lane_marker_move_y = 0
for y in range(marker_height * -2, height, marker_height * 2):
pygame.draw.rect(screen, white, (left_lane + 45, y + lane_marker_move_y, marker_width, marker_height))
pygame.draw.rect(screen, white, (center_lane + 45, y + lane_marker_move_y, marker_width, marker_height))
# draw the player's car
player_group.draw(screen)
# add a vehicle
if len(vehicle_group) < 2:
# ensure there's enough gap between vehicles
add_vehicle = True
for vehicle in vehicle_group:
if vehicle.rect.top < vehicle.rect.height * 1.5:
add_vehicle = False
if add_vehicle:
# select a random lane
lane = random.choice(lanes)
# select a random vehicle image
image = random.choice(vehicle_images)
vehicle = Vehicle(image, lane, height / -2)
vehicle_group.add(vehicle)
# make the vehicles move
for vehicle in vehicle_group:
vehicle.rect.y += speed
# remove vehicle once it goes off screen
if vehicle.rect.top >= height:
vehicle.kill()
# add to score
score += 1
# speed up the game after passing 5 vehicles
if score > 0 and score % 5 == 0:
speed += 1
# draw the vehicles
vehicle_group.draw(screen)
# display the score
font = pygame.font.Font(pygame.font.get_default_font(), 16)
text = font.render('Score: ' + str(score), True, white)
text_rect = text.get_rect()
text_rect.center = (50, 400)
screen.blit(text, text_rect)
# check if there's a head on collision
if pygame.sprite.spritecollide(player, vehicle_group, True):
gameover = True
crash_rect.center = [player.rect.center[0], player.rect.top]
# display game over
if gameover:
screen.blit(crash, crash_rect)
pygame.draw.rect(screen, red, (0, 50, width, 100))
font = pygame.font.Font(pygame.font.get_default_font(), 16)
text = font.render('Game over. Play again? (Enter Y or N)', True, white)
text_rect = text.get_rect()
text_rect.center = (width / 2, 100)
screen.blit(text, text_rect)
pygame.display.update()
# wait for user's input to play again or exit
while gameover:
clock.tick(fps)
for event in pygame.event.get():
if event.type == QUIT:
gameover = False
running = False
# get the user's input (y or n)
if event.type == KEYDOWN:
if event.key == K_y:
# reset the game
gameover = False
speed = 2
score = 0
vehicle_group.empty()
player.rect.center = [player_x, player_y]
elif event.key == K_n:
# exit the loops
gameover = False
running = False
pygame.quit() Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
| Name | Description |
|---|---|
| NumPy | NumPy python library helps users to work on arrays with ease |
| SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
| SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
| Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
| DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |