简体   繁体   English

Python网络/套接字编程,简单的游戏

[英]Python network/socket programming, simple game

Im trying to make a simple game but cant understand how to make it work and send more then one thing over the network. 我试图制作一个简单的游戏,但无法理解如何让它工作,并通过网络发送更多的东西。 It works the first time but it supposed to go 10 times. 它第一次工作,但它应该去10次。 It only sends 1 random number now but i want it to send one new when the game goes again and want a new number. 它现在只发送1个随机数,但我想让它在游戏再次发送时发送一个新的并想要一个新号码。

Server 服务器

import socket, random               

sock = socket.socket()
host = socket.gethostname()
port = 12345
sock.bind((host, port))
sock.listen(5)

c, addr = sock.accept()

cpu = random.choice(range(0, 3))
c.send(cpu)

gameon = c.recv(int(1024))

Client 客户

import socket, random

sock = socket.socket()  # Create a socket object
host = socket.gethostname()  # Get local machine name
port = 12345  # Reserve a port for your service.
sock.connect((host, port))

GAMEON = 'Rock', 'Paper', 'Scissors'
game = 0
iwin = 0
ilose = 0
tie = 0

while game < 10:

    for i in range(0, 3):
        print "%d %s" % (i + 1, GAMEON[i])

    player = int(input ("Choose from 1-3: ")) - 1
    cpu = int(sock.recv(1024))
    print cpu

    print""

    print "%s vs %s" % (GAMEON[player], GAMEON[cpu])
    print ""
    if cpu != player:
      if (player - cpu) % 3 < (cpu - player) % 3:
        print "Player wins\n"
        iwin += 1
      else:
        print "CPU wins\n"
        ilose += 1
    else:
      print "TIE!\n"
      tie += 1 
    game += 1
    sock.send(str(game))
print"Game is done"
print"you win: ", (iwin), "Times"
print"computer wins: ", (ilose), "Times"
print"tie: ", (tie), "Times"

You need threads to do your bidding. 您需要线程来进行出价。

Example code 示例代码

# Listen
s.listen(10)
print 'Socket now listening on port ' + str(PORT) + "..."

while 1:
    # wait
    conn, addr = s.accept()

    print 'Connected with ' + addr[0] + ":" + str(addr[1])

    # Let's fork a thread for each request
    processThread = threading.Thread(target=processConnection, args=(conn, addr[0]));
    processThread.start()


s.close()

Your processConnection will look like this: 您的processConnection将如下所示:

# Process Connection
def processConnection(conn, ip):
    print "Thread started..."
    print "-------------------------------------------------------------";

    cpu = random.choice(range(0, 3))
    conn.send(cpu)

    gameon = conn.recv(int(1024))

    conn.close()

Update 1 更新1

If you need the server to keep talking with the client, then do this. 如果您需要服务器继续与客户端通信,那么执行此操作。 Server will wait for the client to send back a message. 服务器将等待客户端发回消息。 If client sends anything, server will return a random number. 如果客户端发送任何内容,服务器将返回一个随机数。 If the client doesn't need anymore data, just close the connection and server loop will end. 如果客户端不再需要数据,只需关闭连接,服务器循环就会结束。

import socket, random               

sock = socket.socket()
host = socket.gethostname()
port = 12345
sock.bind((host, port))
sock.listen(5)

c, addr = sock.accept()

white True:
    cpu = random.choice(range(0, 3))
    c.send(cpu)

    gameon = c.recv(int(1024))

    if gameon is None:
        break

The problem is that your server will serve one request and then stop. 问题是您的服务器将提供一个请求然后停止。 You need to put it in some kind of while loop. 你需要把它放在某种while循环中。

I wrote a basic IM server/client in Python which might help you: https://github.com/hdgarrood/PyMess/blob/master/server/basictcpserver.py#L59 我用Python编写了一个基本的IM服务器/客户端,可以帮助你: https//github.com/hdgarrood/PyMess/blob/master/server/basictcpserver.py#L59

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM