Introduction
Pygame is a Python package designed primarily for creating and designing games. Pygame only supports 2D games created with various sprites. Pygame isn't ideal for game development because it's difficult to use and lacks a real GUI like Unity, but it does provide logic for more sophisticated applications.
We're going to develop a simple game using the following guidelines:-
- The player can only move vertically.
- In addition to the player block, there will be two other blocks.
- One will be a scoring block, and the other will be an opposition block.
- As soon as the player collides with an enemy block, the game ends. If the player collides with a scoring block, the score increases, and all scoring blocks are necessary.
We'll employ various strategies, including functions, random variables, and several pygame routines, among others.
Installation
We must first install the Python Game Library before we can use it. In the terminal, execute the following command to install it.
pip install pygame
To initialize the pygame library, we need to write the following lines after installing it:-
import pygame
pygame.init()
Python implementation
# Python program to demonstrate an 8-bit game
import sys
import pygame
import random
# initializing pygame
pygame.init()
res = (720, 720)
# randomly assigning a value to variables
c1 = random.randint(125, 255)
c2 = random.randint(0, 255)
c3 = random.randint(0, 255)
display = pygame.display.set_mode(res)
clock = pygame.time.Clock()
colox_c1 = 0
colox_c2 = 0
colox_c3 = 254
colox_c4 = 254
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
color_list = [red, green, blue]
# randomly assigning a colour from color_list to player
player_c = random.choice(color_list)
# light shade of menu buttons
startl = (169, 169, 169)
# dark shade of menu buttons
startd = (100, 100, 100)
white = (255, 255, 255)
start = (255, 255, 255)
width = display.get_width()
height = display.get_height()
# initial X position of player
lead_x = 40
# initial Y position of player
lead_y = height / 2
x = 300
y = 290
width1 = 100
height1 = 40
enemy_size = 50
# Defining font
smallfont = pygame.font.SysFont('Corbel', 35)
# Texts to be shown on screen
text = smallfont.render('Start', True, white)
text1 = smallfont.render('Options', True, white)
exit1 = smallfont.render('Exit', True, white)
colox = smallfont.render('Colox', True, (c3, c2, c1))
x1 = random.randint(width / 2, width)
y1 = random.randint(100, height / 2)
x2 = 40
y2 = 40
speed = 15
# score of the player
count = 0
rgb = random.choice(color_list)
e_p = [width, random.randint(50, height - 50)]
e1_p = [random.randint(width, width + 100), random.randint(50, height
- 100)]
# game_over function
def game_over():
while True:
# when the player clicks the cross button
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
# exit
pygame.quit()
if ev.type == pygame.MOUSEBUTTONDOWN:
if height - 100 < mouse1[1] < height - 80 and 100 < mouse1[0] < 140:
# exit
pygame.quit()
if ev.type == pygame.MOUSEBUTTONDOWN:
if height - 100 < mouse1[1] < height - 80 and width - 180 < mouse1[0] < width - 100:
game(lead_x, lead_y, speed, count)
# covers the screen with the colour specified
display.fill((65, 25, 64))
smallfont = pygame.font.SysFont('Corbel', 60)
smallfont1 = pygame.font.SysFont('Corbel', 25)
game_over = smallfont.render('GAME OVER', True, white)
game_exit = smallfont1.render('exit', True, white)
restart = smallfont1.render('restart', True, white)
mouse1 = pygame.mouse.get_pos()
# exit button
if height - 100 < mouse1[1] < height - 80 and 100 < mouse1[0] < 140:
pygame.draw.rect(display, startl, [100, height - 100, 40,20])
else:
pygame.draw.rect(display, startd, [100, height - 100, 40,20])
# restart button
if height - 100 < mouse1[1] < height - 80 and width - 180 < mouse1[0] < width - 100:
pygame.draw.rect(display, startl, [width - 180, height- 100, 80, 20])
else:
pygame.draw.rect(display, startd, [width - 180, height- 100, 80, 20])
display.blit(game_exit, (100, height - 100))
# one object on top of another
display.blit(restart, (width - 180, height - 100))
display.blit(game_over, (width / 2 - 150, 295))
pygame.display.update()
pygame.draw.rect(display, startd, [100, height - 100, 40, 20])
pygame.draw.rect(display, startd, [width - 180, height - 100, 40, 50])
# main game function
def game(lead_y,lead_X,speed,count,):
while True:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
# The key pressed is recorded by player control.
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
# If the up key is pushed, the player's y position will be decremented by ten.
lead_y -= 10
if keys[pygame.K_DOWN]:
# If the down key is pushed, the player's y position is increased by ten.
lead_y += 10
display.fill((65, 25, 64))
clock.tick(speed)
# a rectangle is drawn on the screen
rect = pygame.draw.rect(display, player_c, [lead_x, lead_y, 40,40])
# side rectangle is drawn on the screen
pygame.draw.rect(display, (c1, c2, c3), [0, 0, width, 40])
# side rectangle is drawn on the screen
pygame.draw.rect(display, (c3, c2, c1), [0, 680, width, 40])
# side rectangle is drawn on the screen
pygame.draw.rect(display, startd, [width - 100, 0, 100, 40])
smallfont = pygame.font.SysFont('Corbel', 35)
exit2 = smallfont.render('Exit', True, white)
# exit
# obtains the mouse pointer's X and Y positions and saves them as a tuple
mouse = pygame.mouse.get_pos()
if 0 < mouse[1] < 40 and width - 100 < mouse[0] < width:
pygame.draw.rect(display, startl, [width - 100, 0, 100, 40])
else:
pygame.draw.rect(display, startd, [width - 100, 0, 100, 40])
if 0 < mouse[1] < 40 and width - 100 < mouse[0] < width:
if ev.type == pygame.MOUSEBUTTONDOWN:
pygame.quit()
# enemy position
if e_p[0] <= width and e_p[0] > 0:
# If the X coordinate of an opponent block is between 0 and the screen width, the X value is decremented by 10.
e_p[0] -= 10
else:
if e_p[1] >= height - 40 or e_p[1] <= 40:
e_p[1] = height / 2
if e1_p[1] >= height - 40 or e1_p[1] <= 40:
e1_p[1] = random.randint(40, height - 40)
e_p[1] = random.randint(enemy_size, height - enemy_size)
e_p[0] = width
# game over and collision detection
if lead_y >= e_p[1] >= lead_y - 40 and lead_x <= e_p[0] <= lead_x + 40:
game_over()
# determines if the player block and the adversary block have collided
if lead_x <= e_p[0] <= lead_x + 40 and lead_y <= e_p[1] + enemy_size <= lead_y + 40:
game_over()
pygame.draw.rect(display, red, [e_p[0], e_p[1], enemy_size,enemy_size])
if e1_p[0] <= width + 100 and e1_p[0] > 0:
e1_p[0] -= 10
else:
if e1_p[1] >= height - 40 or e1_p[1] <= 40:
e1_p[1] = height / 2
e1_p[1] = random.randint(enemy_size, height - 40)
e1_p[0] = width + 100
if lead_y >= e1_p[1] >= lead_y - 40 and lead_x <= e1_p[0] <= lead_x + 40:
e1_p[0] = width + 100
e1_p[1] = random.randint(40, height - 40)
count += 1
speed += 1
if lead_x <= e1_p[0] <= lead_x + 40 and lead_y <= e1_p[1] + enemy_size <= lead_y + 40:
e1_p[0] = width + 100
e1_p[1] = random.randint(40, height - 40)
# The score is raised when the blue box is hit.
count += 1
# The speed increases as the score rises.
speed += 1
if count >= 45:
# The game's FPS is stuck at 60 if the score hits 45 or higher.
speed = 60
if lead_y >= height - 38 or lead_y <= 38:
game_over()
if e1_p[0] <= 0:
game_over()
pygame.draw.rect(display, blue, [e1_p[0], e1_p[1], enemy_size,
enemy_size])
score1 = smallfont.render('Score:', True, white)
display.blit(score1, (width - 120, height - 40))
display.blit(exit2, (width - 80, 0))
pygame.display.update()
# intro function
def intro(colox_c1,colox_c2,colox,exit1,text1,text,):
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
display.fill((65, 25, 64))
mouse = pygame.mouse.get_pos()
# start screen
if y < mouse[1] < y + height1 and x < mouse[0] < x + width1:
# The colour shade lightens as the mouse is hovered over a button.
pygame.draw.rect(display, startl, [x, y, width1, height1])
else:
if y + 70 < mouse[1] < y + 70 + height1 and x < mouse[0] < x + width1 + 40:
pygame.draw.rect(display, startl, [x, y + 70, width1+40,height1])
else:
if y + 140 < mouse[1] < y + 140 + height1 and x < mouse[0] < width1 + x:
pygame.draw.rect(display, startl, [x, y + 140,width1,height1])
else:
pygame.draw.rect(display, startd, [x, y, width1,height1])
pygame.draw.rect(display, startd, [x, y + 70, width1 + 40, height1])
pygame.draw.rect(display, startd, [x, y + 140,width1, height1])
# start
if event.type == pygame.MOUSEBUTTONDOWN:
if x < mouse[0] < x + width1 and y < mouse[1] < y + height1:
game(lead_y, lead_x, speed, count)
if event.type == pygame.MOUSEBUTTONDOWN:
if x < mouse[0] < width1 + x and y + 140 < mouse[1] < y + 140 + height1:
pygame.quit()
# The colour breezing effect is implemented in this way.
if 0 <= colox_c2 <= 254 or 0 <= colox_c1 <= 254:
colox_c1 += 1
colox_c2 += 1
if colox_c2 >= 254 or colox_c1 >= 254:
colox_c1 = c3
colox_c2 = c3
pygame.draw.rect(display, (c2, colox_c1, colox_c2), [0, 0, 40,
height])
pygame.draw.rect(display, (c2, colox_c1, colox_c2), [width - 40,
0, 40, height])
smallfont = pygame.font.SysFont('Corbel', 35)
sig = smallfont.render('Designed by :- Antriksh', True, white)
text = smallfont.render('Start', True, white)
text1 = smallfont.render('Options', True, white)
exit1 = smallfont.render('Exit', True, white)
colox = smallfont.render('Colox', True, (c1, colox_c1,
colox_c2))
display.blit(colox, (312, 50))
display.blit(text, (312, 295))
display.blit(text1, (312, 365))
display.blit(exit1, (312, 435))
display.blit(sig, (320, height - 50))
clock.tick(60)
pygame.display.update()
intro(colox_c1,colox_c2,colox,exit1,text1,text,)