Skip to main content

Command Palette

Search for a command to run...

Create a Simple Game In Python.

Published
โ€ข2 min readโ€ขView as Markdown
H

Founder of SMMA AGENCY NETWIZTECH l ๐Ÿš€ Tech Enthusiast | ๐Ÿ’ป Self-taught Frontend Dev | ๐ŸŒ Full Stack Enthusiast | ๐Ÿค– Generative AI Explorer | ๐Ÿ“š BTECH AI & DS | No Code

simple game written in Python using the pygame library. This game is a basic "dodge the obstacles" game where the player controls a character that moves horizontally across the screen and must avoid obstacles that fall from the top of the screen.

First, we will import the necessary libraries and set up the game window:

import pygame
import random

# Initialize pygame
pygame.init()

# Set the window size and title
width, height = 640, 480
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Dodge the Obstacles")

# Set the colors
black = (0, 0, 0)
white = (255, 255, 255)

# Set the player and obstacle variables
player_pos = [50, height-50]
player_size = 50
obstacles = []

# Set the clock and FPS
clock = pygame.time.Clock()
FPS = 60

Next, we will create a game loop to handle the game's logic and rendering:

# Create the game loop
while True:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Update the player position based on user input
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_pos[0] > 0:
        player_pos[0] -= 5
    if keys[pygame.K_RIGHT] and player_pos[0] < width - player_size:
        player_pos[0] += 5

    # Update the obstacle positions
    for obstacle in obstacles:
        obstacle[1] += 5
        if obstacle[1] > height:
            obstacles.remove(obstacle)

    # Add a new obstacle to the list every few frames
    if len(obstacles) < 10:
        x = random.randrange(0, width - player_size)
        obstacles.append([x, 0])

    # Check for collisions
    for obstacle in obstacles:
        if player_pos[0] < obstacle[0] < player_pos[0] + player_size and obstacle[1] < player_pos[1] < obstacle[1] + player_size:
            pygame.quit()
            sys.exit()

    # Clear the window
    window.fill(black)

    # Draw the player and obstacles
    pygame.draw.rect(window, white, pygame.Rect(player_pos[0], player_pos[1], player_size, player_size))
    for obstacle in obstacles:
        pygame.draw.rect(window, white, pygame.Rect(obstacle[0], obstacle[1], player_size, player_size))

    # Update the display
    pygame.display.update()
    clock.tick(FPS)

This code handles the game's events, updates the player's position based on user input, moves the obstacles down the screen, adds new obstacles to the list every few frames, checks for collisions between the player and obstacles, and renders the player and obstacles on the screen. The game loop also handles quitting the game when the player closes

More from this blog

H

Hardik Kawale

10 posts

Founder of SMMA AGENCY NETWIZTECH l ๐Ÿš€ Tech Enthusiast | ๐Ÿ’ป Self-taught Frontend Dev | ๐ŸŒ Full Stack Enthusiast | ๐Ÿค– Generative AI Explorer | ๐Ÿ“š BTECH AI & DS | No Code