2

Whenever I run my pygame code on 2 configurations for 2 players they don't update the positions so my 2 configurations/players are separate and not in sync. I move the positions for the first configuration/player, but it would update for the second configuration/player.

Here is my client code:

import pygame
from pygame import color
from network import Network

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 220, 0)
LIGHTER_GREEN = (0, 255, 0)
DARK_GREEN = (0, 150, 0)
BLUE = (0, 100, 100)
LIGHTER_BLUE = (0, 128, 128)
ORANGE = (255, 150, 0)
LIGHTER_ORANGE = (255, 165, 0)
YELLOW = (235, 235, 0)
LIGHTER_YELLOW = (255, 255, 0)

width = 700
height = 600
billybob = 300
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tapping Game")

clientNumber = 0

class Player():
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.rect = (x,y,width,height)
        self.vel = 3
        
    def draw(self, screen):
        pygame.draw.rect(screen, self.color, self.rect)
    
    def update(self):
        self.rect = (self.x, self.y, self.width, self.height)
        
def read_pos(str):
    str = str.split(",")
    return int(str[0]), int(str[1])

def make_pos(tup):
    return str(tup[0]) + "," + str(tup[1])
        
             
def redrawWindow(screen, player, player2):
    screen.fill((255,255,255))
    player.draw(screen)
    player2.draw(screen)
    pygame.display.update()
    

def main(screen, billybob):
    run = True
    n = Network()
    startPos = read_pos(n.getPos())
    p = Player(startPos[0], startPos[1],900,600,(BLUE))
    p2 = Player(0,0,900,billybob,(RED))
    while run:
        
        p2Pos = read_pos(n.send(make_pos((p.x, p.y))))
        p2.x = p2Pos[0]
        p2.y = p2Pos[1]
        p2.update()
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
            if event.type == pygame.KEYDOWN:
            #try if statement pressed "p" go onto other if statement maybe
                if event.key == pygame.K_SPACE:
                    billybob = billybob - 10
                    p = Player(startPos[0], startPos[1],900,600,(BLUE))
                    p2 = Player(0,0,900,billybob,(RED))
            

        redrawWindow(screen, p, p2)
        
main(screen, billybob)

Here is my server code:

from os import error
import socket
from _thread import *
import sys

hostname = socket.gethostname()
ipaddr = socket.gethostbyname(hostname)

#Only people on the same wifi can connect
server = ipaddr
port = 5555

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.bind((server, port))
except socket.error as e:
    str(e)
    
    
# The 2 represents number of people allowed to connect at once
s.listen(2)
print("Waiting for a connection, Server Started")

def read_pos(str):
    str = str.split(",")
    return int(str[0]), int(str[1])

def make_pos(tup):
    return str(tup[0]) + "," + str(tup[1])

pos = [(0,0), (0,0)]

def threaded_client(conn, player):
    conn.send(str.encode(make_pos(pos[player])))
    reply = ""
    while True:
        try:
            #If error found increase 2048 bits
            data = read_pos(conn.recv(2048).decode())
            pos[player] = data
            
                        
            if not data:
                print("Disconnected")
                break
            else:
                if player == 1:
                    reply = pos[0]
                else:
                    reply = pos[1]
                
                print("Recieved: ", data)
                print("Sending: ", reply)
                
            conn.sendall(str.encode(make_pos(reply)))
        except:
            break
        
    print("Lost Conncection")
    conn.close()
    
currentPlayer = 0
while True:
    conn, addr = s.accept()
    print("Connected to:", addr)
    
    start_new_thread(threaded_client, (conn, currentPlayer))
    currentPlayer += 1

Here is my network code:

import socket

hostname = socket.gethostname()
ipaddr = socket.gethostbyname(hostname)

class Network:
    def __init__(self):
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server = ipaddr
        self.port = 5555
        self.addr = (self.server, self.port)
        self.pos = self.connect()
        
    def getPos(self):
        return self.pos
        
    def connect(self):
        try:
            self.client.connect(self.addr)
            return self.client.recv(2048).decode()
        except:
            pass
        
    def send(self, data):
        try:
            self.client.send(str.encode(data))
            return self.client.recv(2048).decode()
        except socket.error as e:
            print(e)
Yun
  • 3,056
  • 6
  • 9
  • 28
  • you are not updating `p` in the main loop, only `p2` and why do you create new instances of `Player` when space key is pressed?, also here: `(RED)` parentheses are not necessary, you can just use `RED` – Matiiss Oct 07 '21 at 19:13
  • well i put the new instance so that it will update and also i put p.update() and p2.update() and it did not work – Thomas Mcgown Oct 07 '21 at 19:47
  • but `.update` on its own doesn't do anything, you need to update `.x` and `.y` and don't create a new instance – Matiiss Oct 07 '21 at 21:25
  • can u give me an example – Thomas Mcgown Oct 08 '21 at 18:54

0 Answers0