当前位置:   article > 正文

外星人入侵python的pygame模板_pygame外星人入侵素材包

pygame外星人入侵素材包
需要一个 飞船和外星人和所以的设置类,还有飞船发射子弹的类,最后也需要一个记录分数的尅,有最高分,和当前分数,每个类都有自己的功能

 下面是所以每个类的代码:

主函数,运行
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from pygame.sprite import Group
  4. from settings import Settings
  5. from game_stats import GameStats
  6. from scoreboard import Scoreboard
  7. from button import Button
  8. from ship import Ship
  9. from alien import Alien
  10. import game_functions as gf
  11. def run_game():
  12. pygame.mixer.init()
  13. # 加载音乐
  14. pygame.mixer.music.load("images/alien.mp3")
  15. # Initialize pygame, settings, and screen object.
  16. pygame.init()
  17. ai_settings = Settings()
  18. screen = pygame.display.set_mode(
  19. (ai_settings.screen_width, ai_settings.screen_height))
  20. pygame.display.set_caption("Alien Invasion")
  21. """创建play按钮"""
  22. play_button = Button(ai_settings,screen,"Play")
  23. """创建一个用于存储游戏统计信息的实例"""
  24. stats = GameStats(ai_settings)
  25. sb = Scoreboard(ai_settings,screen,stats)
  26. # Set the background color.
  27. bg_color = (230, 230, 230)
  28. """Make a ship."""
  29. ship = Ship(ai_settings, screen)
  30. # Make a group to store bullets in.
  31. bullets = Group()
  32. aliens = Group()
  33. """创建外星人群"""
  34. gf.create_fleet(ai_settings,screen,ship,aliens)
  35. # Start the main loop for the game.
  36. while True:
  37. # 检查音乐流播放,有返回True,没有返回False
  38. # 如果没有音乐流则选择播放
  39. if pygame.mixer.music.get_busy() == False:
  40. pygame.mixer.music.play()
  41. """获取 所有的事件"""
  42. gf.check_events(ai_settings, screen, stats,sb,play_button,ship,
  43. aliens, bullets)
  44. if stats.game_active:
  45. """更新飞船,飞船的移动"""
  46. ship.update()
  47. """更新子弹,子弹的移动"""
  48. gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets)
  49. """更新外星人,外星人的移动"""
  50. gf.update_aliens(ai_settings,screen,stats,sb,ship,aliens,bullets)
  51. """重新绘制 屏幕"""
  52. gf.update_screen(ai_settings, screen,stats, sb,ship,aliens,
  53. bullets,play_button)
  54. run_game()

