import pygame
import time
import random 
from pygame import mixer

pygame.init()
display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
blue = (0,0,255)

car_width = 50
car_height = 100

mixer.music.load('video-game-music.wav')
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Car Racing")

icon = pygame.image.load('icon.png')   # add icon
pygame.display.set_icon(icon)

clock = pygame.time.Clock()

carImg = pygame.image.load("kr11.png") #load the car image
car2Img = pygame.image.load("kr3.png")
bgImg = pygame.image.load("icon.png")
crash_img = pygame.image.load("crash.png")
bgsImg = pygame.image.load("car.png")



def highscore(count):
 font = pygame.font.SysFont(None,45)
 text = font.render("Score : "+str(count),True,black)
 gameDisplay.blit(text,(30,30))

def draw_things(thingx,thingy,thing):
 gameDisplay.blit(thing,(thingx,thingy))

def car(x,y):
 gameDisplay.blit(carImg,(x,y))

def text_objects(text,font):
 textSurface = font.render(text,True,blue)
 return textSurface,textSurface.get_rect()

def message_display(text,size,x,y):
 font = pygame.font.Font("freesansbold.ttf",size)
 text_surface , text_rectangle = text_objects(text,font)
 text_rectangle.center =(x,y)
 gameDisplay.blit(text_surface,text_rectangle)

def crash(x,y):
 gameDisplay.blit(crash_img,(x,y))
 #message_display("You Crashed",64,display_width/2,display_height/2)
 message_display("GAME OVER",64,display_width/2,display_height/2) 
 pygame.display.update()
 time.sleep(2)
 gameloop() 

def gameloop():
 #pygame.mixer.Sound.stop()
 pygame.mixer.music.play(-1)
 bg_x1 = (display_width/2)-(360/2)
 bg_x2 = (display_width/2)-(360/2)
 bg_y1 = 0
 bg_y2 = -600
 bg_speed = 15
 bg_speed_change = 0
 car_x = ((display_width / 2) - (car_width / 2))
 car_y = (display_height - car_height)
 car_x_change = 0
 road_start_x =  (display_width/2)-130
 road_end_x = (display_width/2)+130

 thing_startx = random.randrange(road_start_x,road_end_x-car_width)
 thing_starty = -600
 thingw = 50
 thingh = 100
 thing_speed = 30
 count=0

 gameExit = False

 while not gameExit:

  for event in pygame.event.get():
   if event.type == pygame.QUIT:
    gameExit = True
    pygame.quit()
    quit()

   if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
     car_x_change = -30
    elif event.key == pygame.K_RIGHT:
     car_x_change = 30


   if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
     car_x_change = 0


  car_x+=car_x_change

  if car_x > road_end_x-car_width:
   crash(car_x,car_y)
  if car_x < road_start_x:
   crash(car_x-car_width,car_y)


  if car_y < thing_starty + thingh:
   if car_x >= thing_startx and car_x <= thing_startx+thingw:
    crash(car_x-25,car_y-car_height/2)
   if car_x+car_width >= thing_startx and car_x+car_width <= thing_startx+thingw:
    crash(car_x,car_y-car_height/2)




  gameDisplay.fill(green) #display white background
  gameDisplay.blit(bgsImg,(0,0))
  gameDisplay.blit(bgsImg,(440,0))

  gameDisplay.blit(bgImg,(bg_x1,bg_y1))
  gameDisplay.blit(bgImg,(bg_x2,bg_y2))

  car(car_x,car_y) #display car
  draw_things(thing_startx,thing_starty,car2Img)

  highscore(count)
  count+=1
  thing_starty += thing_speed

  if thing_starty > display_height:
   thing_startx = random.randrange(road_start_x,road_end_x-car_width)
   thing_starty = -200

  bg_y1 += bg_speed
  bg_y2 += bg_speed

  if bg_y1 >= display_height:
   bg_y1 = -600

  if bg_y2 >= display_height:
   bg_y2 = -600



  pygame.display.update() # update the screen
  clock.tick(60) # frame per sec

gameloop() 

Python Online Compiler

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.

Taking inputs (stdin)

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)

About Python

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.

Tutorial & Syntax help

Loops

1. If-Else:

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

Note:

Indentation is very important in Python, make sure the indentation is followed correctly

2. For:

For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.

Example:

mylist=("Iphone","Pixel","Samsung")
for i in mylist:
    print(i)

3. While:

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 

Collections

There are four types of collections in Python.

1. List:

List is a collection which is ordered and can be changed. Lists are specified in square brackets.

Example:

mylist=["iPhone","Pixel","Samsung"]
print(mylist)

2. Tuple:

Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.

Example:

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)

3. Set:

Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.

Example:

myset = {"iPhone","Pixel","Samsung"}
print(myset)

4. Dictionary:

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.

Example:

mydict = {
    "brand" :"iPhone",
    "model": "iPhone 11"
}
print(mydict)

Supported Libraries

Following are the libraries supported by OneCompiler's Python compiler

NameDescription
NumPyNumPy python library helps users to work on arrays with ease
SciPySciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation
SKLearn/Scikit-learnScikit-learn or Scikit-learn is the most useful library for machine learning in Python
PandasPandas is the most efficient Python library for data manipulation and analysis
DOcplexDOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling