Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

import pygame # Importa la biblioteca Pygame

import sys # Importa el mòdul sys


import random # Importa el mòdul random

pygame.init() # Inicia Pygame

# Configuració de la finestra del joc


width = 800#amplada
height = 600#alçada
screen = pygame.display.set_mode((width, height)) # Estableix la mida de
la finestra
pygame.display.set_caption("Ping Pong") # Estableix el títol de la
finestra
# Definició de colors
black = (52, 104, 153)
white = (255, 255, 255)
gray = (0, 0, 0)
border_color = (139, 234, 218)

# Dimensions de les paletes i la pilota


paddle_width = 16
paddle_height = 102
ball_radius = 10

# Posicions inicials de les paletes i la pilota


player1_y = height // 2 - paddle_height // 2
player2_y = height // 2 - paddle_height // 2
ball_x = width // 2
ball_y = height // 2

# Velocitats inicials de la pilota


ball_speed_x = random.choice([1, -1]) * 5
ball_speed_y = random.uniform(-1, 1) * 5

# Velocitat de les paletes


paddle_speed = 10

# Puntuacions inicials dels jugadors


score_player1 = 0
score_player2 = 0
# Font per mostrar text a la pantalla
font = pygame.font.SysFont(None, 55)

# Rellotge per controlar la velocitat


clock = pygame.time.Clock()

import pygame # Importa la biblioteca Pygame


import sys # Importa el mòdul sys
import random # Importa el mòdul random p

pygame.init() # Inicialitza Pygame

# Configuració de la finestra del joc


width = 800#amplada
height = 600#alçada
screen = pygame.display.set_mode((width, height)) # Estableix la mida de
la finestra
pygame.display.set_caption("Ping Pong") # Estableix el títol de la
finestra

# Definició de colors
black = (52, 104, 153)
white = (255, 255, 255)
gray = (0, 0, 0)
border_color = (139, 234, 218)

# Dimensions de les paletes i la pilota


paddle_width = 16
paddle_height = 102
ball_radius = 10

# Posicions inicials de les paletes i la pilota


player1_y = height // 2 - paddle_height // 2
player2_y = height // 2 - paddle_height // 2
ball_x = width // 2
ball_y = height // 2

# Velocitats inicials de la pilota


ball_speed_x = random.choice([1, -1]) * 5
ball_speed_y = random.uniform(-1, 1) * 5

# Velocitat de les paletes


paddle_speed = 10

# Puntuacions inicials dels jugadors


score_player1 = 0
score_player2 = 0

# Font per mostrar text a la pantalla


font = pygame.font.SysFont(None, 55)

# Rellotge per controlar la velocitat


clock = pygame.time.Clock()

# Temporitzadors per al reinici i el compte enrere


reset_timer = 0
reset_time_limit = 3000
ball_in_center = True
countdown_timer = 0 # al nombre que acaba de comptar
countdown_duration = 3000#la duració del contador

# Bucle principal del joc


while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

keys = pygame.key.get_pressed()
# Maneig d'esdeveniments de teclat per moure les paletes
if keys[pygame.K_w] and player1_y > 0:
player1_y -= paddle_speed
if keys[pygame.K_s] and player1_y < height - paddle_height:
player1_y += paddle_speed
if keys[pygame.K_UP] and player2_y > 0:
player2_y -= paddle_speed
if keys[pygame.K_DOWN] and player2_y < height - paddle_height:
player2_y += paddle_speed
if ball_in_center:
countdown_timer = pygame.time.get_ticks() - reset_timer
if countdown_timer >= countdown_duration:
ball_x += ball_speed_x
ball_y += ball_speed_y

# Comprovació de col·lisions de la pilota amb les paletes


if (
(ball_x - ball_radius < paddle_width and player1_y < ball_y <
player1_y + paddle_height) or
(ball_x + ball_radius > width - paddle_width and player2_y < ball_y
< player2_y + paddle_height)
):
ball_speed_x = -ball_speed_x

# Comprovació de col·lisions de la pilota amb les vores superior i


inferior
if ball_y - ball_radius <= 0 or ball_y + ball_radius >= height:
ball_speed_y = -ball_speed_y

# Maneig de puntuacions i reinici de la pilota quan surt dels límits


laterals
if ball_x - ball_radius < 0:
score_player2 += 1
ball_x = width // 2
ball_y = height // 2
ball_speed_x = random.choice([1, -1]) * 5
ball_speed_y = random.uniform(-1, 1) * 5
ball_in_center = True
reset_timer = pygame.time.get_ticks()

if ball_x + ball_radius > width:


score_player1 += 1
ball_x = width // 2
ball_y = height // 2
ball_speed_x = random.choice([1, -1]) * 5
ball_speed_y = random.uniform(-1, 1) * 5
ball_in_center = True
reset_timer = pygame.time.get_ticks()
# Dibuixa els elements del joc a la pantalla
screen.fill(black)
pygame.draw.rect(screen, border_color, (0, 0, width, height), 5)
pygame.draw.line(screen, white, (width // 2, 0), (width // 2, height),
2)

# Mostra les puntuacions a la pantalla


score_text1 = font.render(str(score_player1), True, white)
score_text2 = font.render(str(score_player2), True, white)
screen.blit(score_text1, (width // 4, 20))
screen.blit(score_text2, (3 * width // 4 - score_text2.get_width(),
20))

# Dibuixa les paletes i la pilota a la pantalla


pygame.draw.rect(screen, white, (paddle_width, player1_y, paddle_width,
paddle_height))
pygame.draw.rect(screen, white, (width - 2 * paddle_width, player2_y,
paddle_width, paddle_height))
pygame.draw.circle(screen, white, (int(ball_x), int(ball_y)),
ball_radius)

# Mostra el compte enrere al centre de la pantalla


if ball_in_center and countdown_timer < countdown_duration:
countdown_text = font.render(str((countdown_duration -
countdown_timer) // 1000 + 1), True, gray)
screen.blit(countdown_text, (width // 2 -
countdown_text.get_width() // 2, height // 2 - countdown_text.get_height()
// 2))

pygame.display.update() # Actualiza la pantalla


clock.tick(60) # Limita la velocitat

You might also like