Advertisement
Arcot

Moving ball pygame

Sep 23rd, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. from tkinter.constants import Y
  2. from PyGame.ponggame4p import Yvelo
  3. import pygame # importing the module
  4. WIDTH = 800
  5. HEIGHT = 600
  6.  
  7. pygame.init() # initializing the module
  8. screen = 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()
  11. run = True
  12. speed = 2
  13. y = 200
  14. dy = speed
  15. while run:           #-> while the game is running >>> 1 frame every time the loop runs
  16.     screen.fill((0,0,0)) # r g b color inside
  17.     clock.tick(60) # controlling the frame rate  
  18.    
  19.     for event in pygame.event.get():  # for each event occuring in the window
  20.         if event.type == pygame.QUIT:  # checking if the close button was pressed
  21.             run = False     # update game status to stop (not running)
  22.            
  23.     y += dy # delta y -> change in y
  24.    
  25.     ball = pygame.draw.circle(screen, (255, 0, 0 ), (100, y), 25)
  26.    
  27.     if ball.bottom >= HEIGHT:
  28.         dy = -speed
  29.     elif ball.top <= 0:
  30.         dy = speed
  31.    
  32.     pygame.display.flip()  # update our window with all the changes that we have done
  33.  
  34. pygame.quit() # close the window
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement