import pygame import pypong SCREEN_SIZE = (800, 600) # Size of the game window GAME_FPS = 60 GAME_COLOR = (255, 255, 255) # Color of all the sprites on screen PADDLE_SIZE = (20, 200) PADDLE_SPEED = 10 class Paddle(pygame.sprite.Sprite): def __init__(self): super().__init__() self.moving_down = False self.moving_up = False self.image = pygame.Surface(PADDLE_SIZE) self.image.fill(GAME_COLOR) self.rect = self.image.get_rect() def update(self): if self.moving_down: self.rect.y += PADDLE_SPEED if self.moving_up: self.rect.y -= PADDLE_SPEED if self.rect.top < 0: self.rect.top = 0 if self.rect.bottom > SCREEN_SIZE[1]: self.rect.bottom = SCREEN_SIZE[1] def game(is_host=False): """ Starts the game. """ pygame.init() window = pygame.display.set_mode(SCREEN_SIZE) surface = pygame.Surface(SCREEN_SIZE) # Set title of window pygame.display.set_caption('PyPong {0}'.format(pypong.__version__)) # Add an icon to the pygame window. # icon = pygame.image.load('NO ICON YET, WIP').convert_alpha() # pygame.display.set_icon(icon) clock = pygame.time.Clock() # Default game state when the game first starts. running = True # A group of all the sprites on screen. Used to update all sprites at once all_sprites = pygame.sprite.Group() # Functions used for drawing the game screen # def draw(): """ Draws the main pygame display. """ # Draws all the sprites on a black background all_sprites.update() surface.fill((0, 0, 0)) all_sprites.draw(surface) window.blit(surface, surface.get_rect()) # Update the entire display pygame.display.flip() def keyboard_movement(paddle): for keyboard_event in pygame.event.get(): if keyboard_event.type == pygame.KEYDOWN: if keyboard_event.key == pygame.K_DOWN: paddle.moving_down = True if keyboard_event.key == pygame.K_UP: paddle.moving_up = True if keyboard_event.type == pygame.KEYUP: if keyboard_event.key == pygame.K_DOWN: paddle.moving_down = False if keyboard_event.key == pygame.K_UP: paddle.moving_up = False # Create the two paddles host_player = Paddle() client_player = Paddle() client_player.rect.right = SCREEN_SIZE[0] # Move client paddle to other side all_sprites.add(host_player, client_player) while running: clock.tick(GAME_FPS) draw() if is_host: keyboard_movement(host_player) else: keyboard_movement(client_player) def main(): """ Calls the game() function to start the game. """ game() pygame.quit()