【中秋徵文】使用Python中秋節嫦娥投食小遊戲《千里嬋娟》

專注的阿熊發表於2022-09-07

'''

猿童學出品 --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 = 外匯跟單gendan5.comself.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()

# 猿童學


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2913782/,如需轉載,請註明出處,否則將追究法律責任。

相關文章