20 Commits

Author SHA1 Message Date
2776532133 rebased networking onto main with new name 2022-12-22 09:15:13 -05:00
38d51409b6 Merge remote-tracking branch 'origin/networking' into networking
# Conflicts:
#	pypong/networking/gui.py
#	pypong/networking/host.py
2022-12-22 09:14:27 -05:00
671533de79 changed max scan requests to 128 2022-12-22 09:13:37 -05:00
6ab6bb5cf8 revised some variables and wording 2022-12-22 09:13:37 -05:00
ac7880e1c2 changed host.py to be a super basic server that just responds with an echo 2022-12-22 09:13:37 -05:00
5b4892ab69 updated networking __init__.py 2022-12-22 09:13:34 -05:00
f87075fa51 added packet.py and the Packet class 2022-12-22 09:13:31 -05:00
b47b59ec7d added docstrings and comments 2022-12-22 09:13:24 -05:00
99feadc7e2 implemented checking for network connections in gui 2022-12-22 09:13:22 -05:00
5c1a61f25c renamed project to PynPong 2022-12-22 09:08:06 -05:00
16b263b3e2 changed max scan requests to 128 2022-12-22 09:00:39 -05:00
07e6eec033 revised some variables and wording 2022-12-21 13:38:33 -05:00
9ab6393065 changed host.py to be a super basic server that just responds with an echo 2022-12-21 13:20:13 -05:00
b5b247f2a7 updated networking __init__.py 2022-12-21 13:08:51 -05:00
1102e075c0 added packet.py and the Packet class 2022-12-21 13:06:04 -05:00
a5c47f72ef added docstrings and comments 2022-12-21 13:00:27 -05:00
dcd1d9c6b0 implemented checking for network connections in gui 2022-12-21 12:30:10 -05:00
ffaedd821b Merge pull request 'Create launcher GUI' (#2) from menu-gui into main
Reviewed-on: ndyer/PyPong#2
2022-12-20 18:20:04 -05:00
10ce4f1595 added launcher gui 2022-12-20 18:17:47 -05:00
690b69b2f4 added file to help get interface information 2022-12-20 12:36:54 -05:00
13 changed files with 348 additions and 184 deletions

View File

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

View File

@@ -1,4 +1,4 @@
# PyPong
# PynPong
[![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.

1
pynpong/__init__.py Normal file
View File

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

View File

@@ -0,0 +1,121 @@
"""
Networking client to connect to a server and find server hosts
"""
import ipaddress
import random
import psutil
import socket
SERVER_PORT = 29987
SERVER_BUFFER = 1024
SCAN_TIMEOUT = 3
class InterfaceInfo:
"""
Class used for storing interface information to make it easier to use
"""
def __init__(self, interface_name, network):
self.interface_name = interface_name
self.network = network
def __str__(self):
return 'Interface:{0}, Network:{1}'.format(self.interface_name, self.network)
def __repr__(self):
return '(Interface:{0}, Network:{1})'.format(self.interface_name, self.network)
def request_server_info(server_ip, server_port):
"""
Request server info from a PyPong server given an IP and a port
:param server_ip: Server IP to request info from
:param server_port: Port to connect to server on
:return: response given by server (or lack thereof)
"""
request = 'PYPONGREQ;SVRINFO'
udp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Timeout is set to SCAN_TIMEOUT plus a random number between 0 and SCAN_TIMEOUT so that threads can
# start asynchronously to avoid a lot of requests being sent to machines at once
# Average time is SCAN_TIMEOUT*1.5
udp_client_socket.settimeout(SCAN_TIMEOUT + (SCAN_TIMEOUT * random.random()))
try:
udp_client_socket.sendto(str.encode(request, 'UTF-8'), (server_ip, server_port))
except OSError:
pass
try:
server_response = udp_client_socket.recvfrom(SERVER_BUFFER)[0].decode('UTF-8')
except TimeoutError:
server_response = '{0};TIMEOUT'.format(server_ip)
except UnicodeDecodeError:
server_response = '{0};MANGLED_RESPONSE'.format(server_ip)
udp_client_socket.close()
return server_response
def get_interface_info():
"""
Gets information using psutil about the current machine's interfaces and their networks. This only works for IPv4
networks and will not return any data in regard to IPv6 links.
:return: List of InterfaceInfo objects containing information about all interfaces on the current machine excluding
the loopback interface (lo or 127.0.0.1)
"""
raw_interface_data = psutil.net_if_addrs()
interface_data = []
for interface in raw_interface_data:
# Extract needed information from psutil and put it into a InterfaceInfo object
address = (raw_interface_data.get(interface)[0]).address
cidr_suffix = ipaddress.IPv4Network('{0}/{1}'.format(address, raw_interface_data.get(interface)[0].netmask),
strict=False).prefixlen
# Do not add the loopback interface or IPv6 interfaces
if ':' not in address and address != '127.0.0.1':
interface_data.append(InterfaceInfo(interface, '{0}/{1}'.format(address, cidr_suffix)))
return interface_data
def get_ips_from_network(network):
"""
Generate a list of IP addresses given an address in CIDR notation. This works from smallest to largest, meaning that
it builds a list starting at <ADDRESS>/32 (aka just the current machine) decrementing the CIDR by 1 until the
correct subnet is reached.
:param network:
:return:
"""
network_split = network.split('/')
address = network_split[0]
cidr = int(network_split[1])
all_addresses = []
test_cidr = 32 # Start with the machine itself
while test_cidr >= cidr:
# Generate all addresses inside the current subnet given by 'address/test_cidr'
current_addresses = [str(ip) for ip in ipaddress.IPv4Network('{0}/{1}'.format(address, test_cidr),
strict=False)]
# Do not add the address again if it is already in the list of all addresses
for test_address in current_addresses:
if test_address not in all_addresses:
all_addresses.append(test_address)
test_cidr -= 1
return all_addresses
def check_ip(ip_address):
"""
Check a single IP address for a running game server
:param ip_address: IP address to check
:return: response of the server (or timeout if none)
"""
response = request_server_info(ip_address, SERVER_PORT)
return response

158
pynpong/networking/gui.py Normal file
View File

@@ -0,0 +1,158 @@
"""
Starting launcher of the game. This is where you host new games or join other ones.
"""
import tkinter as tk
import concurrent.futures
import pypong.networking.client as client
global running
# Don't allow any more than this number of threads when looking for games
# Can cripple a network at high numbers, 128 is recommended (can scan a /24 network in ~9 seconds on average)
# Higher range subnets will exponentially take more time to finish (/16 would take ~38.4 minutes at 128 req/s)
MAX_SCAN_REQUESTS = 128
def main():
"""
Runs the game launcher.
"""
# Executor used for scanning local network for servers
executor = concurrent.futures.ThreadPoolExecutor(max_workers=MAX_SCAN_REQUESTS)
main_window = tk.Tk()
main_window.title('PynPong Launcher')
main_window.resizable(width=False, height=False)
main_window.geometry('500x300')
def quit_launcher():
"""
Quits the main menu.
"""
global running
running = False
# Stop executor from creating new scanning tasks
executor.shutdown(wait=False, cancel_futures=True)
def check_game(game_ip):
"""
Checks to see if there is a game bein ghosted at a given IP
:param game_ip: IP address to check for a host
:return: Response from the server (or timeout if nothing found)
"""
result = client.check_ip(game_ip)
return result
def get_games(network):
"""
Refreshes the game list.
:param network: The network to scan for games on
"""
listbox.delete(0, tk.END)
listbox.insert(0, ' Building IP range...')
main_window.update()
possible_ips = client.get_ips_from_network(network.get().split(' ')[0])
listbox.delete(0, tk.END)
listbox.insert(0, 'this might take some time...')
listbox.insert(0, 'Refreshing server listing,')
main_window.update()
futures = []
# Use the executor to create threads to search for games
for ip in possible_ips:
future = executor.submit(check_game, ip)
futures.append(future)
game_found = False
for future in futures:
result = future.result()
if 'PYPONGRST;SVRINFO' in result: # Check to make sure response has the right header and type
raw_response = result.split(';') # Split response by block (denoted by semicolons)
response = []
# Split up each block by chunks (<COMMAND>:<RESPONSE>)
# e.g. the block 'NAME:Nicholas Dyer' -> ['NAME', 'Nicholas Dyer']
for block in raw_response:
chunks = block.split(':')
for chunk in chunks:
response.append(chunk)
# Make sure the response has the NAME command
if 'NAME' in response:
# Session name is the next index after the command
session_name = response[response.index('NAME') + 1]
if not game_found: # If this is the first game found, clear out the list
listbox.delete(0, tk.END)
game_found = True
listbox.insert(0, session_name)
main_window.update() # Update screen while finding games (laggy, but works)
if not game_found:
listbox.delete(0, tk.END)
listbox.insert(0, 'No games found!')
def get_addresses():
"""
Get the current machine's local interfaces and their corresponding addresses in CIDR notation
:return: Address in CIDR notation along with the interface
Example:
['172.20.126.4/24 [eth0]', 192.168.2.43/16 [wlan0]']
"""
interface_info = client.get_interface_info()
shortened_info = []
for interface in interface_info:
shortened_info.append('{0} [{1}]'.format(interface.network, interface.interface_name))
return shortened_info
# 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
global listbox
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=lambda: get_games(selected_network)).pack()
networks = get_addresses()
selected_network = tk.StringVar()
selected_network.set(networks[0])
interface_menu = tk.OptionMenu(button_frame, selected_network, *networks)
interface_menu.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

@@ -0,0 +1,25 @@
import socket
LOCAL_IP = '127.0.0.1'
SERVER_PORT = 29987
SERVER_BUFFER = 1024
udp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_server_socket.bind((LOCAL_IP, SERVER_PORT))
waiting_for_connection = True
while waiting_for_connection:
received = udp_server_socket.recvfrom(SERVER_BUFFER)
message = received[0]
address = received[1]
decoded_message = message.decode('UTF-8')
if decoded_message.split(';')[0] != 'PYPONGREQ':
message = 'PYPONGRST;ERROR:INVALID HEADER'
udp_server_socket.sendto(message.encode('UTF-8'), address)
else:
message = 'PYPONGRST;ECHO:{0}'.format(decoded_message)
udp_server_socket.sendto(message.encode('UTF-8'), address)
udp_server_socket.close()

View File

@@ -0,0 +1,36 @@
"""
Contains the Packet class to help with the transmission, reading, and verification of infermation over the network
"""
class Packet:
"""
Class that contains information to be sent over the network. Can self-verify and has methods (will have methods) to
easily encode and decode packet information
"""
VALID_HEADERS = [
'PYPONGREQ',
'PYPONGRST'
]
VALID_TYPES = [
'SVRINFO'
]
def __init__(self, header, msg_type, *message):
self.header = header
self.msg_type = msg_type
self.message = []
for x in message:
self.message.append(x)
def integrity_check(self):
"""
Verify that the information in the packet is correct and makes sense
:return: True if packet verifies correctly, False if not
"""
if self.header not in Packet.VALID_HEADERS:
return False
if self.msg_type not in Packet.VALID_TYPES:
return False
return True

View File

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

View File

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

View File

@@ -1,169 +0,0 @@
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 +1,2 @@
pygame~=2.1.2
pygame~=2.1.2
psutil~=5.9.4

View File

@@ -1,5 +1,5 @@
import setuptools
import pypong
import pynpong
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=pypong.__version__,
version=pynpong.__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/PyPong',
url='https://gitea.citruxx.com/ndyer/PynPong',
packages=setuptools.find_packages(),
# https://pypi.org/classifiers/
classifiers=[