当前位置:   article > 正文

python小游戏——飞机大战代码开源_飞机大战python代码

飞机大战python代码

♥️作者:小刘在这里

♥️每天分享云计算网络运维课堂笔记,努力不一定有收获,但一定会有收获加油!一起努力,共赴美好人生!

♥️夕阳下,是最美的,绽放,愿所有的美好,再疫情结束后如约而至。

目录

一.效果呈现

 二,主代码

三,cfg

四,README


一.效果呈现

 二,主代码

  1. '''
  2. 主题:
  3. 简易飞机大战
  4. '''
  5. import sys
  6. import cfg
  7. import pygame
  8. from modules import *
  9. '''游戏界面'''
  10. def GamingInterface(num_player, screen):
  11. # 初始化
  12. pygame.mixer.music.load(cfg.SOUNDPATHS['Cool Space Music'])
  13. pygame.mixer.music.set_volume(0.4)
  14. pygame.mixer.music.play(-1)
  15. explosion_sound = pygame.mixer.Sound(cfg.SOUNDPATHS['boom'])
  16. fire_sound = pygame.mixer.Sound(cfg.SOUNDPATHS['shot'])
  17. font = pygame.font.Font(cfg.FONTPATH, 20)
  18. # 游戏背景图
  19. bg_imgs = [cfg.IMAGEPATHS['bg_big'], cfg.IMAGEPATHS['seamless_space'], cfg.IMAGEPATHS['space3']]
  20. bg_move_dis = 0
  21. bg_1 = pygame.image.load(bg_imgs[0]).convert()
  22. bg_2 = pygame.image.load(bg_imgs[1]).convert()
  23. bg_3 = pygame.image.load(bg_imgs[2]).convert()
  24. # 玩家, 子弹和小行星精灵组
  25. player_group = pygame.sprite.Group()
  26. bullet_group = pygame.sprite.Group()
  27. asteroid_group = pygame.sprite.Group()
  28. # 产生小行星的时间间隔
  29. asteroid_ticks = 90
  30. for i in range(num_player):
  31. player_group.add(Ship(i+1, cfg))
  32. clock = pygame.time.Clock()
  33. # 分数
  34. score_1, score_2 = 0, 0
  35. # 游戏主循环
  36. while True:
  37. for event in pygame.event.get():
  38. if event.type == pygame.QUIT:
  39. pygame.quit()
  40. sys.exit()
  41. # --玩家一: ↑↓←→控制, j射击; 玩家二: wsad控制, 空格射击
  42. pressed_keys = pygame.key.get_pressed()
  43. for idx, player in enumerate(player_group):
  44. direction = None
  45. if idx == 0:
  46. if pressed_keys[pygame.K_UP]:
  47. direction = 'up'
  48. elif pressed_keys[pygame.K_DOWN]:
  49. direction = 'down'
  50. elif pressed_keys[pygame.K_LEFT]:
  51. direction = 'left'
  52. elif pressed_keys[pygame.K_RIGHT]:
  53. direction = 'right'
  54. if direction:
  55. player.move(direction)
  56. if pressed_keys[pygame.K_j]:
  57. if player.cooling_time == 0:
  58. fire_sound.play()
  59. bullet_group.add(player.shot())
  60. player.cooling_time = 20
  61. elif idx == 1:
  62. if pressed_keys[pygame.K_w]:
  63. direction = 'up'
  64. elif pressed_keys[pygame.K_s]:
  65. direction = 'down'
  66. elif pressed_keys[pygame.K_a]:
  67. direction = 'left'
  68. elif pressed_keys[pygame.K_d]:
  69. direction = 'right'
  70. if direction:
  71. player.move(direction)
  72. if pressed_keys[pygame.K_SPACE]:
  73. if player.cooling_time == 0:
  74. fire_sound.play()
  75. bullet_group.add(player.shot())
  76. player.cooling_time = 20
  77. if player.cooling_time > 0:
  78. player.cooling_time -= 1
  79. if (score_1 + score_2) < 500:
  80. background = bg_1
  81. elif (score_1 + score_2) < 1500:
  82. background = bg_2
  83. else:
  84. background = bg_3
  85. # --向下移动背景图实现飞船向上移动的效果
  86. screen.blit(background, (0, -background.get_rect().height + bg_move_dis))
  87. screen.blit(background, (0, bg_move_dis))
  88. bg_move_dis = (bg_move_dis + 2) % background.get_rect().height
  89. # --生成小行星
  90. if asteroid_ticks == 0:
  91. asteroid_ticks = 90
  92. asteroid_group.add(Asteroid(cfg))
  93. else:
  94. asteroid_ticks -= 1
  95. # --画飞船
  96. for player in player_group:
  97. if pygame.sprite.spritecollide(player, asteroid_group, True, None):
  98. player.explode_step = 1
  99. explosion_sound.play()
  100. elif player.explode_step > 0:
  101. if player.explode_step > 3:
  102. player_group.remove(player)
  103. if len(player_group) == 0:
  104. return
  105. else:
  106. player.explode(screen)
  107. else:
  108. player.draw(screen)
  109. # --画子弹
  110. for bullet in bullet_group:
  111. bullet.move()
  112. if pygame.sprite.spritecollide(bullet, asteroid_group, True, None):
  113. bullet_group.remove(bullet)
  114. if bullet.player_idx == 1:
  115. score_1 += 1
  116. else:
  117. score_2 += 1
  118. else:
  119. bullet.draw(screen)
  120. # --画小行星
  121. for asteroid in asteroid_group:
  122. asteroid.move()
  123. asteroid.rotate()
  124. asteroid.draw(screen)
  125. # --显示分数
  126. score_1_text = '玩家一得分: %s' % score_1
  127. score_2_text = '玩家二得分: %s' % score_2
  128. text_1 = font.render(score_1_text, True, (0, 0, 255))
  129. text_2 = font.render(score_2_text, True, (255, 0, 0))
  130. screen.blit(text_1, (2, 5))
  131. screen.blit(text_2, (2, 35))
  132. # --屏幕刷新
  133. pygame.display.update()
  134. clock.tick(60)
  135. '''主函数'''
  136. def main():
  137. pygame.init()
  138. pygame.font.init()
  139. pygame.mixer.init()
  140. screen = pygame.display.set_mode(cfg.SCREENSIZE)
  141. pygame.display.set_caption('简易版——飞机大战 ')
  142. num_player = StartInterface(screen, cfg)
  143. if num_player == 1:
  144. while True:
  145. GamingInterface(num_player=1, screen=screen)
  146. EndInterface(screen, cfg)
  147. else:
  148. while True:
  149. GamingInterface(num_player=2, screen=screen)
  150. EndInterface(screen, cfg)
  151. '''run'''
  152. if __name__ == '__main__':
  153. main()

