詳解如何利用Python制作24點小遊戲

先睹為快

24點

遊戲規則(改編自維基百科)

從1~10這十個數字中隨機抽取4個數字(可重復),對這四個數運用加、減、乘、除和括號進行運算得出24。每個數字都必須使用一次,但不能重復使用。

逐步實現

Step1:制作24點生成器

既然是24點小遊戲,當然要先定義一個24點遊戲生成器啦。主要思路就是隨機生成4個有解的數字,且范圍在1~10之間,代碼實現如下:

	def generate(self):
		self.__reset()
		while True:
			self.numbers_ori = [random.randint(1, 10) for i in range(4)]
			self.numbers_now = copy.deepcopy(self.numbers_ori)
			self.answers = self.__verify()
			if self.answers:
				break

在驗證4個數字是否有解並求出所有解部分,我直接暴力枚舉然後去重瞭,感興趣的同學可以自己再優化一下求解算法(有數字重復的時候)。我的代碼如下圖所示,其實就是遞歸枚舉所有排序然後一一驗證是否有解:

	'''驗證生成的數字是否有答案'''
	def __verify(self):
		answers = []
		for item in self.__iter(self.numbers_ori, len(self.numbers_ori)):
			item_dict = []
			list(map(lambda i: item_dict.append({str(i): i}), item))
			solution1 = self.__func(self.__func(self.__func(item_dict[0], item_dict[1]), item_dict[2]), item_dict[3])
			solution2 = self.__func(self.__func(item_dict[0], item_dict[1]), self.__func(item_dict[2], item_dict[3]))
			solution = dict()
			solution.update(solution1)
			solution.update(solution2)
			for key, value in solution.items():
				if float(value) == self.target:
					answers.append(key)
		# 避免有數字重復時表達式重復(T_T懶得優化瞭)
		answers = list(set(answers))
		return answers
	'''遞歸枚舉'''
	def __iter(self, items, n):
		for idx, item in enumerate(items):
			if n == 1:
				yield [item]
			else:
				for each in self.__iter(items[:idx]+items[idx+1:], n-1):
					yield [item] + each
	'''計算函數'''
	def __func(self, a, b):
		res = dict()
		for key1, value1 in a.items():
			for key2, value2 in b.items():
				res.update({'('+key1+'+'+key2+')': value1+value2})
				res.update({'('+key1+'-'+key2+')': value1-value2})
				res.update({'('+key2+'-'+key1+')': value2-value1})
				res.update({'('+key1+'×'+key2+')': value1*value2})
				value2 > 0 and res.update({'('+key1+'÷'+key2+')': value1/value2})
				value1 > 0 and res.update({'('+key2+'÷'+key1+')': value2/value1})
		return res

Step2:定義遊戲精靈類

因為玩傢需要通過鼠標點擊來操作卡片,這時候就涉及到一些碰撞檢測。所以先定義一些必要的遊戲精靈類。

①卡片類

卡片類的定義也很簡單,在屏幕上根據被賦予的屬性值來顯示自身即可。當然之後也需要根據用戶的操作來改變這些屬性值(內容、顏色、字體等)並在屏幕上根據屬性的改變而改變顯示狀態即可。具體而言代碼實現如下:

