Arcot

pong simplified

Mar 2nd, 2022 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 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. BLACK = (0, 0, 0)
  7.  
  8.  
  9. pygame.init() # initializing the module
  10. window = pygame.display.set_mode((WIDTH, HEIGHT))  # making the window of size WIDTH and HEIGHT
  11. pygame.display.set_caption('Pong Game') # setting the title of the window
  12. clock = pygame.time.Clock() # creating a clock to control frames per second
  13. running = True  # status variable which tells if the game is running or not
  14. x = 100
  15. y = 100
  16. speed = 7
  17. speedx = speed
  18. speedy = speed
  19. yleft = 250
  20. yright = 250
  21.  
  22. #initialize player scores
  23. score_left = 0
  24. score_right = 0
  25.  
  26. while running:           #-> while the game is running >>> 1 frame every time the loop runs
  27.     window.fill((43, 79, 52)) # r g b color inside
  28.     clock.tick(60) # controlling the frame rate -> it will run 60 times in every second
  29.    
  30.     for event in pygame.event.get():  # for each event occuring in the window
  31.         if event.type == pygame.QUIT:  # checking if the close button was pressed
  32.             running = False     # update game status to stop (not running)
  33.            
  34.     x += speedx
  35.     y += speedy
  36.     ball = pygame.draw.circle(window, BALL_COLOR, (x, y), 25)
  37.    
  38.     keys = pygame.key.get_pressed()   # this will get all the buttons that are pressed right now
  39.    
  40.     left = pygame.draw.rect(window, LEFT_COLOR, (30, yleft, 20, 100)) # xposition, yposition, xwidth, ywidth
  41.     right = pygame.draw.rect(window, LEFT_COLOR, (WIDTH-30, yright, 20, 100)) # xposition, yposition, xwidth, ywidth
  42.  
  43.     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)
  44.         yleft -= speed              # yleft = yleft - speed
  45.     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)
  46.         yright -= speed              # yright = yright - speed
  47.    
  48.     if keys[pygame.K_s] and left.bottom < HEIGHT:
  49.         yleft += speed
  50.     if keys[pygame.K_DOWN] and right.bottom < HEIGHT:
  51.         yright += speed
  52.    
  53.     if left.colliderect(ball): # if left rect is colliding with ball rect
  54.         speedx = speed
  55.     if right.colliderect(ball): # if right rect is colliding with ball rect
  56.         speedx = -speed
  57.        
  58.        
  59.     #for ball bouncing over four walls
  60.     if ball.right >= WIDTH:
  61.         speedx = -speed
  62.         score_left+=1
  63.        
  64.     if ball.left <= 0:
  65.         speedx = speed
  66.         score_right+=1
  67.  
  68.     if ball.bottom >= HEIGHT:
  69.         speedy = -speed
  70.        
  71.     if ball.top <= 0:
  72.         speedy = speed
  73.        
  74.     #display scores
  75.     font = pygame.font.Font(None, 50)
  76.     text = font.render(str(score_left), 1, BLACK)
  77.     window.blit(text, (250,10))
  78.     text = font.render(str(score_right), 1, BLACK)
  79.     window.blit(text, (520,10))
  80.  
  81.  
  82.     pygame.display.update()  # update our window with all the changes that we have done
  83. pygame.quit() # closes everything (closes the window)
Add Comment
Please, Sign In to add comment