三,cfg

'''配置文件'''
import os


'''屏幕大小'''
SCREENSIZE = (956, 560)
'''字体路径'''
FONTPATH = os.path.join(os.getcwd(), 'resources/font/simkai.ttf')
'''图片路径'''
IMAGEPATHS = {
    'asteroid': os.path.join(os.getcwd(), 'resources/images/asteroid.png'),
    'bg_big': os.path.join(os.getcwd(), 'resources/images/bg_big.png'),
    'bullet': os.path.join(os.getcwd(), 'resources/images/bullet.png'),
    'seamless_space': os.path.join(os.getcwd(), 'resources/images/seamless_space.png'),
    'ship': os.path.join(os.getcwd(), 'resources/images/ship.png'),
    'ship_exploded': os.path.join(os.getcwd(), 'resources/images/ship_exploded.png'),
    'space3': os.path.join(os.getcwd(), 'resources/images/space3.jpg'),
}
'''声音路径'''
SOUNDPATHS = {
    'boom': os.path.join(os.getcwd(), 'resources/sounds/boom.wav'),
    'Cool Space Music': os.path.join(os.getcwd(), 'resources/sounds/Cool Space Music.mp3'),
    'shot': os.path.join(os.getcwd(), 'resources/sounds/shot.ogg'),
}

四,README

# Introduction
https://mp.weixin.qq.com/s/n-f_6sh8bB7-dtIFJLbnFg

# Environment
```
OS: Windows10
Python: Python3.5+(have installed necessary dependencies)
```

# Usage
```
Step1:
pip install -r requirements.txt
Step2:
run "python Game10.py"
```

# Game Display
![giphy](demonstration/running.gif)

♥️关注,就是我创作的动力

♥️点赞,就是对我最大的认可

♥️这里是小刘,励志用心做好每一篇文章,谢谢大家

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

闽ICP备14008679号