SoFunction
Updated on 2024-11-15

Based on Python to write a Mid-Autumn Festival Chang'e throwing small game

The mountains and rivers are far away, the fireworks are on the earth, another year, a thousand miles to go~

Today we bring you a small game of feeding mooncakes to the Jade Rabbit. On the night of the Mid-Autumn Festival, August 15, let's make a wish to the moon: I wish that all evils will be avoided, all things will be free from fear, and we will be safe and happy, and all things will prevail. I wish everyone a happy Mid-Autumn Festival in advance.

Origin of the Mid-Autumn Festival

The Mid-Autumn Festival originated in the ancient times, became popular in the Han Dynasty, was finalized in the early years of the Tang Dynasty, and flourished after the Song Dynasty. The Mid-Autumn Festival is a synthesis of the seasonal customs of autumn, and most of the festivals and customs it contains have ancient origins. The Mid-Autumn Festival has become a colorful and precious cultural heritage, as the full moon signifies the reunion of people, and it is a way to express one's longing for one's hometown and relatives, and to pray for a good harvest and happiness.

game design

1、Game background

At the beginning of the story, when the Mid-Autumn Festival arrived, the Jade Rabbit was driven down to the earth because of playfulness. Looking up to the sky, he would always unconsciously think of Su Shi's lyrics: "Wishing for a long time to come, and thousands of miles to be together". This is the most tender and poetic festival of the year, but it is a pity that the Jade Rabbit and Chang'e can't get together this year, but Chang'e, in order to prevent the Jade Rabbit from going hungry, on the night of the Mid-Autumn Festival on the 15th day of the 8th month, Chang'e throws mooncakes for the Jade Rabbit on the moon ...... In order to let the Jade Rabbit eat more, Chang'e opens the universal feeding, as long as you give the blogger a three-link to the Jade Rabbit, you can feed the Jade Rabbit! Mooncakes! Can't you bear to see the cute Jade Rabbit go hungry?

2、Functional design

Characters: The Jade Rabbit uses the mouse to control left and right movements

Moon Cake: Randomly land from the upper world to the lower world, when it encounters the Jade Rabbit, 10 points are added, and 5 points are subtracted when it lands in the lower world.

Moon: randomly lands from the upper realm to the lower realm, and when it encounters the Jade Rabbit, it subtracts 5 points and its blood bar is reduced by one.

Bloodstain: HP

The value is 3 squares, the life is full, and when it touches the moon, it is reduced by 1 square. The game ends when the value is reduced to 0.

Start: Start button, mouse click to start the game!

Start over: Start over button, mouse click to start over.

Effective demonstration

The rabbit is not very cute!

Code material

The environment is as follows

python 3.8.0

pygame 2.1.0

coding

See if the material address is consistent, if not, change it to the local address of the material.

import pygame
import random
 
()
sc = .set_mode((600, 695))
.set_caption("The Jade Rabbit eats mooncakes - Ape Kids Learn wishes everyone a Happy Mid-Autumn Festival!")
basket = ("pic/")
bj = ("pic/")
bomb = ("pic/")
coin = ("pic/")
start = ("pic/")
over = ("pic/")
ihp = ("pic/")
btn_up = ("pic/btn_up.png")
btn_down = ("pic/btn_down.png")
bbtn_up = ("pic/bbtn_up.png")
bbtn_down = ("pic/bbtn_down.png")
word = "HP"
font = ("", 32)
text = (word, True, (75, 217, 65))
score = 0
text1 = (str(score), True, (255, 255, 255))
bx = 0
lx, ly = [], []
fx, fy = [], []
speedy = 1
hp = 4
# Sequences for mooncake generation, through which mooncakes can be generated continuously
for i in range(0, 4):
    tx = (0, 586)
    ty = (i - 1) * 150
    (tx)
    (ty)
 
# Sequences generated by the moon
for i in range(0, 2):
    x = (0, 586)
    y = (i - 1) * 300
    (x)
    (y)
 
 
