Files
giochino/pong.py
T

167 lines
5.1 KiB
Python

import pygame
import sys
import random
# Inizializzazione
pygame.init()
W, H = 900, 500
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("Windjammers Arcade")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)
big_font = pygame.font.Font(None, 72)
# Colori
SKY = (135, 206, 235)
GRASS = (125, 206, 160)
WHITE = (255, 255, 255)
RED = (231, 76, 60)
BLUE = (52, 152, 219)
ORANGE = (243, 156, 18)
BLACK = (0, 0, 0)
# Giocatori
p1 = pygame.Rect(100, 200, 30, 80)
p2 = pygame.Rect(770, 200, 30, 80)
p1_score = 0
p2_score = 0
# Disco
disc = pygame.Rect(440, 240, 20, 20)
disc_vx = 0
disc_vy = 0
disc_owner = None # 'p1', 'p2', or None
disc_in_play = False
# Rete
net = pygame.Rect(445, 80, 10, 340)
def reset_disc():
global disc_owner, disc_in_play, disc_vx, disc_vy
disc.center = (450, 250)
disc_vx = 0
disc_vy = 0
disc_owner = 'p1' if random.random() < 0.5 else 'p2'
disc_in_play = False
reset_disc()
def throw_disc(player):
global disc_owner, disc_in_play, disc_vx, disc_vy
disc_owner = None
disc_in_play = True
target_x = 850 if player == 'p1' else 50
target_y = 100 + random.randint(0, 300)
dx = target_x - disc.centerx
dy = target_y - disc.centery
dist = max(1, (dx**2 + dy**2)**0.5)
disc_vx = dx / dist * 8
disc_vy = dy / dist * 8
# Game loop
running = True
mode = 'menu'
while running:
# Eventi
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and mode == 'menu':
mode = 'playing'
reset_disc()
if event.key == pygame.K_ESCAPE and mode == 'playing':
mode = 'menu'
if mode == 'menu':
# Menu
screen.fill((26, 26, 46))
title = big_font.render("WINDJAMMERS", True, RED)
subtitle = font.render("FLYING DISC ARCADE", True, ORANGE)
hint = font.render("Premi SPAZIO per iniziare", True, WHITE)
screen.blit(title, (W//2 - title.get_width()//2, 120))
screen.blit(subtitle, (W//2 - subtitle.get_width()//2, 200))
screen.blit(hint, (W//2 - hint.get_width()//2, 320))
elif mode == 'playing':
# Input P1
keys = pygame.key.get_pressed()
p1_speed = 5
if keys[pygame.K_w] and p1.top > 80: p1.y -= p1_speed
if keys[pygame.K_s] and p1.bottom < 420: p1.y += p1_speed
if keys[pygame.K_a] and p1.left > 50: p1.x -= p1_speed
if keys[pygame.K_d] and p1.right < 430: p1.x += p1_speed
if keys[pygame.K_f] and disc_owner == 'p1':
throw_disc('p1')
# AI P2
if disc_owner == 'p2' and random.random() < 0.02:
throw_disc('p2')
elif disc_in_play and disc_owner is None:
# Move toward disc
if disc.centerx < p2.centerx: p2.x -= 3
if disc.centerx > p2.centerx: p2.x += 3
if disc.centery < p2.centery: p2.y -= 3
if disc.centery > p2.centery: p2.y += 3
# Keep P2 in bounds
p2.clamp_ip(pygame.Rect(470, 80, 380, 340))
p1.clamp_ip(pygame.Rect(50, 80, 380, 340))
# Disco fisso al possessore
if disc_owner == 'p1':
disc.centerx = p1.right + 10
disc.centery = p1.centery
elif disc_owner == 'p2':
disc.centerx = p2.left - 10
disc.centery = p2.centery
elif disc_in_play:
disc.x += disc_vx
disc.y += disc_vy
# Rimbalzi
if disc.top < 80 or disc.bottom > 420: disc_vy *= -0.8
if disc.left < 50 or disc.right > 850: disc_vx *= -0.8
if net.colliderect(disc): disc_vx *= -0.7
# Cattura
if p1.colliderect(disc) and disc_owner is None:
disc_owner = 'p1'
disc_in_play = False
elif p2.colliderect(disc) and disc_owner is None:
disc_owner = 'p2'
disc_in_play = False
# Gol
if disc.left < 50:
p2_score += 3
reset_disc()
elif disc.right > 850:
p1_score += 3
reset_disc()
# Disegno
screen.fill(SKY)
# Campo
pygame.draw.rect(screen, GRASS, (50, 80, 800, 340))
pygame.draw.rect(screen, WHITE, (50, 80, 800, 340), 3)
# Rete
pygame.draw.rect(screen, WHITE, net)
# Porte
pygame.draw.rect(screen, RED, (20, 180, 30, 140))
pygame.draw.rect(screen, BLUE, (850, 180, 30, 140))
# Giocatori
pygame.draw.rect(screen, RED, p1)
pygame.draw.rect(screen, BLUE, p2)
pygame.draw.ellipse(screen, ORANGE, disc)
# Punteggi
s1 = font.render(f"P1: {p1_score}", True, WHITE)
s2 = font.render(f"CPU: {p2_score}", True, WHITE)
screen.blit(s1, (200, 20))
screen.blit(s2, (650, 20))
# Istruzioni
hint = font.render("WASD=muovi F=tiro ESC=menu", True, (150,150,150))
screen.blit(hint, (W//2 - hint.get_width()//2, 460))
pygame.display.flip()
clock.tick(60)
pygame.quit()