Codice: Seleziona tutto
import pygame
import sys
import random
# Costanti
DIM_CELLA = 32
GRIGLIA_ORIGINALE = [
"000000000000000",
"011111111111110",
"010010000010010",
"010010000010010",
"011111111111110",
"010000010000010",
"010000010000010",
"011111111111110",
"010010000010010",
"010010000010010",
"011111111111110",
"000000000000000"
]
LARGHEZZA = len(GRIGLIA_ORIGINALE[0]) * DIM_CELLA
ALTEZZA = len(GRIGLIA_ORIGINALE) * DIM_CELLA
FPS = 60
PACMAN_VELOCITA = 4
FANTASMA_VELOCITA = 2
DURATA_TIMER = 60
NERO = pygame.Color("black")
GRIGIO = pygame.Color("lightgrey")
GIALLO = pygame.Color("gold")
BIANCO = pygame.Color("ghostwhite")
ROSSO = pygame.Color("red")
VERDE = pygame.Color("limegreen")
BLU = pygame.Color("royalblue")
pygame.init()
schermo = pygame.display.set_mode((LARGHEZZA, ALTEZZA))
pygame.display.set_caption("Pack-Gnam")
clock = pygame.time.Clock()
direzione = (0, 0)
prossima_direzione = (0, 0)
GRIGLIA = []
pos_x = 0
pos_y = 0
punta_punteggio = 0
game_over = False
motivo_gameover = ""
tempo_rimanente = DURATA_TIMER
start_ticks = 0
fantasmi = []
def reset_livello():
global GRIGLIA, pos_x, pos_y, direzione, prossima_direzione, punta_punteggio, game_over, motivo_gameover
global tempo_rimanente, start_ticks, fantasmi
GRIGLIA = [riga[:] for riga in GRIGLIA_ORIGINALE]
pos_x = 1 * DIM_CELLA
pos_y = 1 * DIM_CELLA
direzione = (0, 0)
prossima_direzione = (0, 0)
punta_punteggio = 0
game_over = False
motivo_gameover = ""
tempo_rimanente = DURATA_TIMER
start_ticks = pygame.time.get_ticks()
fantasmi = [
{"x": 6 * DIM_CELLA, "y": 4 * DIM_CELLA, "dir": random.choice([(1,0), (-1,0), (0,1), (0,-1)]), "colore": VERDE},
{"x": 9 * DIM_CELLA, "y": 7 * DIM_CELLA, "dir": random.choice([(1,0), (-1,0), (0,1), (0,-1)]), "colore": BLU}
]
def disegna_mappa():
for y, riga in enumerate(GRIGLIA):
for x, cella in enumerate(riga):
px = x * DIM_CELLA
py = y * DIM_CELLA
if cella == '0':
pygame.draw.rect(schermo, GRIGIO, (px, py, DIM_CELLA, DIM_CELLA))
else:
pygame.draw.rect(schermo, NERO, (px, py, DIM_CELLA, DIM_CELLA))
if cella == '1':
pygame.draw.circle(schermo, BIANCO, (px + DIM_CELLA//2, py + DIM_CELLA//2), 4)
def is_muro(px, py):
grid_x = px // DIM_CELLA
grid_y = py // DIM_CELLA
if 0 <= grid_y < len(GRIGLIA) and 0 <= grid_x < len(GRIGLIA[0]):
return GRIGLIA[grid_y][grid_x] == '0'
return True
def puo_muoversi(dx, dy):
nuovo_x = pos_x + dx * PACMAN_VELOCITA
nuovo_y = pos_y + dy * PACMAN_VELOCITA
punti_controllo = [
(nuovo_x, nuovo_y),
(nuovo_x + DIM_CELLA - 1, nuovo_y),
(nuovo_x, nuovo_y + DIM_CELLA - 1),
(nuovo_x + DIM_CELLA - 1, nuovo_y + DIM_CELLA - 1)
]
return all(not is_muro(x, y) for x, y in punti_controllo)
def aggiorna_pacman():
global pos_x, pos_y, direzione, punta_punteggio
if pos_x % DIM_CELLA == 0 and pos_y % DIM_CELLA == 0:
if puo_muoversi(*prossima_direzione):
direzione = prossima_direzione
nuovo_x = pos_x + direzione[0] * PACMAN_VELOCITA
nuovo_y = pos_y + direzione[1] * PACMAN_VELOCITA
punti_controllo = [
(nuovo_x, nuovo_y),
(nuovo_x + DIM_CELLA - 1, nuovo_y),
(nuovo_x, nuovo_y + DIM_CELLA - 1),
(nuovo_x + DIM_CELLA - 1, nuovo_y + DIM_CELLA - 1)
]
if all(not is_muro(x, y) for x, y in punti_controllo):
pos_x = nuovo_x
pos_y = nuovo_y
if pos_x % DIM_CELLA == 0 and pos_y % DIM_CELLA == 0:
cx = pos_x // DIM_CELLA
cy = pos_y // DIM_CELLA
if GRIGLIA[cy][cx] == '1':
GRIGLIA[cy] = GRIGLIA[cy][:cx] + '2' + GRIGLIA[cy][cx+1:]
punta_punteggio += 1
def aggiorna_fantasmi():
for fantasma in fantasmi:
dx, dy = fantasma["dir"]
nuovo_x = fantasma["x"] + dx * FANTASMA_VELOCITA
nuovo_y = fantasma["y"] + dy * FANTASMA_VELOCITA
punti_controllo = [
(nuovo_x, nuovo_y),
(nuovo_x + DIM_CELLA - 1, nuovo_y),
(nuovo_x, nuovo_y + DIM_CELLA - 1),
(nuovo_x + DIM_CELLA - 1, nuovo_y + DIM_CELLA - 1)
]
if all(not is_muro(x, y) for x, y in punti_controllo):
fantasma["x"] = nuovo_x
fantasma["y"] = nuovo_y
else:
fantasma["dir"] = random.choice([(1,0), (-1,0), (0,1), (0,-1)])
def controlla_collisione_con_fantasmi():
pacman_rect = pygame.Rect(pos_x, pos_y, DIM_CELLA, DIM_CELLA)
for fantasma in fantasmi:
fantasma_rect = pygame.Rect(fantasma["x"], fantasma["y"], DIM_CELLA, DIM_CELLA)
if pacman_rect.colliderect(fantasma_rect):
return True
return False
def disegna_pacman():
pygame.draw.circle(schermo, GIALLO, (pos_x + DIM_CELLA//2, pos_y + DIM_CELLA//2), DIM_CELLA // 2 - 2)
def disegna_fantasmi():
for fantasma in fantasmi:
pygame.draw.circle(schermo, fantasma["colore"], (fantasma["x"] + DIM_CELLA//2, fantasma["y"] + DIM_CELLA//2), DIM_CELLA // 2 - 2)
def mostra_punteggio():
font = pygame.font.SysFont(None, 24)
testo = font.render(f"Punteggio: {punta_punteggio}", True, NERO)
schermo.blit(testo, (10, 10))
def mostra_timer():
font = pygame.font.SysFont(None, 24)
testo = font.render(f"Tempo: {tempo_rimanente}s", True, NERO)
schermo.blit(testo, (LARGHEZZA - 130, 10))
def controllo_game_over():
for riga in GRIGLIA:
if '1' in riga:
return False
return True
def mostra_game_over():
font = pygame.font.SysFont(None, 48)
if motivo_gameover == "vittoria":
testo = font.render("Hai vinto!", True, ROSSO)
else:
testo = font.render("Game Over!", True, ROSSO)
rect = testo.get_rect(center=(LARGHEZZA // 2, ALTEZZA // 2))
schermo.blit(testo, rect)
reset_livello()
esegui = True
while esegui:
for evento in pygame.event.get():
if evento.type == pygame.QUIT:
esegui = False
elif evento.type == pygame.KEYDOWN:
if evento.key == pygame.K_r:
reset_livello()
elif not game_over:
if evento.key == pygame.K_UP:
prossima_direzione = (0, -1)
elif evento.key == pygame.K_DOWN:
prossima_direzione = (0, 1)
elif evento.key == pygame.K_LEFT:
prossima_direzione = (-1, 0)
elif evento.key == pygame.K_RIGHT:
prossima_direzione = (1, 0)
if not game_over:
aggiorna_pacman()
aggiorna_fantasmi()
if controlla_collisione_con_fantasmi():
game_over = True
motivo_gameover = "sconfitta"
if controllo_game_over():
game_over = True
motivo_gameover = "vittoria"
secondi_passati = (pygame.time.get_ticks() - start_ticks) // 1000
tempo_rimanente = max(0, DURATA_TIMER - secondi_passati)
if tempo_rimanente == 0 and not game_over:
game_over = True
motivo_gameover = "sconfitta"
schermo.fill(NERO)
disegna_mappa()
disegna_pacman()
disegna_fantasmi()
mostra_punteggio()
mostra_timer()
if game_over:
mostra_game_over()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()