当前位置: 首页>>代码示例>>Python>>正文


Python GameState.toString方法代码示例

本文整理汇总了Python中gamestate.GameState.toString方法的典型用法代码示例。如果您正苦于以下问题:Python GameState.toString方法的具体用法?Python GameState.toString怎么用?Python GameState.toString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gamestate.GameState的用法示例。


在下文中一共展示了GameState.toString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Server

# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import toString [as 别名]

#.........这里部分代码省略.........
		# Maak socket aan en luister.
		self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		self.sock.setblocking(0)
		self.sock.bind((HOSTNAME, PORT))
		
		print "Server listening at '%s:%i'" % (HOSTNAME, PORT)
		
		# Game state.
		self.gameState = GameState()
		
		# Lijst met alle connections.
		self.connections = {}
		
	def handleRequest(self, data, address):
		"""Handel een request af.
		"""
		id = int(data.split(" ")[0])
		command = data.split(" ")[1]
		args = " ".join(data.split(" ")[2:])
		
		# Connecten.
		if command == "connect":
			
			# Informatie ophalen uit packet.
			name = args
			id = getNextId()

			# Check of de speler met die naam al bestaat.			
			if self.gameState.containsPlayerWithName(name) == False:
				# Hij bestaat nog niet - toevoegen.
				
				# Connectie opslaan.
				self.connections[id] = address
				print "Player connected: %s (ID:%i)" % (name, id)
	
				# Player toevoegen aan game.
				self.gameState.addPlayer(name)
				
				# Welcome command terugsturen naar client.
				welcomeString = "0 welcome %s %i" % (name, id)
				self.sock.sendto(welcomeString, address)
	
			else:
				# Hij bestaat al - niet toestaan.
				
				# Stuur denied command terug naar de client.
				print "Player denied: %s (name in use)" % name
				deniedString = "0 denied %s player name in use" % name
				self.sock.sendto(deniedString, address)

		# Disconnecten.
		elif command == "disconnect":
			name = args
			print "Player disconnected: %s (ID:%i)" % (name, id)
			self.gameState.removePlayer(name)
			del(self.connections[id])

		# Moven.
		elif command == "move":
			name = args.split(" ")[0]
			velX = int(args.split(" ")[1])
			velY = int(args.split(" ")[2])
			#print "Moving player %s to %i %i" % (name, velX, velY)
			self.gameState.movePlayer(name, velX, velY)

		# Onbekend request.
		else:
			print "Unknown network command: %s" % data
		
	def run(self):
		"""Run de main loop van de server.
		"""

		# Eeuwig blijven draaien.
		while True:

			# Lees de socket uit tot er niks meer beschikbaar is deze tick.
			empty = False
			while not empty:
				
				# Haal gegevens op.
				try:
					data, address = self.sock.recvfrom( 1024 )
					
				# Except voor als er niks meer is.
				except socket.error:
					empty = True
				
				# Else voor als er wel wat is.
				else:
					#print "Received message:", data
					self.handleRequest(data, address)
			
			# Verstuur naar iedereen de nieuwe posities.
			for id, connection in self.connections.items():
				positionString = "%i %s" % (id, self.gameState.toString())
				self.sock.sendto(positionString, connection)
			
			# Wachten.
			pygame.time.wait(50)
开发者ID:rappie,项目名称:platformo_multiplayer_poc,代码行数:104,代码来源:server.py


注:本文中的gamestate.GameState.toString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。