#子弹类

  1. # -*- coding: utf-8 -*-
  2. # 子弹
  3. import pygame
  4. from pygame.sprite import Sprite
  5. class Bullet(Sprite):
  6. """A class to manage bullets fired from the ship"""
  7. def __init__(self, ai_settings, screen, ship,stats):
  8. """Create a bullet object at the ship's current position."""
  9. super(Bullet, self).__init__()
  10. self.screen = screen
  11. self.num = stats.ships_left
  12. # Create a bullet rect at (0, 0) and then set correct position.
  13. self.rect = pygame.Rect(0, 0, ai_settings.bullet_width,
  14. ai_settings.bullet_height)
  15. self.rect.centerx = ship.rect.centerx
  16. self.rect.top = ship.rect.top
  17. # Store the bullet's position as a decimal value.
  18. self.y = float(self.rect.y)
  19. self.color = ai_settings.bullet_color
  20. self.speed_factor = ai_settings.bullet_speed_factor
  21. if self.num == 2:
  22. self.rect1 = pygame.Rect(0, 0, ai_settings.bullet_width,
  23. ai_settings.bullet_height)
  24. self.rect1.centerx = ship.rect.centerx + 20
  25. self.rect1.top = ship.rect.top
  26. # Store the bullet's position as a decimal value.
  27. self.y1 = float(self.rect.y)
  28. if self.num == 1:
  29. """第二发子弹"""
  30. self.rect1 = pygame.Rect(0, 0, ai_settings.bullet_width,
  31. ai_settings.bullet_height)
  32. self.rect1.centerx = ship.rect.centerx + 20
  33. self.rect1.top = ship.rect.top
  34. # Store the bullet's position as a decimal value.
  35. self.y1 = float(self.rect1.y)
  36. self.x1 = float(self.rect1.x)
  37. """第三发子弹"""
  38. self.rect2 = pygame.Rect(0, 0, ai_settings.bullet_width,
  39. ai_settings.bullet_height)
  40. self.rect2.centerx = ship.rect.centerx - 20
  41. self.rect2.top = ship.rect.top
  42. # Store the bullet's position as a decimal value.
  43. self.y2 = float(self.rect2.y)
  44. self.x2 = float(self.rect2.x)
  45. def update(self):
  46. """Move the bullet up the screen."""
  47. # Update the decimal position of the bullet.
  48. self.y -= self.speed_factor
  49. # Update the rect position.
  50. self.rect.y = self.y
  51. if self.num == 2 :
  52. self.y1 -= self.speed_factor
  53. # Update the rect position.
  54. self.rect1.y = self.y1
  55. if self.num == 1:
  56. self.y1 -= self.speed_factor
  57. self.x1 += 0.5
  58. # Update the rect position.
  59. self.rect1.y = self.y1
  60. self.rect1.x = self.x1
  61. self.y2 -= self.speed_factor
  62. self.x2 -= 0.5
  63. # Update the rect position.
  64. self.rect2.y = self.y2
  65. self.rect2.x = self.x2
  66. def draw_bullet(self):
  67. """Draw the bullet to the screen."""
  68. pygame.draw.rect(self.screen, self.color, self.rect)
  69. if self.num == 2:
  70. pygame.draw.rect(self.screen, self.color, self.rect1)
  71. if self.num == 1:
  72. pygame.draw.rect(self.screen, self.color, self.rect1)
  73. pygame.draw.rect(self.screen, self.color, self.rect2)
#开始按钮
  1. # -*- coding: utf-8 -*-
  2. import pygame.font
  3. class Button():
  4. def __init__(self,ai_settings,screen,msg):
  5. """初始化按钮的属性"""
  6. self.screen = screen
  7. self.screen_rect = screen.get_rect()
  8. #设置按钮的尺寸和其他属性
  9. self.width,self.height = 200,50
  10. self.button_color = (0,255,0)
  11. self.text_color = (255,255,255)
  12. self.font = pygame.font.SysFont(None,48)
  13. #创建按钮的rect对象,并使其剧中
  14. self.rect = pygame.Rect(0,0,self.width,self.height)
  15. self.rect.center = self.screen_rect.center
  16. #按钮的标签只需要一次
  17. self.prep_msg(msg)
  18. def prep_msg(self,msg):
  19. """将msg渲染为图像,并使其在按钮上居中"""
  20. self.msg_image = self.font.render(msg,True,self.text_color,self.button_color)
  21. self.msg_image_rect = self.msg_image.get_rect()
  22. self.msg_image_rect.center = self.rect.center
  23. def draw_button(self):
  24. #绘制一个用颜色填充的按钮,再绘制文本
  25. self.screen.fill(self.button_color,self.rect)
  26. self.screen.blit(self.msg_image,self.msg_image_rect)
#状态

  1. # -*- coding: utf-8 -*-
  2. class GameStats():
  3. """跟踪游戏的统计信息"""
  4. def __init__(self,ai_settings):
  5. """初始化统计信息"""
  6. self.ai_settings = ai_settings
  7. self.reset_stats()
  8. #游戏刚开始处于0活跃状态
  9. self.game_active = False
  10. #在任何情况下都不应重置最高得分
  11. self.high_score = 0
  12. def reset_stats(self):
  13. """初始化在游戏与逆行期间可能变化的统计信息"""
  14. self.ships_left = self.ai_settings.ship_limit
  15. self.score = 0
  16. self.level = 1

