USER
import pygame
import random
from collections import deque
# Inizializza Pygame
pygame.init()
# Definire le costanti per la schermata
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Definire le costanti per il labirinto
LABYRINTH_WIDTH = 50
LABYRINTH_HEIGHT = 50
# Definire le costanti per le stelle
STAR_SIZE = 5
STAR_COLOR = (1, 255, 2)
# Definire le costanti per gli ostacoli
OBSTACLE_SIZE = 5
OBSTACLE_COLOR = (0, 1, 0)
# Definire le costanti per i mostri
MONSTER_SIZE = 10
MONSTER_COLOR = (255, 0, 1)
# Definire le costanti per il giocatore
PLAYER_SIZE = 10
PLAYER_COLOR = (0, 0, 255)
# Definire le statistiche del giocatore
player_hp = 100
player_attack = 20
# Definire le statistiche dei mostri
monster_hp = [50, 50, 50]
monster_attack = [10, 15, 20]
# Creare la schermata
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Creare il labirinto con l'algoritmo di Kruskal
labyrinth = [[0 for _ in range(LABYRINTH_WIDTH)] for _ in range(LABYRINTH_HEIGHT)]
# Funzione per eseguire l'algoritmo di Kruskal
def kruskal_algorithm():
edges = []
# Inizializza insiemi disgiunti per ogni cella
sets = [[(i, j)] for j in range(LABYRINTH_WIDTH) for i in range(LABYRINTH_HEIGHT)]
# Aggiungi i bordi del labirinto come possibili archi
for i in range(LABYRINTH_HEIGHT):
for j in range(LABYRINTH_WIDTH):
if i > 0:
edges.append(((i, j), (i - 1, j)))
if j > 0:
edges.append(((i, j), (i, j - 1)))
# Mescola gli archi
random.shuffle(edges)
# Funzione per trovare l'insieme a cui appartiene una cella
def find_set(cell):
for s in sets:
if cell in s:
return s
# Unisci gli insiemi e rimuovi i muri per collegare le celle
for edge in edges:
set1 = find_set(edge[0])
set2 = find_set(edge[1])
if set1 != set2:
labyrinth[edge[0][0]][edge[0][1]] = 0
set1.extend(set2)
sets.remove(set2)
# Esegui l'algoritmo di Kruskal per generare il labirinto
kruskal_algorithm()
# Creare le stelle
stars = [(random.randint(0, LABYRINTH_WIDTH - 1), random.randint(0, LABYRINTH_HEIGHT - 1)) for _ in range(10)]
# Creare i mostri come liste anziché tuple
monsters = [[random.randint(0, LABYRINTH_WIDTH - 1), random.randint(0, LABYRINTH_HEIGHT - 1)] for _ in range(3)]
# Posizione iniziale del giocatore
player_position = [1, 1]
# Funzione per disegnare il labirinto
def draw_labyrinth():
for i in range(LABYRINTH_HEIGHT):
for j in range(LABYRINTH_WIDTH):
if labyrinth[i][j] == 0:
pygame.draw.rect(screen, (255, 255, 255), (j * 10, i * 10, 10, 10))
else:
pygame.draw.rect(screen, (0, 0, 0), (j * 10, i * 10, 10, 10))
# Funzione per disegnare le stelle
def draw_stars():
for star in stars:
pygame.draw.rect(screen, STAR_COLOR, (star[0] * 10, star[1] * 10, STAR_SIZE, STAR_SIZE))
# Funzione per disegnare i mostri
def draw_monsters():
for monster in monsters:
pygame.draw.rect(screen, MONSTER_COLOR, (monster[0] * 10, monster[1] * 10, MONSTER_SIZE, MONSTER_SIZE))
# Funzione per disegnare il giocatore
def draw_player(player_position):
pygame.draw.rect(screen, PLAYER_COLOR, (player_position[0] * 10, player_position[1] * 10, PLAYER_SIZE, PLAYER_SIZE))
# Funzione per muovere i mostri verso il giocatore usando BFS
def move_monsters_towards_player(player_position, monsters):
for monster in monsters:
visited = [[False for _ in range(LABYRINTH_WIDTH)] for _ in range(LABYRINTH_HEIGHT)]
queue = deque([(monster[0], monster[1])])
visited[monster[1]][monster[0]] = True
while queue:
current_x, current_y = queue.popleft()
# Se abbiamo raggiunto la posizione del giocatore, calcoliamo la direzione per muoversi
if (current_x, current_y) == (player_position[0], player_position[1]):
direction_x = player_position[0] - monster[0]
direction_y = player_position[1] - monster[1]
# Scegli la direzione che riduce maggiormente la distanza
if abs(direction_x) > abs(direction_y):
monster[0] += 1 if direction_x > 0 else -1
else:
monster[1] += 1 if direction_y > 0 else -1
break
# Aggiungi i vicini non visitati alla coda
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
new_x, new_y = current_x + dx, current_y + dy
if 0 <= new_x < LABYRINTH_WIDTH and 0 <= new_y < LABYRINTH_HEIGHT and not visited[new_y][new_x] and labyrinth[new_y][new_x] != OBSTACLE_SIZE:
queue.append((new_x, new_y))
visited[new_y][new_x] = True
# Funzione per controllare lo scontro tra giocatore e mostro
def check_collision(player_position, monsters):
global player_hp
for i, monster in enumerate(monsters):
if player_position == monster:
while player_hp > 0 and monster_hp[i] > 0:
monster_hp[i] -= player_attack
if monster_hp[i] > 0:
player_hp -= monster_attack[i]
if player_hp <= 0:
print("Hai perso! Game Over.")
return True
else:
print("Hai sconfitto il mostro!")
monsters.remove(monster)
return False
return False
# Clock per controllare la velocità del gioco
clock = pygame.time.Clock()
# Nel loop principale
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Movimento del giocatore con i tasti freccia
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_position[0] > 0 and labyrinth[player_position[1]][player_position[0] - 1] != OBSTACLE_SIZE:
player_position[0] -= 1
if keys[pygame.K_RIGHT] and player_position[0] < LABYRINTH_WIDTH - 1 and labyrinth[player_position[1]][player_position[0] + 1] != OBSTACLE_SIZE:
player_position[0] += 1
if keys[pygame.K_UP] and player_position[1] > 0 and labyrinth[player_position[1] - 1][player_position[0]] != OBSTACLE_SIZE:
player_position[1] -= 1
if keys[pygame.K_DOWN] and player_position[1] < LABYRINTH_HEIGHT - 1 and labyrinth[player_position[1] + 1][player_position[0]] != OBSTACLE_SIZE:
player_position[1] += 1
# Muovi i mostri verso il giocatore
move_monsters_towards_player(player_position, monsters)
# Controlla lo scontro tra giocatore e mostro
if check_collision(player_position, monsters):
print("Hai perso! Game Over.")
# Disegna gli elementi del gioco
screen.fill((0, 0, 0)) # Pulisce la schermata con uno sfondo nero
draw_labyrinth()
draw_stars()
draw_monsters()
draw_player(player_position)
# Aggiorna la schermata
pygame.display.flip()
# Imposta la velocità del gioco
clock.tick(30) # Esempio: 30 frame al secondo