Home > Software engineering >  Why is the 'game' module running automatically when I am trying to run 'FootballSim_s
Why is the 'game' module running automatically when I am trying to run 'FootballSim_s

Time:09-02

MODULE I AM TRYING TO RUN - Football_sim module

import socket
from _thread import *
import plyer
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 = [plyer.Player(630, 375, 'R', 'Arsenal', position='ST'),
                    plyer.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()

OTHER MODULE- Game module

import Button_tactics
import pygame
import plyer
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")
        self.clock = pygame.time.Clock()

def RedrawWindow(self, p, p2):
        # Builds the GUI
        self.window.fill((50, 50, 50))
        self.BuildFootballPitch()
        Score.Draw()
        p.DrawPlayerShape()
        p2.DrawPlayerShape()

def main():
    # Team.BuildFormation()
    clock = pygame.time.Clock()
    # Allows the window to stay open
    running = True
    n = Client.Network()
    p = n.ReturnP()
    while running:
    p2 = n.Send(p)

    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            running = False
            pygame.quit()
            pass

    p.Move()
    game.RedrawWindow(p, p2)

game = Game()

if __name__ == '__main__':
    main()
else:
    pass

Every time I try and run the main() in the FootballSim_server is keeps automatically starting the process for the game module. I have tried adding in the if __name__ == '__main__', but this has no effect. I even tried to change the imports at the top from 'from module import class' to just doing import module - however this had no effect. I was wondering if anyone know how to fix this annoying problem?

CodePudding user response:

Move game = Game() into the main() function.

def main():
    game = Game()
    # Team.BuildFormation()
    clock = pygame.time.Clock()
    # Allows the window to stay open
    running = True
    n = Client.Network()
    p = n.ReturnP()
    while running:
    p2 = n.Send(p)

    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            running = False
            pygame.quit()
            pass

    p.Move()
    game.RedrawWindow(p, p2)

if __name__ == '__main__':
    main()
else:
    pass
  • Related