当前位置:   article > 正文

python程序设计第三版pdf

python程序设计第三版pdf

亲爱的读者们,今天我们要讨论,Python程序设计第三版清华出版社PDF Python程序设计第三版 董付国资源,一起探索吧!

这篇文章主要介绍了python简单编程小游戏,具有一定借鉴价值,需要的朋友可以参考下PHP,我学习路上的那盏灯。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。

哈喽铁子们

表弟最近在学Python,总是跟我抱怨很枯燥无味,其实,他有没有认真想过,可能是自己学习姿势不对?

比方说,可以通过打游戏来学编程!

今天给大家分享100个Python小游戏,一定要收藏!

1、简易飞机大战

飞机大战相信大家都玩过吧,非常简单有意思的游戏,咱们通过Python给它复刻出来,回味童年。

素材文件

全部源码:

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

都是一些大家耳熟能详的小游戏

素材

全部源码: 

  1. import os
  2. import sys
  3. import cfg
  4. import pygame
  5. from modules import *
  6. '''游戏主程序'''
  7. def main():
  8. ()
  9. screen = pygame.display.set_mode(cfg.SCREENSIZE)
  10. pygame.display.set_caption('开心消消乐
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/871133
    推荐阅读
    相关标签