基于Python編寫一個中秋節(jié)嫦娥投食小游戲
山河遠闊,煙火人間,又一年,千里嬋娟~
今天給大家?guī)淼氖墙o玉兔投喂月餅的小游戲。八月十五中秋夜晚,讓我們對著月亮許愿:希望我們在意和在意我們的人,諸邪避退、百事無忌、平安喜樂、萬事勝意。提前祝大家中秋節(jié)快樂。
中秋節(jié)的起源
中秋節(jié)起源于上古時代,普及于漢代,定型于唐朝初年,盛行于宋朝以后。中秋節(jié)是秋季時令習俗的綜合,其所包含的節(jié)俗因素,大都有古老的淵源。中秋節(jié)以月之圓兆人之團圓,為寄托思念故鄉(xiāng),思念親人之情,祈盼豐收、幸福,成為豐富多彩、彌足珍貴的文化遺產。
游戲設計
1、游戲背景
故事的開始,中秋佳節(jié)至,玉兔因為貪玩被趕下人間,抬頭望向天際,總是不自覺的會想起蘇軾的詞:“但愿人久,千里共嬋娟”。這是一年中,最溫柔又最有詩意的節(jié)日,可惜玉兔與嫦娥今年不能相聚,但嫦娥為了不讓玉兔餓肚子,在八月十五中秋節(jié)的晚上,嫦娥在月球為玉兔投食月餅……為了讓玉兔吃到更多,嫦娥開啟全民投食,只要給博主三連,就能給玉兔投食月餅啦!難道你忍心看到可愛的玉兔餓肚子嗎?
2、功能設計
人物:玉兔使用鼠標來控制左右運動
月餅:隨機從上界降落至下界,當碰到玉兔時,加10分,落到下界減5分。
月亮:隨機從上界降落至下界,當碰到玉兔時,減5分,且血條減一格。
血條:HP
值為3格時,生命為滿值,當碰到月亮時減1格。減為0格時,游戲結束。
開始:開始按鈕,鼠標點擊即開始游戲
重來:重來按鈕,鼠標點擊即重新開始
效果展示

兔兔是不是很可愛嘞!
代碼素材
環(huán)境如下
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("玉兔吃月餅——猿童學祝大家中秋節(jié)快樂!")
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編寫一個中秋節(jié)嫦娥投食小游戲的文章就介紹到這了,更多相關Python嫦娥投食游戲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