class Card(pygame.sprite.Sprite):
	def __init__(self, x, y, width, height, text, font, font_colors, bg_colors, attribute, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.rect = pygame.Rect(x, y, width, height)
		self.text = text
		self.attribute = attribute
		self.font_info = font
		self.font = pygame.font.Font(font[0], font[1])
		self.font_colors = font_colors
		self.is_selected = False
		self.select_order = None
		self.bg_colors = bg_colors
	'''畫到屏幕上'''
	def draw(self, screen, mouse_pos):
		pygame.draw.rect(screen, self.bg_colors[1], self.rect, 0)
		if self.rect.collidepoint(mouse_pos):
			pygame.draw.rect(screen, self.bg_colors[0], self.rect, 0)
		font_color = self.font_colors[self.is_selected]
		text_render = self.font.render(self.text, True, font_color)
		font_size = self.font.size(self.text)
		screen.blit(text_render, (self.rect.x+(self.rect.width-font_size[0])/2,
								  self.rect.y+(self.rect.height-font_size[1])/2))

②按鈕類

按鈕類和卡片類類似,唯一的不同點就是在用戶點擊按鈕時需要根據該按鈕的功能來響應用戶的本次點擊操作(即實現一次該功能)。因此隻需要繼承卡片類,然後再定義一個響應用戶點擊按鈕事件的回調函數即可。代碼實現如下:

class Button(Card):
	def __init__(self, x, y, width, height, text, font, font_colors, bg_colors, attribute, **kwargs):
		Card.__init__(self, x, y, width, height, text, font, font_colors, bg_colors, attribute)
	'''根據button function執行響應操作'''
	def do(self, game24_gen, func, sprites_group, objs):
		if self.attribute == 'NEXT':
			for obj in objs:
				obj.font = pygame.font.Font(obj.font_info[0], obj.font_info[1])
				obj.text = obj.attribute
			self.font = pygame.font.Font(self.font_info[0], self.font_info[1])
			self.text = self.attribute
			game24_gen.generate()
			sprites_group = func(game24_gen.numbers_now)
		elif self.attribute == 'RESET':
			for obj in objs:
				obj.font = pygame.font.Font(obj.font_info[0], obj.font_info[1])
				obj.text = obj.attribute
			game24_gen.numbers_now = game24_gen.numbers_ori
			game24_gen.answers_idx = 0
			sprites_group = func(game24_gen.numbers_now)
		elif self.attribute == 'ANSWERS':
			self.font = pygame.font.Font(self.font_info[0], 20)
			self.text = '[%d/%d]: ' % (game24_gen.answers_idx+1, len(game24_gen.answers)) + game24_gen.answers[game24_gen.answers_idx]
			game24_gen.answers_idx = (game24_gen.answers_idx+1) % len(game24_gen.answers)
		else:
			raise ValueError('Button.attribute unsupport <%s>, expect <%s>, <%s> or <%s>...' % (self.attribute, 'NEXT', 'RESET', 'ANSWERS'))
		return sprites_group

Step3:實現遊戲主循環

先構思一下怎麼設計遊戲主界面,個人的簡單設計草圖如下(不是特別走心的設計草圖T_T):

OK,開搞。先初始化、加載必要的素材和定義必要的變量,代碼實現如下:

        # 初始化, 導入必要的遊戲素材
	pygame.init()
	pygame.mixer.init()
	screen = pygame.display.set_mode(SCREENSIZE)
	pygame.display.set_caption('24 point - 微信公眾號: Charles的皮卡丘')
	win_sound = pygame.mixer.Sound(AUDIOWINPATH)
	lose_sound = pygame.mixer.Sound(AUDIOLOSEPATH)
	warn_sound = pygame.mixer.Sound(AUDIOWARNPATH)
	pygame.mixer.music.load(BGMPATH)
	pygame.mixer.music.play(-1, 0.0)
	# 24點遊戲生成器
	game24_gen = game24Generator()
	game24_gen.generate()
	# 精靈組
	# --數字
	number_sprites_group = getNumberSpritesGroup(game24_gen.numbers_now)
	# --運算符
	operator_sprites_group = getOperatorSpritesGroup(OPREATORS)
	# --按鈕
	button_sprites_group = getButtonSpritesGroup(BUTTONS)
	# 遊戲主循環
	clock = pygame.time.Clock()
	selected_numbers = []
	selected_operators = []
	selected_buttons = []
	is_win = False

遊戲主循環主要分三個部分,首先是按鍵檢測:

		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				sys.exit(-1)
			elif event.type == pygame.MOUSEBUTTONUP:
				mouse_pos = pygame.mouse.get_pos()
				selected_numbers = checkClicked(number_sprites_group, mouse_pos, 'NUMBER')
				selected_operators = checkClicked(operator_sprites_group, mouse_pos, 'OPREATOR')
				selected_buttons = checkClicked(button_sprites_group, mouse_pos, 'BUTTON')

根據檢測結果更新卡片狀態和一些變量:

'''檢查控件是否被點擊'''
def checkClicked(group, mouse_pos, group_type='NUMBER'):
	selected = []
	# 數字卡片/運算符卡片
	if group_type == GROUPTYPES[0] or group_type == GROUPTYPES[1]:
		max_selected = 2 if group_type == GROUPTYPES[0] else 1
		num_selected = 0
		for each in group:
			num_selected += int(each.is_selected)
		for each in group:
			if each.rect.collidepoint(mouse_pos):
				if each.is_selected:
					each.is_selected = not each.is_selected
					num_selected -= 1
					each.select_order = None
				else:
					if num_selected < max_selected:
						each.is_selected = not each.is_selected
						num_selected += 1
						each.select_order = str(num_selected)
			if each.is_selected:
				selected.append(each.attribute)
	# 按鈕卡片
	elif group_type == GROUPTYPES[2]:
		for each in group:
			if each.rect.collidepoint(mouse_pos):
				each.is_selected = True
				selected.append(each.attribute)
	# 拋出異常
	else:
		raise ValueError('checkClicked.group_type unsupport <%s>, expect <%s>, <%s> or <%s>...' % (group_type, *GROUPTYPES))
	return selected

當有兩個數字和一個運算符被點擊時,則執行被點擊數字1{+/-/×/÷}被點擊數字2操作(數字1、2根據點擊順序確定),並進一步更新卡片屬性和一些必要的變量:

		if len(selected_numbers) == 2 and len(selected_operators) == 1:
			noselected_numbers = []
			for each in number_sprites_group:
				if each.is_selected:
					if each.select_order == '1':
						selected_number1 = each.attribute
					elif each.select_order == '2':
						selected_number2 = each.attribute
					else:
						raise ValueError('Unknow select_order <%s>, expect <1> or <2>...' % each.select_order)
				else:
					noselected_numbers.append(each.attribute)
				each.is_selected = False
			for each in operator_sprites_group:
				each.is_selected = False
			result = calculate(selected_number1, selected_number2, *selected_operators)
			if result is not None:
				game24_gen.numbers_now = noselected_numbers + [result]
				is_win = game24_gen.check()
				if is_win:
					win_sound.play()
				if not is_win and len(game24_gen.numbers_now) == 1:
					lose_sound.play()
			else:
				warn_sound.play()
			selected_numbers = []
			selected_operators = []
			number_sprites_group = getNumberSpritesGroup(game24_gen.numbers_now)

最後根據各個卡片的屬性在屏幕上顯示各個卡片,若遊戲勝利/遊戲失敗,則同時顯示遊戲勝利/遊戲失敗提示框:

		# 精靈都畫到screen上
		for each in number_sprites_group:
			each.draw(screen, pygame.mouse.get_pos())
		for each in operator_sprites_group:
			each.draw(screen, pygame.mouse.get_pos())
		for each in button_sprites_group:
			if selected_buttons and selected_buttons[0] in ['RESET', 'NEXT']:
				is_win = False
			if selected_buttons and each.attribute == selected_buttons[0]:
				each.is_selected = False
				number_sprites_group = each.do(game24_gen, getNumberSpritesGroup, number_sprites_group, button_sprites_group)
				selected_buttons = []
			each.draw(screen, pygame.mouse.get_pos())
		# 遊戲勝利
		if is_win:
			showInfo('Congratulations', screen)
		# 遊戲失敗
		if not is_win and len(game24_gen.numbers_now) == 1:
			showInfo('Game Over', screen)
		pygame.display.flip()
		clock.tick(30)

到此這篇關於詳解如何利用Python制作24點小遊戲的文章就介紹到這瞭,更多相關Python24點遊戲內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: