Python做一个中秋节嫦娥投食小游戏

嗨嗨,大家好,我是小圆。

马上就要到中秋节啦,今天给大家分享一个关于中秋的小游戏!用Python做的中秋节嫦娥投食小游戏

请添加图片描述

游戏设计

1、游戏背景

故事的开始🌕,中秋佳节至,玉兔因为贪玩被赶下人间,抬头望向天际,总是不自觉的会想起苏轼的词:“但愿人久,千里共婵娟”。这是一年中,最温柔又最有诗意的节日,可惜玉兔与嫦娥今年不能相聚,但嫦娥为了不让玉兔饿肚子,在八月十五中秋节的晚上,嫦娥在月球为玉兔投食月饼……🤔为了让玉兔吃到更多,嫦娥开启全民投食😃,只要给博主三连,就能给玉兔投食月饼啦!难道你忍心看到可爱的玉兔🐰饿肚子吗?快点行动起来吧!记得三连噢!

请添加图片描述

2、功能设计

人物:玉兔使用鼠标来控制左右运动

月饼:随机从上界降落至下界,当碰到玉兔时,加10分,落到下界减5分。

月亮:随机从上界降落至下界,当碰到玉兔时,减5分,且血条减一格。

血条:HP

值为3格时,生命为满值,当碰到月亮时减1格。减为0格时,游戏结束。

开始:开始按钮,鼠标点击即开始游戏

重来:重来按钮,鼠标点击即重新开始

效果展示

请添加图片描述
快看看是不是很可爱啊

代码素材
环境如下
  • python 3.8.0
  • pygame 2.1.0
代码

看看素材地址是否一致,不一致要改成素材本地的地址。

import pygame
import random
 
pygame.init()
sc = pygame.display.set_mode((600, 695))
pygame.display.set_caption("玉兔吃月饼——猿童学祝大家中秋节快乐!")
basket = pygame.image.load("pic/basket.png")
bj = pygame.image.load("pic/bj.jpg")
bomb = pygame.image.load("pic/bomb.png")
coin = pygame.image.load("pic/coin.png")
start = pygame.image.load("pic/start.jpg")
over = pygame.image.load("pic/over.jpg")
ihp = pygame.image.load("pic/hp.png")
btn_up = pygame.image.load("pic/btn_up.png")
btn_down = pygame.image.load("pic/btn_down.png")
bbtn_up = pygame.image.load("pic/bbtn_up.png")
bbtn_down = pygame.image.load("pic/bbtn_down.png")
word = "HP"
font = pygame.font.SysFont("", 32)
text = font.render(word, True, (75, 217, 65))
score = 0
text1 = font.render(str(score), True, (255, 255, 255))
bx = 0
lx, ly = [], []
fx, fy = [], []
speedy = 1
hp = 4
# 月饼生成的序列,通过序列可以源源不断生成月饼
for i in range(0, 4):
    tx = random.randint(0, 586)
    ty = (i - 1) * 150
    lx.append(tx)
    ly.append(ty)
 
# 月亮生成的序列
for i in range(0, 2):
    x = random.randint(0, 586)
    y = (i - 1) * 300
    fx.append(x)
    fy.append(y)
 
 
# 按钮类和按钮点击事件
class Button(object):
    def __init__(self, btn_up, btn_down, position):
        self.btn_up = btn_up
        self.btn_down = btn_down
        self.position = position
 
    def isOver(self):
        point_x, point_y = pygame.mouse.get_pos()
        x, y = self.position
        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 event.type == pygame.MOUSEBUTTONDOWN:
            point_x, point_y = pygame.mouse.get_pos()
            x, y = self.position
            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 = self.position
 
        if self.isOver():
            sc.blit(self.btn_down, (x - w / 2, y - h / 2))
        else:
            sc.blit(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 pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    # 游戏开始界面
    sc.blit(start, (0, 0))
    bbutton.render()
    if bbutton.isPressed():
        hp = 3
        score = 0
        text1 = font.render(str(score), True, (255, 255, 255))
    # 进入游戏
    if hp > 0 and hp < 4 and score >= 0:
        sc.blit(bj, (0, 0))
        sc.blit(text, (10, 583))
        sc.blit(text1, (570, 570))
        sc.blit(basket, (bx, 540))
        # 难度变化
        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)):
            sc.blit(coin, (lx[i], ly[i] - 600))
            ly[i] += speedy
            if ly[i] > 610 + 600:
                ly[i] = 600
                lx[i] = random.randint(0, 540)
                score -= 5
                text1 = font.render(str(score), True, (255, 255, 255))
            # 玉兔的宽62 高 48
            # 碰撞判断
            if lx[i] + 24 > bx and \
                    lx[i] + 24 < bx + 62 and \
                    ly[i] >= 1120 and \
                    ly[i] <= 1140:
                ly[i] = 600
                lx[i] = random.randint(0, 586)
                score += 10
                text1 = font.render(str(score), True, (255, 255, 255))
        for i in range(len(fx)):
            sc.blit(bomb, (fx[i], fy[i] - 600))
            fy[i] += speedy
            if fy[i] > 610 + 600:
                fy[i] = 600
                fx[i] = random.randint(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] = random.randint(0, 586)
 
        # 篮子跟随鼠标运动
        if event.type == pygame.MOUSEMOTION:
            mx, my = pygame.mouse.get_pos()
            bx = mx - 24
        if bx < 0:
            bx = 0
        if bx > 610 - 62:
            bx = 548
        # 通过键盘控制篮子
        keys = pygame.key.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):
            sc.blit(ihp, (22 * i + 40, 585))
 
    # 重新开始游戏
    if hp == 0 or score < 0:
        # 重新初始化游戏
        bx = 0
        speedy = 1
        # 月饼生成的序列
        for i in range(len(lx)):
            lx[i] = random.randint(0, 586)
            ly[i] = (i - 1) * 150
 
        # 月亮生成的序列
        for i in range(len(fx)):
            fx[i] = random.randint(0, 586)
            fy[i] = (i - 1) * 300
        sc.blit(over, (0, 0))
        button.render()
        # 点击按钮后重新开始游戏
        if button.isPressed():
            hp = 3
            score = 0
            text1 = font.render(str(score), True, (255, 255, 255))
    pygame.display.update()

素材

框框里的是图片的名字,可以在网上找自己喜欢的素材替换,名字一致即可,下面有一些也提供了多个选择。

素材放在pic文件夹中,有如下材料:

basket.png
请添加图片描述
请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

这些图片都有水印欸,如果需要无水印的可以评论留言或者私信我!!

那其他的图片我就不发啦 ~

最后嘿嘿,祝大家中秋节快乐!

对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦
觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

请添加图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值