python外星人入侵游戏代码.docx

上传人:安*** 文档编号:73266354 上传时间:2023-02-17 格式:DOCX 页数:14 大小:20.49KB
返回 下载 相关 举报
python外星人入侵游戏代码.docx_第1页
第1页 / 共14页
python外星人入侵游戏代码.docx_第2页
第2页 / 共14页
点击查看更多>>
资源描述

《python外星人入侵游戏代码.docx》由会员分享,可在线阅读,更多相关《python外星人入侵游戏代码.docx(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、python外星人入侵游戏代码这篇文章主要为大众详细介绍了Python外星人入侵游戏编程完好的实现思路文中例如代码介绍的非常详细具有一定的参考价值感兴趣的小伙伴们可以参考一下PYTHON游戏编程外星人入侵的完好实现思路详细内容如下准备工作下载python比方Anaconda3(64bit)导入pygame游戏包1.外星人设置alien.py代码(PS下面便是源码需要源码以及素材的童鞋可以点击下载即可下载importpygamefrompygame.spriteimportSpriteclassAlien(Sprite):表示单个外星人的类def_init_(self,ai_settings,s

2、creen):初始化外星人并设置其他位置super(Alien,self)._init_()self.screenscreenself.ai_settingsai_settings#加载外星人图像并设置其rect属性self.imagepygame.image.load(images/alien.bmp)self.rectself.image.get_rect()#每个外星人最初都在屏幕左上角附近self.rect.xself.rect.widthself.rect.yself.rect.height#存储外星人的准确位置self.xfloat(self.rect.x)defblitme(se

3、lf):在指定位置绘制外星人self.screen.blit(self.image,self.rect)defcheck_edges(self):假如外星人位于屏幕边缘就返回Truescreen_rectself.screen.get_rect()ifself.rect.rightscreen_rect.right:returnTrueelifself.rect.left0:returnTruedefupdate(self):向右挪动外星人self.x(self.ai_settings.alien_speed_factor*self.ai_settings.fleet_direction)se

4、lf.rect.xself.x2.游戏主程序alien_invasion.py代码:importpygamefromsettingsimportSettingsfromgame_statsimportGameStatsfrombuttonimportButtonfromshipimportShipfrompygame.spriteimportGroupimportgame_functionsasgffromscoreboardimportScoreboarddefrun_game():pygame.init()#初始化背景设置ai_settingsSettings()#全局设置screenpy

5、game.display.set_mode(#创立screen显示窗口(ai_settings.screen_width,ai_settings.screen_height)pygame.display.set_caption(AlienInvasion)#标题#新建Play按钮play_buttonButton(ai_settings,screen,Play)#创立一个用于存储游戏统计信息的实例,并创立记分牌statsGameStats(ai_settings)sbScoreboard(ai_settings,screen,stats)#创立飞船shipShip(ai_settings,sc

6、reen)#创立子弹编组bulletsGroup()#创立一个外星人aliensGroup()#创立外星人群gf.create_fleet(ai_settings,screen,ship,aliens)#开场游戏主循环whileTrue:#监视键盘以及鼠标事件gf.check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets)ifstats.game_active:#挪动飞船gf.update_ship(ship)#更新子弹位置gf.update_bullets(ai_settings,screen,stats

7、,sb,ship,aliens,bullets)#更新外星人gf.update_aliens(ai_settings,stats,screen,sb,ship,aliens,bullets)#更新屏幕gf.update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button)run_game()3.设置子弹bullet.py代码importpygamefrompygame.spriteimportSpriteimporttimeclassBullet(Sprite):飞船子弹进展管理def_init_(self,ai

8、_settings,screen,ship):super(Bullet,self)._init_()self.screenscreen#创立子弹矩形初始位置(0,0,3,15)分别对应leftop宽高self.rectpygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)self.rect.centerxship.rect.centerx#设置中心点x轴坐标跟飞船一致self.rect.topship.rect.top#设置y轴坐标顶部跟飞船一致#设置成小数进展计算self.topfloat(self.rect.t

9、op)self.colorai_settings.bullet_colorself.speed_factorai_settings.bullet_speed_factordefupdate(self):self.top-self.speed_factorself.rect.topself.topprint(self.rect.top)defdraw_bullet(self):pygame.draw.rect(self.screen,self.color,self.rect)4.设置Play按钮button.py代码importpygame.fontclassButton():def_init_

10、(self,ai_settings,screen,msg):初始化按钮属性self.screenscreenself.screen_rectscreen.get_rect()#设置按钮的尺寸以及其他属性self.width,self.height200,50self.button_color(0,255,0)self.text_color(255,255,255)self.fontpygame.font.SysFont(None,48)#创立按钮的rect对象并使其居中self.rectpygame.Rect(0,0,self.width,self.height)self.rect.cente

