• 外星人入侵游戏-(创新版)


    🌈write in front🌈
    🧸大家好,我是Aileen🧸.希望你看完之后,能对你有所帮助,不足请指正!共同学习交流.
    🆔本文由Aileen_0v0🧸 原创 CSDN首发🐒 如需转载还请通知⚠️
    📝个人主页:Aileen_0v0🧸—CSDN博客
    🎁欢迎各位→点赞👍 + 收藏⭐️ + 留言📝​
    📣系列专栏:Aileen_0v0🧸的PYTHON学习系列专栏——CSDN博客
    🗼我的格言:"没有罗马,那就自己创造罗马~" 

    6a78491801a74bbd8d932659b06e11db.gif#pic_center

    目录

    首先,在python上 安装pygame

    然后,创建文件夹 要注意分级别​

     插入的图片

    主函数 Aileen_invasion的文件

    创建外星人aileen的文件

    子弹bullet的文件

    按钮button的文件

     游戏数据game_stats的文件

    游戏分数scoreboard的文件

    游戏设置settings的文件 

    游戏飞船ship的文件

    ​编辑 全屏模式下的游戏

    ​编辑

    小窗口下的游戏


    首先,在python上 安装pygame

    资源--->https://download.csdn.net/download/Aileenvov/88301424?spm=1001.2014.3001.5503

    然后转到228页,根据步骤进行安装

    然后,创建文件夹 要注意分级别d83c30c505d14fdab2d805e6d097195b.png

     插入的图片

    插入图片:需要注意图片的大小比例,否则可能显示不出来,这需要根据系统屏幕大小进行设置,

    将所需要的图片和音乐拖到对应的文件夹,这是我的图片

    72e4c4b42e4c49199e797ea4d03f55fc.png

    4905cbd862144a6bb592f81855e39f3d.png196c3ebedc5d44b9a7f1de63927e7341.pnga71eecce79f4436a91ae3d3a6a01f863.png6ad6d2d591844a9687367caeb96299e6.pnge4b6302a36574834a2d94592a999b4c9.png

    主函数 Aileen_invasion的文件

    1. import sys
    2. from time import sleep
    3. import pygame
    4. from settings import Settings
    5. from game_starts import GameStats
    6. from scoreboard import Scoreboard
    7. from button import Button
    8. from ship import Ship
    9. from bullet import Bullet
    10. from aileen import Aileen
    11. class AileenInvasion:
    12. """Overall class to manage game assets and behavior.整体类来管理游戏资产和行为"""
    13. def __init__(self):#初始化
    14. """Initialize the game, and create game resources."""
    15. pygame.init()
    16. self.settings = Settings()
    17. self.screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
    18. self.settings.screen_width = self.screen.get_rect().width
    19. self.settings.screen_height = self.screen.get_rect().height
    20. # self.screen = pygame.display.set_mode(
    21. # (self.settings.screen_width,self.settings.screen_height))
    22. #类中变量前有self,说明该变量绑定在当前实例化本身
    23. pygame.display.set_caption("Aileen Invasion")
    24. # Creation an instance to store game statistics.
    25. self.stats = GameStats(self)
    26. # Create an instance to store game statics,
    27. # and create a scoreboard.
    28. self.sb = Scoreboard(self)
    29. self.ship = Ship(self)
    30. self.bullets = pygame.sprite.Group()#Group可以批量使用的函数
    31. self.aileens = pygame.sprite.Group()
    32. self._create_fleet()
    33. #Make the play button.
    34. self.play_button = Button(self,"Play")
    35. self.bg_color =(0,0,225) #代表红 绿 蓝 三颜色
    36. # def _create_fleet(self):
    37. # """Create the fleet of aileens."""
    38. # #Make a aileen
    39. # aileen = Aileen(self)
    40. # self.aileens.add(aileen)
    41. # #set the background color.
    42. def run_game(self): #功能:1监听事件,2处理事件,3更新屏幕事件
    43. """Start the main loop for the game."""
    44. while True:
    45. # 1监听事件函数--监听和处理用户的行为
    46. self._check_events()#将监听事件(封装)外包给这个函数,减轻run_game的工作量---这个过程叫重构(refactory)
    47. if self.stats.game_active:
    48. # 2处理事件函数
    49. self.ship.update()
    50. #更新子弹
    51. self._update_bullets()
    52. self._update_aileens()
    53. # 3 屏幕更新函数
    54. self._update_screen()
    55. def _update_bullets(self):
    56. self.bullets.update()
    57. if not self.aileens:
    58. # Destory existing bullets and create new fleet
    59. self.bullets.empty()
    60. self._create_fleet()
    61. # Get rid of the bullets that have disappeared.
    62. for bullet in self.bullets.copy():
    63. if bullet.rect.bottom <= 0:
    64. self.bullets.remove(bullet)
    65. self._check_bullet_aileen_collisions()
    66. def _check_bullet_aileen_collisions(self):
    67. """Respond to bullet-aileen collisions."""
    68. """ Remove any bullets and aileens that have collided """
    69. #👇
    70. collisions = pygame.sprite.groupcollide(
    71. self.bullets, self.aileens,True,True)
    72. if collisions: #collision是字典类型变量
    73. # for aileens in collisions.values():
    74. self.stats.score += self.settings.aileen_points #* len(aileens)
    75. for aileens in collisions.values():
    76. self.stats.score += self.settings.aileen_points * len(aileens)
    77. self.sb.prep_score()
    78. self.sb.check_high_score()
    79. self.sb.prep_high_score()
    80. if not self.aileens:
    81. # Destory existing bullets and create new fleet.
    82. self.bullets.empty()
    83. self._create_fleet()
    84. self.settings.increase_speed()
    85. # Increase level
    86. self.stats.level += 1
    87. self.sb.prep_level()
    88. # print(len(self.bullets))
    89. # Watch for keyboard and mouse events. 监听用户在做什么操作--这里设置游戏菜单栏
    90. def _check_events(self): #
    91. #--snip--
    92. """Respond to keypress and mouse events"""
    93. for event in pygame.event.get():
    94. if event.type == pygame.QUIT:
    95. sys.exit()
    96. elif event.type == pygame.KEYDOWN:
    97. self._check_keydown_events(event)
    98. elif event.type == pygame.KEYUP:
    99. self._check_keyup_events(event)
    100. elif event.type == pygame.MOUSEBUTTONDOWN:
    101. mouse_pos = pygame.mouse.get_pos()
    102. self._check_play_button(mouse_pos)
    103. def _check_play_button(self,mouse_pos):
    104. """Start a new game when the player clicks Play."""
    105. button_clicked = self.play_button.rect.collidepoint(mouse_pos)
    106. if button_clicked and not self.stats.game_active:
    107. # Hide the mouse cursor.
    108. pygame.mouse.set_visible(False)
    109. # Reset the game statistics.
    110. self.stats.reset_stats()
    111. self.stats.game_active = True
    112. # 确保分数清0
    113. self.sb.prep_score()
    114. self.sb.prep_level()
    115. self.sb.prep_ships()
    116. # Get rid of any remaining aileens and bullets.
    117. self.aileens.empty()
    118. self.bullets.empty()
    119. #Create A new fleet and center the ship.
    120. self._create_fleet()
    121. self.ship.center_ship()
    122. #Reset the game settings.
    123. self.settings.initialize_dynamic_settings()
    124. pygame.mixer.init()
    125. pygame.mixer.music.load("music/香香 - 猪之歌.mp3")
    126. # pygame.mixer.music.set_volume(2)
    127. pygame.mixer.music.play()
    128. def _check_keydown_events(self,event):
    129. """Respond to keypress"""
    130. #判断右键
    131. if event.key == pygame.K_RIGHT:
    132. #处理右键
    133. self.ship.moving_right = True
    134. elif event.key == pygame.K_LEFT:
    135. self.ship.moving_left = True
    136. elif event.key == pygame.K_UP:
    137. self.ship.moving_up = True
    138. elif event.key == pygame.K_DOWN:
    139. self.ship.moving_down = True
    140. elif event.key == pygame.K_q:#按q键退出游戏
    141. sys.exit()
    142. elif event.key == pygame.K_SPACE:
    143. self._fire_bullet()
    144. def _check_keyup_events(self,event):
    145. """Respond to key release."""
    146. if event.key == pygame.K_RIGHT:
    147. self.ship.moving_right = False
    148. elif event.key == pygame.K_LEFT:
    149. self.ship.moving_left = False
    150. # Move the ship to the right.
    151. self.ship.rect.x += 1
    152. elif event.key == pygame.K_m:
    153. self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
    154. self.settings.screen_width =self.screen.get_rect().width
    155. self.settings.screen_height = self.screen.get_rect().height
    156. self.ship = Ship(self)
    157. self.aliens = pygame.sprite.Group()
    158. self._create_fleet()
    159. elif event.key == pygame.K_n:
    160. self.settings = Settings()
    161. self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
    162. self.ship = Ship(self)
    163. self.aliens = pygame.sprite.Group()
    164. self._create_fleet()
    165. elif event.key == pygame.K_UP:
    166. self.ship.moving_up = False
    167. elif event.key == pygame.K_DOWN:
    168. self.ship.moving_down = False
    169. def _fire_bullet(self):
    170. """create a new bullet and add it to the bullets group."""
    171. if len(self.bullets) < self.settings.bullets_allowed:
    172. new_bullet = Bullet(self)
    173. self.bullets.add(new_bullet)
    174. def _create_fleet(self):#self传的也是ai
    175. """Create the fleet of aileens."""
    176. #Create an aileen and find the number of aileens in a row
    177. #Spacing between each aileen is equal to one aileen width.
    178. aileen = Aileen(self)
    179. aileen_width, aileen_height = aileen.rect.size
    180. available_space_x = self.settings.screen_width - (2 * aileen_width)
    181. print(available_space_x)
    182. print(self.settings.screen_width)
    183. print(2 * aileen_width)
    184. number_aileen_x = available_space_x // (2 * aileen_width)
    185. # Determine the number of rows of aileens that fit on the screen.
    186. ship_height =self.ship.rect.height
    187. available_space_y = (self.settings.screen_height -
    188. (3 * aileen_height) - ship_height)
    189. number_rows = available_space_y // (2 * aileen_height)
    190. #Create the first row of aileens.
    191. for row_number in range(number_rows):
    192. for aileen_number in range(number_aileen_x):
    193. self._create_aileen(aileen_number, row_number)
    194. def _create_aileen(self,aileen_number, row_number):
    195. # Create an aileen and place it in row.
    196. # Make a aileen
    197. aileen = Aileen(self)
    198. aileen_width, aileen_height =aileen.rect.size
    199. aileen.x = aileen_width + 2 * aileen_width * aileen_number
    200. aileen.rect.x = aileen.x
    201. aileen.rect.y = aileen.rect.height + 2 * aileen.rect.height * row_number
    202. self.aileens.add(aileen)
    203. def _check_aileens_bottom(self):
    204. """Check if any aileens have reached the bottom of the screen."""
    205. screen_rect = self.screen.get_rect()
    206. for aileen in self.aileens.sprites():
    207. if aileen.rect.bottom >= screen_rect.bottom:
    208. #Treat this the same as if the ship got hit.
    209. self._ship_hit()
    210. break
    211. # Redraw the screen during each pass through the loop. 更新画布颜色
    212. def _update_aileens(self):
    213. """
    214. Check if the fleet is at an edge,
    215. then update the positions of all aliens in the fleet.
    216. """
    217. self._check_fleet_edges()
    218. """Update the positions of all aileens in the fleet."""
    219. self.aileens.update()
    220. # Look for aileen-ship collisions.
    221. if pygame.sprite.spritecollideany(self.ship, self.aileens):
    222. self._ship_hit()
    223. print("Ship hit!!!")
    224. # Look for aileens hitting the bottom of the screen
    225. self._check_aileens_bottom()
    226. def _check_fleet_edges(self):
    227. """Respond appropriately if any aileens have reached an edge."""
    228. for aileen in self.aileens.sprites():
    229. if aileen.check_edges():
    230. self._change_fleet_direction()
    231. break
    232. def _change_fleet_direction(self):
    233. """Drop the entire fleet and change the fleet's direction."""
    234. for aileen in self.aileens.sprites():
    235. aileen.rect.y += self.settings.fleet_drop_speed
    236. self.settings.fleet_direction *= -1
    237. def _ship_hit(self):
    238. """Respond to the ship being hit by an allien"""
    239. if self.stats.ships_left > 0:
    240. # Decrement ships_left.and update scoreboard
    241. self.stats.ships_left -= 1
    242. self.sb.prep_ships()
    243. # Get rid of any remaining aileens and bullets.
    244. self.aileens.empty()
    245. self.bullets.empty()
    246. # Create a new fleet and center the ship.
    247. self._create_fleet()
    248. self.ship.center_ship()
    249. # Pause
    250. sleep(0.5)
    251. else:
    252. self.stats.game_active = False
    253. pygame.mouse.set_visible(True)
    254. def _update_screen(self):
    255. """Update images on the screen , and flip to the new screen"""
    256. self.screen.fill(self.settings.bg_color)
    257. self.ship.blitme()
    258. for bullet in self.bullets.sprites():
    259. bullet.draw_bullet()
    260. self.aileens.draw(self.screen)
    261. #Draw the score information.
    262. self.sb.show_score()
    263. # Draw the play button if the games is inactive.
    264. if not self.stats.game_active:
    265. self.play_button.draw_button()
    266. self.bullets.draw(self.screen)
    267. pygame.display.flip()
    268. def check_high_score(self):
    269. """Check to see if there's a new high score."""
    270. if self.stats.score > self.stats.high_score:
    271. self.stats.high_score = self.stats.score
    272. self.prep_high_score()
    273. if __name__ == '__main__':
    274. # Make a game instance , and run the game.
    275. ai = AileenInvasion()#游戏本身的实例化,ai就是那个AileenInvasion
    276. ai.run_game()

    创建外星人aileen的文件

    1. import pygame
    2. from pygame.sprite import Sprite
    3. class Aileen(Sprite):
    4. """A class to represent a single alien in the fleet"""
    5. def __init__(self,ai_game):
    6. """Initilize the aileen and set its starting position."""
    7. super().__init__()
    8. self.screen = ai_game.screen
    9. self.settings = ai_game.settings
    10. # Load the aileen image and set its rect attribute.
    11. self.image = pygame.image.load("images/aileen.png")
    12. self.rect = self.image.get_rect()
    13. # Start each new aileen near the top left of the screen.
    14. self.rect.x = self.rect.width#将矩形左上角的值作为外星人的宽度
    15. self.rect.y = self.rect.height
    16. #通过飞船左上角的坐标来控制其它图片的位置
    17. #Store the aileen's exact horizontal position.
    18. self.x = float(self.rect.x)
    19. def check_edges(self):
    20. """Return True if aileen is at edge of screen"""
    21. screen_rect = self.screen.get_rect()
    22. if self.rect.right >= screen_rect.right or self.rect.left <= 0:
    23. return True
    24. def update(self):
    25. """Move the aileen to the right or left"""
    26. self.x += (self.settings.aileen_speed *
    27. self.settings.fleet_direction)
    28. self.rect.x = self.x

    子弹bullet的文件

    1. import pygame
    2. import random
    3. from pygame.sprite import Sprite
    4. class Bullet(Sprite):#子弹bullet继承Sprite类--继承
    5. """A class to manage bullets fired from the ship"""
    6. def __init__(self,ai_game):
    7. """Create a bullet object at the ship's current position"""
    8. super().__init__()#调用父类初始化函数
    9. self.screen = ai_game.screen
    10. self.settings = ai_game.settings
    11. self.color = self.settings.bullet_color
    12. # Load the aileen image and set its rect attribute.
    13. self.bullet_images = [pygame.image.load("images/heart.png"),pygame.image.load("images/banana.png"),pygame.image.load("images/cherry.png")]
    14. self.image = random.choice(self.bullet_images)
    15. self.rect = self.image.get_rect()
    16. #Create a bullet rect at(0,0) and then set correct position.
    17. self.rect =pygame.Rect(0,0,self.settings.bullet_width,
    18. self.settings.bullet_height)
    19. self.rect.midtop =ai_game.ship.rect.midtop#利用ai将船和子弹初始位置绑定,使得子弹在船中上方
    20. # Store the bullet's position as a decimal value.
    21. self.y = float(self.rect.y)
    22. def update(self):
    23. """move the bullet up the screen."""
    24. #update the decimal position of the bullet.
    25. self.y -= self.settings.bullet_speed
    26. #update the rect position.
    27. self.rect.y = self.y
    28. def draw_bullet(self):
    29. """draw the bullet to the screen"""
    30. self.screen.blit(self.image,self.rect)
    31. pygame.draw.rect(self.screen, self.color, self.rect)

    按钮button的文件

    1. import pygame.font
    2. class Button:
    3. def __init__(self,ai_game,msg):
    4. """Initialize button attributes."""
    5. self.screen = ai_game.screen
    6. self.screen_rect = self.screen.get_rect()
    7. # Set the dimensions and properties(财产,属性==attributes) of the button
    8. self.width, self.height = 200, 50
    9. self.button_color = (0, 255, 0)
    10. self.text_color = (255,255,255)
    11. self.font = pygame.font.SysFont(None, 48)
    12. # Build the button's rect object and center it.
    13. self.rect = pygame.Rect(0,0,self.width,self.height)
    14. self.rect.center = self.screen_rect.center
    15. # The button message needs to be prepped(准备) only once.
    16. self._prep_msg(msg)
    17. def _prep_msg(self,msg):
    18. """Turn msg into a rendered image and center and center text on the button."""
    19. self.msg_image = self.font.render(msg,True, self.text_color,
    20. 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. # Draw blank button and then draw message.
    25. self.screen.fill(self.button_color,self.rect)
    26. self.screen.blit(self.msg_image,self.msg_image_rect)

     游戏数据game_stats的文件

    1. class GameStats:
    2. """Track statics for Aileen Invasion."""
    3. def __init__(self,ai_game):
    4. # High score should never be reset.
    5. self.high_score = 0
    6. # Start Aileen Invasion in an active state.
    7. self.game_active = True
    8. """Initiallize statistics."""
    9. self.settings = ai_game.settings
    10. self.reset_stats()
    11. # Start game in an inactive state.
    12. self.game_active = False
    13. def reset_stats(self):
    14. """Initialize statistics that can change during the game."""
    15. self.ships_left = self.settings.ship_limit
    16. self.score = 0
    17. self.level=1

    游戏分数scoreboard的文件

    1. import pygame.font
    2. from pygame.sprite import Group
    3. from ship import Ship
    4. class Scoreboard:
    5. """A class to report scoring information."""
    6. def __init__(self,ai_game):
    7. """Initialize scorekeeping attributes."""
    8. self.ai_game = ai_game
    9. self.screen = ai_game.screen
    10. self.screen_rect = self.screen.get_rect()
    11. self.settings = ai_game.settings
    12. self.stats = ai_game.stats
    13. # Font settings for scoring information.
    14. self.text_color = (30,30,30)
    15. self.font = pygame.font.SysFont(None,48)
    16. #Prepare the initial score image.
    17. self.prep_score()
    18. self.prep_high_score()
    19. self.prep_level()
    20. self.prep_ships()
    21. def prep_ships(self):
    22. """Show how many ships are left."""
    23. self.ships = Group ()
    24. for ship_number in range(self.stats.ships_left):
    25. ship= Ship(self.ai_game)
    26. ship.rect.x = 10 + ship_number * ship.rect.width
    27. ship.rect.y = 10
    28. self.ships.add(ship)
    29. def prep_score(self):
    30. """Turn the score into a rendered image (将分数转换为渲染图像)"""
    31. score_str = str(self.stats.score)
    32. """Turn the score into a rendered image."""
    33. rounded_score = round(self.stats.score,-1)
    34. score_str = "{:,}".format(rounded_score) #round 取整
    35. self.score_image = self.font.render(score_str,True,
    36. self.text_color,self.settings.bg_color)
    37. #Display the score at the top right of the screen.
    38. self.score_rect = self.score_image.get_rect()
    39. self.score_rect.right = self.screen_rect.right - 20
    40. self.score_rect.top = 20
    41. def prep_high_score(self):
    42. """Turn the high score into a rendered image"""
    43. high_score = round(self.stats.high_score,-1)
    44. high_score_str = "{:,}".format(high_score)
    45. self.high_score_image = self.font.render(high_score_str,True,
    46. self.text_color,self.settings.bg_color)
    47. # Center the high score at the top of the screen.
    48. self.high_score_rect = self.high_score_image.get_rect()
    49. self.high_score_rect.centerx = self.screen_rect.centerx
    50. self.high_score_rect.top = self.score_rect.top
    51. def check_high_score(self):
    52. """Check to see if there's a new high score."""
    53. if self.stats.score > self.stats.high_score:
    54. self.stats.high_score = self.stats.score
    55. self.prep_high_score()
    56. def prep_level(self):
    57. """Turn the level into a rendered image."""
    58. level_str = str(self.stats.level)
    59. self.level_image = self.font.render(level_str,True,
    60. self.text_color,self.settings.bg_color)
    61. #Position the level below the score.
    62. self.level_rect = self.level_image.get_rect()
    63. self.level_rect.right = self.score_rect.right
    64. self.level_rect.top = self.score_rect.bottom + 10
    65. def show_score(self):
    66. """Draw score,level,and ships to the screen"""
    67. self.screen.blit(self.score_image,self.score_rect)
    68. self.screen.blit(self.high_score_image,self.high_score_rect)
    69. self.screen.blit(self.level_image,self.level_rect)
    70. self.ships.draw(self.screen)

    游戏设置settings的文件 

    1. class Settings:
    2. """A class store all settings for Aileen Invasion."""
    3. def __init__(self):
    4. """Initialize the game'settings """
    5. #Ship settings
    6. self.ship_speed = 1.5
    7. self.ship_limit = 3
    8. # Screen settings
    9. self.screen_width =1200
    10. self.screen_height=800
    11. self.bg_color =(255,255,255)
    12. # Bullet settings
    13. self.bullet_speed = 1
    14. self.bullet_width = 3
    15. self.bullet_height = 15
    16. self.bullet_color = (60,60,60)
    17. self.bullets_allowed = 3
    18. #Aileen settings
    19. self.aileen_speed = 1.0
    20. self.fleet_drop_speed = 10
    21. # How quickly the game speeds up
    22. self.speedup_scale = 2
    23. # How quickly the aileen point values increase
    24. self.score_scale =1.5
    25. self.initialize_dynamic_settings()
    26. def initialize_dynamic_settings(self):
    27. """Initialize speed settings"""
    28. self.ship_speed = 1.5
    29. self.bullet_speed = 1.5
    30. self.aileen_speed = 10
    31. # Scoring
    32. self.aileen_points =50
    33. # fleet_direction of 1 represents right ; -1 represents left.
    34. self.fleet_direction = 1
    35. def increase_speed(self):
    36. """Increase speed settings"""
    37. """Increase speed settings and aileen point values"""
    38. self.ship_speed *= self.speedup_scale
    39. self.bullet_speed *= self.speedup_scale
    40. self.aileen_speed *= self.speedup_scale
    41. self.aileen_points = int(self.aileen_points * self.score_scale)
    42. print(self.aileen_points)

    游戏飞船ship的文件

    1. import pygame
    2. from pygame.sprite import Sprite
    3. class Ship(Sprite):
    4. """A class to manage the ship."""
    5. def __init__(self,ai_game):
    6. """Initialize the ship and set its starting position."""
    7. super().__init__()
    8. self.screen = ai_game.screen
    9. self.settings = ai_game.settings
    10. self.screen_rect = ai_game.screen.get_rect()#返回窗口矩形
    11. # Load the ship image and get its rect.
    12. self.image = pygame.image.load("images/ship.png")
    13. self.rect=self.image.get_rect()#取得屏幕的矩形 rect=rectangle 拿到图片矩形
    14. # Start each new ship at the bottom center of the screen.
    15. self.rect.midbottom = self.screen_rect.midbottom
    16. #通过赋值:图片的中下方的坐标,等于屏幕中下方的坐标
    17. # Store a decimal value for the ship's horizontal position.
    18. self.x = float(self.rect.x)
    19. self.y = float(self.rect.y)
    20. # Movement flag
    21. self.moving_right =False
    22. self.moving_left =False
    23. self.moving_up =False
    24. self.moving_down =False
    25. def center_ship(self):
    26. """Center the ship on the screen"""
    27. self.rect.midbottom = self.screen_rect.midbottom
    28. self.x = float(self.rect.x)
    29. def update(self):
    30. """Update the ship's position based on the movement flag"""
    31. # Update the ship's value, not the rect.
    32. # if self.moving_right:
    33. if self.moving_right and self.rect.right < self.screen_rect.right:
    34. self.x += self.settings.ship_speed#不断增加飞船左上角的横坐标带动图片移动
    35. self.rect.x += 1
    36. # if self.moving_left:
    37. if self.moving_left and self.rect.left > 0:
    38. self.x -= self.settings.ship_speed
    39. self.rect.x -= 1
    40. #top
    41. if self.moving_up and self.rect.top > 720 :
    42. self.y -= self.settings.ship_speed
    43. self.rect.y -= 1
    44. if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
    45. self.y += self.settings.ship_speed
    46. self.rect.y += 1
    47. def blitme(self):#渲染函数,让图片显示出来
    48. """Draw the ship at its current location."""
    49. self.screen.blit(self.image,self.rect)

    b39e013c493b46929401b46a647a7783.gif 全屏模式下的游戏

    8cadd39be55e4a9eba3aced8e01751b2.gif

    小窗口下的游戏

    🐖今天的打猪游戏就分享到这里啦~🐖

    🐖喜欢就一键三连支持一下吧~🐖

    🐖谢谢家人们!🐖

    7d7797f9f2844be68123225a5303dd80.webp

  • 相关阅读:
    MySQL【窗口函数】【共用表表达式】
    【极简python】第六章 for循环与while循环
    2022速卖通宠物用品热销及需求品类推荐!
    python 之异常处理结构
    在 Docker 容器内集成 Crontab 定时任务
    算法训练营day49|动态规划 part10:(LeetCode 121. 买卖股票的最佳时机、122.买卖股票的最佳时机II)
    聊聊微服务架构思想
    Junit执行源码分析,Junit是怎么跑起来的(二)
    你知道STM32和51单片机的区别吗?
    最长重复子数组
  • 原文地址:https://blog.csdn.net/Aileenvov/article/details/132896432