如何将这个python游戏带到多人游戏?

时间:2019-07-17 05:03:38

标签: python

我想在我的python游戏中添加多人游戏,以便可以与2个玩家一起玩。我已经为服务器和游戏编写了代码,但是我不知道如何链接它们。 有人可以帮忙吗?

这是游戏的代码:

def multi():

    mixer.init()

    WIDTH = 800
    HEIGHT = 600

    fenster = turtle.Screen()
    fenster.setup(WIDTH, HEIGHT,0,0)
    fenster.title("Crossy Road von Julian")
    fenster.bgpic('back.gif')
    fenster.tracer(0)

    colors = ["white", "orange", "yellow", "blue", "gray"]
    images = ["a.gif", "b.gif", "c.gif", "p.gif", "f.gif"]
    music = ["m.waf"]

    sound1 = mixer.Sound("m.wav")
    sound1.play()

    for image in images:
        turtle.register_shape(image)




    class Bus(turtle.Turtle):
        def __init__(self):
            super(Bus, self).__init__()
            self.penup()
            self.shape("f.gif")
            self.shapesize(1,20)
            self.setpos(0, 280)
            self.in_game_speed = 0


        def update(self):
            self.goto(self.xcor() + self.in_game_speed, self.ycor())
            if self.xcor() > WIDTH:
                self.setx(-600)
                self.fillcolor("Gray")



    class Muenzen(turtle.Turtle):
        def __init__(self):
            super(Muenzen, self).__init__()
            self.penup()
            self.shape("circle")
            self.shapesize(0.5)
            self.pencolor("yellow")
            self.fillcolor("gold")
            self.setpos(0,0)
            self.y_positions = [-150, 200, 60 ,90]
            self.setx(random.randrange(-400,400))
            self.sety(random.choice(self.y_positions))


    class Auto(turtle.Turtle):
        def __init__(self):
            super(Auto, self).__init__()
            self.penup()
            self.shape("square")
            self.shapesize(2,5)
            # self.pencolor("blue")
            self.directions = ["left", "right"]
            self.direction = "left"
            self.in_game_speed = 2.3

        def update(self):
            if self.direction == "right":
                if self.xcor() > WIDTH:
                    self.setx(random.randrange(-600,-400))

            if self.direction == "left":
                if self.xcor() < -WIDTH:
                    self.setx(400)

            if self.direction == "right":
                self.goto(self.xcor() + self.in_game_speed, self.ycor())
            else:
                self.goto(self.xcor() - self.in_game_speed, self.ycor())



    class Spieler(turtle.Turtle):
        def __init__(self, auto, coin, bus):
            super(Spieler, self).__init__()
            self.penup()
            self.shape("p.gif")
            self.shapesize(1,1)
            self.setpos(0,-250)
            # self.pencolor("yellow")
            self.in_game_speed = 4.3
            self.speed(0)
            self.score = 0


        def right(self):
            self.goto(self.xcor() + self.in_game_speed, self.ycor())

        def left(self):
            self.goto(self.xcor() - self.in_game_speed, self.ycor())

        def up(self):
            self.goto(self.xcor(), self.ycor() + self.in_game_speed)

        def down(self):
            self.goto(self.xcor(), self.ycor() - self.in_game_speed)

        def powershell(self):
            os.system('start powershell.exe')

        def is_collision(self, other):
            a = self.xcor() - other.xcor()
            b = self.ycor() - other.ycor()
            distance = math.sqrt((a**2) + (b**2))
            if distance < 25:
                return True
            else:
                return False

        def update(self):
            for coin in coins:
                if self.is_collision(coin):
                    self.score += 1
                    scorecount.clear()
                    scorecount.write(self.score, font = font)
                    coin.setpos(0,450)
                    coins.remove(coin)
            for auto in group_6:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_5:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_4:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_3:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_2:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
            for auto in group_1:
                if self.is_collision(auto):
                    scorecount.clear()
                    self.score = 0
                    if auto.direction == "right":
                        self.setpos(10,-280)
                        self.setpos(10,-280)
                    if auto.direction == "left":
                        self.setpos(10,-280)
                        self.setpos(10,-280)

            if self.is_collision(bus):
                self.fillcolor("Gray")
                self.setpos(10,-280)
                self.setpos(10,-280)
                scorecount.setpos(700,700)
                font2 = ("arial", 40)
                scorecount.color("black")
                scorecount.write("Super!!!", font = font2)
                os.system('msg * Super!')
                sys.exit()



    class Score(turtle.Turtle):
        def __init__(self):
            super(Score, self).__init__()
            self.hideturtle
            self.penup()
            self.pencolor("white")
            self.pensize(2)

    scorefont = Score()
    scorefont.setpos(200,-280)
    font = ("Germania One", 18)         
    scorefont.write("Geld:", font = font)

    scorecount = Score()
    scorecount.setpos(310,-280)

    coins = []
    for coin in range(3):
        coin = Muenzen()
        coins.append(coin)

    group_1 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(200)
        auto.setx(-400)
        auto.in_game_append = 5
        auto.shape("a.gif")
        group_1.append(auto)

    group_2 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(-50)
        auto.setx(-500)
        auto.in_game_append = 5
        auto.shape("b.gif")
        group_2.append(auto)

    group_3 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(180)
        auto.setx(300)
        auto.in_game_append = 5
        auto.shape("c.gif")
        group_3.append(auto)

    group_4 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(0)
        auto.setx(400)
        auto.in_game_append = 1
        auto.shape("a.gif")
        group_4.append(auto)

    group_5 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(100)
        auto.setx(100)
        auto.in_game_append = 1
        auto.shape("b.gif")
        group_5.append(auto)

    group_6 = []
    for auto in range(1):
        auto = Auto()
        auto.sety(-100)
        auto.setx(-50)
        auto.in_game_append = 1
        auto.shape("c.gif")
        group_6.append(auto)

    bus = Bus()


    spieler = Spieler(auto, coin, bus)

    turtle.listen()
    fenster.onkeypress(spieler.right,"Right")
    fenster.onkeypress(spieler.left,"Left")
    fenster.onkeypress(spieler.up,"Up") 
    fenster.onkeypress(spieler.down,"Down")
    fenster.onkeypress(spieler.powershell,"p")

    running = True
    while running:
        fenster.update()
        spieler.update()
        for auto in group_1:
            auto.update()
        for auto in group_2:
            auto.update()
        for auto in group_3:
            auto.update()
        for auto in group_4:
            auto.update()
        for auto in group_5:
            auto.update()
        for auto in group_6:
            auto.update()
        bus.update()

    Network()


    turtle.mainloop()

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

multi()
network()

这是服务器的代码:

import socket
from _thread import *
import sys

server = "192.168.178.70"
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(2)
print('Server started. Listening on port', port)

def threaded_client(conn):
    conn.send(str.encode("Connected"))
    reply = ""
    while True:
        try:
            data = conn.recv(2048)
            reply = data.decode('utf-8')

            if not data:
                print('Disconnected')
                break
            else:
                print('Received: ', reply)
                print('Sending: ', reply)

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

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

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

我可以使用游戏中的网络类连接到服务器,但是我不知道如何将游戏与服务器链接。

0 个答案:

没有答案