This is a pong style game which is very basic but involves sending and receiving data using TCP. The data is encoded using pickle which seems to be very inefficient yet I am still using it.
The Server:
The server calculates the new positions of the paddles and balls depending on the inputs from each player.
import socket, time, pickle, random
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(("YOUR_IP", CHOSEN_PORT))
serversocket.listen(2)
arr = [400,400,400,400,0,0]
connection = []
ball_y_speed = 1
ball_x_speed = 1
def process_positions(array, player_1, player_2):
#info[0] = key_up
#info[1] = key_down
global ball_y_speed, ball_x_speed
'''PADDLE MOVING'''
if player_1[0] == True:
array[0]-=1
else:
array[0] = array[0]
if player_1[1] == True:
array[0]+=1
else:
array[0] = array[0]
if player_2[0] == True:
array[1]-=1
else:
array[1] = array[1]
if player_2[1] == True:
array[1]+=1
else:
array[1] = array[1]
if array[0]<0:
array[0] = 0
elif array[0] > 540:
array[0] = 540
if array[1]<0:
array[1] = 0
elif array[1] > 540:
array[1] = 540
'''PADDLE MOVING'''
'''BALL MOVING'''
array[2] += round(ball_y_speed)
array[3] += round(ball_x_speed)
negative_speed = [-0.6, -0.65, -0.7, -0.75, -0.8, -0.85, -0.9, -0.95, -1]
positive_speed = [-1, -1.05, -1.1, -1.15, -1.2, -1.25, -1.3, -1.35, -1.4, -1.45, -1.5]
if array[2] > 595:
if ball_y_speed >= 1:
ball_y_speed *= random.choice(negative_speed)
elif ball_y_speed < 1:
ball_y_speed *= random.choice(positive_speed)
if array[2] < 0:
if ball_y_speed >= 1:
ball_y_speed *= random.choice(negative_speed)
elif ball_y_speed < 1:
ball_y_speed *= random.choice(positive_speed)
if array[3]>795:
if ball_x_speed >= 1:
ball_x_speed *= random.choice(negative_speed)
elif ball_x_speed < 1:
ball_x_speed *= random.choice(positive_speed)
array[4] += 1
if array[3]<0:
if ball_x_speed >= 1:
ball_x_speed *= random.choice(negative_speed)
elif ball_x_speed < 1:
ball_x_speed *= random.choice(positive_speed)
array[5] += 1
'''BALL MOVING'''
'''PADDLE DETECTION'''
if array[3]<20 and (array[0]<array[2] and array[0]+60>array[2]):
ball_x_speed *=-1
if array[3]>780 and (array[1]<array[2] and array[1]+60>array[2]):
ball_x_speed *=-1
#info = [player_1_y, player_2_y, ball_y, ball_x, score_1, score_2]
return array
def waiting_for_connections():
while len(connection)<2:
conn, addr = serversocket.accept()
connection.append(conn)
print(conn)
print(connection)
def recieve_information():
player_1_info = pickle.loads(connection[0].recv(1024))
player_2_info = pickle.loads(connection[1].recv(1024))
return player_1_info, player_2_info
while True:
waiting_for_connections()
data_arr = pickle.dumps(arr)
print(data_arr)
connection[0].send(data_arr)
connection[1].send(data_arr)
player1, player2 = recieve_information()
arr = process_positions(arr,player1, player2)
The Client:
Displays the game for each player and receives the inputs using pygame of whether you press the up or down arrow key.
import pygame
import socket
import time
import pickle
pygame.init()
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0,255,0)
display_width = 800
display_height = 600
clock = pygame.time.Clock()
gameDisplay = pygame.display.set_mode((display_width, display_height))
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(("YOUR_IP ", CHOSEN_PORT))
def message_display(text,x,y):
largeText = pygame.font.Font('freesansbold.ttf',45)
TextSurf, TextRect = text_objects(str(text), largeText, GREEN)
TextRect.center = ((x),(y))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def text_objects(text, font, colour):
textSurface = font.render(text, True, colour)
return textSurface, textSurface.get_rect()
def recieve_data():
data = clientsocket.recv(1024)
data = pickle.loads(data)
return data
def draw_paddles(x,y,p):
if p == 1:
pygame.draw.rect(gameDisplay, RED, [x, y, 10, 60])
if p == 2:
pygame.draw.rect(gameDisplay, BLUE, [x, y, 10, 60])
def draw_ball(x,y):
pygame.draw.circle(gameDisplay, BLACK, [x,y], 5)
def display():
game_finished = False
data = []
key_up = False
key_down = False
while game_finished == False:
info = recieve_data()
gameDisplay.fill(WHITE)
draw_paddles(10, info[0], 1)
draw_paddles(display_width-20, info[1], 2)
draw_ball(info[3], info[2])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
key_up = True
if event.key == pygame.K_DOWN:
key_down = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
key_up = False
if event.key == pygame.K_DOWN:
key_down = False
arr = [key_up, key_down]
data_arr = pickle.dumps(arr)
clientsocket.send(data_arr)
message_display(info[4], 250, 300)
message_display(info[5], 550, 300)
#info = [player_1_y, player_2_y, ball_y, ball_x, score_1, score_2]
display()