首頁 > 軟體

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

2022-03-05 19:00:38

先睹為快

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點遊戲內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com