0% encontró este documento útil (0 votos)
20 vistas4 páginas

Yes

El documento es un código en Python que implementa un juego llamado 'Brawl Arena' utilizando la biblioteca Pygame. El juego permite a un jugador controlar un personaje llamado Paladín, enfrentándose a enemigos que aparecen aleatoriamente en la pantalla, mientras gestiona la vida y la puntuación del jugador. Incluye funcionalidades para el movimiento del jugador, la generación de enemigos, y la gestión de un menú y pantalla de Game Over.

Cargado por

Arceto Lagenza
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
20 vistas4 páginas

Yes

El documento es un código en Python que implementa un juego llamado 'Brawl Arena' utilizando la biblioteca Pygame. El juego permite a un jugador controlar un personaje llamado Paladín, enfrentándose a enemigos que aparecen aleatoriamente en la pantalla, mientras gestiona la vida y la puntuación del jugador. Incluye funcionalidades para el movimiento del jugador, la generación de enemigos, y la gestión de un menú y pantalla de Game Over.

Cargado por

Arceto Lagenza
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd

import pygame

import sys
import random
import math

# Inicializar Pygame
pygame.init()
pygame.mixer.init()

# Configuraciones de pantalla
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Brawl Arena")

# Colores
BLANCO = (255, 255, 255)
NEGRO = (0, 0, 0)
ROJO = (255, 0, 0)
VERDE = (0, 255, 0)
AZUL = (0, 0, 255)

# Clase de Personaje Base


class Personaje(pygame.sprite.Sprite):
def __init__(self, x, y, color, velocidad, vida, daño):
super().__init__()
self.image = pygame.Surface((50, 50))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.velocidad = velocidad
self.vida_max = vida
self.vida = vida
self.daño = daño
self.nivel = 1

def mover(self, dx, dy):


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

# Limitar movimiento a la pantalla


self.rect.clamp_ip(screen.get_rect())

def recibir_daño(self, cantidad):


self.vida -= cantidad
return self.vida <= 0

def dibujar_barra_vida(self, superficie):


longitud_barra = self.rect.width
barra_vida_actual = longitud_barra * (self.vida / self.vida_max)
barra_vida = pygame.Rect(self.rect.x, self.rect.y - 10, barra_vida_actual,
5)
barra_vida_fondo = pygame.Rect(self.rect.x, self.rect.y - 10,
longitud_barra, 5)

pygame.draw.rect(superficie, ROJO, barra_vida_fondo)


pygame.draw.rect(superficie, VERDE, barra_vida)

# Personajes específicos basados en las imágenes


class Paladín(Personaje):
def __init__(self, x, y):
super().__init__(x, y, (100, 100, 150), 5, 200, 15)

class Tick(Personaje):
def __init__(self, x, y):
super().__init__(x, y, (255, 0, 0), 6, 150, 20)

