首页IT科技python中秋编程(用Python做一个中秋节嫦娥投食小游戏《千里婵娟》)

python中秋编程(用Python做一个中秋节嫦娥投食小游戏《千里婵娟》)

时间2025-05-04 12:20:09分类IT科技浏览3703
导读:今天给大家带来的是给玉兔投喂月饼的小游戏。八月十五中秋夜晚,让我们对着月亮许愿:希望我们在意和在意我们的人,诸邪避退、百事无忌、平安喜乐、万事胜意。提前祝大家中秋节快乐。...

今天给大家带来的是给玉兔投喂月饼的小游戏            。八月十五中秋夜晚            ,让我们对着月亮许愿:希望我们在意和在意我们的人               ,诸邪避退            、百事无忌               、平安喜乐     、万事胜意               。提前祝大家中秋节快乐     。

中秋节的起源

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

游戏设计

1       、游戏背景

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

2      、功能设计

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

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

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

血条:HP

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

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

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

效果展示

兔兔是不是很可爱嘞!

代码素材

环境如下

python 3.8.0 pygame 2.1.0

代码

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

import pygame import random# 全部源码和素材都在这个群了 279199867 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

bj.jpg

bomb.png

coin.png

start.jpg

over.jpg

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

hp.png

btn_up.png

btn_down.png

bbtn_up.png

bbtn_down.png

结束语

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

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

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

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

最后给大家分享一套Python教程:代码总是学完就忘记?100个爬虫实战项目!让你沉迷学习丨学以致用丨下一个Python大神就是你!

声明:本站所有文章         ,如无特殊说明或标注                ,均为本站原创发布   。任何个人或组织       ,在未征得本站同意时      ,禁止复制            、盗用、采集               、发布本站内容到任何网站               、书籍等各类媒体平台            。如若本站内容侵犯了原著者的合法权益                 ,可联系我们进行处理               。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
如何提高网站的优化速度(优化网站的技巧) js切换选项卡(前端实现tab栏切换,这么常见的案例你学会了吗?)