Advertisement
ZEdKasat

Moving Ball Siddharth

Aug 12th, 2021
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 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.  
  6.  
  7. pygame.init() # initializing the module
  8. window = pygame.display.set_mode((WIDTH, HEIGHT))  # making the window of size WIDTH and HEIGHT
  9. pygame.display.set_caption('Pong Game') # setting the title of the window
  10. clock = pygame.time.Clock() # creating a clock to control frames per second
  11. running = True  # status variable which tells if the game is running or not
  12. x = WIDTH/2
  13. y = HEIGHT/2
  14. speed = 5
  15. xspeed = speed
  16. yspeed = speed
  17. while running:           #-> while the game is running >>> 1 frame every time the loop runs
  18.     window.fill((43, 79, 52)) # r g b color inside
  19.     clock.tick(60) # controlling the frame rate -> it will run 60 times in every second
  20.    
  21.     for event in pygame.event.get():  # for each event occuring in the window
  22.         if event.type == pygame.QUIT:  # checking if the close button was pressed
  23.             running = False     # update game status to stop (not running)
  24.     x += xspeed
  25.     y += yspeed
  26.     ball = pygame.draw.circle(window, BALL_COLOR, (x, y), 25)
  27.    
  28.     if ball.right >= WIDTH:
  29.         xspeed = -speed
  30.     if ball.left <= 0:
  31.         xspeed = speed
  32.        
  33.     if ball.bottom >= HEIGHT:
  34.         yspeed = -speed
  35.     if ball.top <= 0:
  36.         yspeed = speed
  37.        
  38.     pygame.display.update()  # update our window with all the changes that we have done
  39. pygame.quit() # closes everything (closes the window)
  40.  
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement