好了,所以我使用python学习socket编程。我会在适当的时候转移到c。Socket编程与Python
我以为我会写一个无聊的游戏去学习与插座;随意使用源代码(.bmp文件是我在gimp中制作的50x50像素图像)。服务器在此时不起作用。 (我敢肯定,它挂在“c, addr = s.accept()”)
我想通过使用两个线程来处理这个问题。一个线程来处理通信服务器端。还有一个线程来处理游戏循环,也是服务器端。目标是能够在服务器和客户端上玩游戏。我已经读过'扭曲的模块'可以处理多线程,我以前从来没有做过多线程,但它应该是相当不重要的。
其次,我想实现这样的,我不超载的网络,并刺激我的ISP(互联网服务提供商,对于缺乏经验的开发者)的方式在服务器和客户端之间的通讯。有没有一种方法可以测试网络并查看服务器和客户端之间实际传递的信息量?
现在,我只有少量信息在客户端和服务器之间传递(只有当玩家通过按下按钮与客户端或服务器进行交互时,才会传递信息)。有没有更聪明的消息传递模型,我应该使用?
GameServer.py
import pygame, sys, os
from pygame.locals import *
import socket
s = socket.socket()
host = socket.gethostname()
port = 6000
s.bind(('0.0.0.0',port))
s.listen(5)
#while True:
# c, addr = s.accept()
# print 'Got connection from', addr
# c.send('Hi blork, I am not rational either, but this works. So I am happy!')
# c.close()
window = pygame.display.set_mode((640,50))
pygame.display.set_caption("My Trivial Network Game: Get to the right side of the screen!")
class Ship():
xvel = 0
yvel = 0
x = 0
y = 0
image = pygame.image.load('ship.bmp')
def physics(self):
self.x += self.xvel
self.y += self.yvel
return self
class Bolt():
xvel = 0
yvel = 0
x = 640
y = 0
image = pygame.image.load('bolt.bmp')
def physics (self):
self.x += self.xvel
self.y += self.yvel
return self
def input(events):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
# elif event.key == pygame.K_f:
# ship.xvel = 2
elif event.key == pygame.K_z:
bolt.xvel = -4
s.send('zap')
pygame.display.set_caption("Oh no! Jon's being a dork!!")
c.close()
if event.type == QUIT:
sys.exit(0)
#def MakeShip():
# pygame
ship = Ship()
bolt = Bolt()
box = pygame.Rect(0,0,640,50)
loopVal = 0
while True:
pygame.draw.rect(window,(0,0,0),box,0)
window.blit(ship.image, (ship.x,ship.y))
window.blit(bolt.image, (bolt.x,bolt.y))
if loopVal == 0:
loopVal += 1
c, addr = s.accept()
print "Now have connection from address: ", addr
input(pygame.event.get())
ship.physics()
bolt.physics()
pygame.display.update()
if (ship.x > 640):
pygame.display.set_caption("You win!")
GameClient.py
import pygame, sys, os
from pygame.locals import *
import socket
s = socket.socket()
host = socket.gethostname()
port = 6000
window = pygame.display.set_mode((640,50))
pygame.display.set_caption("My Small Network Game: Get to the right side of the screen!")
class Ship():
xvel = 0
yvel = 0
x = 0
y = 0
image = pygame.image.load('ship.bmp')
def physics(self):
self.x += self.xvel
self.y += self.yvel
return self
class Bolt():
xvel = 0
yvel = 0
x = 640
y = 0
image = pygame.image.load('bolt.bmp')
def physics (self):
self.x += self.xvel
self.y += self.yvel
return self
def input(events):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_f:
ship.xvel = 2
# elif event.key == pygame.K_z:
# bolt.xvel = -4
# pygame.display.set_caption("Oh no! Jon's being a dork!!")
if event.type == QUIT:
sys.exit(0)
#def MakeShip():
# pygame
ship = Ship()
bolt = Bolt()
box = pygame.Rect(0,0,640,50)
s.connect((host, port))
reply = s.recv(1024)
print reply
while True:
pygame.draw.rect(window,(0,0,0),box,0)
window.blit(ship.image, (ship.x,ship.y))
window.blit(bolt.image, (bolt.x,bolt.y))
input(pygame.event.get())
ship.physics()
bolt.physics()
#reply = s.recv(1024)
print "He replied with zap!"
if reply == 'zap':
bolt.xvel = -4
pygame.display.set_caption("Oh no! Jon's being a dork!!")
pygame.display.update()
if (ship.x > 640):
pygame.display.set_caption("You win!")
你不太可能会导致你的ISP的问题一个简单的网络游戏。我不会太在意测量带宽使用情况。 – Amber
这是一种解脱。呃...供将来参考,我将如何去测量带宽使用情况? – user2522001