Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- #Iniciar o pygame
- pygame.init()
- #Configuração da tela
- WIDTH, HEIGHT = 800, 600
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Pong Game")
- #Definir cores
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- RED = (255, 0, 0)
- #Defone as configurações da raquete(Player)
- PADDLE_WIDTH, PADDLE_HEIGHT = 100, 10
- player_x = (WIDTH - PADDLE_WIDTH) // 2
- player_y = HEIGHT - 50
- player_speed = 0
- # Configurações da bola
- BALL_SIZE = 15
- ball_x = WIDTH // 2
- ball_y = HEIGHT // 2
- ball_speed_x = 4
- ball_speed_y = -4
- #Configuração da pontuação
- score = 0
- best_score = 0
- fonte = pygame.font.Font(None, 74)#Define a fonte do pygame.
- #Define FPS
- clock = pygame.time.Clock()
- FPS = 60
- #loop do jogo
- run = True
- while run:
- #Prenche a cor da tela para peeto
- screen.fill(BLACK)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run= False
- #detecta o towue na tela para movimentar a raquete
- elif event.type == pygame.FINGERDOWN or event.type == pygame.FINGERMOTION:
- player_x = event.x * WIDTH - (PADDLE_WIDTH // 2)
- #Limita o jogador para ficar dentro da tela
- player_x = max(0, min(WIDTH - PADDLE_WIDTH, player_x))
- # Movimento da bola
- ball_x += ball_speed_x
- ball_y += ball_speed_y
- # Colisão com as laterais
- if ball_x <= 0 or ball_x >= WIDTH - BALL_SIZE:
- ball_speed_x *= -1
- # Colisão com o topo
- if ball_y <= 0:
- ball_speed_y *= -1
- # Colisão com a raquete
- if (player_y < ball_y + BALL_SIZE < player_y + PADDLE_HEIGHT and
- player_x < ball_x < player_x + PADDLE_WIDTH):
- ball_speed_y *= -1 # Inverte a direção vertical
- #Adiviona 1 a pontuação
- score += 1
- # Aumenta a velocidade da bola em 10%
- ball_speed_x *= 1.1
- ball_speed_y *= 1.1
- #Colisão de derrota
- if ball_y > HEIGHT:
- #Verifica se a pontuação atual é msior aue a melhor.
- if score > best_score:
- best_score = score
- ball_x, ball_y = WIDTH // 2, HEIGHT // 2
- ball_speed_y = -4 # Reinicia a direção da bola
- ball_speed_x = 4
- ball_speed_y = -4
- score = 0 #Retorna a pontuação para 0
- #Desenha o player(raquete) na tela
- pygame.draw.rect(screen, WHITE, (player_x, player_y, PADDLE_WIDTH, PADDLE_HEIGHT))
- #Desemha a bola
- pygame.draw.ellipse(screen, WHITE, (ball_x, ball_y, BALL_SIZE, BALL_SIZE))
- #Desenha a pontuação
- score_text = fonte.render(f"pontos: {score}", True, WHITE)
- screen.blit(score_text, (WIDTH // 2 - (-70), 10))
- best_score_text = fonte.render(f"best: {best_score}", True, RED)
- screen.blit(best_score_text, (WIDTH // 2 - 300, 10))
- #Atualiza a tela
- pygame.display.flip()
- clock.tick(FPS)
- #Fecha o pygame
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement