当前位置:   article > 正文

《Python编程 从入门到实践》第十四章练习题14-2答案_python人马大战csdn

python人马大战csdn

题目:创建一个矩形,它在屏幕右边缘以固定的速度上下移动。然后,在屏幕左边缘创建一艘飞船,玩家可上下移动该飞船,并射击前述矩形目标。添加一 个用于开始游戏的Play按钮,在玩家三次未击中目标时结束游戏,并重新显示Play按钮,让玩家能够通过单击该按钮来重新开始游戏。

完成效果:

Python:飞船射击

优势:跟网上为数不多的解决方案比较,本方案划分为几个功能文件,代码结构和思路更清晰。

心得:函数for循环里面的变量无法递增,可以考虑建一个类(比如Settings),将这个变量作为类的属性(self.fail_times=0),再实例化这个类(比如ai_settings)。最后在函数for循环里调用(ai_settings.fail_times)。

ship的原图:(下载后跟以下源代码放到同一个文件夹内再运行程序)

源代码:

文件1:space_war.py

  1. import pygame
  2. from pygame.sprite import Group
  3. from settings import Settings
  4. from ship import Ship
  5. import game_functions as gf
  6. from game_stats import GameStats
  7. from button import Button
  8. from target import Target
  9. def run_game():
  10. pygame.init()
  11. ai_settings=Settings()
  12. screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
  13. pygame.display.set_caption("Space War")
  14. play_button=Button(ai_settings,screen,"Play")
  15. stats=GameStats(ai_settings)
  16. ship=Ship(ai_settings,screen)
  17. bullets=Group()
  18. target=Target(ai_settings,screen)
  19. while True:
  20. gf.check_events(ai_settings,screen,stats,play_button,ship,target,bullets)
  21. if stats.game_active:
  22. ship.update()
  23. target.update()
  24. gf.update_bullets(ai_settings,screen,ship,target,bullets)
  25. gf.update_target(ai_settings,stats,screen,ship,target,bullets)
  26. gf.check_fail(ai_settings,bullets,stats,target,screen)
  27. gf.update_screen(ai_settings,screen,stats,ship,target,bullets,play_button)
  28. run_game()

文件2: ship.py

  1. import pygame
  2. class Ship():
  3. def __init__(self,ai_settings,screen):
  4. self.screen=screen
  5. self.ai_settings=ai_settings
  6. self.image=pygame.image.load('ship.bmp')
  7. self.rect=self.image.get_rect()
  8. self.screen_rect=screen.get_rect()
  9. self.rect.centery=self.screen_rect.centery
  10. self.rect.left=self.screen_rect.left
  11. self.center=float(self.rect.centery)
  12. self.moving_up=False
  13. self.moving_down=False
  14. def update(self):
  15. if self.moving_up and self.rect.top>self.screen.get_rect().top:
  16. self.center-=self.ai_settings.ship_speed_factor
  17. if self.moving_down and self.rect.bottom<self.screen.get_rect().bottom:
  18. self.center+=self.ai_settings.ship_speed_factor
  19. self.rect.centery=self.center
  20. def blitme(self):
  21. self.screen.blit(self.image,self.rect)
  22. def center_ship(self):
  23. self.center=self.screen_rect.centery

文件3: target.py

  1. import pygame
  2. from pygame.sprite import Sprite
  3. class Target(Sprite):
  4. def __init__(self,ai_settings,screen):
  5. super(Target,self).__init__()
  6. self.screen=screen
  7. self.ai_settings=ai_settings
  8. self.rect=pygame.Rect(0,0,ai_settings.target_width,ai_settings.target_height)
  9. self.rect.right=self.screen.get_rect().right
  10. self.rect.centery=self.screen.get_rect().centery
  11. self.y=float(self.rect.y)
  12. self.color=ai_settings.target_color
  13. self.speed_factor=ai_settings.target_speed_factor
  14. def check_edges(self):
  15. screen_rect=self.screen.get_rect()
  16. if self.rect.top<0:
  17. return True
  18. elif self.rect.bottom>screen_rect.bottom:
  19. return True
  20. def update(self):
  21. self.y+=(self.speed_factor*self.ai_settings.fleet_direction)
  22. self.rect.y=self.y
  23. def draw_target(self):
  24. pygame.draw.rect(self.screen,self.color,self.rect)

文件4: settings.py

  1. class Settings():
  2. def __init__(self):
  3. self.screen_width=1200
  4. self.screen_height=800
  5. self.bg_color=(230,230,230)
  6. self.ship_speed_factor=5
  7. self.bullet_speed_factor=10
  8. self.bullet_width=30
  9. self.bullet_height=15
  10. self.bullet_color=60,60,60
  11. self.bullets_allowed=3
  12. self.target_speed_factor=1
  13. self.target_direction=1
  14. self.target_width=300
  15. self.target_height=100
  16. self.target_color=180
  17. self.fleet_direction=1
  18. self.ship_limit=3
  19. self.bullets_left=3
  20. self.fail_times = 0

 文件5: game_stats.py

  1. class GameStats():
  2. def __init__(self,ai_settings):
  3. self.ai_settings=ai_settings
  4. self.reset_stats()
  5. self.game_active=False
  6. def reset_stats(self):
  7. self.ships_left=self.ai_settings.ship_limit

文件6: bullet.py

  1. import pygame
  2. from pygame.sprite import Sprite
  3. class Bullet(Sprite):
  4. def __init__(self,ai_settings,screen,ship):
  5. super(Bullet,self).__init__()
  6. self.screen=screen
  7. self.rect=pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
  8. self.rect.centery=ship.rect.centery
  9. self.x=float(self.rect.x)
  10. self.color=ai_settings.bullet_color
  11. self.speed_factor=ai_settings.bullet_speed_factor
  12. def update(self):
  13. self.x+=self.speed_factor
  14. self.rect.x=self.x
  15. def draw_bullet(self):
  16. pygame.draw.rect(self.screen,self.color,self.rect)

