Compare commits

..

6 Commits

11 changed files with 184 additions and 110 deletions

View File

@ -1 +1 @@
recursive-include pynpong *
recursive-include pypong *

View File

@ -1,4 +1,4 @@
# PynPong
# PyPong
[![License: GNU GPL v3.0](https://img.shields.io/badge/license-GNU%20GPL%20v3.0-blue)](LICENSE)
A game of pong made in PyGame, designed to be used over a network connection.

View File

@ -1 +0,0 @@
__version__ = 'alpha_0.0.1'

View File

@ -1,66 +0,0 @@
"""
Starting launcher of the game. This is where you host new games or join other ones.
"""
import tkinter as tk
global running
def main():
"""
Runs the game launcher.
"""
main_window = tk.Tk()
main_window.title('PynPong Launcher')
main_window.resizable(width=False, height=False)
main_window.geometry('500x250')
def quit_launcher():
"""
Quits the main menu.
"""
global running
running = False
def get_games():
"""
Refresh the games list.
"""
listbox.delete(0, tk.END)
listbox.insert(0, 'Searching for games...')
# Networking code will go here eventually
# Create the top title label
title_label = tk.Label(text='PynPong Launcher', fg='white', bg='#000040', padx=1, pady=20)
title_label.pack(side=tk.TOP, fill=tk.BOTH)
# Create the list of games
listbox = tk.Listbox(main_window)
listbox.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
# Add scrollbar
scrollbar = tk.Scrollbar(main_window)
scrollbar.pack(side=tk.LEFT, fill=tk.Y)
# Link scrollbar to list of games
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
# Create buttons
button_frame = tk.Frame(main_window)
tk.Button(button_frame, text='Host a Game', height=3, width=30).pack()
tk.Button(button_frame, text='Join Selected Game', height=3, width=30).pack()
tk.Button(button_frame, text='Refresh Game List', height=2, width=20, command=get_games).pack()
button_frame.pack(side=tk.RIGHT)
# Set it so that if the X is pressed the application quits
main_window.protocol('WM_DELETE_WINDOW', quit_launcher)
global running
running = True
while running:
main_window.update()
if __name__ == '__main__':
main()

View File

@ -1,36 +0,0 @@
import psutil
class InterfaceInfo:
"""
Class used for storing interface information to make it easier to use
"""
def __init__(self, interface_name, address, subnet_mask):
self.interface_name = interface_name
self.address = address
self.subnet_mask = subnet_mask
def __str__(self):
return '(Interface:{0}, Address:{1}, Subnet Mask:{2})'.format(self.interface_name,
self.address, self.subnet_mask)
def __repr__(self):
return '(Interface:{0}, Address:{1}, Subnet Mask:{2})'.format(self.interface_name,
self.address, self.subnet_mask)
def main():
raw_interface_data = psutil.net_if_addrs()
interface_data = []
# Extract needed information from psutil and put it into a InterfaceInfo object
for interface in raw_interface_data:
address = (raw_interface_data.get(interface)[0]).address
subnet_mask = (raw_interface_data.get(interface)[0]).netmask
interface_data.append(InterfaceInfo(interface, address, subnet_mask))
print(interface_data)
if __name__ == '__main__':
main()

1
pypong/__init__.py Normal file
View File

@ -0,0 +1 @@
__version__ = 'alpha_0.0.1'

8
pypong/__main__.py Normal file
View File

@ -0,0 +1,8 @@
"""
Launch script
"""
from pypong.game_files.game import main as game_main
if __name__ == '__main__':
game_main()

169
pypong/game_files/game.py Normal file
View File

@ -0,0 +1,169 @@
import math
import pygame
import pypong
import random
import time
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
BALL_SIZE = 30
BALL_SPEED = 7
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]
class Ball(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# Booleans to randomize direction of the ball upon spawning
initial_x_vel_negative = bool(random.getrandbits(1))
initial_y_vel_negative = bool(random.getrandbits(1))
self.x_vel = BALL_SPEED
self.y_vel = BALL_SPEED
if initial_x_vel_negative: # Flip ball speed based on random bools
self.x_vel = -BALL_SPEED
if initial_y_vel_negative:
self.y_vel = -BALL_SPEED
self.image = pygame.Surface((BALL_SIZE, BALL_SIZE), pygame.SRCALPHA)
pygame.draw.circle(self.image, GAME_COLOR, (BALL_SIZE / 2, BALL_SIZE / 2), math.floor(BALL_SIZE / 2.0))
self.rect = self.image.get_rect()
def update(self):
self.rect.centerx += self.x_vel
self.rect.centery += self.y_vel
# Always bounce ball if it hits the top or bottom of the screen
if self.rect.top <= 0:
self.rect.top = 0
self.y_vel *= -1
if self.rect.bottom >= SCREEN_SIZE[1]:
self.rect.bottom = SCREEN_SIZE[1]
self.y_vel *= -1
def game(is_host=True):
"""
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()
host_player.rect.centery = SCREEN_SIZE[1] / 2
client_player.rect.centery = SCREEN_SIZE[1] / 2
client_player.rect.right = SCREEN_SIZE[0] # Move client paddle to other side
all_sprites.add(host_player, client_player)
paddle_sprites = pygame.sprite.Group()
paddle_sprites.add(host_player, client_player)
# Create the ball
ball = Ball()
ball.rect.centerx = SCREEN_SIZE[0] / 2
# The ball is moved up or down a random amount
ball.rect.centery = (SCREEN_SIZE[1] / 2) + random.randint(-25, 25)
all_sprites.add(ball)
# Create the scoreboard
# host_score = 0
# client_score = 0
draw()
time.sleep(1)
while running:
clock.tick(GAME_FPS)
draw()
if is_host:
keyboard_movement(host_player)
else:
keyboard_movement(client_player)
# Bounce ball off of paddles
if pygame.sprite.spritecollideany(ball, paddle_sprites):
ball.x_vel *= -1
def main():
"""
Calls the game() function to start the game.
"""
game()
pygame.quit()

View File

@ -1,2 +1 @@
pygame~=2.1.2
psutil~=5.9.4
pygame~=2.1.2

View File

@ -1,5 +1,5 @@
import setuptools
import pynpong
import pypong
with open('requirements.txt') as fh:
required = fh.read().splitlines()
@ -9,13 +9,13 @@ with open('README.md', 'r') as fh:
setuptools.setup(
name='PyPong',
version=pynpong.__version__,
version=pypong.__version__,
author='Nicholas Dyer',
description='A game of pong made in PyGame for play over a local network',
license='GNU GPL-3.0',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://gitea.citruxx.com/ndyer/PynPong',
url='https://gitea.citruxx.com/ndyer/PyPong',
packages=setuptools.find_packages(),
# https://pypi.org/classifiers/
classifiers=[