# Button classes and button click events
class Button(object):
    def __init__(self, btn_up, btn_down, position):
        self.btn_up = btn_up
        self.btn_down = btn_down
         = position
 
    def isOver(self):
        point_x, point_y = .get_pos()
        x, y = 
        w, h = self.btn_down.get_size()
 
        in_x = x - w / 2 < point_x < x + w / 2
        in_y = y - h / 2 < point_y < y + h / 2
        return in_x and in_y
 
    def isPressed(self):
        if  == :
            point_x, point_y = .get_pos()
            x, y = 
            w, h = self.btn_down.get_size()
            in_x = x - w / 2 < point_x < x + w / 2
            in_y = y - h / 2 < point_y < y + h / 2
            return True
 
    def render(self):
        w, h = self.btn_up.get_size()
        x, y = 
 
        if ():
            (self.btn_down, (x - w / 2, y - h / 2))
        else:
            (self.btn_up, (x - w / 2, y - h / 2))
 
 
button = Button(btn_up, btn_down, (288, 460))
 
bbutton = Button(bbtn_up, bbtn_down, (288, 460))
 
while True:
    for event in ():
        if  == :
            exit()
    # Game start screen
    (start, (0, 0))
    ()
    if ():
        hp = 3
        score = 0
        text1 = (str(score), True, (255, 255, 255))
    # Going into the game
    if hp > 0 and hp < 4 and score >= 0:
        (bj, (0, 0))
        (text, (10, 583))
        (text1, (570, 570))
        (basket, (bx, 540))
        # Changes in difficulty
        if score <= 50:
            speedy = 0.4
        if score > 100:
            speedy = 0.8
        if score > 150:
            speedy = 1.2
        if score > 200:
            speedy = 1.6
        for i in range(len(lx)):
            (coin, (lx[i], ly[i] - 600))
            ly[i] += speedy
            if ly[i] > 610 + 600:
                ly[i] = 600
                lx[i] = (0, 540)
                score -= 5
                text1 = (str(score), True, (255, 255, 255))
            # The hare is 62 wide by 48 high #
            # Collision judgment
            if lx[i] + 24 > bx and \
                    lx[i] + 24 < bx + 62 and \
                    ly[i] >= 1120 and \
                    ly[i] <= 1140:
                ly[i] = 600
                lx[i] = (0, 586)
                score += 10
                text1 = (str(score), True, (255, 255, 255))
        for i in range(len(fx)):
            (bomb, (fx[i], fy[i] - 600))
            fy[i] += speedy
            if fy[i] > 610 + 600:
                fy[i] = 600
                fx[i] = (0, 545)
 
            if fx[i] + 24 > bx and \
                    fx[i] + 24 < bx + 62 and \
                    fy[i] >= 1120 and \
                    fy[i] <= 1140:
                hp -= 1
                fy[i] = 600
                fx[i] = (0, 586)
 
        # Basket follows mouse movement
        if  == :
            mx, my = .get_pos()
            bx = mx - 24
        if bx < 0:
            bx = 0
        if bx > 610 - 62:
            bx = 548
        # Basket control via keyboard
        keys = .get_pressed()
        if keys[pygame.K_a] or \
                keys[pygame.K_RIGHT]:
            bx += 5
        if keys[pygame.K_d] or \
                keys[pygame.K_LEFT]:
            bx += -5
        for i in range(0, hp):
            (ihp, (22 * i + 40, 585))
 
    # Restart the game
    if hp == 0 or score < 0:
        # Reinitialize the game
        bx = 0
        speedy = 1
        # Sequences generated by mooncakes
        for i in range(len(lx)):
            lx[i] = (0, 586)
            ly[i] = (i - 1) * 150
 
        # Sequences generated by the moon
        for i in range(len(fx)):
            fx[i] = (0, 586)
            fy[i] = (i - 1) * 300
        (over, (0, 0))
        ()
        # Restart the game after clicking the button
        if ():
            hp = 3
            score = 0
            text1 = (str(score), True, (255, 255, 255))
    ()

source material (in literature and art)

The box is the name of the picture, you can find your favorite material on the Internet to replace, the name of the same can be, there are some below also provides more than one choice.

The material is placed in the pic folder with the following material:

I don't want to look for it here, so it's the same, and you can change it to your favorite background.

btn_up.png

btn_down.png

bbtn_up.png

bbtn_down.png

This article on Python based on writing a Mid-Autumn Festival Chang'e food game is introduced to this article, more related Python Chang'e food game content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future!