如何让别人看到我的精灵动画在一个多人游戏中使用pygame中的套接字

qnzebej0  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(252)

我正在尝试用socket在pygame中制作一个多人游戏。我已经在我的播放器上应用了一个精灵动画,这样当它移动时就会显示一个正在运行的动画。但是当我通过服务器连接另一个播放器时,另一个播放器看不到动画。所以,我希望其他人看到精灵动画。请帮助我让其他人看到动画。我已经上传了整个代码,以及服务器代码。
游戏.py

import pygame
from network import Network

width = 800
height = 600
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Client")

clientNumber = 0

class Player():
    def __init__(self,x,y):
        self.left = False
        self.right = False
        self.count = 0
        self.x = x
        self.y = y
        self.standing = pygame.image.load('white.png')
        self.walkRight = [pygame.image.load('wr1.png'),pygame.image.load('wr2.png'),pygame.image.load('wr3.png'),pygame.image.load('wr4.png'),pygame.image.load('wr5.png')]
        self.walkLeft = [pygame.image.load('w1.png'),pygame.image.load('w2.png'),pygame.image.load('w3.png'),pygame.image.load('w4.png'),pygame.image.load('w5.png')]
        self.rect = (x,y)
        self.vel = 13

    def draw(self, win):
        if self.count +1 > 10:
            self.count = 0
        if self.left :
            win.blit(self.walkLeft[self.count//2],self.rect)
            self.count+=1
        elif self.right :
            win.blit(self.walkRight[self.count//2],self.rect)
            self.count+=1
        elif not (self.left or self.right):
            win.blit(self.standing,self.rect)

    def move(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT]:
            self.x -= self.vel
            self.left = True
            self.right = False

        if keys[pygame.K_RIGHT]:
            self.x += self.vel
            self.left = False
            self.right = True

        if keys[pygame.K_UP]:
            self.y -= self.vel
            if keys[pygame.K_LEFT]:
                self.left = True
            elif keys[pygame.K_RIGHT]:
                self.right = True
            else:
                self.left = True

        if keys[pygame.K_DOWN]:
            self.y += self.vel
            if keys[pygame.K_LEFT]:
                self.left = True
            elif keys[pygame.K_RIGHT]:
                self.right = True
            else:
                self.left = True

        if not keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT] and not keys[pygame.K_UP] and not keys[pygame.K_DOWN]:
            self.left = False
            self.right = False
            self.count = 0

        self.update()

    def update(self):

        self.rect = (self.x, self.y)

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(win,player, player2):
    win.fill((255,255,255))
    player.draw(win)
    player2.draw(win)
    pygame.display.update()

def main():
    run = True
    n = Network()
    startPos = read_pos(n.getPos())
    p = Player(startPos[0],startPos[1])

    p2 = Player(0,0)

    clock = pygame.time.Clock()

    while run:
        clock.tick(15)
        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()

        p.move()
        redrawWindow(win, p, p2)

main()

服务器.py

import socket
from _thread import *
import sys

server ="192.168.12.125"
port = 5555

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

try:
    s.bind((server, port))
except socket.error as e:
    str(e)

s.listen(4)
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),(100,100)]

def threaded_client(conn, player):
    conn.send(str.encode(make_pos(pos[player])))
    reply = ""
    while True:
        try:
            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("Received: ", data)
                print("Sending : ", reply)

            conn.sendall(str.encode(make_pos(reply)))
        except:
            break

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

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

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

网络.py

import socket

class Network:
    def __init__(self):
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server = "192.168.12.125"
        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)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题