65 lines
2.2 KiB
Python
65 lines
2.2 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
|
||
|
PLAYER_START_X_OFFSET = 50 # The amount of pixels the player is away from the wall on the X axis
|
||
|
PLAYER_START_Y_OFFSET = 200 # The amount of pixels the player is away from the wall on the Y axis
|
||
|
|
||
|
class Player:
|
||
|
def __init__(self, x, y):
|
||
|
self.x = x
|
||
|
self.y = y
|
||
|
|
||
|
# 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")
|
||
|
|
||
|
self.player1 = Player(PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
|
||
|
self.player2 = Player(SCREEN_SIZE[0] - PLAYER_START_X_OFFSET, PLAYER_START_Y_OFFSET)
|
||
|
|
||
|
# 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((self.player1.x, self.player1.y))
|
||
|
self.draw_paddle((self.player2.x, self.player2.y))
|
||
|
|
||
|
# 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()
|