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


Python Graphics.setWord方法代码示例

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


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

示例1: __init__

# 需要导入模块: from graphics import Graphics [as 别名]
# 或者: from graphics.Graphics import setWord [as 别名]

#.........这里部分代码省略.........
		
		self.logger.log('\'%s\' is a %s!' % (letter.upper(), result), kind='log')

		self.graphics.guess(letter, result in ('MATCH', 'WIN'), str(self.logic)), # TODO: Let Graphics take care of the representation for us (?)
		
		# TODO: Clean up the 'switch' logic
		#{'WIN': self.win, 'LOSE': self.lose}.get(result, lambda: None)()
		# return { ('MATCH', 'WIN'):  }
		
		if result == 'WIN':
			self.win()
		elif result == 'LOSE':
			self.lose()


	def validGuess(self, letter):
		''' Determines if a letter is a valid guess '''

		# TODO: Make this configurable (list of requirements?)

		# Normalize input
		letter = letter.upper()

		# Check all requirements
		if letter not in self.characterSet:
			# Make sure the character is a guessable letter
			self.logger.log('\'%s\' is not in the character set!' % letter, kind='log')
			return False
		elif len(letter) != 1:
			# Make sure the guess is only one letter
			self.logger.log('\'%s\' does not have exactly one letter!' % letter, kind='log')
			return False
		elif self.logic.hasGuessed(letter):
			# Make sure it's not a repeat guess
			self.logger.log('\'%s\' has already been guessed!' % letter, kind='log')
			return False
		else:
			return True


	def win(self):
		''' Victorious feedback, schedules the next round '''
		self.logger.log('Phew. You figured it out!', kind='log')
		self.effects.win.play()
		self.scheduleRestart()
	
	
	def lose(self):
		''' Failure feedback, schedules the next round '''
		# TODO: Show correct word before restarting (?)
		self.logger.log('You\'ve been hanged. Requiescat in pace!', kind='log')
		self.effects.lose.play()
		if self.revealWhenLost:
			self.graphics.setWord(self.word) # Reveal answer
		self.scheduleRestart()


	def scheduleRestart(self):
		''' Schedules a new round and disables guessing in the interim '''
		self.validState = False # Disable guessing between rounds
		self.root.after(self.restartDelay, self.restart)
		

	def restart(self):
		''' Starts a new game '''
		self.logger.log('\n{0}\n{1:^40}\n{0}\n'.format('-'*40, 'NEW GAME'), kind='log', identify=False) # TODO: Enable identify options in Logger.log

		self.word, self.hint = next(self.wordFeed)
		self.graphics.showHint(self.hint)

		self.logic.new(self.word)
		self.graphics.play(str(self.logic))

		self.validState = True # Ready to accept guesses again


	def quit(self, message=None, prompt=False):
		''' Exits the application '''
		# TODO: Find a way to close Python
		#print('\n'.join(self.messages))
		self.root.quit()


	def createWordFeed(self, name):
		''' Creates a word feed from the dictionary specified by the name '''
		# TODO: Give class a reference to words (?)
		# TODO: Wise to hard-code path (?)
		# TODO: Handle incorrectly structured dictionaries
		self.logger.log('Creating word feed from \'{name}\'.'.format(name=name), kind='log')
		fn = self.dictData[name]['file']
		with open('data/dicts/%s' %  fn, 'r', encoding='utf-8') as wordFile:
			words = wordFile.read().split('\n')
		while True:
			try:
				line = choice(words)
				word, hint = line.split('|') # Word|Hint
				yield word, hint
			except ValueError as e:
				words.remove(line) # Remove the culprit from the word feed
				self.logger.log('Removing invalid definition ({0}).'.format(line), kind='error')
开发者ID:SwiftsNamesake,项目名称:Hangman,代码行数:104,代码来源:main.py


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