0% found this document useful (0 votes)
21 views4 pages

Trenta

This document is a Python script that implements a simple 2D game using Pygame, where a player controls a character that must avoid enemies while gaining experience and leveling up. The game features character and enemy classes, movement mechanics, collision detection, and a scoring system. The player can restart the game after a game over by pressing the 'R' key.

Uploaded by

Arceto Lagenza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Trenta

This document is a Python script that implements a simple 2D game using Pygame, where a player controls a character that must avoid enemies while gaining experience and leveling up. The game features character and enemy classes, movement mechanics, collision detection, and a scoring system. The player can restart the game after a game over by pressing the 'R' key.

Uploaded by

Arceto Lagenza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import pygame

import random
import math

# Initialize Pygame
pygame.init()

# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Pixel Battle Arena")

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Character Class
class Character:
def __init__(self, x, y, color, speed, health, damage):
self.x = x
self.y = y
self.color = color
self.speed = speed
self.health = health
self.max_health = health
self.damage = damage
self.level = 1
self.exp = 0
self.rect = pygame.Rect(x, y, 50, 50)

def move(self, dx, dy):


self.x += dx * self.speed
self.y += dy * self.speed
self.rect.x = self.x
self.rect.y = self.y

def draw(self, surface):


pygame.draw.rect(surface, self.color, self.rect)
# Health bar
health_width = self.rect.width * (self.health / self.max_health)
health_rect = pygame.Rect(self.rect.x, self.rect.y - 10, health_width, 5)
pygame.draw.rect(surface, GREEN, health_rect)

def take_damage(self, amount):


self.health -= amount
if self.health <= 0:
return True
return False

def level_up(self):
self.level += 1
self.max_health += 20
self.damage += 5
self.health = self.max_health
# Enemy Class
class Enemy:
def __init__(self, x, y):
self.x = x
self.y = y
self.speed = random.uniform(1, 3)
self.color = (random.randint(50, 200), random.randint(50, 200),
random.randint(50, 200))
self.rect = pygame.Rect(x, y, 40, 40)
self.health = 30
self.damage = 10

def move_towards_player(self, player):


# Calculate direction vector
dx = player.x - self.x
dy = player.y - self.y
dist = math.hypot(dx, dy)

# Normalize direction
if dist != 0:
dx = dx / dist
dy = dy / dist

# Move
self.x += dx * self.speed
self.y += dy * self.speed
self.rect.x = self.x
self.rect.y = self.y

def draw(self, surface):


pygame.draw.rect(surface, self.color, self.rect)

# Game Class
class Game:
def __init__(self):
self.player = Character(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, BLUE, 5,
100, 10)
self.enemies = []
self.score = 0
self.font = pygame.font.Font(None, 36)
self.spawn_timer = 0
self.game_over = False

def spawn_enemy(self):
# Spawn enemies at screen edges
side = random.randint(0, 3)
if side == 0: # Top
x = random.randint(0, SCREEN_WIDTH)
y = -50
elif side == 1: # Right
x = SCREEN_WIDTH + 50
y = random.randint(0, SCREEN_HEIGHT)
elif side == 2: # Bottom
x = random.randint(0, SCREEN_WIDTH)
y = SCREEN_HEIGHT + 50
else: # Left
x = -50
y = random.randint(0, SCREEN_HEIGHT)
self.enemies.append(Enemy(x, y))

def handle_collisions(self):
# Player-Enemy Collisions
for enemy in self.enemies[:]:
if self.player.rect.colliderect(enemy.rect):
if self.player.take_damage(enemy.damage):
self.game_over = True
self.enemies.remove(enemy)
self.score += 1

def update(self):
if self.game_over:
return

# Spawn enemies
self.spawn_timer += 1
if self.spawn_timer >= 60: # Spawn every second
self.spawn_enemy()
self.spawn_timer = 0

# Move enemies
for enemy in self.enemies:
enemy.move_towards_player(self.player)

# Check collisions
self.handle_collisions()

# Level up logic
if self.score > 0 and self.score % 10 == 0:
self.player.level_up()

def draw(self, surface):


# Draw background
surface.fill(WHITE)

# Draw player
self.player.draw(surface)

# Draw enemies
for enemy in self.enemies:
enemy.draw(surface)

# Draw score and level


score_text = self.font.render(f"Score: {self.score}", True, BLACK)
level_text = self.font.render(f"Level: {self.player.level}", True, BLACK)
surface.blit(score_text, (10, 10))
surface.blit(level_text, (10, 50))

# Game over screen


if self.game_over:
game_over_text = self.font.render("Game Over!", True, RED)
restart_text = self.font.render("Press R to Restart", True, BLACK)
surface.blit(game_over_text, (SCREEN_WIDTH//2 - 100, SCREEN_HEIGHT//2 -
50))
surface.blit(restart_text, (SCREEN_WIDTH//2 - 100, SCREEN_HEIGHT//2 +
50))

# Main game loop


def main():
clock = pygame.time.Clock()
game = Game()
running = True

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

# Game over restart


if game.game_over:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
game = Game() # Restart the game

# Player movement
if not game.game_over:
keys = pygame.key.get_pressed()
move_x = keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]
move_y = keys[pygame.K_DOWN] - keys[pygame.K_UP]
game.player.move(move_x, move_y)

# Update game state


game.update()

# Draw everything
game.draw(screen)

# Update display
pygame.display.flip()

# Cap the frame rate


clock.tick(60)

pygame.quit()

# Run the game


if __name__ == "__main__":
main()

You might also like