0
点赞
收藏
分享

微信扫一扫

《极速切水果游戏》有Python版了,曾风靡一时的手游能否富过“二代”?

扶摇_hyber 2022-01-16 阅读 116

前言

正文

随着冬季到来,又到了一年中采摘草莓的最佳季节。

小编在周末的时候已经去过几次拉,很nice 哈哈哈.jpg 但是草莓园的话小伙伴儿们需要好好选择

拉,之前还踩了坑的。说到草莓🍓的话今天小编就给大家制作一款切水果小游戏!

环境安装——

1)准备好相应的识别的图片,这里是随机到网上寻找的素材图片!

2)环境安装准备好Python版本基本上都可以、小编用的Python3.7、Pycharm2021的,游戏模块

Pygame,然后一些自带的不用管 直接导入即可!

安装模块也就是第三方模块的小编经常用的方法是:pip install +模块名或者提速需要用到镜像源,

百度下或者csdn搜下就会出来很多安装模块的镜像源这里就不一一介绍了!

一、准备中

1)素材背景

这块儿的话就是整体的界面小程序的背景图,大家可以自己找的这边小编找的如下👇

还有各种水果的图片素材,大家自由寻找即可!

完整的源码:

import pygame, sys
import os
import random

player_lives = 3                                                # 生命
score = 0                                                       # 得分
fruits = ['melon', 'orange', 'pomegranate', 'guava', 'bomb']    # 水果和炸弹

# 游戏窗口
WIDTH = 800
HEIGHT = 500
FPS = 12                                                # gameDisplay的帧率,1/12秒刷新一次
pygame.init()
pygame.display.set_caption('极速切水果小游戏')                   # 标题
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))   # 游戏窗口
clock = pygame.time.Clock()

# 用到的颜色
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)

background = pygame.image.load('背景图/03.png')                                  # 背景
font = pygame.font.Font(os.path.join(os.getcwd(), '字体/comic.ttf'), 42)         # 字体
score_text = font.render('Score : ' + str(score), True, (255, 255, 255))    # 得分的字体样式


# 随机生成水果的位置与数据存放
def generate_random_fruits(fruit):
    fruit_path = "images/" + fruit + ".png"
    data[fruit] = {
        'img': pygame.image.load(fruit_path),
        'x' : random.randint(100,500),          # 水果在x坐标轴上的位置
        'y' : 800,
        'speed_x': random.randint(-10,10),      # 水果在x方向时的速度和对角线移动
        'speed_y': random.randint(-80, -60),    # y方向时的速度
        'throw': False,                         # 如果生成水果的位置在gameDisplay之外,将被丢弃
        't': 0,                                 
        'hit': False,
    }

    if random.random() >= 0.75:     # 返回在[0.0, 1.0]范围内的下一个随机浮点数,以保持水果在游戏中的显示。
        data[fruit]['throw'] = True
    else:
        data[fruit]['throw'] = False

# 用一个字典来存放水果的数据
data = {}
for fruit in fruits:
    generate_random_fruits(fruit)

def hide_cross_lives(x, y):
    gameDisplay.blit(pygame.image.load("images/red_lives.png"), (x, y))

# 在屏幕中绘制字体
font_name = pygame.font.match_font('comic.ttf')
def draw_text(display, text, size, x, y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, WHITE)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    gameDisplay.blit(text_surface, text_rect)

# 绘制玩家的生命
def draw_lives(display, x, y, lives, image) :
    for i in range(lives) :
        img = pygame.image.load(image)
        img_rect = img.get_rect()       
        img_rect.x = int(x + 35 * i)    
        img_rect.y = y                  
        display.blit(img, img_rect)

# 游戏开始与结束画面
def show_gameover_screen():
    gameDisplay.blit(background, (0,0))
    draw_text(gameDisplay, "FRUIT NINJA!", 90, WIDTH / 2, HEIGHT / 4)
    if not game_over :
        draw_text(gameDisplay,"Score : " + str(score), 50, WIDTH / 2, HEIGHT /2)

    draw_text(gameDisplay, "Press any key to start the game", 64, WIDTH / 2, HEIGHT * 3 / 4)
    pygame.display.flip()
    waiting = True
    while waiting:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYUP:
                waiting = False

# 游戏主循环
first_round = True
game_over = True        # 超过3个炸弹,终止游戏循环
game_running = True     # 管理游戏循环
while game_running :
    if game_over :
        if first_round :
            show_gameover_screen()
            first_round = False
        game_over = False
        player_lives = 3
        draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
        score = 0

    for event in pygame.event.get():
        # 检查是否关闭窗口
        if event.type == pygame.QUIT:
            game_running = False

    gameDisplay.blit(background, (0, 0))
    gameDisplay.blit(score_text, (0, 0))
    draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')

    for key, value in data.items():
        if value['throw']:
            value['x'] += value['speed_x']          # x方向上移动水果
            value['y'] += value['speed_y']          # y方向上移动 
            value['speed_y'] += (1 * value['t'])    # 递增
            value['t'] += 1                         

            if value['y'] <= 800:
                gameDisplay.blit(value['img'], (value['x'], value['y']))    # 动态显示水果
            else:
                generate_random_fruits(key)

            current_position = pygame.mouse.get_pos()   # 获取鼠标的位置,单位为像素

            if not value['hit'] and current_position[0] > value['x'] and current_position[0] < value['x']+60 \
                    and current_position[1] > value['y'] and current_position[1] < value['y']+60:
                if key == 'bomb':
                    player_lives -= 1
                    if player_lives == 0:
                        
                        hide_cross_lives(690, 15)
                    elif player_lives == 1 :
                        hide_cross_lives(725, 15)
                    elif player_lives == 2 :
                        hide_cross_lives(760, 15)
                    # 超过3次炸弹,提示游戏结束,重置窗口
                    if player_lives < 0 :
                        show_gameover_screen()
                        game_over = True

                    half_fruit_path = "images/explosion.png"
                else:
                    half_fruit_path = "images/" + "half_" + key + ".png"

                value['img'] = pygame.image.load(half_fruit_path)
                value['speed_x'] += 10
                if key != 'bomb' :
                    score += 1
                score_text = font.render('Score : ' + str(score), True, (255, 255, 255))
                value['hit'] = True
        else:
            generate_random_fruits(key)

    pygame.display.update()
    clock.tick(FPS)      
                        

pygame.quit()

效果展示:

游戏规则:相应的水果鼠标点击即可加分,点到雷即消失一个生命值,总三个生命值消失级游戏结

束!

游戏界面——

 

 游戏运行中——

 点到雷了——

 

 游戏结束——

 总结

好啦!《极速切水果🍓小游戏》就到这里结束拉,老规矩源码私信我即可拉!

关注小编获取更多精彩内容!

​制作不易,记得一键三连哦!! 如需打包好的源码+素材免费分享滴!传送门

举报

相关推荐

0 条评论