Advertisement
Ka1noob

Untitled

Mar 16th, 2025 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | Gaming | 0 0
  1. import pygame
  2. #Iniciar o pygame
  3. pygame.init()
  4. #Configuração da tela
  5. WIDTH, HEIGHT = 800, 600
  6. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  7. pygame.display.set_caption("Pong Game")
  8. #Definir cores
  9. WHITE = (255, 255, 255)
  10. BLACK = (0, 0, 0)
  11. RED = (255, 0, 0)
  12. #Defone as configurações da raquete(Player)
  13. PADDLE_WIDTH, PADDLE_HEIGHT = 100, 10
  14. player_x = (WIDTH - PADDLE_WIDTH) // 2
  15. player_y = HEIGHT - 50
  16. player_speed = 0
  17. # Configurações da bola
  18. BALL_SIZE = 15
  19. ball_x = WIDTH // 2
  20. ball_y = HEIGHT // 2
  21. ball_speed_x = 4
  22. ball_speed_y = -4
  23. #Configuração da pontuação
  24. score = 0
  25. best_score = 0
  26. fonte = pygame.font.Font(None, 74)#Define a fonte do pygame.
  27. #Define FPS
  28. clock = pygame.time.Clock()
  29. FPS = 60
  30. #loop do jogo
  31. run = True
  32. while run:
  33.     #Prenche a cor da tela para peeto
  34.     screen.fill(BLACK)
  35.     for event in pygame.event.get():
  36.         if event.type == pygame.QUIT:
  37.             run= False
  38.            
  39.         #detecta o towue na tela para movimentar a raquete
  40.         elif event.type == pygame.FINGERDOWN or event.type == pygame.FINGERMOTION:
  41.             player_x = event.x * WIDTH - (PADDLE_WIDTH // 2)
  42.            
  43.     #Limita o jogador para ficar dentro da tela
  44.     player_x = max(0, min(WIDTH - PADDLE_WIDTH, player_x))
  45.    
  46.     # Movimento da bola
  47.     ball_x += ball_speed_x
  48.     ball_y += ball_speed_y
  49.     # Colisão com as laterais
  50.     if ball_x <= 0 or ball_x >= WIDTH - BALL_SIZE:
  51.         ball_speed_x *= -1
  52.     # Colisão com o topo
  53.     if ball_y <= 0:
  54.         ball_speed_y *= -1
  55.     # Colisão com a raquete
  56.     if (player_y < ball_y + BALL_SIZE < player_y + PADDLE_HEIGHT and
  57.         player_x < ball_x < player_x + PADDLE_WIDTH):
  58.         ball_speed_y *= -1  # Inverte a direção vertical
  59.        
  60.         #Adiviona 1 a pontuação
  61.         score += 1
  62.    
  63.         # Aumenta a velocidade da bola em 10%
  64.         ball_speed_x *= 1.1  
  65.         ball_speed_y *= 1.1
  66.     #Colisão de derrota
  67.     if ball_y > HEIGHT:
  68.        
  69.         #Verifica se a pontuação atual é msior aue a melhor.
  70.         if score > best_score:
  71.             best_score = score
  72.        
  73.         ball_x, ball_y = WIDTH // 2, HEIGHT // 2
  74.         ball_speed_y = -4  # Reinicia a direção da bola  
  75.         ball_speed_x = 4
  76.         ball_speed_y = -4
  77.         score = 0 #Retorna a pontuação para 0  
  78.    
  79.     #Desenha o player(raquete) na tela
  80.     pygame.draw.rect(screen, WHITE, (player_x, player_y, PADDLE_WIDTH, PADDLE_HEIGHT))    
  81.    
  82.     #Desemha a bola
  83.     pygame.draw.ellipse(screen, WHITE, (ball_x, ball_y, BALL_SIZE, BALL_SIZE))
  84.    
  85.    #Desenha a pontuação
  86.     score_text = fonte.render(f"pontos: {score}", True, WHITE)
  87.     screen.blit(score_text, (WIDTH // 2 - (-70), 10))
  88.    
  89.     best_score_text = fonte.render(f"best: {best_score}", True, RED)
  90.     screen.blit(best_score_text, (WIDTH // 2 - 300, 10))
  91.    
  92.            
  93.     #Atualiza a tela
  94.     pygame.display.flip()
  95.     clock.tick(FPS)
  96.            
  97. #Fecha o pygame
  98. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement