当前位置:   article > 正文

星际争霸之小霸王之小蜜蜂(十五)--剧将终场

星际争霸之小霸王之小蜜蜂(十五)--剧将终场

系列文章目录

星际争霸之小霸王之小蜜蜂(十四)--资本家的眼泪

星际争霸之小霸王之小蜜蜂(十三)--接着奏乐接着舞

星际争霸之小霸王之小蜜蜂(十二)--猫有九条命

 星际争霸之小霸王之小蜜蜂(十一)--杀杀杀

 星际争霸之小霸王之小蜜蜂(十)--鼠道

星际争霸之小霸王之小蜜蜂(九)--狂鼠之灾

 星际争霸之小霸王之小蜜蜂(八)--蓝皮鼠和大脸猫

  星际争霸之小霸王之小蜜蜂(七)--消失的子弹

星际争霸之小霸王之小蜜蜂(六)--让子弹飞

 星际争霸之小霸王之小蜜蜂(五)--为小蜜蜂降速

 星际争霸之小霸王之小蜜蜂(四)--事件监听-让小蜜蜂动起来


目录

系列文章目录

文章目录

前言

二、整体分析

 alien_invasion.py

 game_functions.py

 alien.py

 bullet.py

 settings.py

 button.py

 game_stats.py

 scoreboard.py

 ship.py

总结


前言

我们已经基本完成了书上的所有操作,共用了14天学习,今天我们进行整理,将完整代码整理出来,供大家学习,也为今后我自己调阅。


一、心得体会

经过这段时间的学习,可以说浅显了解了pygame这个模块的一些函数使用,确实针对游戏的设计方面提供了很便利的支持。初步掌握了这些方法,我们可以很方便的作出一些其他的游戏。比如扫雷游戏,通过鼠标点击判断,再比如说贪食蛇,主要核心应该是碰撞的监测。至于纸牌或是其他的游戏目前还没有想好。

 除了接触到一些新的函数,还学习了面对对象编程的方法,虽然不在公司上班,只是个人的小打小闹,但是先进的编程理念学起来肯定没有错。同时还感受到了规范的命名规则,对于我来说有点困难,因为英语不行,所以这个命名会大大降低我的编写速度,在调用函数时,要反复的看单词怎么写,太浪费时间了。

二、整体分析

