In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use Python script to achieve Motta Mini Game". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "how to use Python script to achieve Motta Mini Game" can help you solve the problem.
Development tools
Python version: 3.7.4
Related modules:
Cpgames module
And some modules that come with python.
Environment building
Install Python and add it to the environment variable, and pip installs the relevant modules you need.
Brief introduction of principle
In this issue, we implement some features that have not been implemented before, as well as do some functional optimization (part of the content for testing convenience, I will set the character to invincible state). First of all, it is the prompt effect of game events such as picking items. The core code is as follows:
Def showinfo (self, screen): if self.obtain_tips is None: return self.show_obtain_tips_count + = 1 if self.show_obtain_tips_count > self.max_obtain_tips_count: self.show_obtain_tips_count = 0 self.obtain_tips = None # frame left, top = self.cfg.BLOCKSIZE / / 2,100 width Height = self.cfg.SCREENSIZE [0] / / self.cfg.BLOCKSIZE-1,2 pygame.draw.rect (screen, (1999,97,20), (left-4, top-4, self.cfg.BLOCKSIZE * width + 8, self.cfg.BLOCKSIZE * height + 8) 7) for col in range (width): for row in range (height): image = self.resource_loader.images ['mapelements'] [' 0'] [0] image = pygame.transform.scale (image, (self.cfg.BLOCKSIZE, self.cfg.BLOCKSIZE)) screen.blit (image, (left + col * self.cfg.BLOCKSIZE) Top + row * self.cfg.BLOCKSIZE) # text if isinstance (self.obtain_tips, list): assert len (self.obtain_tips) = = 2 font = pygame.font.Font (self.fontpath, 30) font_render1 = font.render (self.obtain_tips [0], True, (255,255,255) font_render2 = font.render (self.obtain_tips [1], True, (255,255) Rect1 = font_render1.get_rect () rect2 = font_render2.get_rect () rect1.midtop = left + width * self.cfg.BLOCKSIZE / / 2, top + 10 rect2.midtop = left + width * self.cfg.BLOCKSIZE / / 2, top + 10 + self.blocksize screen.blit (font_render1, rect1) screen.blit (font_render2, rect2) else: font = pygame.font.Font (self.fontpath) 40) font_render = font.render (self.obtain_tips, True, (255,255,255)) rect = font_render.get_rect () rect.midtop = left + width * self.cfg.BLOCKSIZE / / 2, top + height * self.cfg.BLOCKSIZE / / 2-20 screen.blit (font_render, rect)
Effect:
Def showbuyinterface (self, screen, scenes, shop_type): # purchase function def buy (hero, coins_cost=0, experience_cost=0, add_life_value=0, add_attack_power=0, add_defense_power=0, add_level=0, add_yellow_keys=0, add_purple_keys=0, add_red_keys=0): if hero.num_coins
< coins_cost: return if hero.experience < experience_cost: return if add_yellow_keys < 0 and hero.num_yellow_keys < 1: return if add_purple_keys < 0 and hero.num_purple_keys < 1: return if add_red_keys < 0 and hero.num_red_keys < 1: return hero.num_coins -= coins_cost hero.experience -= experience_cost hero.life_value += add_life_value + 1000 * add_level hero.attack_power += add_attack_power + 7 * add_level hero.defense_power += add_defense_power + 7 * add_level hero.level += add_level hero.num_yellow_keys += add_yellow_keys hero.num_purple_keys += add_purple_keys hero.num_red_keys += add_red_keys # 选项定义 # --第三层商店 if self.map_level_pointer == 3 and shop_type == 'buy_from_shop': choices_dict = { '增加 800 点生命(25 金币)': lambda: buy(self.hero, coins_cost=25, add_life_value=800), '增加 4 点攻击(25 金币)': lambda: buy(self.hero, coins_cost=25, add_attack_power=4), '增加 4 点防御(25 金币)': lambda: buy(self.hero, coins_cost=25, add_defense_power=4), '离开商店': lambda: buy(self.hero), } id_image = self.resource_loader.images['mapelements']['22'][0] # --第十一层商店 elif self.map_level_pointer == 11 and shop_type == 'buy_from_shop': choices_dict = { '增加 4000 点生命(100 金币)': lambda: buy(self.hero, coins_cost=100, add_life_value=4000), '增加 20 点攻击(100 金币)': lambda: buy(self.hero, coins_cost=100, add_attack_power=20), '增加 20 点防御(100 金币)': lambda: buy(self.hero, coins_cost=100, add_defense_power=20), '离开商店': lambda: buy(self.hero), } id_image = self.resource_loader.images['mapelements']['22'][0] # --第五层神秘老人 elif self.map_level_pointer == 5 and shop_type == 'buy_from_oldman': choices_dict = { '提升一级(100 经验)': lambda: buy(self.hero, experience_cost=100, add_level=1), '增加 5 点攻击(30 经验)': lambda: buy(self.hero, experience_cost=30, add_attack_power=5), '增加 5 点防御(30 经验)': lambda: buy(self.hero, experience_cost=30, add_defense_power=5), '离开商店': lambda: buy(self.hero), } id_image = self.resource_loader.images['mapelements']['26'][0] # --第十三层神秘老人 elif self.map_level_pointer == 13 and shop_type == 'buy_from_oldman': choices_dict = { '提升三级(270 经验)': lambda: buy(self.hero, experience_cost=270, add_level=1), '增加 17 点攻击(95 经验)': lambda: buy(self.hero, experience_cost=95, add_attack_power=17), '增加 17 点防御(95 经验)': lambda: buy(self.hero, experience_cost=95, add_defense_power=17), '离开商店': lambda: buy(self.hero), } id_image = self.resource_loader.images['mapelements']['26'][0] # --第五层商人 elif self.map_level_pointer == 5 and shop_type == 'buy_from_businessman': choices_dict = { '购买 1 把黄钥匙(10 金币)': lambda: buy(self.hero, coins_cost=10, add_yellow_keys=1), '购买 1 把蓝钥匙(50 金币)': lambda: buy(self.hero, coins_cost=50, add_purple_keys=1), '购买 1 把红钥匙(100 金币)': lambda: buy(self.hero, coins_cost=100, add_red_keys=1), '离开商店': lambda: buy(self.hero), } id_image = self.resource_loader.images['mapelements']['27'][0] # --第十二层商人 elif self.map_level_pointer == 12 and shop_type == 'buy_from_businessman': choices_dict = { '卖出 1 把黄钥匙(7 金币)': lambda: buy(self.hero, coins_cost=-7, add_yellow_keys=-1), '卖出 1 把蓝钥匙(35 金币)': lambda: buy(self.hero, coins_cost=-35, add_purple_keys=-1), '卖出 1 把红钥匙(70 金币)': lambda: buy(self.hero, coins_cost=-70, add_red_keys=-1), '离开商店': lambda: buy(self.hero), } id_image = self.resource_loader.images['mapelements']['27'][0] id_image = pygame.transform.scale(id_image, (self.cfg.BLOCKSIZE, self.cfg.BLOCKSIZE)) # 主循环 clock, selected_idx = pygame.time.Clock(), 1 font = pygame.font.Font(self.cfg.FONT_PATHS_NOPRELOAD_DICT['font_cn'], 20) while True: screen.fill((0, 0, 0)) screen.blit(self.background_images['gamebg'], (0, 0)) self.map_parser.draw(screen) for scene in scenes: screen.blit(scene[0], scene[1]) self.hero.draw(screen) # --按键检测 for event in pygame.event.get(): if event.type == pygame.QUIT: QuitGame() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: list(choices_dict.values())[selected_idx-1]() if selected_idx == 4: return elif event.key == pygame.K_w or event.key == pygame.K_UP: selected_idx = max(selected_idx - 1, 1) elif event.key == pygame.K_s or event.key == pygame.K_DOWN: selected_idx = min(selected_idx + 1, 4) # --对话框 # ----底色 width, height = 8, 3 left, bottom = self.hero.rect.left + self.hero.rect.width // 2 - width // 2 * self.cfg.BLOCKSIZE, self.hero.rect.bottom for col in range(width): for row in range(height): image = self.resource_loader.images['mapelements']['0'][0] image = pygame.transform.scale(image, (self.cfg.BLOCKSIZE, self.cfg.BLOCKSIZE)) screen.blit(image, (left + col * self.cfg.BLOCKSIZE, bottom + row * self.cfg.BLOCKSIZE)) # ----边框 pygame.draw.rect(screen, (199, 97, 20), (left - 4, bottom - 4, self.cfg.BLOCKSIZE * width + 8, self.cfg.BLOCKSIZE * height + 8), 7) # ----展示选项 for idx, choice in enumerate(['请选择:'] + list(choices_dict.keys())): if selected_idx == idx and idx >0: choice = '➤' + choice font_render = font.render (choice, True, (255,0,0) elif idx > 0: choice =''+ choice font_render = font.render (choice, True, (255,255,255)) else: font_render = font.render (choice, True, 255,255) rect = font_render.get_rect () rect.left, rect.top = left + self.cfg.BLOCKSIZE + 20, bottom + 10 + idx * 30 screen.blit (font_render, rect) #-display avatar screen.blit (id_image, (left + 10) Bottom + 10)) #-refresh pygame.display.flip () clock.tick (self.cfg.FPS)
That is, there are mainly three types of stores: one is an open shop that trades with gold coins to increase its health, attack and defense; one is a merchant who trades with gold coins and can obtain / sell keys of different colors; and there is a mysterious old man who trades with experience and can get hints of grade, attack and defense. The effect is as follows
Then, let's realize the special effects of some treasures that can be found in the map, including the compass of the wind, the holy emblem of light, the nut of the star god and the cross of luck.
Among them, the compass of the wind is used to jump between the floors that have been passed. The code is as follows:
Def showjumplevel (self, screen, scenes): # main loop clock, selected_level = pygame.time.Clock (), 1 font = pygame.font.Font (self.cfg.FONT_PATHS_NOPRELOAD_DICT ['font_cn'], 20) while True: screen.fill ((0,0,0) screen.blit (self.background_images [' gamebg'], (0) 0) self.map_parser.draw (screen) for scene in scenes: screen.blit (scene [0] Scene [1]) self.hero.draw (screen) #-- key detection for event in pygame.event.get (): if event.type = = pygame.QUIT: QuitGame () elif event.type = = pygame.KEYDOWN: if event.key = = pygame.K_SPACE: return selected_level Elif event.key = = pygame.K_w or event.key = = pygame.K_UP: selected_level = max (selected_level-1 0) elif event.key = = pygame.K_s or event.key = = pygame.K_DOWN: selected_level = min (selected_level + 1, self.max_map_level_pointer) #-dialog #-background width, height = 11,4 left, top = self.cfg.SCREENSIZE [0] / / 2-width / / 2 * self.cfg.BLOCKSIZE Self.cfg.SCREENSIZE [1] / / 2-height * self.cfg.BLOCKSIZE for col in range (width): for row in range (height): image = self.resource_loader.images ['mapelements'] [' 0'] [0] image = pygame.transform.scale (image, (self.cfg.BLOCKSIZE, self.cfg.BLOCKSIZE)) screen.blit (image (left + col * self.cfg.BLOCKSIZE, top + row * self.cfg.BLOCKSIZE) #-Border pygame.draw.rect (screen, (1999,97,20), (left-4, top-4, self.cfg.BLOCKSIZE * width + 8, self.cfg.BLOCKSIZE * height + 8) 7) #-display options for idx in list (range (self.max_map_level_pointer+1)): if selected_level = = idx: text = f '➤ layer {idx}' font_render = font.render (text, True, (255,0) 0) else: text = f 'layer {idx}' font_render = font.render (text, True, (255,255,255)) rect = font_render.get_rect () rect.left, rect.top = left + 20 + idx / / 6 * self.cfg.BLOCKSIZE * 2 Top + 20 + (idx% 6) * 30 screen.blit (font_render, rect) #-refresh pygame.display.flip () clock.tick (self.cfg.FPS)
Effect:
Then there is the holy emblem, which is used to view the basic situation of the monster. The code is implemented as follows:
Def showforecastlevel (self, screen, scenes): # main loop clock = pygame.time.Clock () font = pygame.font.Font (self.cfg.FONT_PATHS_NOPRELOAD_DICT ['font_cn'], 20) monsters = self.map_parser.getallmonsters () if len (monsters)
< 1: return monsters_show_pointer, max_monsters_show_pointer = 1, round(len(monsters) / 4) show_tip_text, show_tip_text_count, max_show_tip_text_count = True, 1, 15 return_flag = False while True: screen.fill((0, 0, 0)) screen.blit(self.background_images['gamebg'], (0, 0)) self.map_parser.draw(screen) for scene in scenes: screen.blit(scene[0], scene[1]) self.hero.draw(screen) # --按键检测 for event in pygame.event.get(): if event.type == pygame.QUIT: QuitGame() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_l: return_flag = True elif event.key == pygame.K_SPACE: monsters_show_pointer = monsters_show_pointer + 1 if monsters_show_pointer >Max_monsters_show_pointer: monsters_show_pointer = 1 elif event.type = = pygame.KEYUP: if event.key = = pygame.K_l and return_flag: return #-dialog #-background width, height = 14,5 left, top = self.cfg.SCREENSIZE [0] / / 2-width / / 2 * self.cfg.BLOCKSIZE Self.cfg.SCREENSIZE [1] / / 2-height * self.cfg.BLOCKSIZE for col in range (width): for row in range (height): image = self.resource_loader.images ['mapelements'] [' 0'] [0] image = pygame.transform.scale (image, (self.cfg.BLOCKSIZE, self.cfg.BLOCKSIZE)) screen.blit (image (left + col * self.cfg.BLOCKSIZE, top + row * self.cfg.BLOCKSIZE) #-Border pygame.draw.rect (screen, (1999,97,20), (left-4, top-4, self.cfg.BLOCKSIZE * width + 8, self.cfg.BLOCKSIZE * height + 8), 7) #-display option for idx Monster in enumerate (monsters [(monsters_show_pointer-1) * 4: monsters_show_pointer*4]): id_image = self.resource_loader.images ['mapelements'] [monster [6]] [0] id_image = pygame.transform.scale (id_image, (self.cfg.BLOCKSIZE-10, self.cfg.BLOCKSIZE-10)) screen.blit (id_image, (left + 10) Top + 20 + idx * self.cfg.BLOCKSIZE) text = f' name: {monster [0]} Life: {monster [1]} attack: {monster [2]} Defense: {monster [3]} Gold Coin: {monster [4]} experience: {monster [5]} loss: {self.hero.winmonster (monster) [1]} 'font_render = font.render (text, True, (255,255) Rect = font_render.get_rect () rect.left, rect.top = left + 15 + self.cfg.BLOCKSIZE, top + 30 + idx * self.cfg.BLOCKSIZE screen.blit (font_render) Rect) #-Operation Tip show_tip_text_count + = 1 if show_tip_text_count = = max_show_tip_text_count: show_tip_text_count = 1 show_tip_text = not show_tip_text if show_tip_text: tip_text = 'Spacebar' font_render = font.render (tip_text, True, 255,255) rect.left, rect.bottom = self.cfg.BLOCKSIZE * width + 30, self.cfg.BLOCKSIZE * (height + 1) + 10 screen.blit (font_render, rect) #-Refresh pygame.display.flip () clock.tick (self.cfg.FPS)
Effect:
Then let's implement the Lucky Cross and give it to the fairy in the preface so that we can improve all our abilities (attack defense and health).
# define all conversations if self.hero.has_cross: conversations = [['fairy, I have found that cross.'], ['you have done well. So now I will begin to give you more power! Mira do Mimi beep. 'well, I've improved your ability now!' remember: if you don't have enough strength, don't go to the twenty-first floor. On that floor,' 'The mana of all your treasures will lose its effect.] self.hero.has_cross = False self.hero.life_value = int (self.hero.life_value * 4 / 3) self.hero.attack_power = int (self.hero.attack_power * 4 / 3) self.hero.defense_power = int (self.hero.defense_power * 4 / 3) this is the end of the introduction to "how to use Python scripts to achieve Motta Mini Game". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.