import pygame import time import random import sys # Initializing the pygame pygame.init() # window screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("CAR RACING") # Loading all images c1_img = pygame.image.load("car1.jpg") clock = pygame.time.Clock() grass = pygame.image.load("grass.jpg") y_strip = pygame.image.load("y_strip.jpg") strip = pygame.image.load("strip.jpg") start = pygame.image.load("start.jpg") # Function for getting all images def background(): screen.blit(grass, (0, 0)) screen.blit(grass, (700, 0)) screen.blit(y_strip, (400, 0)) screen.blit(y_strip, (400, 100)) screen.blit(y_strip, (400, 200)) screen.blit(y_strip, (400, 300)) screen.blit(y_strip, (400, 400)) screen.blit(y_strip, (400, 500)) screen.blit(y_strip, (400, 600)) screen.blit(strip, (120, 0)) screen.blit(strip, (680, 0)) # Getting the car on screen def car(x, y): screen.blit(c1_img, (x, y)) x_change = 0 x = 400 y = 470 car_width = 56 op_speed = 10 obs = 0 y_change = 0 obs_x = random.randrange(200, 650) obs_y = -750 op_width = 56 op_height = 125 car_passed = 0 score = 0 level = 0 # Function for enemy cars def obstacle(obs_x, obs_y, obs): if obs == 0: obs_img = pygame.image.load("car2.jpg") elif obs == 1: obs_img = pygame.image.load("car3.jpg") elif obs == 2: obs_img = pygame.image.load("car4.jpg") elif obs == 3: obs_img = pygame.image.load("car5.jpg") elif obs == 4: obs_img = pygame.image.load("car6.jpg") elif obs == 5: obs_img = pygame.image.load("car7.jpg") screen.blit(obs_img, (obs_x, obs_y)) # message to display font = pygame.font.SysFont("None", 150) render = font.render("CAR CRASHED", True, (0, 0, 0)) # Function to display score def sc(car_passed, score): s_font = pygame.font.SysFont(None, 35) passed = s_font.render("Passed:" + str(car_passed), True, (0, 0, 0)) score = s_font.render("Score:" + str(score), True, (0, 0, 0)) screen.blit(passed, (0, 30)) screen.blit(score, (0, 70)) # Text on Buttons def text(text, font): texts = font.render(text, True, (255, 255, 255)) return texts, texts.get_rect() # Game loop def game_loop(): global op_speed, x, car_passed, level, x_change, y_change, y, obs_y, obs_x, obs, score, font running = True while running: # for checking the events for event in pygame.event.get(): if event.type == pygame.QUIT: # QUIT event running = False if event.type == pygame.KEYDOWN: # KEYDOWN event if event.key == pygame.K_LEFT: x_change = -5 if event.key == pygame.K_RIGHT: x_change = 5 if event.key == pygame.K_s: # Increase speed op_speed += 2 if event.key == pygame.K_b: # decrease speed op_speed -= 2 if event.type == pygame.KEYUP: # KEYUP event if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: x_change = 0 x += x_change screen.fill((119, 119, 119)) background() obs_y -= (op_speed / 4) obstacle(obs_x, obs_y, obs) obs_y += op_speed car(x, y) sc(car_passed, score) # Restricting the movement if x > 680 - car_width or x 600: obs_y = 0 - op_height obs_x = random.randrange(170, 600) obs = random.randrange(0, 6) car_passed += 1 score = car_passed * 10 if int(car_passed) % 10 == 0: level += 1 op_speed += 2 font = pygame.font.SysFont(None, 50) level_text = font.render("Level" + str(level), True, (0, 0, 0)) screen.blit(level_text, (0, 100)) pygame.display.flip() # car crash logic if y obs_x and x obs_x and x + car_width 80 and mouse[0] 490 and mouse[1] 580 and mouse[0] 490 and mouse[1] < 590: pygame.draw.rect(screen, (255, 0, 0), (580, 540, 150, 50)) if click == (1, 0, 0): pygame.quit() t = pygame.font.SysFont("arial", 30, "bold") texts, textr = text("START", t) textr.center = ((80 + (150 / 2)), (540 + (50 / 2))) screen.blit(texts, textr) texts, textr = text("QUIT", t) textr.center = ((580 + (150 / 2)), (540 + (50 / 2))) screen.blit(texts, textr) pygame.display.update() intro() # calling the intro function game_loop() # calling the game loop 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 |