11、rself.screen_rect.center#按钮的标签只需创立一次self.prep_msg(msg)defprep_msg(self,msg):将msg渲染为图像并使其在按钮上居中self.msg_imageself.font.render(msg,True,self.text_color,self.button_color)self.msg_image_rectself.msg_image.get_rect()self.msg_image_rect.centerself.rect.centerdefdraw_button(self):#绘制一个用颜色填充的按钮再绘制文本self.sc

12、reen.fill(self.button_color,self.rect)self.screen.blit(self.msg_image,self.msg_image_rect)5.设置游戏功能game_functions.py代码importsysimportpygamefrombulletimportBulletfromalienimportAlienfromtimeimportsleepdefcheck_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets):#监视键盘以及鼠标事件foreventinpyg

13、ame.event.get():ifevent.typepygame.QUIT:#关闭窗口退出sys.exit()elifevent.typepygame.KEYDOWN:check_keydown_events(event,ai_settings,screen,ship,bullets)elifevent.typepygame.KEYUP:check_keyup_events(event,ship)elifevent.typepygame.MOUSEBUTTONDOWN:mouse_x,mouse_ypygame.mouse.get_pos()check_play_button(ai_set

14、tings,screen,stats,sb,play_button,ship,aliens,bullets,mouse_x,mouse_y)defcheck_play_button(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets,mouse_x,mouse_y):在玩家单击Play按钮时开场游戏button_clickedplay_button.rect.collidepoint(mouse_x,mouse_y)ifbutton_clickedandnotstats.game_active:#重置游戏设置ai_settin

15、gs.initialize_dynamic_settings()#隐藏光标pygame.mouse.set_visible(False)#重置游戏统计信息stats.reset_stats()stats.game_activeTrue#重置计分牌图像sb.prep_score()sb.prep_high_score()sb.prep_level()sb.prep_ships()#清空外星人列表以及子弹列表aliens.empty()bullets.empty()#创立一群新的外星人并让飞船居中create_fleet(ai_settings,screen,ship,aliens)ship.ce

16、nter_ship()defupdate_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button):更新屏幕上的图片并切换到新屏幕screen.fill(ai_settings.bg_color)#设置背景颜色ship.blitme()#绘制飞船aliens.draw(screen)#循环子弹组里面的元素进展绘制为空时不执行forbulletinbullets.sprites():bullet.draw_bullet()#绘制子弹#显示得分sb.show_score()#假如游戏处于非活泼状态就显示Play按钮ifn

17、otstats.game_active:play_button.draw_button()#显示最新屏幕擦拭旧屏幕pygame.display.flip()#print(1)defcheck_keydown_events(event,ai_settings,screen,ship,bullets):ifevent.keypygame.K_RIGHT:ship.moving_rightTrueelifevent.keypygame.K_LEFT:ship.moving_leftTrueelifevent.keypygame.K_SPACE:fire_bullet(ai_settings,scre

18、en,ship,bullets)elifevent.keypygame.K_q:sys.exit()defcheck_keyup_events(event,ship):ifevent.keypygame.K_RIGHT:ship.moving_rightFalseelifevent.keypygame.K_LEFT:ship.moving_leftFalsedefupdate_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets):更新子弹位置删除子弹bullets.update()#子弹组每个成员执行self.update()操作fo

19、rbulletinbullets.sprites():ifbullet.rect.bottom0:#子弹出界删除bullets.remove(bullet)check_bullet_alien_collisions(ai_settings,screen,stats,sb,ship,aliens,bullets)defcheck_bullet_alien_collisions(ai_settings,screen,stats,sb,ship,aliens,bullets):响应外星人以及子弹的碰撞#删除发生碰撞的子弹以及外星人collisionspygame.sprite.groupcollid

20、e(bullets,aliens,True,True)ifcollisions:foraliensincollisions.values():stats.scoreai_settings.alien_points*len(aliens)sb.prep_score()check_high_score(stats,sb)iflen(aliens)0:#删除现有的子弹并新建一群外星人,加快游戏进度节奏bullets.empty()ai_settings.increase_speed()#进步等级stats.level1sb.prep_level()create_fleet(ai_settings,s

21、creen,ship,aliens)defupdate_ship(ship):ship.update()deffire_bullet(ai_settings,screen,ship,bullets):#创立一个子弹对象参加到子弹组iflen(bullets)ai_settings.bullets_allowed:#子弹少于允许值时再生成new_bulletBullet(ai_settings,screen,ship)bullets.add(new_bullet)defget_number_aliens_x(ai_settings,alien_width):计算每行可包容多少个外星人availa

