当前位置:   article > 正文

python实现小游戏_用python设计小游戏

用python设计小游戏
  1. import pygame
  2. import random
  3. # 初始化游戏
  4. pygame.init()
  5. # 显示开始游戏界面
  6. font = pygame.font.Font(None, 36)
  7. def show_start_screen():
  8. screen.fill(black)
  9. title_text = font.render("打飞机游戏", True, white)
  10. instruction_text = font.render("按任意键开始游戏", True, white)
  11. title_rect = title_text.get_rect(center=(screen_width // 2, screen_height // 2 - 50))
  12. instruction_rect = instruction_text.get_rect(center=(screen_width // 2, screen_height // 2))
  13. screen.blit(title_text, title_rect)
  14. screen.blit(instruction_text, instruction_rect)
  15. pygame.display.flip()
  16. # 设置窗口尺寸
  17. screen_width = 800
  18. screen_height = 600
  19. screen = pygame.display.set_mode((screen_width, screen_height))
  20. pygame.display.set_caption("IKUN小游戏")
  21. # 定义颜色
  22. white = (255, 255, 255)
  23. black = (0, 0, 0)
  24. red = (255, 0, 0)
  25. # 加载飞机和敌人图片
  26. player_img = pygame.image.load("player4.png")
  27. enemy_img = pygame.image.load("enemy6.png")
  28. # 获取飞机和敌人图片的宽度和高度
  29. player_width = player_img.get_width()
  30. player_height = player_img.get_height()
  31. enemy_width = enemy_img.get_width()
  32. enemy_height = enemy_img.get_height()
  33. # 设置游戏时钟
  34. clock = pygame.time.Clock()
  35. # 定义玩家飞机类
  36. class Player(pygame.sprite.Sprite):
  37. def __init__(self):
  38. super().__init__()
  39. self.image = player_img
  40. self.rect = self.image.get_rect()
  41. self.rect.x = screen_width // 2 - player_width // 2
  42. self.rect.y = screen_height - player_height - 10
  43. self.speed = 5
  44. def update(self):
  45. keys = pygame.key.get_pressed()
  46. if keys[pygame.K_LEFT] and self.rect.x > 0:
  47. self.rect.x -= self.speed
  48. if keys[pygame.K_RIGHT] and self.rect.x < screen_width - player_width:
  49. self.rect.x += self.speed
  50. def shoot(self):
  51. bullet = Bullet(self.rect.x + player_width // 2, self.rect.y)
  52. all_sprites.add(bullet)
  53. bullets.add(bullet)
  54. # 定义敌人飞机类
  55. class Enemy(pygame.sprite.Sprite):
  56. def __init__(self):
  57. super().__init__()
  58. self.image = enemy_img
  59. self.rect = self.image.get_rect()
  60. self.rect.x = random.randint(0, screen_width - enemy_width)
  61. self.rect.y = random.randint(-enemy_height, -10)
  62. self.speed = random.randint(1, 3)
  63. def update(self):
  64. self.rect.y += self.speed
  65. if self.rect.y > screen_height:
  66. self.rect.x = random.randint(0, screen_width - enemy_width)
  67. self.rect.y = random.randint(-enemy_height, -10)
  68. self.speed = random.randint(1, 3)
  69. # 定义子弹类
  70. class Bullet(pygame.sprite.Sprite):
  71. def __init__(self, x, y):
  72. super().__init__()
  73. self.image = pygame.image.load("basketball1.png") # 加载篮球图片
  74. self.image = pygame.transform.scale(self.image, (20, 20)) # 调整篮球图片尺寸
  75. self.rect = self.image.get_rect()
  76. self.rect.x = x
  77. self.rect.y = y
  78. self.speed = -8 # 调整篮球的移动速度
  79. def update(self):
  80. self.rect.y += self.speed
  81. if self.rect.y < 0:
  82. self.kill()
  83. # 创建玩家飞机和敌人飞机的精灵组
  84. all_sprites = pygame.sprite.Group()
  85. enemies = pygame.sprite.Group()
  86. bullets = pygame.sprite.Group()
  87. player = Player()
  88. all_sprites.add(player)
  89. # 创建敌人飞机
  90. for _ in range(10):
  91. enemy = Enemy()
  92. all_sprites.add(enemy)
  93. enemies.add(enemy)
  94. # 游戏主循环
  95. running = True
  96. game_over = False
  97. while running:
  98. if game_over:
  99. # 游戏结束界面
  100. screen.fill(black)
  101. game_over_text = font.render("游戏结束", True, white)
  102. restart_text = font.render("按R键重新开始", True, white)
  103. game_over_rect = game_over_text.get_rect(center=(screen_width // 2, screen_height // 2 - 50))
  104. restart_rect = restart_text.get_rect(center=(screen_width // 2, screen_height // 2))
  105. screen.blit(game_over_text, game_over_rect)
  106. screen.blit(restart_text, restart_rect)
  107. pygame.display.flip()
  108. for event in pygame.event.get():
  109. if event.type == pygame.QUIT:
  110. running = False
  111. elif event.type == pygame.KEYDOWN:
  112. if event.key == pygame.K_r:
  113. # 重新开始游戏
  114. game_over = False
  115. all_sprites.empty()
  116. enemies.empty()
  117. bullets.empty()
  118. player = Player()
  119. all_sprites.add(player)
  120. for _ in range(10):
  121. enemy = Enemy()
  122. all_sprites.add(enemy)
  123. enemies.add(enemy)
  124. else:
  125. # 游戏逻辑
  126. for event in pygame.event.get():
  127. if event.type == pygame.QUIT:
  128. running = False
  129. elif event.type == pygame.KEYDOWN:
  130. if event.key == pygame.K_SPACE:
  131. player.shoot()
  132. all_sprites.update()
  133. hits = pygame.sprite.spritecollide(player, enemies, False)
  134. if hits:
  135. game_over = True
  136. hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
  137. for hit in hits:
  138. enemy = Enemy()
  139. all_sprites.add(enemy)
  140. enemies.add(enemy)
  141. screen.fill(black)
  142. all_sprites.draw(screen)
  143. pygame.display.flip()
  144. clock.tick(60)
  145. # 退出游戏
  146. pygame.quit()

 

 

 

 

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

闽ICP备14008679号