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


Python Level.createObjects方法代码示例

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


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

示例1: GameSpace

# 需要导入模块: from level import Level [as 别名]
# 或者: from level.Level import createObjects [as 别名]
class GameSpace(object):
	def __init__(self):
		#1) Basic initialization
		pygame.init()

		self.size = width,height = (640,480)
		self.screen = pygame.display.set_mode(self.size)
		self.background = pygame.image.load("imgs/wood.png")
		self.back_rect = self.background.get_rect()

		self.cf = ClientConnFactory(self)	
		reactor.connectTCP(CLIENT_HOST, CLIENT_PORT, self.cf)
		reactor.run()

	def start(self):
		#2) Set up game objects
		self.level = Level(self)
		self.objects = self.level.createObjects()
		self.tank1 = self.objects['Player 2']
		self.teammate = self.objects['Player 1']
		self.enemies = self.objects['Enemies']
		self.blocks = self.objects['Blocks']
		self.pellets = []
		self.explosions = []
		self.tank1_life = True
		self.endgame = False

		#3) Start game loop
		self.hold = False	
		
	def handleEvents(self, tank, events, mouse):	
		for event in events:
			if event.type == QUIT:
				sys.exit()
			elif event.type == KEYUP:
				tank.hold = False
				tank.key = 0
			#	print 'keyup'
			#elif hold is True:
			#	print 'yes'
			#	self.tank1.move(key)
			elif event.type == KEYDOWN:
				if event.key == 32:
					tank.tofire = True
					tank.fire_x, tank.fire_y = mouse
				else:
					tank.hold = True
					tank.key = event.key
			#	key = event.key
			#	self.tank1.move(event.key)
			elif event.type == MOUSEBUTTONDOWN:
				tank.tofire = True
				tank.fire_x, tank.fire_y = mouse
			elif event.type == MOUSEBUTTONUP:
				tank.tofire = False

	def handleRemoteEvents(self, tank, events, mouse):
		for event in events:
			if event['type'] == 'quit':
				sys.exit()
			elif event['type'] == 'keyup':
				tank.hold = False
				tank.key = 0
			elif event['type'] == 'keydown':
				if event['key'] == 32:
					tank.tofire = True
					tank.fire_x, tank.fire_y = mouse
				else:
					tank.hold = True
					tank.key = event['key']
			elif event['type'] == 'mousedown':
				tank.tofire = True
				tank.fire_x, tank.fire_y = mouse
			elif event['type'] == 'mouseup':
				tank.tofire = False

	def packageEvents(self, events):
		evs = []
		for event in events:
			ev = {}
			ev['type'] = ''
			if event.type == QUIT:
				ev['type'] = 'quit'
			elif event.type == KEYUP:
				ev['type'] = 'keyup'
				ev['key'] = event.key
			elif event.type == KEYDOWN:
				ev['type'] = 'keydown'
				ev['key'] = event.key
			elif event.type == MOUSEBUTTONDOWN:
				ev['type'] = 'mousedown'
			elif event.type == MOUSEBUTTONUP:
				ev['type'] = 'mouseup'
			evs.append(ev)
		return evs

	def tick(self):
		state = {}
		state['mouse_pos'] = pygame.mouse.get_pos()
		events = pygame.event.get()
#.........这里部分代码省略.........
开发者ID:bdevorem,项目名称:tanks,代码行数:103,代码来源:twisted_main_p2.py

示例2: GameSpace

# 需要导入模块: from level import Level [as 别名]
# 或者: from level.Level import createObjects [as 别名]
class GameSpace(object):
	def __init__(self):
		#1) Basic initialization
		pygame.init()

		self.size = width,height = (640,480)
		self.screen = pygame.display.set_mode(self.size)
		self.background = pygame.image.load("imgs/wood.png")
		self.back_rect = self.background.get_rect()

		# Start trying to connect to the host
		self.cf = ClientConnFactory(self)	
		reactor.connectTCP(HOST, CLIENT_PORT, self.cf)
		reactor.run()

	def start(self):
		#2) Set up game objects
		self.level = Level(self)
		# Level inits objects, grab it from there
		self.objects = self.level.createObjects()
		# Keep good track of objects
		self.tank1 = self.objects['Player 2']
		self.teammate = self.objects['Player 1']
		self.enemies = self.objects['Enemies']
		self.blocks = self.objects['Blocks']
		self.pellets = []
		self.explosions = []
		# Endgame bools
		self.tank1_life = True
		self.tank2_life = True
		self.endgame = False

		#3) Start game loop
		self.hold = False	
		
	# Handlers for events on our local machine
	def handleEvents(self, tank, events, mouse):
		# Communicate mouse position	
		self.tank1.mx = mouse[0]
		self.tank1.my = mouse[1]

		# Loop through events
		for event in events:
			if event.type == QUIT:
				sys.exit()
			# If key is up, stop moving the character
			elif event.type == KEYUP:
				tank.hold = False
				tank.key = 0
			#	print 'keyup'
			#elif hold is True:
			#	print 'yes'
			#	self.tank1.move(key)
			elif event.type == KEYDOWN:
				if event.key == 32: # Fire (space bar)
					tank.tofire = True
					tank.fire_x, tank.fire_y = mouse
				else: # pass this on to the tank
					tank.hold = True
					tank.key = event.key
			#	key = event.key
			#	self.tank1.move(event.key)
			# FIRE!!
			elif event.type == MOUSEBUTTONDOWN:
				tank.tofire = True
				tank.fire_x, tank.fire_y = mouse
			# No longer fire
			elif event.type == MOUSEBUTTONUP:
				tank.tofire = False

	# Handle events coming from other computer
	# This is essentially identical to the code above
	def handleRemoteEvents(self, tank, events, mouse):
		for event in events:
			if event['type'] == 'quit':
				sys.exit()
			elif event['type'] == 'keyup':
				tank.hold = False
				tank.key = 0
			elif event['type'] == 'keydown':
				if event['key'] == 32:
					tank.tofire = True
					tank.fire_x, tank.fire_y = mouse
				else:
					tank.hold = True
					tank.key = event['key']
			elif event['type'] == 'mousedown':
				tank.tofire = True
				tank.fire_x, tank.fire_y = mouse
			elif event['type'] == 'mouseup':
				tank.tofire = False

	# Put events (as a dict) in a list so pickle can handle it
	def packageEvents(self, events):
		evs = []
		# Read each event and grab the type and keycode related to it
		for event in events:
			ev = {}
			ev['type'] = ''
			if event.type == QUIT:
