Project 6: Reinforcement Gaming Agent

Game Building

The game built is a simple spaceship dodger game where a player-controlled spaceship must avoid collission with enemies to gain points by surviving. The game features dynamic enemy spawning, randomized movement patterns, and increasing difficulty as more enemies appear.

  • The player starts at the center of the screen and can move in four directions using discrete actions (UP, DOWN, LEFT, RIGHT).
  • Enemies spawn randomly from the edges of the screen and move toward the player’s position.
  • The goal is to avoid any collission with enemies as long as possible.

This video shows me playing the game:



The game is built according to mentioned environment or lab gym using Python and Pygame for real-time gameplay and rendering.


import pygame
import random
import math

#MAIN GAME CONFIG
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 900
PLAYER_SIZE = 20
ENEMY_RADIUS = 10

class Player(pygame.sprite.Sprite):
	def __init__(self):
		super().__init__()
		self.image = pygame.Surface((PLAYER_SIZE, PLAYER_SIZE))
		self.image.fill((0, 255, 0))
		self.rect = self.image.get_rect()
		self.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
		self.speed = 12

class Enemy(pygame.sprite.Sprite):
	def __init__(self, x, y, vx, vy):
		super().__init__()
		self.image = pygame.Surface((ENEMY_RADIUS * 2, ENEMY_RADIUS * 2), pygame.SRCALPHA)
		pygame.draw.circle(self.image, (255, 0, 0), (ENEMY_RADIUS, ENEMY_RADIUS), ENEMY_RADIUS)
		self.rect = self.image.get_rect(center=(x, y))
		self.vx = vx
		self.vy = vy

	def update(self):
		self.rect.x += self.vx
		self.rect.y += self.vy
		if (self.rect.x < -ENEMY_RADIUS * 2 or self.rect.x > SCREEN_WIDTH + ENEMY_RADIUS * 2 or
			self.rect.y < -ENEMY_RADIUS * 2 or self.rect.y > SCREEN_HEIGHT + ENEMY_RADIUS * 2):
			self.kill()

def spawn_enemy(player_position):
	player_x, player_y = player_position

	edge = random.randint(0, 3)
	if edge == 0:
		x = random.randint(-ENEMY_RADIUS, SCREEN_WIDTH + ENEMY_RADIUS)
		y = -ENEMY_RADIUS
	elif edge == 1:
		x = SCREEN_WIDTH + ENEMY_RADIUS
		y = random.randint(-ENEMY_RADIUS, SCREEN_HEIGHT + ENEMY_RADIUS)
	elif edge == 2:
		x = random.randint(-ENEMY_RADIUS, SCREEN_WIDTH + ENEMY_RADIUS)
		y = SCREEN_HEIGHT + ENEMY_RADIUS
	else:
		x = -ENEMY_RADIUS
		y = random.randint(-ENEMY_RADIUS, SCREEN_HEIGHT + ENEMY_RADIUS)

	target_x, target_y = player_x, player_y
	dx = target_x - x
	dy = target_y - y
	distance = math.sqrt(dx ** 2 + dy ** 2)
	speed = random.uniform(2, 12)
	vx = dx / distance * speed
	vy = dy / distance * speed

	return Enemy(x, y, vx, vy)
								

This code enables the user to play themselves:


import pygame
import sys
from main import Player, Enemy, spawn_enemy

SCREEN_WIDTH = 900
SCREEN_HEIGHT = 900

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Play Mode - Spaceship Dodger")
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 36)

player = Player()
player_group = pygame.sprite.Group(player)
enemy_group = pygame.sprite.Group()

running = True
spawn_timer = 0
score = 0

while running:
    screen.fill((0, 0, 0))

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

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player.rect.x -= player.speed
    if keys[pygame.K_RIGHT]:
        player.rect.x += player.speed
    if keys[pygame.K_UP]:
        player.rect.y -= player.speed
    if keys[pygame.K_DOWN]:
        player.rect.y += player.speed

    if player.rect.left < 0:
        player.rect.left = 0
    if player.rect.right > SCREEN_WIDTH:
        player.rect.right = SCREEN_WIDTH
    if player.rect.top < 0:
        player.rect.top = 0
    if player.rect.bottom > SCREEN_HEIGHT:
        player.rect.bottom = SCREEN_HEIGHT

    spawn_timer += 1
    if spawn_timer % 30 == 0:
        player_position = player.rect.center
        enemy_group.add(spawn_enemy(player_position))

    enemy_group.update()

    if pygame.sprite.spritecollideany(player, enemy_group):
        print(f"Game Over! Score: {score}")
        running = False

    player_group.draw(screen)
    enemy_group.draw(screen)

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

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

pygame.quit()
sys.exit()