这次我们总共写了9个类,其实还是有规律的,不同的对象各自建一个类,比如说小花猫、小老鼠和子弹。然后需要修改的参数放在一个类,事件监听放在一个类等等,现在将所有的类代码整理如下:

 alien_invasion.py

  1. import pygame
  2. import settings
  3. from ship import Ship
  4. import game_functions as gf
  5. from pygame.sprite import Group
  6. from alien import Alien
  7. from game_stats import GameStats
  8. from button import Button
  9. from scoreboard import Scoreboard
  10. def run_game():
  11. pygame.init()
  12. new_setting=settings.Settings()
  13. screen = pygame.display.set_mode((new_setting.screen_width,new_setting.screen_height))
  14. ship = Ship(screen,new_setting)
  15. alien = Alien(new_setting,screen)
  16. pygame.display.set_caption("狂敲代码的橘子")
  17. bullets = Group()
  18. aliens = Group()
  19. gf.create_fleet(new_setting,screen,aliens)
  20. stats = GameStats(new_setting)
  21. play_button = Button(new_setting,screen,'Play')
  22. sb = Scoreboard(new_setting,screen,stats)
  23. while True:
  24. gf.check_events(new_setting,screen,ship,bullets,stats,play_button,aliens,sb)
  25. if stats.game_active:
  26. ship.update()
  27. gf.update_bullets(new_setting,screen,bullets,aliens,stats,sb)
  28. gf.update_aliens(new_setting,stats,screen,ship,aliens,bullets)
  29. gf.update_screen(new_setting,screen,stats,ship,bullets,aliens,play_button,sb)
  30. run_game()

 game_functions.py

  1. import sys
  2. import pygame
  3. import time
  4. from bullet import Bullet
  5. from alien import Alien
  6. def check_keydown_events(event,new_setting,screen,ship,bullets):
  7. if event.key == pygame.K_RIGHT:
  8. ship.moving_right = True
  9. elif event.key == pygame.K_LEFT:
  10. ship.moving_left = True
  11. elif event.key == pygame.K_SPACE:
  12. fire_bullet(new_setting, screen, ship, bullets)
  13. elif event.key == pygame.K_q:
  14. sys.exit()
  15. def check_keyup_events(event,ship):
  16. if event.key == pygame.K_RIGHT:
  17. ship.moving_right = False
  18. if event.key == pygame.K_LEFT:
  19. ship.moving_left = False
  20. def check_events(new_setting,screen,ship,bullets,stats,play_button,aliens,sb):
  21. for event in pygame.event.get():
  22. if event.type == pygame.QUIT:
  23. sys.exit()
  24. elif event.type==pygame.KEYDOWN:
  25. check_keydown_events(event,new_setting,screen,ship,bullets)
  26. elif event.type==pygame.KEYUP:
  27. check_keyup_events(event, ship)
  28. elif event.type == pygame.MOUSEBUTTONDOWN:
  29. mouse_x,mouse_y = pygame.mouse.get_pos()
  30. check_play_button(stats,play_button,mouse_x,mouse_y,new_setting,aliens,bullets,ship,screen,sb)
  31. def check_play_button(stats,play_button,mouse_x,mouse_y,new_setting,aliens,bullets,ship,screen,sb):
  32. if play_button.rect.collidepoint(mouse_x,mouse_y) and not stats.game_active:
  33. pygame.mouse.set_visible(False)
  34. stats.reset_stats()
  35. stats.game_active = True
  36. aliens.empty()
  37. bullets.empty()
  38. sb.prep_score()
  39. new_setting.initialize_dynamic_settings()
  40. create_fleet(new_setting,screen,aliens)
  41. ship.center_ship()
  42. def fire_bullet(new_setting,screen,ship,bullets):
  43. if len(bullets) < new_setting.bullets_allowed:
  44. new_bullet = Bullet(new_setting, screen, ship)
  45. bullets.add(new_bullet)
  46. def update_bullets(new_setting,screen,bullets,aliens,stats,sb):
  47. check_bullet_alien_collisions(new_setting,screen,bullets,aliens,stats,sb)
  48. bullets.update()
  49. for bullet in bullets.copy():
  50. if bullet.rect.bottom <= 0:
  51. bullets.remove(bullet)
  52. def check_bullet_alien_collisions(new_setting,screen,bullets,aliens,stats,sb):
  53. collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
  54. if collisions:
  55. for aliens in collisions.values():
  56. stats.score += len(aliens)
  57. sb.prep_score()
  58. if len(aliens) == 0:
  59. bullets.empty()
  60. new_setting.increase_speed()
  61. create_fleet(new_setting, screen, aliens)
  62. def create_fleet(new_setting,screen,aliens):
  63. for row_number in range(3):
  64. for alien_number in range(6):
  65. alien = Alien(new_setting,screen)
  66. alien.x = alien.rect.width+2*alien.rect.width*alien_number
  67. alien.rect.x = alien.x
  68. alien.rect.y = alien.rect.height+2*alien.rect.height*row_number
  69. aliens.add(alien)
  70. def update_screen(new_setting,screen,stats,ship,bullets,aliens,play_button,sb):
  71. screen.fill(new_setting.bg_color)
  72. for bullet in bullets.sprites():
  73. bullet.draw_bullet()
  74. ship.blitme()
  75. aliens.draw(screen)
  76. sb.show_score()
  77. if not stats.game_active:
  78. play_button.draw_button()
  79. pygame.display.flip()
  80. def ship_hit(new_setting,stats,screen,ship,aliens,bullets):
  81. stats.ships_left -= 1
  82. aliens.empty()
  83. bullets.empty()
  84. create_fleet(new_setting, screen, aliens)
  85. ship.center_ship()
  86. if stats.ships_left > 0:
  87. stats.ships_left -= 1
  88. time.sleep(0.5)
  89. else:
  90. stats.game_active = False
  91. pygame.mouse.set_visible(True)
  92. def check_aliens_bottom(new_setting,stats,screen,ship,aliens,bullets):
  93. screen_rect = screen.get_rect()
  94. for alien in aliens.sprites():
  95. if alien.rect.bottom >= screen_rect.bottom:
  96. ship_hit(new_setting,stats,screen,ship,aliens,bullets)
  97. break
  98. def update_aliens(new_setting,stats,screen,ship,aliens,bullets):
  99. check_fleet_edges(new_setting,aliens)
  100. aliens.update()
  101. if pygame.sprite.spritecollideany(ship,aliens):
  102. ship_hit(new_setting, stats, screen, ship, aliens, bullets)
  103. check_aliens_bottom(new_setting, stats, screen, ship, aliens, bullets)
  104. def change_fleet_direction(new_setting,aliens):
  105. for alien in aliens.sprites():
  106. alien.rect.y += new_setting.fleet_drop_speed
  107. new_setting.fleet_direction *=-1
  108. def check_fleet_edges(new_setting,aliens):
  109. for alien in aliens.sprites():
  110. if alien.check_edges():
  111. change_fleet_direction(new_setting,aliens)
  112. break

 alien.py

  1. import pygame
  2. from pygame.sprite import Sprite
  3. class Alien(Sprite):
  4. def __init__(self,new_setting,screen):
  5. super(Alien,self).__init__()
  6. self.screen = screen
  7. self.new_setting = new_setting
  8. self.image = pygame.image.load('mouse.jpg')
  9. self.rect = self.image.get_rect()
  10. self.rect.x = self.rect.width
  11. self.rect.y = self.rect.height
  12. self.x = float(self.rect.x)
  13. def blitme(self):
  14. self.screen.blit(self.image,self.rect)
  15. def update(self):
  16. self.x += (self.new_setting.alien_speed_factor*self.new_setting.fleet_direction)
  17. self.rect.x = self.x
  18. def check_edges(self):
  19. screen_rect = self.screen.get_rect()
  20. if self.rect.right >= screen_rect.right:
  21. return True
  22. elif self.rect.left<=0:
  23. return True

 bullet.py

  1. import pygame
  2. from pygame.sprite import Sprite
  3. class Bullet(Sprite):
  4. def __init__(self,new_setting,screen,ship):
  5. super(Bullet,self).__init__()
  6. self.screen = screen
  7. self.rect = pygame.Rect(0,0,new_setting.bullet_width,new_setting.bullet_hight)
  8. self.rect.centerx = ship.rect.centerx
  9. self.rect.top = ship.rect.top
  10. self.y = float(self.rect.y)
  11. self.color = new_setting.bullet_color
  12. self.speed_factor = new_setting.bullet_speed_factor
  13. def update(self):
  14. self.y -=self.speed_factor
  15. self.rect.y =self.y
  16. def draw_bullet(self):
  17. pygame.draw.rect(self.screen,self.color,self.rect)

 settings.py

  1. class Settings():
  2. def __init__(self):
  3. self.screen_width = 800
  4. self.screen_height = 600
  5. self.bg_color = (255,255,255)
  6. self.ship_limit = 2
  7. self.bullet_width = 2
  8. self.bullet_hight = 5
  9. self.bullet_color = 60,60,60
  10. self.bullets_allowed = 20
  11. self.fleet_drop_speed = 30
  12. self.speedup_scale = 1.2
  13. self.initialize_dynamic_settings()
  14. def initialize_dynamic_settings(self):
  15. self.ship_speed_factor = 0.1
  16. self.bullet_speed_factor = 0.5
  17. self.alien_speed_factor = 0.5
  18. self.fleet_direction = 1
  19. def increase_speed(self):
  20. self.ship_speed_factor *= self.speedup_scale
  21. self.bullet_speed_factor *= self.speedup_scale
  22. self.alien_speed_factor *= self.speedup_scale

 button.py

  1. import pygame.font
  2. class Button():
  3. def __init__(self,new_setting,screen,msg):
  4. self.screen = screen
  5. self.screen_rect =screen.get_rect()
  6. #设置按钮的尺寸
  7. self.width,self.height = 200,50
  8. self.button_color = (0,255,0)
  9. self.text_color = (255,255,255)
  10. self.font = pygame.font.SysFont(None,48)
  11. #创建按钮的rect对象
  12. self.rect = pygame.Rect(0,0,self.width,self.height)
  13. self.rect.center = self.screen_rect.center
  14. self.prep_msg(msg)
  15. def prep_msg(self,msg):
  16. self.msg_image = self.font.render(msg,True,self.text_color,self.button_color)
  17. self.msg_image_rect = self.msg_image.get_rect()
  18. self.msg_image_rect.center = self.rect.center
  19. def draw_button(self):
  20. self.screen.fill(self.button_color,self.rect)
  21. self.screen.blit(self.msg_image,self.msg_image_rect)

 game_stats.py

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

 scoreboard.py

  1. import pygame.font
  2. class Scoreboard():
  3. def __init__(self,new_setting,screen,stats):
  4. self.screen = screen
  5. self.screen_rect = screen.get_rect()
  6. self.new_setting = new_setting
  7. self.stats = stats
  8. self.text_color = (30,30,30)
  9. self.font = pygame.font.SysFont(None,48)
  10. self.prep_score()
  11. def prep_score(self):
  12. score_str = str(self.stats.score)
  13. self.score_image = self.font.render(score_str,True,self.text_color,self.new_setting.bg_color)
  14. self.score_rect = self.score_image.get_rect()
  15. self.score_rect.right = self.screen_rect.right-20
  16. self.score_rect.top = 20
  17. def show_score(self):
  18. self.screen.blit(self.score_image,self.score_rect)

 ship.py

  1. import pygame
  2. class Ship():
  3. def __init__(self,screen,new_settings):
  4. self.screen = screen
  5. self.image = pygame.image.load('cat.png')
  6. self.rect = self.image.get_rect()
  7. self.screen_rect = screen.get_rect()
  8. self.rect.centerx = self.screen_rect.centerx
  9. self.rect.bottom=self.screen_rect.bottom
  10. self.new_settings = new_settings
  11. self.moving_right = False
  12. self.moving_left = False
  13. self.center = float(self.rect.centerx)
  14. def update(self):
  15. if self.moving_right and self.rect.right < self.screen_rect.right:
  16. self.center += self.new_settings.ship_speed_factor
  17. if self.moving_left and self.rect.left > 0:
  18. self.center -= self.new_settings.ship_speed_factor
  19. self.rect.centerx = 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.centerx

总结

后面我准备将游戏在改一改,时代在进步,游戏画面我是无能为力,但是游戏机制可以改一改,这种老土的游戏机制我实在玩不下去,不符合我们现在玩的飞机大战,将小花猫的行动轨迹改一改,可以全屏移动,然后将小老鼠也改成不规则的,而不是集体向下 向右,如果有可能的话,改成躲避类,最后小老鼠也可以放子弹。

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

闽ICP备14008679号