QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 3529|回复: 0

[Python] Python写一个水果忍者游戏

[复制链接]

等级头衔

积分成就    金币 : 2804
   泡泡 : 1516
   精华 : 6
   在线时间 : 1243 小时
   最后登录 : 2024-5-2

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2021-7-4 22:58:17 | 显示全部楼层 |阅读模式
       水果忍者的玩法很简单,尽可能的切开抛出的水果就行。今天就用python简单的模拟一下这个游戏。在这个简单的项目中,用鼠标选择水果来切割,同时炸弹也会隐藏在水果中,如果切开了三次炸弹,玩家就会失败。2 g) j! k6 b  b! Z9 y8 Z
1.jpg 6 T  Q$ e" |" ~2 T, f9 C
一、需要导入的包
' H% x0 a" D* u0 C, c" n3 |
  1. import pygame, sys
  2. import os
  3. import random
二、窗口界面设置
  1. # 游戏窗口
  2. WIDTH = 800
  3. HEIGHT = 500
  4. FPS = 15             # gameDisplay的帧率,1/12秒刷新一次
  5. pygame.init()
  6. pygame.display.set_caption('水果忍者') # 标题
  7. gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) # 固定窗口大小
  8. clock = pygame.time.Clock()
  9. # 用到的颜色
  10. WHITE = (255,255,255)
  11. BLACK = (0,0,0)
  12. RED = (255,0,0)
  13. GREEN = (0,255,0)
  14. BLUE = (0,0,255)
  15. background = pygame.image.load('背景.jpg') # 背景
  16. font = pygame.font.Font(os.path.join(os.getcwd(), 'comic.ttf'), 42) # 字体
  17. score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) # 得分字体样式
三、随机生成水果位置; C& H( W: k- i' L+ u
  1. def generate_random_fruits(fruit):
  2. fruit_path = "images/" + fruit + ".png"
  3. data[fruit] = {
  4. 'img': pygame.image.load(fruit_path),
  5. 'x' : random.randint(100,500),
  6. 'y' : 800,
  7. 'speed_x': random.randint(-10,10),
  8. 'speed_y': random.randint(-80, -60),
  9. 'throw': False,
  10. 't': 0,
  11. 'hit': False,
  12. }
  13.     if random.random() >= 0.75:
  14. data[fruit]['throw'] = True
  15. else:
  16. data[fruit]['throw'] = False
  17. data = {}
  18. for fruit in fruits:
  19. generate_random_fruits(fruit)
  • 这个函数用于随机生成水果和保存水果的数据
  • 'x'和'y'存储水果在x坐标和y坐标上的位置。
  • Speed_x和speed_y是存储水果在x和y方向的移动速度。它也控制水果的对角线移动。
  • throw,用于判断生成的水果坐标是否在游戏之外。如果在外面,那么将被丢弃。
  • data字典用于存放随机生成的水果的数据。
    # k+ G6 z# V( q" a3 e4 I
四、绘制字体' M) J8 G- @7 t+ {9 ~
  1. font_name = pygame.font.match_font('comic.ttf')
  2. def draw_text(display, text, size, x, y):
  3. font = pygame.font.Font(font_name, size)
  4. text_surface = font.render(text, True, WHITE)
  5. text_rect = text_surface.get_rect()
  6. text_rect.midtop = (x, y)
  7. gameDisplay.blit(text_surface, text_rect)
  • Draw_text函数可以在屏幕上绘制文字。
  • get_rect() 返回 Rect 对象。
  • X和y是X方向和Y方向的位置。
  • blit()在屏幕上的指定位置绘制图像或写入文字。' X) o( {/ d! Q4 D2 p( U
五、玩家生命的提示) M" ]0 Z) A  S2 w$ e8 P* z
  1. # 绘制玩家的生命
  2. def draw_lives(display, x, y, lives, image) :
  3. for i in range(lives) :
  4. img = pygame.image.load(image)
  5. img_rect = img.get_rect()
  6. img_rect.x = int(x + 35 * i)
  7. img_rect.y = y
  8. display.blit(img, img_rect)
  9. def hide_cross_lives(x, y):
  10. gameDisplay.blit(pygame.image.load("images/red_lives.png"), (x, y))
  • img_rect获取十字图标的(x,y)坐标(位于最右上方)
  • img_rect .x 设置下一个十字图标距离前一个图标35像素。
  • img_rect.y负责确定十字图标从屏幕顶部开始的位置。
    0 I1 i# `8 o* Q' N7 i' t0 W
