当前位置: 首页 > news >正文

【中秋征文】使用Python中秋节嫦娥投食小游戏《千里婵娟》

 一、前言

        大家好,我是猿童学🐵,“山河远阔,烟火人间,又一年,千里婵娟”。欢迎大家收看第二期的中秋创作,这期给大家带来的是给玉兔投喂月饼的小游戏。八月十五中秋夜晚,让我们对着月亮许愿:希望我们在意和在意我们的人,诸邪避退、百事无忌、平安喜乐、万事胜意。提前祝大家中秋节快乐。

中秋节的起源

        中秋节起源于上古时代,普及于汉代,定型于唐朝初年,盛行于宋朝以后。中秋节是秋季时令习俗的综合,其所包含的节俗因素,大都有古老的渊源。中秋节以月之圆兆人之团圆,为寄托思念故乡,思念亲人之情,祈盼丰收、幸福,成为丰富多彩、弥足珍贵的文化遗产。

二、游戏设计

2.1 游戏背景

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

2.2 功能设计

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

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

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

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

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

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

三、效果展示

兔兔是不是很可爱嘞!

四、代码素材

4.1 代码

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

'''
猿童学出品--2022年9月1日
环境如下:
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()
#猿童学

4.2 素材:

说明:

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

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

basket.png

 bj.jpg

 bomb.png

coin.png

 start.jpg

 over.jpg

 这里不想找了,所以就一样了,大家可以更换成自己喜欢的背景。

hp.png

btn_up.png

 btn_down.png

 bbtn_up.png

 bbtn_down.png

五、结束语

”但愿人长久,千里共婵娟“,比起这对现实无奈的诗词,我更希望是:”但愿人长久,一起过中秋”

中秋是一个月亮的狂欢,一群人月饼的孤单。

如果你愿意一层一层地拨开我的心,你会发现,你会哭泣,因为网上个五仁月饼。🤣🤣🤣

 给个三连吧,祝你诸邪避退、百事无忌、平安喜乐、万事胜意。😊😊😊

英语时间

中秋节:Mid-Autumn Festival

农历:lunar calendar

嫦娥:Chang'e

玉兔:jade rabbit

月饼:mooncake

家庭团聚:family reunion

 往期:

【中秋征文】使用Python创意中秋节画月饼《花好月圆》

【中秋征文】使用Python中秋节嫦娥投食游戏《千里婵娟》

相关文章:

  • OSI网络七层模型和TCP/IP模型
  • 猿创征文|Linux环境Redis部署及最佳实践
  • 猿创征文|C++来时路 _ 重温经典之C++类和对象 | 三大特性之一 - 封装 | 腾讯面试题
  • VueJS面试常见的300道题(英文版)
  • CREO:CREO软件之零件【渲染】之对三维零件实现渲染图文教程之详细攻略
  • Java数据结构之数组的增删改查
  • 函数栈桢原理
  • JSP面试题(重要)
  • 华为FreeBuds pro2大风场景下降噪差原因
  • 网课搜题接口对接教程
  • ORM基本操作
  • 数据结构-压缩软件核心-C++(利用哈夫曼树进行编码,对文件进行压缩与解压缩)
  • SSM学生成绩管理系统毕业设计-附源码070942
  • springboot宴会预定平台毕业设计-附源码231718
  • springboot大学新生小助手小程序毕业设计-附源码060917
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • 2017-09-12 前端日报
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • cookie和session
  • FastReport在线报表设计器工作原理
  • JavaScript函数式编程(一)
  • js面向对象
  • Mysql数据库的条件查询语句
  • node和express搭建代理服务器(源码)
  • orm2 中文文档 3.1 模型属性
  • Python3爬取英雄联盟英雄皮肤大图
  • python学习笔记-类对象的信息
  • Python学习之路16-使用API
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • vue-router的history模式发布配置
  • vue脚手架vue-cli
  • Yeoman_Bower_Grunt
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 那些年我们用过的显示性能指标
  • 入口文件开始,分析Vue源码实现
  • 新版博客前端前瞻
  • 一、python与pycharm的安装
  • 责任链模式的两种实现
  • 怎样选择前端框架
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #include到底该写在哪
  • #QT(串口助手-界面)
  • #预处理和函数的对比以及条件编译
  • $.ajax,axios,fetch三种ajax请求的区别
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (9)STL算法之逆转旋转
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (三)Honghu Cloud云架构一定时调度平台
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)Linux下编译安装log4cxx