Advertisement
roch2128

paddle

Jul 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. import pygame
  2. import time
  3. pygame.init()
  4. pygame.key.set_repeat(0, 0)
  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.         gameDisplay.fill(white)
  53.         pygame.draw.rect(gameDisplay, black, paddle)
  54.         pygame.draw.rect(gameDisplay, black, block)
  55.         pygame.draw.rect(gameDisplay, black, block2)
  56.         pygame.display.flip()
  57.  
  58.  
  59. #---------- mouvement -----------------
  60. def move():
  61.     if event.type == pygame.KEYDOWN:
  62.         if event.key == pygame.K_UP:
  63.             paddle.y -= 10
  64.         if event.key == pygame.K_DOWN:
  65.             paddle.y += 10
  66.  
  67.  
  68. # --- MainLoop ---
  69. launched = False
  70. while not launched:
  71.     for event in pygame.event.get():
  72.         if event.type == pygame.QUIT:
  73.             launched = True
  74.  
  75.         if event.type == pygame.KEYDOWN:
  76.             if event.key == pygame.K_UP:
  77.                 paddle.y -= 10
  78.             if event.key == pygame.K_DOWN:
  79.                 paddle.y += 10
  80.  
  81.     collide()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement