使用python编写一个水果忍者小游戏

  介绍

本篇文章为大家展示了使用python编写一个水果忍者小游戏,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

一、需要导入的包

import  pygame,系统   import 操作系统   import 随机

二、窗口界面设置

#,游戏窗口   WIDTH =800   HEIGHT =500   时间=FPS  15,,,,,,,,,,,,, #, gameDisplay的帧率,1/12秒刷新一次   pygame.init ()   pygame.display.set_caption(& # 39;水果忍者& # 39;),#,标题   时间=gameDisplay  pygame.display.set_mode((宽度,高度),#,固定窗口大小   时间=clock  pygame.time.Clock ()      #,用到的颜色   WHITE =, (255255255)   BLACK =, (0, 0, 0)   RED =, (255, 0, 0)   GREEN =, (0255 0)   BLUE =, (0, 0255)      时间=background  pygame.image.load(& # 39;背景jpg # 39;), #,背景   时间=font  pygame.font.Font (os.path.join (os.getcwd (), & # 39; comic.ttf& # 39;),, 42), #,字体   score_text =, font.render (& # 39; Score : & # 39;, +, str(分数),,真的,,(255,,255,,255)),#,得分字体样式

三,随机生成水果位置

def  generate_random_fruits(水果):   ,,,fruit_path =,“图像/?+,fruit  +,“.png"   ,,,的数据(水果),=,{   ,,,,,,,& # 39;img # 39;:, pygame.image.load (fruit_path),   ,,,,,,,& # 39;x # 39;,:, random.randint (100500),   ,,,,,,,& # 39;y # 39;,:, 800年,   ,,,,,,,& # 39;speed_x& # 39;:, random.randint(-10年,10),   ,,,,,,,& # 39;speed_y& # 39;:, random.randint (-80,, -60),   ,,,,,,,& # 39;把# 39;:,假的,   ,,,,,,,& # 39;t # 39;:, 0,   ,,,,,,,& # 39;冲击# 39;:,假的,   ,,,}      ,,,if  random.random(),在=,0.75:   ,,,,,,,数据(水果)[& # 39;把# 39;],=,真的   ,,,:   ,,,,,,,数据(水果)[& # 39;把# 39;],=,假的      data =, {}   for  fruit 水果:拷贝   ,,,generate_random_fruits(水果) <李>

这个函数用于随机生成水果和保存水果的数据

<李>

& # 39; x # 39;和& # 39;y # 39;存储水果在x坐标和y坐标上的位置。

<李>

Speed_x和speed_y是存储水果在x和y方向的移动速度。它也控制水果的对角线移动。

<李>

,用于判断生成的水果坐标是否在游戏之外。如果在外面,那么将被丢弃。

<李>

数据字典用于存放随机生成的水果的数据。

四,绘制字体

font_name =, pygame.font.match_font (& # 39; comic.ttf& # 39;)      def  draw_text(显示、,文本,大小,,x,, y):   ,,,font =, pygame.font.Font (font_name,大小)   ,,,text_surface =, font.render(文本,,真的,,白色)   ,,,text_rect =, text_surface.get_rect ()   ,,,text_rect.midtop =, (x, y)   ,,,gameDisplay.blit (text_surface, text_rect) <李>

Draw_text函数可以在屏幕上绘制文字。

<李>

get_rect()返回矩形对象。

<李>

X和y是X方向和y方向的位置。

<李>

位块传输()在屏幕上的指定位置绘制图像或写入文字。

五,玩家生命的提示

#,绘制玩家的生命   def  draw_lives(显示,x,, y,,生活,,图片),:   for 才能小姐:范围(生活),拷贝:   ,,,img =, pygame.image.load(图片)   ,,,img_rect =, img.get_rect (),,,,   ,,,img_rect.x =, int (x  +, 35, *, i),,   ,,,img_rect.y =, y ,,,,,,,,   ,,,display.blit (img, img_rect) def  hide_cross_lives (x, y):   gameDisplay.blit才能(pygame.image.load(“图像/red_lives.png"), (x, y)) <李>

img_rect获取十字图标的(x, y)坐标(位于最右上方)

<李>

img_rect。x设置下一个十字图标距离前一个图标35像素。

<李>

img_rect。y负责确定十字图标从屏幕顶部开始的位置。

六、游戏开始与结束的画面

def  show_gameover_screen ():   ,,,gameDisplay.blit(背景,,(0,0))   ,,,draw_text (gameDisplay,“FRUIT 忍者!“,,90,,WIDTH /, 2, HEIGHT /, 4)   ,,,if  not  game_over :   ,,,,,,,draw_text (gameDisplay,“Score ,:,“, +, str(分数),50岁,WIDTH /, 2, HEIGHT /2)      null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

使用python编写一个水果忍者小游戏