class Enemigo(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((40, 40))
self.image.fill((random.randint(50, 200), random.randint(50, 200),
random.randint(50, 200)))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.velocidad = random.uniform(2, 4)
self.vida = 30
self.daño = 10

def seguir_jugador(self, jugador):


dx = jugador.rect.centerx - self.rect.centerx
dy = jugador.rect.centery - self.rect.centery
dist = math.hypot(dx, dy)

if dist != 0:
dx, dy = dx / dist, dy / dist

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

# Clase del Juego


class Juego:
def __init__(self):
self.estado = "menu"
self.fuente = pygame.font.Font(None, 36)
self.fuente_grande = pygame.font.Font(None, 72)
self.reloj = pygame.time.Clock()
self.reiniciar()

def reiniciar(self):
self.jugador = Paladín(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
self.todos_sprites = pygame.sprite.Group(self.jugador)
self.enemigos = pygame.sprite.Group()
self.puntuacion = 0
self.temporizador_enemigos = 0
self.game_over = False

def spawn_enemigos(self):
self.temporizador_enemigos += 1
if self.temporizador_enemigos >= 60:
lado = random.randint(0, 3)
if lado == 0: # Arriba
x = random.randint(0, SCREEN_WIDTH)
y = -50
elif lado == 1: # Derecha
x = SCREEN_WIDTH + 50
y = random.randint(0, SCREEN_HEIGHT)
elif lado == 2: # Abajo
x = random.randint(0, SCREEN_WIDTH)
y = SCREEN_HEIGHT + 50
else: # Izquierda
x = -50
y = random.randint(0, SCREEN_HEIGHT)

enemigo = Enemigo(x, y)
self.enemigos.add(enemigo)
self.todos_sprites.add(enemigo)
self.temporizador_enemigos = 0

def manejar_colisiones(self):
colisiones = pygame.sprite.spritecollide(self.jugador, self.enemigos, True)
for enemigo in colisiones:
if self.jugador.recibir_daño(enemigo.daño):
self.game_over = True
self.puntuacion += 1

def dibujar_menu(self):
screen.fill(BLANCO)
titulo = self.fuente_grande.render("Brawl Arena", True, NEGRO)
instrucciones = self.fuente.render("Presiona ESPACIO para comenzar", True,
NEGRO)

screen.blit(titulo, (SCREEN_WIDTH//2 - titulo.get_width()//2,


SCREEN_HEIGHT//2 - 100))
screen.blit(instrucciones, (SCREEN_WIDTH//2 - instrucciones.get_width()//2,
SCREEN_HEIGHT//2 + 50))

def dibujar_game_over(self):
screen.fill(BLANCO)
game_over = self.fuente_grande.render("Game Over", True, ROJO)
puntuacion = self.fuente.render(f"Puntuación: {self.puntuacion}", True,
NEGRO)
reintentar = self.fuente.render("Presiona R para reintentar", True, NEGRO)

screen.blit(game_over, (SCREEN_WIDTH//2 - game_over.get_width()//2,


SCREEN_HEIGHT//2 - 100))
screen.blit(puntuacion, (SCREEN_WIDTH//2 - puntuacion.get_width()//2,
SCREEN_HEIGHT//2))
screen.blit(reintentar, (SCREEN_WIDTH//2 - reintentar.get_width()//2,
SCREEN_HEIGHT//2 + 100))

def bucle_principal(self):
running = True
while running:
for evento in pygame.event.get():
if evento.type == pygame.QUIT:
return False

if self.estado == "menu":
if evento.type == pygame.KEYDOWN and evento.key ==
pygame.K_SPACE:
self.estado = "jugando"

if self.estado == "game_over":
if evento.type == pygame.KEYDOWN:
if evento.key == pygame.K_r:
self.reiniciar()
self.estado = "jugando"
elif evento.key == pygame.K_ESCAPE:
self.estado = "menu"

if self.estado == "menu":
self.dibujar_menu()

elif self.estado == "jugando":


# Movimiento del jugador
teclas = pygame.key.get_pressed()
move_x = teclas[pygame.K_RIGHT] - teclas[pygame.K_LEFT]
move_y = teclas[pygame.K_DOWN] - teclas[pygame.K_UP]
self.jugador.mover(move_x, move_y)

# Actualizar
self.spawn_enemigos()

# Mover enemigos
for enemigo in self.enemigos:
enemigo.seguir_jugador(self.jugador)

# Colisiones
self.manejar_colisiones()

# Dibujar
screen.fill(BLANCO)
self.todos_sprites.draw(screen)
self.jugador.dibujar_barra_vida(screen)

# Mostrar puntuación
texto_puntuacion = self.fuente.render(f"Puntuación:
{self.puntuacion}", True, NEGRO)
screen.blit(texto_puntuacion, (10, 10))

# Game Over
if self.jugador.vida <= 0:
self.estado = "game_over"

elif self.estado == "game_over":


self.dibujar_game_over()

pygame.display.flip()
self.reloj.tick(60)

return True

# Función principal
def main():
juego = Juego()
juego.bucle_principal()
pygame.quit()
sys.exit()

# Ejecutar juego
if __name__ == "__main__":
main()

También podría gustarte