
水果忍者的玩法很简单,尽可能的切开抛出的水果就行。今天小五就用python简单的模拟一下这个游戏。在这个简单的项目中,我们用鼠标选择水果来切割,同时炸弹也会隐藏在水果中,如果切开了三次炸弹,玩家就会失败。一、需要导入的包
二、窗口界面设置
- FPS = 15 # gameDisplay的帧率,1/12秒刷新一次
- pygame.display.set_caption('水果忍者') # 标题
- gameDisplay = pygame.
display.set_mode((WIDTH, HEIGHT)) # 固定窗口大小
- clock = pygame.time.Clock()
- background = pygame.image.load('背景.jpg') # 背景
- 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"
- 'img': pygame.image.
load(fruit_path),
- 'x' : random.randint(100,500),
- 'speed_x': random.randint(-10,10),
-
'speed_y': random.randint(-80, -60),
- if random.random() >= 0.75:
- data[fruit]['throw'] = True
- data[fruit]['throw']
= False
- generate_random_fruits(fruit)
- Speed_x和speed_y是存储水果在x和y方向的移动速度。它也控制水果的对角线移动。
- throw,用于判断生成的水果坐标是否在游戏之外。如果在外面,那么将被丢弃。
四、绘制字体
- 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
)
- blit()在屏幕上的指定位置绘制图像或写入文字。
五、玩家生命的提示
- def draw_lives(display, x, y, lives, image) :
- img = pygame.image.load(image)
- img_rect = img.get_rect()
- img_rect.x = int(x + 35 * i)
- display.blit(img, img_rect)
- def hide_cross_lives(x, y):
- gameDisplay.blit(pygame.image.load("images/red_lives.png"),
(x, y))
- img_rect获取十字图标的(x,y)坐标(位于最右上方)
- img_rect .x 设置下一个十字图标距离前一个图标35像素。
- img_rect.y负责确定十字图标从屏幕顶部开始的位置。
六、游戏开始与结束的画面
- def show_gameover_screen():
- gameDisplay.blit(background, (0,0))
- draw_text(gameDisplay,
"FRUIT NINJA!", 90, WIDTH / 2, HEIGHT / 4)
- draw_text(gameDisplay,"Score : " + str(score), 50, WIDTH
/ 2, HEIGHT /2)
- draw_text(gameDisplay, "Press a key to begin!", 64, WIDTH / 2, HEIGHT * 3 / 4)
- for event in pygame.event.get():
- if
event.type == pygame.QUIT:
- if event.type == pygame.KEYUP:
- show_gameover_screen()函数显示初始游戏画面和游戏结束画面。
- pygame.display.flip()将只更新屏幕的一部分,但如果没有参数传递,则会更新整个屏幕。
- pygame.event.get()将返回存储在pygame事件队列中的所有事件。
- 如果事件类型等于quit,那么pygame将退出。
- event.KEYUP事件,当按键被按下和释放时发生的事件。
七、游戏主循环
- draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- 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():
- value['x'] += value[
'speed_x']
- value['y'] += value['speed_y']
- value['speed_y'] += (1 * value['t'])
- gameDisplay.blit(value['img'], (value['x'], value['y']))
-
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:
- hide_cross_lives(690, 15)
- hide_cross_lives(725, 15)
- hide_cross_lives(760, 15)
- half_fruit_path = "images/explosion.png"
- half_fruit_path = "images/" + "half_" + key +
".png"
- value['img'] = pygame.image.load(half_fruit_path)
- score_text = font.render('Score : ' + str(score), True, (255, 255, 255))
- generate_random_fruits(key)
-
如果超过3个炸弹被切掉,game_over终止游戏,同时循环。
- 如果一个水果没有被切开,那么它将不会发生任何事情。如果水果被切开,那么一个半切开的水果图像应该出现在该水果的地方
- 如果用户点击了三次炸弹,将显示GAME OVER信息,并重置窗口。
- clock.tick()将保持循环以正确的速度运行。循环应该在每1/12秒后更新一次
文章转载:Python编程学习圈
(版权归原作者所有,侵删)


点击下方“阅读原文”查看更多