#.........这里部分代码省略.........
开发者ID:bdevorem,项目名称:tanks,代码行数:103,代码来源:client.py

示例3: GameSpace

# 需要导入模块: from level import Level [as 别名]
# 或者: from level.Level import createObjects [as 别名]
class GameSpace(object):
	def start(self):
		#1) Basic initialization
		pygame.init()

		self.size = width,height = (640,480)
		self.screen = pygame.display.set_mode(self.size)
		self.background = pygame.image.load("imgs/wood.png")
		self.back_rect = self.background.get_rect()

		#2) Set up game objects
		self.clock = pygame.time.Clock()
		self.level = Level(self)
		self.objects = self.level.createObjects()
		self.tank1 = self.objects['Player 1']
		self.teammate = self.objects['Player 2']
		self.enemies = self.objects['Enemies']
		self.blocks = self.objects['Blocks']
		self.pellets = []
		self.explosions = []
		self.tank1_life = True
		self.endgame = False

		#3) Start game loop
		hold = False
		while 1:
			#4) Clock tick regulation
			self.clock.tick(60)

			#5) Handle user inputs
			events = pygame.event.get()
			self.handleEvents(self.tank1, events, pygame.mouse.get_pos())

			#6) Send ticks to objects
			self.tank1.tick()
			for pellet in self.pellets:
				pellet.tick()
			for expl in self.explosions:
				expl.tick()
			for enemy in self.enemies:
				enemy.tick()

			#7) Display game objects
			if len(self.enemies) >= 1:
				if not self.endgame:
					self.screen.blit(self.background, self.back_rect)
					if self.tank1_life:
						self.screen.blit(self.tank1.image, self.tank1.rect)
						self.screen.blit(self.tank1.gun.image,self.tank1.gun.rect)
						self.screen.blit(self.teammate.image, self.teammate.rect)
						self.screen.blit(self.teammate.gun.image,self.teammate.gun.rect)	
					for enemy in self.enemies:
						self.screen.blit(enemy.image, enemy.rect)
						self.screen.blit(enemy.gun.image, enemy.gun.rect)
					for block in self.blocks:
						self.screen.blit(block.image, block.rect)
					for pellet in self.pellets:
						self.screen.blit(pellet.image, pellet.rect)
					for expl in self.explosions:
						self.screen.blit(expl.image, expl.rect)
				else:
					self.background = pygame.image.load("imgs/gameover.png")
					self.back_rect = self.background.get_rect()
					self.screen.blit(self.background, self.back_rect)
			else: #user wins
				self.background = pygame.image.load("imgs/youwin.png")
				self.back_rect = self.background.get_rect()
				self.screen.blit(self.background, self.back_rect)

			pygame.display.flip()

	def handleEvents(self, tank, events, mouse):
		for event in events:
			if event.type == QUIT:
				sys.exit()
			elif event.type == KEYUP:
				tank.hold = False
				tank.key = 0
			#	print 'keyup'
			#elif hold is True:
			#	print 'yes'
			#	self.tank1.move(key)
			elif event.type == KEYDOWN:
				if event.key == 32:
					tank.tofire = True
					tank.fire_x, tank.fire_y = mouse
				else:
					tank.hold = True
					tank.key = event.key
			#	key = event.key
			#	self.tank1.move(event.key)
			elif event.type == MOUSEBUTTONDOWN:
				tank.tofire = True
				tank.fire_x, tank.fire_y = mouse
			elif event.type == MOUSEBUTTONUP:
				tank.tofire = False
开发者ID:bdevorem,项目名称:tanks,代码行数:98,代码来源:main.py


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