Advertisement
ZEdKasat

Pong Game

Feb 26th, 2022
257
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. import pygame # importing the module
  2. WIDTH = 800
  3. HEIGHT = 600
  4. BALL_COLOR = (0, 255, 0) # Green color  (Red, Green, Blue)
  5. LEFT_COLOR = (255, 0, 0)
  6.  
  7.  
  8. pygame.init() # initializing the module
  9. window = pygame.display.set_mode((WIDTH, HEIGHT))  # making the window of size WIDTH and HEIGHT
  10. pygame.display.set_caption('Pong Game') # setting the title of the window
  11. clock = pygame.time.Clock() # creating a clock to control frames per second
  12. running = True  # status variable which tells if the game is running or not
  13. x = 100
  14. y = 100
  15. speed = 3
  16. speedx = speed
  17. speedy = speed
  18. yleft = 250
  19. yright = 250
  20. while running:           #-> while the game is running >>> 1 frame every time the loop runs
  21.     window.fill((43, 79, 52)) # r g b color inside
  22.     clock.tick(60) # controlling the frame rate -> it will run 60 times in every second
  23.    
  24.     for event in pygame.event.get():  # for each event occuring in the window
  25.         if event.type == pygame.QUIT:  # checking if the close button was pressed
  26.             running = False     # update game status to stop (not running)
  27.            
  28.     x += speedx
  29.     y += speedy
  30.     ball = pygame.draw.circle(window, BALL_COLOR, (x, y), 25)
  31.    
  32.     keys = pygame.key.get_pressed()   # this will get all the buttons that are pressed right now
  33.    
  34.     left = pygame.draw.rect(window, LEFT_COLOR, (30, yleft, 20, 100)) # xposition, yposition, xwidth, ywidth
  35.     right = pygame.draw.rect(window, LEFT_COLOR, (WIDTH-30, yright, 20, 100)) # xposition, yposition, xwidth, ywidth
  36.  
  37.     if keys[pygame.K_w] and left.top > 0: # if the KEY_"w" is pressed and the top of left player > 0 ( player is not already at the top)
  38.         yleft -= speed              # yleft = yleft - speed
  39.     if keys[pygame.K_UP] and right.top > 0: # if the KEY_UP is pressed and the top of right player > 0 ( player is not already at the top)
  40.         yright -= speed              # yright = yright - speed
  41.    
  42.     if keys[pygame.K_s] and left.bottom < HEIGHT:
  43.         yleft += speed
  44.     if keys[pygame.K_DOWN] and right.bottom < HEIGHT:
  45.         yright += speed
  46.    
  47.     if left.colliderect(ball): # if left rect is colliding with ball rect
  48.         speedx = speed
  49.     if right.colliderect(ball): # if right rect is colliding with ball rect
  50.         speedx = -speed
  51.        
  52.        
  53.        
  54.     if ball.right >= WIDTH:
  55.         speedx = -speed
  56.        
  57.     if ball.left <= 0:
  58.         speedx = speed
  59.  
  60.     if ball.bottom >= HEIGHT:
  61.         speedy = -speed
  62.        
  63.     if ball.top <= 0:
  64.         speedy = speed
  65.        
  66.     pygame.display.update()  # update our window with all the changes that we have done
  67. pygame.quit() # closes everything (closes the window)
  68.  
  69.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement