SHARE
TWEET

pong

roch2128 Jul 17th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import time
  3. pygame.init()
  4.  
  5. #------------------ resolution ------------------------
  6. display_width = 640
  7. display_height = 480
  8.  
  9. # ------------------- Colors ------------------------
  10.  
  11. white = (255, 255, 255)
  12. black = (0, 0, 0)
  13.  
  14. # ------------------------ gamedisplay -------------------
  15.  
  16. gameDisplay = pygame.display.set_mode((display_width, display_height))
  17. pygame.display.set_caption("Pong")
  18.  
  19. clock = pygame.time.Clock()
  20. # ----------------- Rect variable -----------------
  21. lead_x = 10
  22. lead_y = display_height / 2
  23.  
  24. size_x = 20
  25. size_y = 89
  26.  
  27. lead2_x = 1
  28. lead2_y = 1
  29.  
  30. size2_x = display_width
  31. size2_y = 0
  32.  
  33. block_x = 0
  34. block_y = 478
  35.  
  36. size3_y = 0
  37.  
  38. lead_x_change = 0
  39. lead_y_change = 0
  40.  
  41. # ------------------------ declare Rect -----------------
  42.  
  43. paddle = pygame.Rect(lead_x, lead_y, size_x, size_y)
  44. block = pygame.Rect(lead2_x, lead2_y, size2_x, size2_y)
  45. block2 = pygame.Rect(block_x, block_y, size2_x, size3_y)
  46.  
  47. #--------- detect collision ------------
  48.  
  49.  
  50. def collide():
  51.     while not paddle.colliderect(block) and not paddle.colliderect(block2):
  52.         move()
  53.         gameDisplay.fill(white)
  54.         pygame.draw.rect(gameDisplay, black, paddle)
  55.         pygame.draw.rect(gameDisplay, black, block)
  56.         pygame.draw.rect(gameDisplay, black, block2)
  57.         pygame.display.flip()
  58.  
  59.  
  60. #---------- mouvement -----------------
  61. def move():
  62.     for events in pygame.event.get():
  63.         if events.type == pygame.KEYDOWN:
  64.             if events.key == pygame.K_UP:
  65.                 paddle.y -= 10
  66.             if events.key == pygame.K_DOWN:
  67.                 paddle.y += 10
  68.  
  69.  
  70. # --- MainLoop ---
  71. launched = False
  72. while not launched:
  73.     for event in pygame.event.get():
  74.         if event.type == pygame.QUIT:
  75.             launched = True
  76.     collide()
  77.     move()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top