六、游戏开始与结束的画面/ o; ~! E2 y+ z$ J( `
  1. def show_gameover_screen():
  2. gameDisplay.blit(background, (0,0))
  3. draw_text(gameDisplay, "FRUIT NINJA!", 90, WIDTH / 2, HEIGHT / 4)
  4. if not game_over :
  5. draw_text(gameDisplay,"Score : " + str(score), 50, WIDTH / 2, HEIGHT /2)
  6.     draw_text(gameDisplay, "Press a key to begin!", 64, WIDTH / 2, HEIGHT * 3 / 4)
  7. pygame.display.flip()
  8. waiting = True
  9. while waiting:
  10. clock.tick(FPS)
  11. for event in pygame.event.get():
  12. if event.type == pygame.QUIT:
  13. pygame.quit()
  14. if event.type == pygame.KEYUP:
  15. waiting = False
  • show_gameover_screen()函数显示初始游戏画面和游戏结束画面。
  • pygame.display.flip()将只更新屏幕的一部分,但如果没有参数传递,则会更新整个屏幕。
  • pygame.event.get()将返回存储在pygame事件队列中的所有事件。
  • 如果事件类型等于quit,那么pygame将退出。
  • event.KEYUP事件,当按键被按下和释放时发生的事件。
    0 F! d% B. z. V; c
七、游戏主循环
! u9 N. E' Z$ p9 U  j6 g' V
  1. first_round = True
  2. game_over = True        
  3. game_running = True   
  4. while game_running :
  5. if game_over :
  6. if first_round :
  7. show_gameover_screen()
  8. first_round = False
  9. game_over = False
  10. player_lives = 3
  11. draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
  12. score = 0
  13.     for event in pygame.event.get():
  14.         if event.type == pygame.QUIT:
  15. game_running = False
  16. gameDisplay.blit(background, (0, 0))
  17. gameDisplay.blit(score_text, (0, 0))
  18. draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
  19.     for key, value in data.items():
  20. if value['throw']:
  21. value['x'] += value['speed_x']
  22. value['y'] += value['speed_y']
  23. value['speed_y'] += (1 * value['t'])
  24. value['t'] += 1
  25.             if value['y'] <= 800:
  26. gameDisplay.blit(value['img'], (value['x'], value['y']))
  27. else:
  28. generate_random_fruits(key)
  29.             current_position = pygame.mouse.get_pos()
  30.             if not value['hit'] and current_position[0] > value['x'] and current_position[0] < value['x']+60 \
  31. and current_position[1] > value['y'] and current_position[1] < value['y']+60:
  32. if key == 'bomb':
  33. player_lives -= 1
  34. if player_lives == 0:
  35. hide_cross_lives(690, 15)
  36. elif player_lives == 1 :
  37. hide_cross_lives(725, 15)
  38. elif player_lives == 2 :
  39. hide_cross_lives(760, 15)
  40.                     if player_lives < 0 :
  41. show_gameover_screen()
  42. game_over = True
  43.                     half_fruit_path = "images/explosion.png"
  44. else:
  45. half_fruit_path = "images/" + "half_" + key + ".png"
  46.                 value['img'] = pygame.image.load(half_fruit_path)
  47. value['speed_x'] += 10
  48. if key != 'bomb' :
  49. score += 1
  50. score_text = font.render('Score : ' + str(score), True, (255, 255, 255))
  51. value['hit'] = True
  52. else:
  53. generate_random_fruits(key)
  54.     pygame.display.update()
  55. clock.tick(FPS)
  56. pygame.quit()
  • 这是游戏的主循环
  • 如果超过3个炸弹被切掉,game_over终止游戏,同时循环。
  • game_running 用于管理游戏循环。
  • 如果事件类型是退出,那么游戏窗口将被关闭。
  • 在这个游戏循环中,我们动态显示屏幕内的水果。
  • 如果一个水果没有被切开,那么它将不会发生任何事情。如果水果被切开,那么一个半切开的水果图像应该出现在该水果的地方
  • 如果用户点击了三次炸弹,将显示GAME OVER信息,并重置窗口。
  • clock.tick()将保持循环以正确的速度运行。循环应该在每1/12秒后更新一次( Z. ~6 x: O1 t' f' V) r  X: i. F
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-3 00:34

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表