#分数类

  1. # -*- coding: utf-8 -*-
  2. import pygame.font
  3. from pygame.sprite import Group
  4. from ship import Ship
  5. class Scoreboard():
  6. """显示得分信息的类"""
  7. def __init__(self,ai_settings,screen,stats):
  8. """初始化显示得分涉及的属性"""
  9. self.screen = screen
  10. self.screen_rect = screen.get_rect()
  11. self.ai_settings = ai_settings
  12. self.stats = stats
  13. #显示得分信息时候使用的字体设置
  14. self.text_color = (30,30,30)
  15. self.font = pygame.font.SysFont(None,48)
  16. #准备初始得分图像
  17. self.prep_score()
  18. self.prep_high_score()
  19. self.prep_level()
  20. self.prep_ships()
  21. #准备包含最高分和当前的分的图像
  22. self.prep_score()
  23. self.prep_high_score()
  24. def prep_score(self):
  25. """将得分转化为一副渲染的图像"""
  26. rounded_score = int(round(self.stats.score,-1))
  27. score_str = "Score:"+"{: ,}".format(rounded_score)
  28. self.score_image = self.font.render(score_str,True,self.text_color,
  29. self.ai_settings.bg_color1)
  30. #将得分放在屏幕右上角
  31. self.score_rect = self.score_image.get_rect()
  32. self.score_rect.right = self.screen_rect.right -20
  33. self.score_rect.top = 20
  34. def prep_high_score(self):
  35. """将最高分转换为渲染的图像"""
  36. high_score = int(round(self.stats.high_score,-1))
  37. high_score_str = "High Score:"+"{: ,}".format(high_score)
  38. self.high_score_image = self.font.render(high_score_str, True,
  39. self.text_color,self.ai_settings.bg_color1)
  40. # 将最高得分放在屏幕中央
  41. self.high_score_rect = self.high_score_image.get_rect()
  42. self.high_score_rect.centerx = self.screen_rect.centerx
  43. self.high_score_rect.top = self.score_rect.top
  44. def show_score(self):
  45. """在屏幕上显示得分和最高得分"""
  46. self.screen.blit(self.score_image,self.score_rect)
  47. self.screen.blit(self.high_score_image,self.high_score_rect)
  48. self.screen.blit(self.level_image,self.level_rect)
  49. #绘制飞船
  50. self.ships.draw(self.screen)
  51. def prep_level(self):
  52. """将等级转化为渲染的图像"""
  53. level_str = "level:" + "{: ,}".format(self.stats.level)
  54. self.level_image = self.font.render(level_str,True,
  55. self.text_color,self.ai_settings.bg_color1)
  56. #将等级放在得分下面
  57. self.level_rect = self.level_image.get_rect()
  58. self.level_rect.right = self.score_rect.right
  59. self.level_rect.top = self.score_rect.bottom +10
  60. def prep_ships(self):
  61. """显示还剩下多少搜飞船"""
  62. self.ships = Group()
  63. for ship_number in range(self.stats.ships_left-1):
  64. ship = Ship(self.ai_settings,self.screen)
  65. ship.rect.x = 10+ship_number * ship.rect.width
  66. ship.rect.y = 10
  67. self.ships.add(ship)

