Rabbit Eats Mooncake Game Tutorial Using Python Pygame
This article walks through creating a simple Python pygame game where a rabbit moves with WSAD or arrow keys to eat stationary mooncakes, gaining weight, with collision detection, dynamic mooncake spawning, and a game‑over condition when the rabbit’s weight exceeds a threshold, including full source code.
This tutorial demonstrates how to build a small arcade‑style game with Python's pygame library. The player controls a rabbit using the WSAD keys or arrow keys, guiding it to eat stationary mooncakes that appear on the screen.
Each mooncake remains at a fixed position after being generated; when the rabbit collides with a mooncake, the mooncake disappears, the rabbit’s weight increases, and new mooncakes are spawned to keep the total count around a defined limit.
The implementation starts by importing the required modules and initializing the pygame window:
import sys
import pygame
width = 800
height = 800
# Initialize all pygame modules
pygame.init()
# Create the main window
windows = pygame.display.set_mode((width, height))
pygame.display.set_caption('兔子吃月饼!!')
while True:
windows.fill((204, 204, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()The Rabbit class defines the player character, storing its position, size, image, movement flags, speed, and weight:
class Rabbit:
"""玩家兔子类"""
def __init__(self, top, left, height, width):
self.top = top
self.left = left
self.height = height
self.width = width
self.rect = pygame.Rect(self.left, self.top, self.width, self.height)
self.player_image = pygame.image.load('兔子.png')
self.player_stretched_image = pygame.transform.scale(self.player_image, (height, width))
self.move_left = False
self.move_right = False
self.move_up = False
self.move_down = False
self.MOVESPEED = 5
self.weight = 5
def move(self):
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.MOVESPEEDThe MoonCake class creates mooncake objects with random positions and randomly chosen images:
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)))The main game loop handles input, updates the rabbit’s movement flags, draws the rabbit and mooncakes, and checks for collisions. When a collision occurs, the mooncake is removed and the rabbit’s weight is increased:
def game_run():
global width, height
end = False
clock = pygame.time.Clock()
moon_cake_limit = 20
player = Rabbit(300, 100, 64, 64)
moon_cakes = [MoonCake() for _ in range(20)]
while 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_left = True
player.move_right = False
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
player.move_right = True
player.move_left = False
if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]:
player.move_up = True
player.move_down = False
if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]:
player.move_down = True
player.move_up = False
player.move()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
for moon_cake in moon_cakes:
windows.blit(moon_cake.moon_cake_image, moon_cake.rect)
windows.blit(player.player_stretched_image, player.rect)
for moon_cake in moon_cakes[:]:
if player.rect.colliderect(moon_cake.rect):
moon_cakes.remove(moon_cake)
player.weight += 2
if len(moon_cakes) < moon_cake_limit:
moon_cakes.append(MoonCake())
pygame.display.flip()
clock.tick(40)A game‑over condition is added: when the rabbit’s weight exceeds a defined threshold (e.g., 10 kg), the loop stops and a “Game Over” screen is displayed. Pressing any key resets the game state, recreating the rabbit and a fresh set of mooncakes.
The article also includes screenshots of the running game and a disclaimer that the content is collected from the internet, with copyright belonging to the original author.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.