Hello guys,
in this blog, I am going to show you how you can create you simple car game using Python.
This project isn't too hard, but you need to understand Python well to make it. Let me show you what it looks like, then we can look at the code.
before diving into the project what do you want pre-requirement:
1. install Pygame (for 2d games we wanna use this library)
here is how you can install this :
1. open vs code editor
2. go to the terminal -> new terminal -> type pip install Pygame
2. Understanding Python syntax, concepts
ok now we're going to build our own game the first step you wanna import the Pygame library
and another library we should have to import that one is :
this one is used to exit the game when users quit the game this one is very important if you don't use sys it causes an error to quit the game
new we wanna set the screen width and height
screen_width = 1200
screen_height = 600
I have assigned these values you can use any value whatever you want.
write this code to set the height and width, caption, color of the screen
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("car game")
Now we're going to take two car pictures to set to play. you can download 2d game photos through this website https://opengameart.org/content/2d-top-view
once you download the picture that you want make sure you have these pictures inside the now-working project document otherwise this project will cause an error
once you set up you need to use these images in your game
here is how you can do this
car1 = pygame.image.load(r"image_path")
#use this code for adjust your image size
resize_the_car1 = pygame.transform.scale(car1,(100,50))
car2 = pygame.image.load(r"image_path")
resize_the_car2 = pygame.transform.scale(car2,(100,50))
make sure you replace the image path with your actual image path
#assign a function for creating this game
def game():
#intilise the car1 position
car1_x , car1_y = 100,100
#intilise the car2 position
car2_x , car2_y = 100,300
#assign the speed to move the car
car_speed = 1
#assign the finish line which car
reach this line then
we can announce that the car is the winner
#in this code I have assigned
finish line wants to be screen width - 100 which means
screen width = 1200 - 100 = 1100(finish line)
finish_line = screen_width - 100
#initially we assign a winner is none
winner = None
#assign the Boolean run_game = True
run_game = True
#this loop will run until run_game becomes a False
while run_game:
#this loop is for if the user quits
run_game will be False
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_game = False
#Earlier we assigned black,
which is used to fill the screen
screen.fill(BLACK)
#get the state of all keywords being pressed
keys = pygame.key.get_pressed()
#check if the left key is pressed
if keys[pygame.K_LEFT]:
#Move car1 to the left by subtracting
car_speed from its x-coordinate
car1_x -= car_speed
#check if the right key is pressed
if keys[pygame.K_RIGHT]:
car1_x += car_speed
#check if the d key is pressed
if keys[pygame.K_d]:
car2_x += car_speed
#check if the key is pressed
if keys[pygame.K_a]:
car2_x -= car_speed
#check if the car1 crossed the finish line
if(car1_x >= finish_line):
run_game = False
winner = "Car1"
#check if the car2 crossed the finish line
elif(car2_x >= finish_line):
run_game = False
winner = "Car2"
#Blit (draw) the resized image of car1
onto the screen at position (car1_x, car1_y)
screen.blit(resize_the_car1, (car1_x , car1_y))
#Blit (draw) the resized image of car2
onto the screen at position (car2_x, car2_y)
screen.blit(resize_the_car2, (car2_x ,car2_y))
#this code draws the finish line
pygame.draw.rect(screen, (255, 0, 0), (finish_line, 0, 5, screen_height))
# Update the display to show the changes made to the screen
pygame.display.flip()
print("winner is : " + winner)
game()
pygame.quit()
sys.exit()
ok now we have completed our own 2d car game here is the full code :
import pygame
import sys
screen_width = 1200
screen_height = 600
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("car game")
BLACK = (0,0,0)
car1 = pygame.image.load(r"image path")
resize_the_car1 = pygame.transform.scale(car1,(100,50))
car2 = pygame.image.load(r"image path")
resize_the_car2 = pygame.transform.scale(car2,(100,50))
def game():
car1_x , car1_y = 100,100
car2_x , car2_y = 100,300
car_speed = 1
finish_line = screen_width - 100
winner = None
run_game = True
while run_game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_game = False
screen.fill(BLACK)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
car1_x -= car_speed
if keys[pygame.K_RIGHT]:
car1_x += car_speed
if keys[pygame.K_d]:
car2_x += car_speed
if keys[pygame.K_a]:
car2_x -= car_speed
if(car1_x >= finish_line):
run_game = False
winner = "Car1"
elif(car2_x >= finish_line):
run_game = False
winner = "Car2"
screen.blit(resize_the_car1, (car1_x , car1_y))
screen.blit(resize_the_car2, (car2_x ,car2_y))
pygame.draw.rect(screen, (255, 0, 0), (finish_line, 0, 5, screen_height))
pygame.display.flip()
print("winner is : " + winner)
game()
pygame.quit()
sys.exit()
Conclusion:
I hope you found this project easy to understand and fun to work on! If you have any questions or run into any problems, don't hesitate to reach out. Happy coding 😊
Comments
Post a Comment