#设置类

  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. # 管理所有与游戏设置有关的操作
  4. class Settings():
  5. """A class to store all settings for Alien Invasion."""
  6. def __init__(self):
  7. """Initialize the game's settings."""
  8. # Screen settings
  9. self.screen_width = 550
  10. self.screen_height = 520
  11. self.bg_color = (230, 230, 230)
  12. self.bg_color1 = pygame.image.load('images/b.jpg')
  13. self.rect = self.bg_color1.get_rect()
  14. # 飞船设置
  15. #self.ship_speed_factor = 1.5
  16. #self.bullet_speed_factor = 3
  17. self.ship_limit = 3
  18. # Bullet settings
  19. self.bullet_speed_factor = 1.5
  20. self.bullet_width = 3
  21. self.bullet_height = 25
  22. self.bullet_color = 225, 67, 209
  23. self.bullets_allowed = 5
  24. #外星人设置
  25. #self.alien_speed_factor = 1
  26. # self.fleet_drop_speed = 10
  27. #fleet_direction为1表示向右移动,-1表示向左移动
  28. self.fleet_direction = 1
  29. #以什么样的速度加快游戏节奏
  30. self.fleet_drop_speed = 20
  31. #加快游戏节奏的速度
  32. self.speedup_scale = 1.1
  33. #外星人点数提高
  34. self.score_scale = 1.5
  35. self.initialize_dynamic_settings()
  36. def increase_speed(self):
  37. """提高速度设置"""
  38. self.ship_speed_factor *= self.speedup_scale
  39. self.bullet_speed_factor *= self.speedup_scale
  40. self.alien_speed_factor *= self.speedup_scale
  41. self.alien_points = int(self.alien_points * self.score_scale)
  42. #print(self.alien_points)
  43. def initialize_dynamic_settings(self):
  44. """初始化岁游戏进行二变化的设置"""
  45. self.ship_speed_factor = 2
  46. self.bullet_speed_factor = 5
  47. self.alien_speed_factor = 1.5
  48. # fleet_direction为1表示向右移动,-1表示向左移动
  49. self.fleet_direction = 1
  50. #积分
  51. self.alien_points = 50

#飞船类

  1. # -*- coding: utf-8 -*-
  2. # ship
  3. import pygame
  4. from pygame.sprite import Sprite
  5. class Ship(Sprite):
  6. def __init__(self, ai_settings, screen):
  7. super(Ship,self).__init__()
  8. """初始化飞船并设置其初始位置"""
  9. self.screen = screen
  10. self.ai_settings = ai_settings
  11. """加载飞船图像并获取其外接矩形"""
  12. self.image = pygame.image.load('images/ship.png')
  13. self.rect = self.image.get_rect()
  14. self.screen_rect = screen.get_rect()
  15. """将每艘飞船放在屏幕底部中央"""
  16. self.rect.centerx = self.screen_rect.centerx
  17. self.rect.bottom = self.screen_rect.bottom
  18. self.center = float(self.rect.centerx)
  19. """移动标记"""
  20. self.moving_right = False
  21. self.moving_left = False
  22. def update(self):
  23. """根据标记的状态调整飞船的位置"""
  24. if self.moving_right and self.rect.right < self.screen_rect.right:
  25. self.center += self.ai_settings.ship_speed_factor
  26. elif self.moving_left and self.rect.left > self.screen_rect.left:
  27. self.center -= self.ai_settings.ship_speed_factor
  28. self.rect.centerx = self.center
  29. def blitme(self):
  30. """在制定位置绘制飞船"""
  31. self.screen.blit(self.image, self.rect)
  32. def center_ship(self):
  33. self.center = self.screen_rect.centerx

#外星人类

  1. # -*- coding: utf-8 -*-
  2. # 外星人类
  3. import pygame
  4. from pygame.sprite import Sprite
  5. class Alien(Sprite):
  6. """表示单个外星人"""
  7. def __init__(self,ai_settings,screen):
  8. """初始化外星人并设置其起始位置"""
  9. super(Alien,self).__init__()
  10. self.screen = screen
  11. self.ai_settings = ai_settings
  12. #加载外星人图像,并设置rect属性
  13. self.image = pygame.image.load('images/alien.png')
  14. self.rect = self.image.get_rect()
  15. #每个外星人最初都在屏幕右上角附近
  16. self.rect.x = self.rect.width
  17. self.rect.y = self.rect.height
  18. #存储外星人的准确位置
  19. self.x = float(self.rect.x)
  20. def check_edges(self):
  21. """如果外星人位于屏幕边缘,就返回True"""
  22. screen_rect = self.screen.get_rect()
  23. if self.rect.right >=screen_rect.right:
  24. return True
  25. elif self.rect.left <=0:
  26. return True
  27. def update(self):
  28. """向左或者向右移动外星人"""
  29. self.x += (self.ai_settings.fleet_direction * self.ai_settings.alien_speed_factor)
  30. self.rect.x = self.x
  31. def blitme(self):
  32. """在指定位置绘制外星人"""
  33. self.screen.blit(self.image,self.rect).rect.x = self.x

 

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

闽ICP备14008679号