Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame # importing the module
- WIDTH = 800
- HEIGHT = 600
- BALL_COLOR = (0, 255, 0) # Green color (Red, Green, Blue)
- pygame.init() # initializing the module
- window = pygame.display.set_mode((WIDTH, HEIGHT)) # making the window of size WIDTH and HEIGHT
- pygame.display.set_caption('Pong Game') # setting the title of the window
- clock = pygame.time.Clock() # creating a clock to control frames per second
- running = True # status variable which tells if the game is running or not
- x = WIDTH/2
- y = HEIGHT/2
- speed = 5
- xspeed = speed
- yspeed = speed
- while running: #-> while the game is running >>> 1 frame every time the loop runs
- window.fill((43, 79, 52)) # r g b color inside
- clock.tick(60) # controlling the frame rate -> it will run 60 times in every second
- for event in pygame.event.get(): # for each event occuring in the window
- if event.type == pygame.QUIT: # checking if the close button was pressed
- running = False # update game status to stop (not running)
- x += xspeed
- y += yspeed
- ball = pygame.draw.circle(window, BALL_COLOR, (x, y), 25)
- if ball.right >= WIDTH:
- xspeed = -speed
- if ball.left <= 0:
- xspeed = speed
- if ball.bottom >= HEIGHT:
- yspeed = -speed
- if ball.top <= 0:
- yspeed = speed
- pygame.display.update() # update our window with all the changes that we have done
- pygame.quit() # closes everything (closes the window)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement