Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a gem Xiaole Mini Game with Python

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to write a gem Xiaole Mini Game with Python". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Development tools

Python version: 3.6.4

Related modules:

Pygame; 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

Rules of the game:

Players exchange adjacent puzzles through the mouse, and if there are three consecutive identical puzzles in the horizontal / vertical direction after the exchange, the puzzles disappear, the player scores, and a new puzzle is generated to supplement the missing part, otherwise, the exchange fails and the player does not score. Players need to get the highest possible score within a specified period of time.

Implementation process:

First load some necessary game material:

Load background music:

Pygame.mixer.init () pygame.mixer.music.load (os.path.join (ROOTDIR, "resources/audios/bg.mp3")) pygame.mixer.music.set_volume (0.6) pygame.mixer.music.play (- 1)

Load sound effects:

Sounds = {} sounds ['mismatch'] = pygame.mixer.Sound (os.path.join (ROOTDIR,' resources/audios/badswap.wav')) sounds ['match'] = [] for i in range (6): sounds [' match'] .append (pygame.mixer.Sound (os.path.join (ROOTDIR, 'resources/audios/match%s.wav'% I)

Load fonts:

Font = pygame.font.Font (os.path.join (ROOTDIR, 'resources/font.TTF'), 25)

Image loading:

Gem_imgs = [] for i in range (1,8): gem_imgs.append (os.path.join (ROOTDIR, 'resources/images/gem%s.png'% I))

Then we're going to set up the main loop of the game.

Main cycle:

Game = gemGame (screen, sounds, font, gem_imgs) while True: score = game.start () flag = False

Let me tell you how it works:

In fact, the logic is very simple, which is to constantly detect whether there is a mouse click event, if so, to determine whether the location of the mouse click is within the location area of a puzzle block. If so, the puzzle block is selected, otherwise it is not selected.

When a second jigsaw puzzle block is selected, it is judged whether the two jigsaw puzzle blocks meet the condition of jigsaw puzzle exchange, and if so, the puzzle blocks are exchanged and rewarded, otherwise the selected states of the two jigsaw pieces are not exchanged and deselected.

In the end, it must be to set the end and exit of the game:

After the countdown to the game, enter the end of the game interface, which displays the user's current score. At the same time, if the user types the R key to restart the game, type the ESC key to quit the game.

After the end of the game, the player chooses to reopen or exit: the source code is as follows

While True: for event in pygame.event.get (): if event.type = = pygame.QUIT or (event.type = = pygame.KEYUP and event.key = = pygame.K_ESCAPE): pygame.quit () sys.exit () elif event.type = = pygame.KEYUP and event.key = = pygame.K_r: Flag = True if flag: break screen.fill ( 206235) text0 = 'Final score:% s'% score text1 = 'Press to restart the game.' Text2 = 'Press to quit the game.' Y = 150for idx, text in enumerate ([text0, text1, text2]): text_render = font.render (text, 1, (85,65,0)) rect = text_render.get_rect () if idx = 0: rect.left, rect.top = Y) elif idx = = 1: rect.left, rect.top = (122.5, y) else: rect.left, rect.top = (126.5, y) y + = 100screen.blit (text_render, rect) pygame.display.update () game.reset ()

The above is a step-by-step explanation of the code. I will put the source code below:

Import osimport pygamefrom utils import * from config import *''Game main Program' 'def main (): pygame.init () screen = pygame.display.set_mode ((WIDTH, HEIGHT)) pygame.display.set_caption (' Gemgem-Python Exchange Group: 932574150) # load background music pygame.mixer.init () pygame.mixer.music.load (os.path.join (ROOTDIR) "resources/audios/bg.mp3") pygame.mixer.music.set_volume (0.6) pygame.mixer.music.play (- 1) # load sound sounds = {} sounds ['mismatch'] = pygame.mixer.Sound (os.path.join (ROOTDIR) 'resources/audios/badswap.wav')) sounds [' match'] = [] for i in range (6): sounds ['match'] .append (pygame.mixer.Sound (os.path.join (ROOTDIR,' resources/audios/match%s.wav'% I) # load font font = pygame.font.Font (os.path.join (ROOTDIR, 'resources/font.TTF')) 25) # Image loading gem_imgs = [] for i in range (1,8): gem_imgs.append (os.path.join (ROOTDIR, 'resources/images/gem%s.png'% I)) # main loop game = gemGame (screen, sounds, font) Gem_imgs) while True: score = game.start () flag = False # after the first round of the game, players choose to replay or exit while True: for event in pygame.event.get (): if event.type = = pygame.QUIT or (event.type = = pygame.KEYUP and event.key = = pygame.K_ESCAPE): Pygame.quit () sys.exit () elif event.type = = pygame.KEYUP and event.key = = pygame.K_r: flag = True if flag: break screen.fill 206235) text0 = 'Final score:% s'% score text1 = 'Press to restart the game.' Text2 = 'Press to quit the game.' Y = 150for idx, text in enumerate ([text0, text1, text2]): text_render = font.render (text, 1, (85,65,0)) rect = text_render.get_rect () if idx = 0: rect.left, rect.top = Y) elif idx = = 1: rect.left, rect.top = (122.5, y) else: rect.left, rect.top = (126.5, y) y + = 100screen.blit (text_render) Rect) pygame.display.update () game.reset ()''test'''if _ _ name__ = =' _ _ main__': main () "how to write a gem Xiaoxiaole Mini Game with Python" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report