--Server--
import socket
from _thread import *
from plyer import Player, FootBall
import pickle
class GameServer:
def __init__(self):
self.Server = '127.0.0.1'
self.Port = 52674
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.PlayerId = 0
st1 = Player(630, 375, (255, 0, 0), 'Arsenal', 'ST')
lw1 = Player(900, 300, (255, 0, 0), 'Arsenal', 'CB')
rw1 = Player(370, 300, (255, 0, 0), 'Arsenal', 'RW')
rcm1 = Player(630, 375, (255, 0, 0), 'Arsenal', 'ST')
lcm1 = Player(630, 375, (255, 0, 0), 'Arsenal', 'ST')
rcb1 = Player(630, 375, (255, 0, 0), 'Arsenal', 'ST')
lcb1 = Player(630, 375, (255, 0, 0), 'Arsenal', 'ST')
rb1 = Player(630, 375, (255, 0, 0), 'Arsenal', 'ST')
lb1 = Player(630, 375, (255, 0, 0), 'Arsenal', 'ST')
gk1 = Player(630, 375, (255, 0, 0), 'Arsenal', 'ST')
st2 = Player(630, 300, (0, 255, 0), 'Spurs', 'ST')
lw2 = Player(370, 425, (0, 255, 0), 'Spurs', 'CB')
rw2 = Player(900, 425, (0, 255, 0), 'Spurs', 'RW')
football = FootBall(630, 375)
self.p = [st1, st2, lw1, lw2, rw1, rw2, rcm1, lcm1, rcb1, lcb1] # Players that are currently being used
self.b = football
def Bind(self):
# Connects the client to the server
try:
self.s.bind((self.Server, self.Port))
except socket.error:
print("Error")
# Listens to maximum 2 players only
self.s.listen(2)
print("Waiting for a connection...")
def ReturnSocket(self):
return self.s
def ThreadedClient(self, conn, curr_player):
conn.send(pickle.dumps(self.p[curr_player]))
# conn.sendall(pickle.dumps(self.b))
print(self.p)
playerCount = 0
while True:
# Continuously runs when the client is connected
try:
pos_data = pickle.loads(conn.recv(2048))
# self.p[curr_player] = pos_data
# pos_data is the players current list of players
next = len(pos_data)
print("NEXT:", next, self.p)
if not pos_data:
print("Player Left")
break
else:
if curr_player == 1:
reply = self.p[next]
else:
reply = self.p[next]
# Send the ball when all the players have been sent.
reply2 = self.b
print("[PLAYER] Received: ", pos_data)
# print("[BALL] Received", reply2)
print("[PLAYER] Sending: ", reply)
if playerCount < 11:
# conn.sendall(pickle.dumps(self.b))
conn.sendall(pickle.dumps(reply))
playerCount += 1
else:
pass
except:
pass
print("Connection Lost")
conn.close()
def main():
GS = GameServer()
GS.Bind()
s = GS.ReturnSocket()
while True:
# Continuously looking for connections
conn, addr = s.accept()
print(addr, "has joined the game")
# Function runs in the background
start_new_thread(GS.ThreadedClient, (conn, GS.PlayerId))
GS.PlayerId += 1
if __name__ == '__main__':
main()
--Game--
import pygame
from Client import Network
class Game:
def __init__(self):
self.width = 1280
self.height = 800
self.window = pygame.display.set_mode((1280, 800))
pygame.display.set_caption("Football Simulator")
def RedrawWindow(self):
# score = Score()
# Builds the GUI
self.window.fill((50, 50, 50))
self.BuildFootballPitch()
# score.Draw()
# plyer1.DrawPlayerShape(self.window)
# plyer1.HoverCheck(self.window)
# p2.HoverCheck(self.window)
def BuildFootballPitch(self, dark_green=(76, 153, 0), white=(255, 255, 255)):
# This draws the football pitch on the screen
# rectangle = x, y, width, height
# circle = x, y
# line = start x end y, start x end y, width
# MAIN FIELD
rect1 = (260, 0, 750, 775)
pygame.draw.rect(self.window, dark_green, rect1)
rect2 = (315, 40, 640, 675)
pygame.draw.rect(self.window, white, rect2, 2)
pygame.draw.line(self.window, white, (315, 375), (953, 375), 2)
circle1 = (630, 375)
pygame.draw.circle(self.window, white, circle1, 50, 2)
# THE GOALPOST AND BOX
rect3 = (532.5, 40, 200, 100)
pygame.draw.rect(self.window, white, rect3, 2)
rect4 = (582.5, 40, 100, 53.25)
pygame.draw.rect(self.window, white, rect4, 2)
rect5 = (532.5, 615, 200, 100)
pygame.draw.rect(self.window, white, rect5, 2)
rect6 = (582.5, 662.5, 100, 53.25)
pygame.draw.rect(self.window, white, rect6, 2)
# CORNER MARKS
circle2 = (315, 715)
pygame.draw.circle(self.window, white, circle2, 40, 2, True, False, False, False)
circle3 = (955, 715)
pygame.draw.circle(self.window, white, circle3, 40, 2, False, True, False, False)
circle4 = (955, 40)
pygame.draw.circle(self.window, white, circle4, 40, 2, False, False, True, False)
circle5 = (315, 40)
pygame.draw.circle(self.window, white, circle5, 40, 2, False, False, False, True)
def Loading():
# Team.BuildFormation()
# Allows the window to stay open
loading = True
n = Network()
s = []
p = n.ReturnP()
while loading:
if p is None or len(s) > 12:
print("False")
loading = False
else:
print("True")
s.append(p)
n.Send(s)
p = n.GetNextPlayer()
def main():
clock = pygame.time.Clock()
running = True
game = Game()
while running:
clock.tick(60)
for events in pygame.event.get():
if events.type == pygame.QUIT:
running = False
pygame.quit()
game.RedrawWindow()
pygame.display.update()
if __name__ == '__main__':
Loading()
main()
--Client Server--
import socket
import pickle
class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = '127.0.0.1'
self.port = 52674
self.addr = (self.host, self.port)
self.p = self.Connect()
def ReturnP(self):
return self.p
def GetNextPlayer(self):
try:
return pickle.loads(self.client.recv(2048))
except:
return None
def Connect(self):
try:
self.client.connect(self.addr)
return pickle.loads(self.client.recv(2048))
except:
pass
def Send(self, data):
try:
self.client.send(pickle.dumps(data))
except socket.error as e:
return str(e)
When I run the game, it only runs the Loading() funtion and does not continue onto the main() function when the length of s has reached to the size of 10. It prints true every time a player has come through to the client, but then once it has printed it 1- times it stops and the console does not do anything else. However, when I shut down the server after, the pygame window opens and I am able to see the screen perfectly. Also, when the Loading() function runs and seemingly finishes, the console does not print 'False', which it should do when the loop has broken. This suggests that the loop has broken, but I do not know where the problem is coming from, that is stopping the process from continuing. I have used the pygame debugger to try and analyse the problem, however it does not pick any errors up. Thank you in advance for any replies.