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

Mini Game Code sharing of Rabbit eating Moon Cake realized by python pygame

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the "python pygame implementation of rabbit eating mooncakes Mini Game code sharing" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, then 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!

Catalogue

A brief introduction to Mini Game rules

Realize

Initialize the game window

Game logic

Implement the player class

Realize mooncakes

Interactive logic

The Mid-Autumn Festival is approaching, to the bosses of the whole rabbit to eat moon cakes Mini Game to boost the fun, nonsense, open and complete.

A brief introduction to Mini Game rules

Players through the "wsad" or "↑↓←→" keys to control the rabbit movement, so that the rabbit can eat more moon cakes, once generated after the location will not change, will not disappear, waiting for the rabbit to eat, it is as simple as that. But eating moon cakes will get heavier, heavy to a certain extent will have an unexpected effect.

Realize

Using Python pygame module development, pygame is used to develop game software Python third-party library, based on SDL library development. You can create feature-rich games and multimedia programs, which is very suitable for developing Mini Game.

Initialize the game window

Import sysimport pygamewidth = 800height = 80 initialize all pygame modules pygame.init () # create the game main window width * heightwindows = pygame.display.set_mode ((width, height)) pygame.display.set_caption ('Rabbit eats moon cakes!') # Game cycle while True: # fill the screen with color windows.fill ((204,204,255)) for event in pygame.event.get (): # determine whether the event is an exit event, then exit if event.type = = pygame.QUIT: # exit the pygame window first Then exit the program pygame.quit () sys.exit () pygame.display.flip ()

Game logic

Implement the player class

The player should include the following attributes: position, character size, score, etc., and there should be a way to move the position move. Because the player controls a rabbit and plays by eating moon cakes, the score is changed to weight.

Class Rabbit: "" player rabbit "def _ init__ (self, top, left, height, width): # initial position and size self.top = top self.left = left self.height = height self.width = width # initial position self.rect = pygame.Rect (self.left, self.top, self.width Self.height) self.player_image = pygame.image.load ('Rabbit .png') self.player_stretched_image = pygame.transform.scale (self.player_image, (height) Width) # controls the movement variable self.move_left = False self.move_right = False self.move_up = False self.move_down = False # the size of each move self.MOVESPEED = 5 # weight self.weight = 5 def move (self): "" controls the movement Return: "if self.move_down and self.rect.bottom

< height: self.rect.top += self.MOVESPEED self.rect.bottom += self.MOVESPEED if self.move_up and self.rect.top >

0: self.rect.top-= self.MOVESPEED self.rect.bottom-= self.MOVESPEED if self.move_left and self.rect.left > 0: self.rect.left-= self.MOVESPEED self.rect.right-= self.MOVESPEED if self.move_right and self.rect.right

< width: self.rect.left += self.MOVESPEED self.rect.right += self.MOVESPEED实现月饼类 月饼包括月饼的位置大小,及月饼的图像,月饼的图像在已有的图像中随机选择。 class MoonCake: """ 月饼类 """ def __init__(self): # 位置及大小 self.rect = pygame.Rect(random.randint(0, 750), random.randint(0, 750), 20, 20) # 图像 self.moon_cake_image = pygame.image.load("./月饼/月饼{}.png".format(random.randint(1, 8)))交互逻辑 主要在游戏窗口内生成并显示玩家兔子和月饼,然后监听键盘事件,监听"wsad"或者"↑↓←→"键,来控制兔子上下左右的进行移动,再实现碰撞检测来验证兔子是否吃到月饼,吃到月饼则重量增加。运行过程中也会不断判断月饼的数量,并不断增加。 def game_run(): global width global height # 是否结束 end = False # 创建时钟对象 (可以控制游戏循环频率) clock = pygame.time.Clock() # 月饼计数器 moon_cake_limit = 20 player = None if not player: player = Rabbit(300, 100, 64, 64) moon_cakes = [] for i in range(20): moon_cake = MoonCake() moon_cakes.append(moon_cake) # 游戏循环 while True and not end: # 给屏幕填充颜色 windows.fill((204, 204, 255)) # 监听键盘事件 key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]: player.move_right = False player.move_left = True if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]: player.move_left = False player.move_right = True if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]: player.move_down = False player.move_up = True if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]: player.move_up = False player.move_down = True player.move() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYUP: if event.key == K_ESCAPE: pygame.quit() sys.exit() if event.key == K_LEFT or event.key == K_a: player.move_left = False if event.key == K_RIGHT or event.key == K_d: player.move_right = False if event.key == K_UP or event.key == K_w: player.move_up = False if event.key == K_DOWN or event.key == K_s: player.move_down = False # 月饼不足20时生成新的月饼 if len(moon_cakes) < moon_cake_limit: # 生成新的月饼 moon_cakes.append(MoonCake()) # 画兔子 windows.blit(player.player_stretched_image, player.rect) # 显示兔子的重量 text = pygame.font.SysFont("microsoftyaheimicrosoftyaheiui", 30) text_fmt = text.render("重量:{}斤!".format(player.weight), True, (255, 255, 255)) windows.blit(text_fmt, (0, 0)) # 画月饼 for moon_cake in moon_cakes: windows.blit(moon_cake.moon_cake_image, moon_cake.rect) # 碰撞检测 for moon_cake in moon_cakes: if player.rect.colliderect(moon_cake.rect): moon_cakes.remove(moon_cake) # 吃到月饼兔子会变重 player.weight += 2 pygame.display.flip() pygame.display.update() # 通过时钟对象指定循环频率 clock.tick(40) 运行如下: 突然发现,这样的话这个游戏永远不会结束,可以一直玩下去,上面我们说了,兔子吃到月饼重量会增加,那我们增加一个机制:等兔子的重量大于100斤的时候,游戏结束,对!你没有看错!,就是这么草率!!! 在碰撞检测循环里加入以下逻辑: if player.weight >

10: end = True while end: # Game over windows.fill ((204,204,255)) text = pygame.font.SysFont ("microsoftyaheimicrosoftyaheiui", 50) text_fmt1 = text.render ("your rabbit is dead!" ! ".format (player.weight), True, (255,255,255) text_fmt2 = text.render (" game over! ".format (player.weight), True, (255,255,255)) windows.blit (text_fmt1, (200,200)) windows.blit (text_fmt2, Clock.tick (40) for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () sys.exit () # press any key to continue if event.type = = pygame.KEYDOWN: end = False # Regenerate the player's rabbit and moon cake player = Rabbit (random.randint (0) Random.randint (0750), 64,64) moon_cakes.clear () for i in range (20): moon_cake = MoonCake () moon_cakes.append (moon_cake) pygame.display.flip () pygame.display.update ()

In order to facilitate debugging, the rabbit weighs more than 10 jin, that is, the end.

The operation is as follows:

"python pygame implementation of rabbit eating mooncakes Mini Game code sharing" content is introduced here, thank you for 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