文件7: button.py

  1. import pygame.font
  2. class Button():
  3. def __init__(self,ai_settings,screen,msg):
  4. self.screen=screen
  5. self.screen_rect=screen.get_rect()
  6. self.width,self.height=200,50
  7. self.button_color = (0, 255, 0)
  8. self.text_color = (255, 255, 255)
  9. self.font=pygame.font.SysFont(None,48)
  10. self.rect=pygame.Rect(0,0,self.width,self.height)
  11. self.rect.center=self.screen_rect.center
  12. self.prep_msg(msg)
  13. def prep_msg(self,msg):
  14. self.msg_image=self.font.render(msg,True,self.text_color,self.button_color)
  15. self.msg_image_rect=self.msg_image.get_rect()
  16. self.msg_image_rect.center=self.rect.center
  17. def draw_button(self):
  18. self.screen.fill(self.button_color,self.rect)
  19. self.screen.blit(self.msg_image,self.msg_image_rect)

文件8: game_functions.py

  1. import sys
  2. import pygame
  3. from bullet import Bullet
  4. from target import Target
  5. from time import sleep
  6. def check_keydown_events(event,ai_settings,screen,ship,bullets):
  7. if event.key==pygame.K_DOWN:
  8. ship.moving_down=True
  9. elif event.key==pygame.K_UP:
  10. ship.moving_up=True
  11. elif event.key==pygame.K_SPACE:
  12. fire_bullet(ai_settings,screen,ship,bullets)
  13. elif event.key==pygame.K_q:
  14. sys.exit()
  15. def fire_bullet(ai_settings,screen,ship,bullets):
  16. if len(bullets)<ai_settings.bullets_allowed:
  17. new_bullet=Bullet(ai_settings, screen,ship)
  18. bullets.add(new_bullet)
  19. def check_fail(ai_settings,bullets,stats,target,screen): #定义一个函数,在主函数中被调用
  20. for bullet in bullets.copy(): #bullets为其他程序其他地方定义的一个编组
  21. if bullet.rect.right>=screen.get_rect().right: #判断bullet对象是否飞出屏幕右侧
  22. bullets.remove(bullet) #若bullet飞出右侧,则销毁该bullet
  23. ai_settings.fail_times+=1 #fail_times自增1
  24. print(ai_settings.fail_times) #控制台输出fail_times,用于监控不碰撞的次数
  25. elif pygame.sprite.spritecollideany(target,bullets):
  26. ai_settings.fail_times=0 #失败清零``
  27. elif ai_settings.fail_times>2:
  28. ai_settings.fail_times=0 #失败清零
  29. stats.game_active=False #游戏终止
  30. pygame.mouse.set_visible(True) #鼠标可见
  31. def check_keyup_events(event,ship):
  32. if event.key==pygame.K_DOWN:
  33. ship.moving_down=False
  34. elif event.key==pygame.K_UP:
  35. ship.moving_up=False
  36. def check_events(ai_settings,screen,stats,play_button,ship,target,bullets):
  37. for event in pygame.event.get():
  38. if event.type==pygame.QUIT:
  39. sys.exit()
  40. elif event.type==pygame.MOUSEBUTTONDOWN:
  41. mouse_x,mouse_y=pygame.mouse.get_pos()
  42. check_play_button(ai_settings,screen,stats,play_button,ship,target,bullets,mouse_x,mouse_y)
  43. elif event.type==pygame.KEYDOWN:
  44. check_keydown_events(event,ai_settings,screen,ship,bullets)
  45. elif event.type==pygame.KEYUP:
  46. check_keyup_events(event,ship)
  47. def check_play_button(ai_settings,screen,stats,play_button,ship,target,bullets,mouse_x,mouse_y):
  48. button_clicked=play_button.rect.collidepoint(mouse_x,mouse_y)
  49. if button_clicked and not stats.game_active:
  50. pygame.mouse.set_visible(False)
  51. stats.reset_stats()
  52. stats.game_active=True
  53. bullets.empty()
  54. create_fleet(ai_settings,screen,ship,target)
  55. ship.center_ship()
  56. def update_screen(ai_settings, screen, stats, ship, target,bullets, play_button):
  57. screen.fill(ai_settings.bg_color)
  58. for bullet in bullets.sprites():
  59. bullet.draw_bullet()
  60. ship.blitme()
  61. target.draw_target()
  62. if not stats.game_active:
  63. play_button.draw_button()
  64. pygame.display.flip()
  65. def update_bullets(ai_settings, screen, ship, target,bullets):
  66. bullets.update()
  67. for bullet in bullets.copy():
  68. if bullet.rect.right>screen.get_rect().right:
  69. bullets.remove(bullet)
  70. check_bullet_alien_collisions(ai_settings,screen,ship,target,bullets)
  71. def check_bullet_alien_collisions(ai_settings,screen,ship,target,bullets):
  72. collisions=pygame.sprite.spritecollideany(target,bullets,False)
  73. def create_fleet(ai_settings,screen,ship,target):
  74. target=Target(ai_settings,screen)
  75. def check_fleet_edges(ai_settings,target):
  76. if target.check_edges():
  77. change_fleet_direction(ai_settings,target)
  78. def change_fleet_direction(ai_settings,target):
  79. ai_settings.fleet_direction*=-1
  80. def update_target(ai_settings,stats,screen,ship,target,bullets):
  81. check_fleet_edges(ai_settings,target)
  82. target.update()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/368405
推荐阅读
相关标签
  

闽ICP备14008679号