0

Im making a game, using pygame, where players should be able to connect to the server which hosts the game and then be able to interact with the server using the client to change the direction of the player. My issue is that when the first client connects this error occurs on the client:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\charl\Desktop\PyCharm 2019.2.1\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Users\charl\Desktop\PyCharm 2019.2.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/charl/OneDrive - King Edward VI College, Stourbridge/Programming project course work/Main game/Tron 5.1/Tron client.py", line 91, in <module>
    g.run()
  File "C:/Users/charl/OneDrive - King Edward VI College, Stourbridge/Programming project course work/Main game/Tron 5.1/Tron client.py", line 85, in run
    self.send()
  File "C:/Users/charl/OneDrive - King Edward VI College, Stourbridge/Programming project course work/Main game/Tron 5.1/Tron client.py", line 78, in send
    data = (read_pos(self.n.send(make_pos(self.direction))))
  File "C:/Users/charl/OneDrive - King Edward VI College, Stourbridge/Programming project course work/Main game/Tron 5.1/Tron client.py", line 35, in read_pos
    return json.loads(str)  # returns as tuple
  File "C:\Users\charl\Desktop\Coding\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\charl\Desktop\Coding\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\charl\Desktop\Coding\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

But when I run the code in another window it works correctly.

This is how i start the game on the sever:

g = Game(650, 600, "tron")
start = time.time()
g.runGame()

currentPlayer = 0
while True:
   conn, addr = player1.accept()
   print("Connected to:", addr)

   start_new_thread(threaded_client, (conn, currentPlayer))
   currentPlayer += 1

   if currentPlayer == 2:
       break

I think the problem lies with the 'if currentPlayer == 2:' but without this the clients wont connect

The only other code I think could also be causing a problem is this:

def threaded_client(conn, player):
  global startData
  if player == 0:  # player 1
      startData = direction[0]
  if player == 1:  # player 2
      startData = direction[1]

  conn.send(str.encode(make_pos(startData)))  # converts to a string then sends to player

  while True:
      try:
          data = read_pos(conn.recv(2048).decode())
          if type(data) == list:
              direction[player] = data  # updates player in window

              g.p1.direction = direction[0]
              g.p2.direction = direction[1]

              reply = "got dir"
              conn.sendall(str.encode(make_pos(reply)))


      except:
          break

  print("Lost connection")
  conn.close()

This is the code on the client:

class game:
   def __init__(self, h, w, t):
       self.WIDTH = w
       self.HEIGHT = h
       self.TITLE = t
       self.direction = (0, 0)
       self.clock = pygame.time.Clock()
       self.n = Network()

       self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
       pygame.display.set_caption(self.TITLE)

   def start(self):
       self.direction = read_pos(self.n.getStartData())

   def Events(self):
       for event in pygame.event.get():  # gets all events in last tick
           if event.type == pygame.KEYDOWN:  # If key is pressed
               # *** Player one control ***
               if event.key == pygame.K_w:
                   self.direction = (0, -2)

               elif event.key == pygame.K_s:
                   self.direction = (0, 2)

               elif event.key == pygame.K_d:
                   self.direction = (2, 0)

               elif event.key == pygame.K_a:
                   self.direction = (-2, 0)

   def flip(self):
       pygame.display.flip()  # Update the screen
       self.clock.tick(60)  # Controls the FPS

   def send(self):
       data = (read_pos(self.n.send(make_pos(self.direction))))
       print(data)

   def run(self):
       self.start()
       while True:
           self.Events()
           self.send()
           print(self.direction)
           self.flip()
charlie s
  • 93
  • 6

0 Answers0