目录
导语
正文
一、准备中
1.0 游戏规则
1.1 游戏图片素材(可修改)
1.2 游戏字体素材(可修改)
二、环境安装
三、开始敲代码
3.0 设置界面玩家生命值等
3.1 导入模块
3.2 界面背景、字体设置
3.3 游戏窗口设置
3.4 随机生成水果的位置与数据存放
3.5 用一个字典来存放水果的数据
3.6 在屏幕中绘制字体
3.7 绘制玩家的生命
3.8 游戏开始与结束画面
3.9 游戏主循环
四、游戏展示效果
4.1 Part 1 动态视频展示效果如下
4.2 Part 2 静态截图展示效果如下
4.3 Part 3 静态进入游戏界面截图如下
总结
完整的免费源码领取处:
新增解答方面——
往期游戏热门文章推荐:
文章汇总——
导语
Hey!下午好,我是木木子療,关注我,一起玩游戏吧~
微信小游戏很久之前刮起了一股切水果热潮,还记得嘛?我记得纯粹是因为这个游戏家里的孩
子依旧没放弃~
比如:果盘忍者|水果切切切|一起切水果|全民切西瓜|王牌飞刀手......
那时候——各种同类型的切水果小游戏层出不穷,并“前仆后继”地纷纷霸占小程序排行榜前列。那
场面简直是经典手游《水果忍者》的强势諾回归!
今天木木子手把手教大家写一款简单又好玩儿的切水果小游戏,来比拼一下吧——battlebattle!
还没有“切过水果”的朋友们你们就OUT了!关注「顾木子吖」CSDN账号,点开文章汇总游戏系列
一栏【水果忍者游戏】就能获取游戏入口哦!
正文
本文是写的水果忍者小游戏啦~还是用的大家所熟悉的Pygame模块啦~本文超详细讲解哦!
一、准备中
1.0 游戏规则
Python版本的水果忍者小编初始化设置是玩家3条生命值,切到相应的水果相应加分,切到易爆物
比如炸弹这些就会相应的减少生命值,在生命值内可以一直切切切,切的越多分数越高,相应的生
命值耗尽即结束游戏哦!快试试你能得几分?
哈哈哈,今天也录制了游戏视频的,看着视频更有玩游戏的感觉嘛~
1.1 游戏图片素材(可修改)
1.2 游戏字体素材(可修改)
二、环境安装
本文木子是用的Python3、Pycharm写的。模块Pygame、random随机出现水果以及一些自带的。
这里模块安装命令统一镜像源豆瓣:
pip install -i https://pypi.douban.com/simple/ +模块名
三、开始敲代码
3.0 设置界面玩家生命值等
player_lives = 3 # 生命
score = 0 # 得分
fruits = ['melon', 'orange', 'pomegranate', 'guava', 'bomb'] # 水果和炸弹
3.1 导入模块
import pygame, sys
import os
import random
3.2 界面背景、字体设置
background = pygame.image.load('背景图/02.png') # 背景
font = pygame.font.Font(os.path.join(os.getcwd(), '字体/comic.ttf'), 42) # 字体
score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) # 得分的字体样式
3.3 游戏窗口设置
WIDTH = 800
HEIGHT = 500
FPS = 12 # gameDisplay的帧率,1/12秒刷新一次
pygame.init()
pygame.display.set_caption('水果忍者_csdn账号:顾木子吖') # 标题
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) # 游戏窗口
clock = pygame.time.Clock()
3.4 随机生成水果的位置与数据存放
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
3.5 用一个字典来存放水果的数据
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))
3.6 在屏幕中绘制字体
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)
3.7 绘制玩家的生命
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)
3.8 游戏开始与结束画面
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
3.9 游戏主循环
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()
四、游戏展示效果
4.1 Part 1 动态视频展示效果如下
player_lives = 3 # 生命 score = 0 # 得分 fruits = ['melon', 'orange', 'pomegranate', 'guava', 'bomb'] # 水果和炸弹
3.1 导入模块
import pygame, sys
import os
import random
3.2 界面背景、字体设置
background = pygame.image.load('背景图/02.png') # 背景
font = pygame.font.Font(os.path.join(os.getcwd(), '字体/comic.ttf'), 42) # 字体
score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) # 得分的字体样式
3.3 游戏窗口设置
WIDTH = 800
HEIGHT = 500
FPS = 12 # gameDisplay的帧率,1/12秒刷新一次
pygame.init()
pygame.display.set_caption('水果忍者_csdn账号:顾木子吖') # 标题
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) # 游戏窗口
clock = pygame.time.Clock()
3.4 随机生成水果的位置与数据存放
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
3.5 用一个字典来存放水果的数据
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))
3.6 在屏幕中绘制字体
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)
3.7 绘制玩家的生命
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)
3.8 游戏开始与结束画面
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
3.9 游戏主循环
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()
四、游戏展示效果
4.1 Part 1 动态视频展示效果如下
background = pygame.image.load('背景图/02.png') # 背景
font = pygame.font.Font(os.path.join(os.getcwd(), '字体/comic.ttf'), 42) # 字体
score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) # 得分的字体样式
3.3 游戏窗口设置
WIDTH = 800
HEIGHT = 500
FPS = 12 # gameDisplay的帧率,1/12秒刷新一次
pygame.init()
pygame.display.set_caption('水果忍者_csdn账号:顾木子吖') # 标题
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) # 游戏窗口
clock = pygame.time.Clock()
3.4 随机生成水果的位置与数据存放
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
3.5 用一个字典来存放水果的数据
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))
3.6 在屏幕中绘制字体
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)
3.7 绘制玩家的生命
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)
3.8 游戏开始与结束画面
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
3.9 游戏主循环
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()
四、游戏展示效果
4.1 Part 1 动态视频展示效果如下
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
3.5 用一个字典来存放水果的数据
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))
3.6 在屏幕中绘制字体
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)
3.7 绘制玩家的生命
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)
3.8 游戏开始与结束画面
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
3.9 游戏主循环
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()
四、游戏展示效果
4.1 Part 1 动态视频展示效果如下
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)
3.7 绘制玩家的生命
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)
3.8 游戏开始与结束画面
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
3.9 游戏主循环
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()
四、游戏展示效果
4.1 Part 1 动态视频展示效果如下
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
3.9 游戏主循环
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()
四、游戏展示效果
4.1 Part 1 动态视频展示效果如下
4.1 Part 1 动态视频展示效果如下
Python版水果忍者,有趣有趣~
4.2 Part 2 静态截图展示效果如下
(1)游戏进入界面——
(2)修改下背景图进入的界面——这个感觉貌似好看点儿~
4.3 Part 3 静态进入游戏界面截图如下
总结
哈哈哈~到这里就写完了哦~你感觉这2款水果忍者背景的,那款更适合你呢?
如果表示都感觉不好看~哭辽,那你自己来制作一款专属的Python版水果忍者吧~
完整的免费源码领取处:
如需完整的项目源码+素材源码基地见:#私信小编06#或者点击蓝色文字添加即可获取免费的福利!
你们的支持是我最大的动力!!记得三连哦~mua 欢迎大家阅读往期的文章哦~
新增解答方面——
如果在学习Python方面遇到问题、有时间会给大家解答滴啦!需要自行添加哈!
放WX号如下:apython68
往期游戏热门文章推荐:
1.0 Pygame实战:爆肝!几千行代码实现《机甲闯关冒险游戏》,太牛了!(❤️建议收藏起来慢慢学❤️)
1.9 Pygame实战:慎点|虐单身狗的最高境界是…【附源码】
2.0Pygame实战:利用Python实现智能五子棋,实现之后发现我玩不赢它!
1.2 Pygame实战:据说这是史上最难扫雷游戏,没有之一!你们感受下......
1.3 Pygame实战:对象突然想玩坦克大战,我用Python三十分钟实现!看!他开心的像个孩子!
文章汇总——
1.1Python—2021 |已有文章汇总 | 持续更新,直接看这篇就够了~



