Advertisement
Arcot

Pong game with scores

Mar 6th, 2023
1,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. import pygame
  2.  
  3. WIDTH = 800
  4. HEIGHT = 600
  5. BALL_COLOR = (0, 255, 0)
  6. LEFT_COLOR = (255, 0, 0)
  7. BLACK = (0, 0, 0)
  8.  
  9. pygame.init()
  10. window = pygame.display.set_mode((WIDTH, HEIGHT))
  11. pygame.display.set_caption('Ping Pong')
  12. clock = pygame.time.Clock()
  13. running = True
  14. x = 0
  15. y = 0
  16. speed = 7
  17. speedx = speed
  18. speedy = speed
  19. yleft = 250
  20. yright = 250
  21.  
  22. score_left = 0
  23. score_right = -3
  24.  
  25. while running:
  26.     window.fill((43, 79, 52))
  27.     clock.tick(60)
  28.  
  29.     for event in pygame.event.get():
  30.         if event.type == pygame.QUIT:
  31.             running = False
  32.    
  33.     x += speedx
  34.     y += speedy
  35.     ball = pygame.draw.circle(window, BALL_COLOR, (x, y), 25)
  36.  
  37.     keys = pygame.key.get_pressed()
  38.    
  39.     left = pygame.draw.rect(window, LEFT_COLOR, (30, yleft, 20, 100))
  40.     right = pygame.draw.rect(window, LEFT_COLOR, (WIDTH-30, yright, 20, 100))
  41.  
  42.     if keys[pygame.K_w] and left.top > 0:
  43.         yleft -= speed
  44.     if keys[pygame.K_UP] and right.top > 0:
  45.         yright -= speed
  46.  
  47.     if keys[pygame.K_s] and left.bottom < HEIGHT:
  48.         yleft += speed
  49.     if keys[pygame.K_DOWN] and right.bottom < HEIGHT:
  50.         yright += speed
  51.  
  52.     if left.colliderect(ball):
  53.         speedx = speed
  54.     if right.colliderect(ball):
  55.         speedx = -speed
  56.  
  57.     if ball.right >= WIDTH:
  58.         speedx = -speed
  59.         score_left += 1
  60.  
  61.     if ball.left <= 0:
  62.         speedx = speed
  63.         score_right += 1
  64.  
  65.     if ball.bottom >=HEIGHT:
  66.         speedy = -speed    
  67.    
  68.     if ball.top <= 0:
  69.         speedy = speed
  70.  
  71.     font = pygame.font.Font(None, 50)
  72.     text = font.render(str(score_left), 1, BLACK)
  73.     window.blit(text, (250, 10))
  74.     text = font.render(str(score_right),1, BLACK)
  75.     window.blit(text, (520, 10))
  76.  
  77.     pygame.display.update()
  78. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement