CODE THAT I AM RUNNING: Module Server
import socket
from _thread import *
from plyer import Player. <--- Module that is causing the problem
import json
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.p = [] # Starting positions for player 1 and player 2
self.PlayerId = 0
self.Players = [Player(630, 375, 'R', 'Arsenal', position='ST'),
Player(630, 440, 'B', 'Arsenal', position='ST')]
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(json.dumps(self.Players[curr_player]))
reply = ""
while True:
# Continuously runs when the client is connected
try:
data = json.loads(conn.recv(2048))
self.Players[curr_player] = data
if not data:
print("Player Left")
break
else:
if curr_player == 1:
reply = self.Players[0]
else:
reply = self.Players[1]
print("Received: ", data)
print("Sending: ", reply)
conn.sendall(json.dumps(reply))
except:
break
print("Connection Lost")
conn.close()
def main():
GS = GameServer()
GS.Bind()
while True:
s = GS.ReturnSocket()
# 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()
CODE THAT I AM IMPORTING: Module plyer
import pygame
import sqlite3
import random
class Player:
def __init__(self, x, y, colour, team, position):
self.window = pygame.display.set_mode((1280, 800))
# Connecting to the database
self.conn = sqlite3.connect('/Users/kelanwestwood/Desktop/College Computer science project/Cvs '
'project/FifaStats.db')
self.cur = self.conn.cursor()
self.red = (255, 0, 0)
self.blue = (0, 0, 255)
self.Team = team
self.Position = position
self.PlayerName = self.PlayerFinder('Name')
if self.PlayerName is None:
if self.Position == 'LW':
self.Position = 'LM'
elif self.Position == 'RW':
self.Position = 'RM'
else:
pass
# Stats
self.PlayerName = self.PlayerFinder('Name')
self.Nationality = ""
self.Pace = ""
self.Shoot = ""
self.Pass = ""
self.Dribble = ""
self.GKDive = ""
self.Reflex = ""
# Player
self.x = x
self.y = y
self.width = 20
self.height = 20
self.Colour = colour
self.rect = (self.x, self.y, self.width, self.height)
# Barrier
self.barrier = pygame.Rect(0, 0, 200, 200)
# Player Controls
self.HoldingBall = False
self.DefendActive = False
self.LockedPass = True
def DrawPlayerShape(self):
# Colour option
if self.Colour == 'R':
self.Colour = self.red
elif self.Colour == 'B':
self.Colour = self.blue
# print(co_ordinate)
pygame.draw.rect(self.window, self.Colour, self.rect)
When I run the module Server, it keeps booting up Pygame and opening an empty black window. I have tried changing the import from import *, to from module import class, however the Pygame window keeps booting up. I do not even have anything running in the plyer module, to begin the Pygame window to open. Why does Pygame keep running when I import the plyer module?
CodePudding user response:
It is NOT problem with import
. You run PyGame
in your server.
When you run server then you run GameServer()
and this runs self.Players = [Player(...), ...]
in GameServer.__init__()
and this runs pygame.display.set_mode((1280, 800))
in Player.__init__()
.
And this displays PyGame
window.
You would have to remove pygame.display.set_mode((1280, 800))
to stop displaying window.
But later this can make problem with other functions, ie. pygame.draw.rect
in DrawPlayerShape
.
Frankly, server rather shouldn't use any PyGame
functions. Only clients should use PyGame