22、ble_space_xai_settings.screen_width-2*alien_widthnumber_aliens_xint(available_space_x/(2*alien_width)returnnumber_aliens_xdefget_number_rows(ai_settings,ship_height,alien_height):计算屏幕可包容多少行外星人available_space_y(ai_settings.screen_height-(3*alien_height)-ship_height)number_rowsint(available_space_y/(2

23、*alien_height)returnnumber_rowsdefcreate_aliens(ai_settings,screen,aliens,alien_number,row_number):创立一个外星人并将其放在当期行alienAlien(ai_settings,screen)alien_widthalien.rect.widthalien.xalien_width2*alien_width*alien_numberalien.rect.xalien.xalien.rect.yalien.rect.height2*alien.rect.height*row_numberaliens.

24、add(alien)defcreate_fleet(ai_settings,screen,ship,aliens):创立外星人群#创立一个外星人并计算一行可以包容多少个外星人#外星人间距为外星人宽度alienAlien(ai_settings,screen)number_aliens_xget_number_aliens_x(ai_settings,alien.rect.width)number_rowsget_number_rows(ai_settings,ship.rect.height,alien.rect.height)#创立第一行外星人forrow_numberinrange(num

25、ber_rows):foralien_numberinrange(number_aliens_x):#创立一个外星人并将其参加当前行create_aliens(ai_settings,screen,aliens,alien_number,row_number)defcheck_fleet_edges(ai_settings,aliens):有外星人到达边缘时采取相应措施foralieninaliens.sprites():ifalien.check_edges():change_fleet_direction(ai_settings,aliens)breakdefchange_fleet_di

26、rection(ai_settings,aliens):将整群外星人下移并改变他们的运动方向foralieninaliens.sprites():alien.rect.yai_settings.fleet_drop_speedai_settings.fleet_direction*-1defship_hit(ai_settings,stats,screen,sb,ship,aliens,bullets):响应被外星人撞到的飞船ifstats.ships_left0:#将ship_left减1stats.ships_left-1#更新记分牌sb.prep_ships()#清空外星人列表以及子弹列

27、表aliens.empty()bullets.empty()#创立一群新的外星人并将飞船放到屏幕低端中央create_fleet(ai_settings,screen,ship,aliens)ship.center_ship()#暂停sleep(0.5)else:stats.game_activeFalsepygame.mouse.set_visible(True)defcheck_aliens_bottom(ai_settings,stats,screen,sb,ship,aliens,bullets):检查是否有外星人到达屏幕低端screen_rectscreen.get_rect()fo

28、ralieninaliens.sprites():ifalien.rect.bottomscreen_rect.bottom:#像飞船被撞到一样进展处理ship_hit(ai_settings,stats,screen,sb,ship,aliens,bullets)breakdefupdate_aliens(ai_settings,stats,screen,sb,ship,aliens,bullets):更新外星人群中所有外星人的位置check_fleet_edges(ai_settings,aliens)aliens.update()#检测外星人以及飞船之间的碰撞ifpygame.sprit

29、e.spritecollideany(ship,aliens):ship_hit(ai_settings,stats,screen,sb,ship,aliens,bullets)#检查是否有外星人到达屏幕低端check_aliens_bottom(ai_settings,stats,screen,sb,ship,aliens,bullets)defcheck_high_score(stats,sb):检查是否诞生了新的最高纪录ifstats.scorestats.high_score:stats.high_scorestats.scoresb.prep_high_score()6.游戏统计信息

30、game_stats.py代码classGameStats():跟踪游戏的统计信息def_init_(self,ai_settings):初始化统计信息self.ai_settingsai_settingsself.reset_stats()#游戏刚启动时处于非活动状态self.game_activeFalse#在任何情况下不应该重置最高分self.high_score0self.level1defreset_stats(self):初始化在游戏运行期间可能变化的统计信息self.ships_leftself.ai_settings.ship_limitself.score07.分数设置sco

31、reboard.py代码importpygame.fontfrompygame.spriteimportGroupfromshipimportShipclassScoreboard():显示得分信息的类def_init_(self,ai_settings,screen,stats):初始化显示得分涉及的属性self.screenscreenself.screen_rectscreen.get_rect()self.ai_settingsai_settingsself.statsstats#显示得分信息时使用的字体设置self.text_color(30,30,30)self.fontpygam

32、e.font.SysFont(None,48)#准备初始化得分图像以及当前最高分数self.prep_score()self.prep_high_score()self.prep_level()self.prep_ships()defprep_score(self):将得分转换为一幅渲染的图像rounded_scoreint(round(self.stats.score,-1)score_str:,.format(rounded_score)self.score_imageself.font.render(score_str,True,self.text_color,self.ai_setti

33、ngs.bg_color)#将得分放在右上角self.score_rectself.score_image.get_rect()self.score_rect.rightself.screen_rect.right-20self.score_rect.top5defprep_high_score(self):将最高得分转换为渲染图像high_scoreint(round(self.stats.high_score,-1)high_score_str:,.format(high_score)self.high_score_imageself.font.render(high_score_str,

