55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
|
import pygame
|
||
|
import sys
|
||
|
|
||
|
# Some constants that we use in the program
|
||
|
|
||
|
BACKGROUND_COLOR = (0, 0, 0) # Black background
|
||
|
SCREEN_SIZE = (500, 500) # Screen size is 500 x 500
|
||
|
PADDLE_COLOR = (255, 255, 255) # Paddle color is white
|
||
|
PADDLE_SIZE = (10, 100) # Paddle size is 10 x 100
|
||
|
BALL_COLOR = (255, 255, 255) # Ball color is white
|
||
|
BALL_RADIUS = 10 # Ball radius is 10 pixels
|
||
|
|
||
|
# This class will represent our game
|
||
|
class Game:
|
||
|
# This function will be called when the class is first created
|
||
|
def __init__(self):
|
||
|
pygame.init()
|
||
|
self.window = pygame.display.set_mode(SCREEN_SIZE)
|
||
|
pygame.display.set_caption("Hello world")
|
||
|
|
||
|
# Draw one paddle at a given location
|
||
|
def draw_paddle(self, location):
|
||
|
pygame.draw.rect(self.window, PADDLE_COLOR, (location[0], location[1], PADDLE_SIZE[0], PADDLE_SIZE[1]))
|
||
|
|
||
|
# Draw the ball at a given location
|
||
|
def draw_ball(self, location):
|
||
|
pygame.draw.circle(self.window, BALL_COLOR, location, BALL_RADIUS)
|
||
|
|
||
|
# This function will render the contents of the screen
|
||
|
def render(self):
|
||
|
self.window.fill(BACKGROUND_COLOR)
|
||
|
|
||
|
# Draw the two players
|
||
|
self.draw_paddle((50, 200))
|
||
|
self.draw_paddle((450, 200))
|
||
|
|
||
|
# Draw the ball
|
||
|
self.draw_ball((250, 250))
|
||
|
|
||
|
pygame.display.flip()
|
||
|
|
||
|
def start(self):
|
||
|
# The main loop will run until the QUIT event is triggered
|
||
|
# this event will be triggered by closing the window
|
||
|
while True:
|
||
|
for event in pygame.event.get():
|
||
|
if event.type == pygame.QUIT:
|
||
|
pygame.quit()
|
||
|
sys.exit()
|
||
|
|
||
|
self.render()
|
||
|
|
||
|
# Create the game object and start the game
|
||
|
game = Game()
|
||
|
game.start()
|