Pong game in Python

Post your programming questions here regarding Microsoft Agent or related topics. Recommended languages: Visual Basic 5.0/6.0, Visual Basic .NET
Post Reply
User avatar
united123
Posts: 151
Joined: Tue Feb 10, 2026 7:28 am
Location: India
Favorite Microsoft Agent: Bonzi
Operating System: Windows 11 Pro 25H2
Contact:

Pong game in Python

Post by united123 »



Here's the code:

Code: Select all

import pygame
import random
import math

# Initialize Pygame
pygame.init()

# Screen setup
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Space Invaders")

# Clock for FPS control
clock = pygame.time.Clock()

# Player
player_width = 40
player_height = 30
player_x = screen_width // 2 - player_width // 2
player_y = screen_height - player_height - 10
player_speed = 5

# Bullet
bullet_width = 5
bullet_height = 15
bullet_speed = 7
bullet_x = 0
bullet_y = player_y
bullet_active = False

# Enemy
enemy_width = 40
enemy_height = 30
enemy_x = random.randint(0, screen_width - enemy_width)
enemy_y = 50
enemy_speed = 3

# Score
score = 0
font = pygame.font.Font(None, 36)

def draw_player(x, y):
    pygame.draw.rect(screen, (0, 255, 0), (x, y, player_width, player_height))

def draw_bullet(x, y):
    pygame.draw.rect(screen, (255, 255, 0), (x, y, bullet_width, bullet_height))

def draw_enemy(x, y):
    pygame.draw.rect(screen, (255, 0, 0), (x, y, enemy_width, enemy_height))

def show_score():
    score_text = font.render(f"Score: {score}", True, (255, 255, 255))
    screen.blit(score_text, (10, 10))

def is_collision(ex, ey, bx, by):
    distance = math.hypot(ex + enemy_width//2 - bx, ey + enemy_height//2 - by)
    return distance < 27

# Game loop
running = True
while running:
    screen.fill((0, 0, 0))  # Black background

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Key states
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player_x -= player_speed
    if keys[pygame.K_RIGHT]:
        player_x += player_speed
    if keys[pygame.K_SPACE] and not bullet_active:
        bullet_x = player_x + player_width // 2 - bullet_width // 2
        bullet_y = player_y
        bullet_active = True

    # Keep player within bounds
    player_x = max(0, min(player_x, screen_width - player_width))

    # Bullet movement
    if bullet_active:
        bullet_y -= bullet_speed
        draw_bullet(bullet_x, bullet_y)
        if bullet_y < 0:
            bullet_active = False

    # Enemy movement
    enemy_x += enemy_speed
    if enemy_x <= 0 or enemy_x >= screen_width - enemy_width:
        enemy_speed *= -1
        enemy_y += 40

    draw_enemy(enemy_x, enemy_y)

    # Collision detection
    if bullet_active and is_collision(enemy_x, enemy_y, bullet_x, bullet_y):
        score += 1
        bullet_active = False
        enemy_x = random.randint(0, screen_width - enemy_width)
        enemy_y = 50

    draw_player(player_x, player_y)
    show_score()

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Hey there!

I am United World, who is a Techy, Mapper, Ident uploader and an MS Agent Tuber.

Image
Post Reply