34、True,self.text_color,self.ai_settings.bg_color)#将最高分放在屏幕最中央self.high_score_rectself.high_score_image.get_rect()self.high_score_rect.centerxself.screen_rect.centerxself.high_score_rect.top5defprep_level(self):将等级转换为渲染图像self.level_imageself.font.render(str(self.stats.level),True,self.text_color,self.a

35、i_settings.bg_color)#将得分放在右上角self.level_rectself.score_image.get_rect()self.level_rect.rightself.screen_rect.rightself.level_rect.topself.score_rect.bottomdefprep_ships(self):显示还剩下多少艘飞船self.shipsGroup()forship_numberinrange(self.stats.ships_left):shipShip(self.ai_settings,self.screen)ship.rect.x10sh

36、ip_number*ship.rect.widthship.rect.y10self.ships.add(ship)defshow_score(self):在屏幕上显示得分以及等级self.screen.blit(self.score_image,self.score_rect)self.screen.blit(self.high_score_image,self.high_score_rect)self.screen.blit(self.level_image,self.level_rect)#绘制飞船self.ships.draw(self.screen)8.设置settings.py代码

37、classSettings():存储外星人入侵中所有的设置def_init_(self):初始化设置#屏幕设置self.screen_width1200self.screen_height600self.bg_color(230,230,230)#设置背风光灰色#飞船设置self.ship_limit3self.ship_image_pathimages/ship.bmp#飞船图片途径#子弹设置self.bullet_width3self.bullet_height15self.bullet_color60,60,60self.bullets_allowed3#允许屏幕中出现子弹的数量#外星人

38、设置self.fleet_drop_speed10#以什么样的速度加快游戏节奏self.speedup_scale1.1#外星人点数进步速度self.score_scale1.5self.initialize_dynamic_settings()definitialize_dynamic_settings(self):初始化随游戏进展而变化的设置self.ship_speed_factor1.5self.bullet_speed_factor3self.alien_speed_factor1#fleet_direction为1表示向右移为-1表示向左移self.fleet_direction1

39、#计分self.alien_points50defincrease_speed(self):进步速度设置,外星人点数self.ship_speed_factor*self.speedup_scaleself.bullet_speed_factor*self.speedup_scaleself.alien_speed_factor*self.speedup_scaleself.alien_pointsint(self.alien_points*self.score_scale)print(self.alien_points)9.飞船设置ship.py代码importpygamefrompygam

40、e.spriteimportSpriteclassShip(Sprite):飞船所有信息def_init_(self,ai_settings,screen):初始化飞船并设置其起始位置super(Ship,self)._init_()self.screenscreenself.ai_settingsai_settings#加载飞船图片、获取外接矩形self.imagepygame.image.load(self.ai_settings.ship_image_path)#加载图片self.imagepygame.transform.smoothscale(self.image,(40,60)se

41、lf.rectself.image.get_rect()#获取图片外接矩形self.screen_rectscreen.get_rect()#获取屏幕外接矩形#将每搜新飞船放到并木底部中心self.rect.centerxself.screen_rect.centerxself.rect.bottomself.screen_rect.bottom#设置成浮点类型self.centerfloat(self.rect.centerx)#self.rect.centerx设置不了浮点数只能另设置一个变量进展运算#挪动标志self.moving_rightFalseself.moving_leftFa

42、lsedefblitme(self):在指定位置绘制飞船self.screen.blit(self.image,self.rect)defupdate(self):#向右挪动飞船ifself.moving_rightandself.rect.rightself.screen_rect.right:self.centerself.ai_settings.ship_speed_factor#向左挪动飞船ifself.moving_leftandself.rect.leftself.screen_rect.left:self.center-self.ai_settings.ship_speed_factorself.rect.centerxself.centerdefcenter_ship(self):让飞船在屏幕上居中self.centerself.screen_rect.centerx效果展示游戏资源图片资源完好工程代码获取后台私信小编01即可weixin_39887925

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 工程图纸

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