詳解如何利用Python制作24點(diǎn)小游戲
先睹為快
游戲規(guī)則(改編自維基百科)
從1~10這十個數(shù)字中隨機(jī)抽取4個數(shù)字(可重復(fù)),對這四個數(shù)運(yùn)用加、減、乘、除和括號進(jìn)行運(yùn)算得出24。每個數(shù)字都必須使用一次,但不能重復(fù)使用。
逐步實(shí)現(xiàn)
Step1:制作24點(diǎn)生成器
既然是24點(diǎn)小游戲,當(dāng)然要先定義一個24點(diǎn)游戲生成器啦。主要思路就是隨機(jī)生成4個有解的數(shù)字,且范圍在1~10之間,代碼實(shí)現(xiàn)如下:
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
在驗(yàn)證4個數(shù)字是否有解并求出所有解部分,我直接暴力枚舉然后去重了,感興趣的同學(xué)可以自己再優(yōu)化一下求解算法(有數(shù)字重復(fù)的時候)。我的代碼如下圖所示,其實(shí)就是遞歸枚舉所有排序然后一一驗(yàn)證是否有解:
'''驗(yàn)證生成的數(shù)字是否有答案''' 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) # 避免有數(shù)字重復(fù)時表達(dá)式重復(fù)(T_T懶得優(yōu)化了) 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 '''計算函數(shù)''' 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:定義游戲精靈類
因?yàn)橥婕倚枰ㄟ^鼠標(biāo)點(diǎn)擊來操作卡片,這時候就涉及到一些碰撞檢測。所以先定義一些必要的游戲精靈類。
①卡片類
卡片類的定義也很簡單,在屏幕上根據(jù)被賦予的屬性值來顯示自身即可。當(dāng)然之后也需要根據(jù)用戶的操作來改變這些屬性值(內(nèi)容、顏色、字體等)并在屏幕上根據(jù)屬性的改變而改變顯示狀態(tài)即可。具體而言代碼實(shí)現(xiàn)如下:
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))
②按鈕類
按鈕類和卡片類類似,唯一的不同點(diǎn)就是在用戶點(diǎn)擊按鈕時需要根據(jù)該按鈕的功能來響應(yīng)用戶的本次點(diǎn)擊操作(即實(shí)現(xiàn)一次該功能)。因此只需要繼承卡片類,然后再定義一個響應(yīng)用戶點(diǎn)擊按鈕事件的回調(diào)函數(shù)即可。代碼實(shí)現(xiàn)如下:
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) '''根據(jù)button function執(zhí)行響應(yīng)操作''' 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:實(shí)現(xiàn)游戲主循環(huán)
先構(gòu)思一下怎么設(shè)計游戲主界面,個人的簡單設(shè)計草圖如下(不是特別走心的設(shè)計草圖T_T):
OK,開搞。先初始化、加載必要的素材和定義必要的變量,代碼實(shí)現(xiàn)如下:
# 初始化, 導(dǎo)入必要的游戲素材 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點(diǎn)游戲生成器 game24_gen = game24Generator() game24_gen.generate() # 精靈組 # --數(shù)字 number_sprites_group = getNumberSpritesGroup(game24_gen.numbers_now) # --運(yùn)算符 operator_sprites_group = getOperatorSpritesGroup(OPREATORS) # --按鈕 button_sprites_group = getButtonSpritesGroup(BUTTONS) # 游戲主循環(huán) clock = pygame.time.Clock() selected_numbers = [] selected_operators = [] selected_buttons = [] is_win = False
游戲主循環(huán)主要分三個部分,首先是按鍵檢測:
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')
根據(jù)檢測結(jié)果更新卡片狀態(tài)和一些變量:
'''檢查控件是否被點(diǎn)擊''' def checkClicked(group, mouse_pos, group_type='NUMBER'): selected = [] # 數(shù)字卡片/運(yùn)算符卡片 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
當(dāng)有兩個數(shù)字和一個運(yùn)算符被點(diǎn)擊時,則執(zhí)行被點(diǎn)擊數(shù)字1{+/-/×/÷}被點(diǎn)擊數(shù)字2操作(數(shù)字1、2根據(jù)點(diǎn)擊順序確定),并進(jìn)一步更新卡片屬性和一些必要的變量:
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)
最后根據(jù)各個卡片的屬性在屏幕上顯示各個卡片,若游戲勝利/游戲失敗,則同時顯示游戲勝利/游戲失敗提示框:
# 精靈都畫到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)
到此這篇關(guān)于詳解如何利用Python制作24點(diǎn)小游戲的文章就介紹到這了,更多相關(guān)Python24點(diǎn)游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python寫一個簡單上課點(diǎn)名系統(tǒng)(附源碼)
這篇文章主要介紹了Python寫一個簡單上課點(diǎn)名系統(tǒng),文章圍繞Python得性概念資料寫一個簡的得上課點(diǎn)名系統(tǒng),并附上詳細(xì)的代碼即過程總結(jié),需要的朋友可以參考一下,希望對你有所幫助2021-11-11PyTorch中torch.utils.data.DataLoader實(shí)例詳解
torch.utils.data.DataLoader主要是對數(shù)據(jù)進(jìn)行batch的劃分,下面這篇文章主要給大家介紹了關(guān)于PyTorch中torch.utils.data.DataLoader的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Python 給屏幕打印信息加上顏色的實(shí)現(xiàn)方法
這篇文章主要介紹了Python 給屏幕打印信息加上顏色的實(shí)現(xiàn)方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04Python3實(shí)現(xiàn)將文件歸檔到zip文件及從zip文件中讀取數(shù)據(jù)的方法
這篇文章主要介紹了Python3實(shí)現(xiàn)將文件歸檔到zip文件及從zip文件中讀取數(shù)據(jù)的方法,涉及Python針對zip文件操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05