当前位置:   article > 正文

用Python制作小游戏之‘植物大战僵尸’(一)_植物大战僵尸python

植物大战僵尸python

1.引入需要的模块

  1. import pygame
  2. import random

2.配置图片地址及页面宽高等

  1. IMAGE_PATH = 'D:\桌面\练习\python\植物大战僵尸\imgs'
  2. scrrr_width = 800
  3. scrrr_height = 560
  4. # 1 创建控制游戏结束的状态
  5. GAMEOVER = False
  6. # 4 图片加载报错处理
  7. LOG = '文件:{}中的方法:{}出错'.format(__file__, __name__)

3.创建地图类

  1. class Map():
  2. # 3 存储两张不同颜色的图片名称
  3. map_names_list = [IMAGE_PATH + 'map1.png', IMAGE_PATH + 'map2.png']
  4. def __init__(self, x, y, img_index):
  5. self.image = pygame.image.load(Map.map_names_list[img_index])
  6. self.position = (x, y)
  7. self.can_grow = True
  8. # 3 加载地图
  9. def load_map(self):
  10. MainGame.window.blit(self.image, self.position)

4.植物类

  1. class Plant(pygame.sprite.Sprite):
  2. def __init__(self):
  3. super(Plant, self).__init__()
  4. self.live = True
  5. # 加载图片
  6. def load_image(self):
  7. if hasattr(self, 'image') and hasattr(self, 'rect'):
  8. MainGame.window.blit(self.image, self.rect)
  9. else:
  10. print(LOG)
  11. class Sunflower(Plant):
  12. def __init__(self, x, y):
  13. super(Sunflower, self).__init__()
  14. self.image = pygame.image.load('imgs/sunflower.png')
  15. self.rect = self.image.get_rect()
  16. self.rect.x = x
  17. self.rect.y = y
  18. self.price = 50
  19. self.hp = 100
  20. self.time_count = 0

5.增加射击方法

  1. def shot(self):
  2. should_fire = False
  3. for zombie in MainGame.zombie_list:
  4. if zombie.rect.y == self.rect.y and zombie.rect.x < 800 and zombie.rect.x > self.rect.x:
  5. should_fire = True
  6. # 6 如果活着
  7. if self.live and should_fire:
  8. self.shot_count += 1
  9. # 6 计数器到25发射一次
  10. if self.shot_count == 25:

7.豌豆子弹

  1. class PeaBullet(pygame.sprite.Sprite):
  2. def __init__(self, peashooter):
  3. self.live = True
  4. self.image = pygame.image.load('imgs/peabullet.png')
  5. self.damage = 50
  6. self.speed = 10
  7. self.rect = self.image.get_rect()
  8. self.rect.x = peashooter.rect.x + 60
  9. self.rect.y = peashooter.rect.y + 15

8.僵尸类

  1. class Zombie(pygame.sprite.Sprite):
  2. def __init__(self, x, y):
  3. super(Zombie, self).__init__()
  4. self.image = pygame.image.load('imgs/zombie.png')
  5. self.rect = self.image.get_rect()
  6. self.rect.x = x
  7. self.rect.y = y
  8. self.hp = 1000
  9. self.damage = 2
  10. self.speed = 1
  11. self.live = True
  12. self.stop = False

9.以及主程序部分

  1. class MainGame():
  2. # 1 加载游戏窗口
  3. def init_window(self):
  4. # 1 调用显示模块的初始化
  5. pygame.display.init()
  6. # 1 创建窗口
  7. MainGame.window = pygame.display.set_mode([scrrr_width, scrrr_height])
  8. # 2 文本绘制
  9. def draw_text(self, content, size, color):
  10. pygame.font.init()
  11. font = pygame.font.SysFont('kaiti', size)
  12. text = font.render(content, True, color)
  13. return text
  14. def init_plant_points(self):
  15. for y in range(1, 7):
  16. points = []
  17. for x in range(10):
  18. point = (x, y)
  19. points.append(point)
  20. MainGame.map_points_list.append(points)
  21. print("MainGame.map_points_list", MainGame.map_points_list)
  22. def init_map(self):
  23. for points in MainGame.map_points_list:
  24. temp_map_list = list()
  25. for point in points:
  26. # map = None
  27. if (point[0] + point[1]) % 2 == 0:
  28. map = Map(point[0] * 80, point[1] * 80, 0)
  29. else:
  30. map = Map(point[0] * 80, point[1] * 80, 1)
  31. # 将地图块加入到窗口中
  32. temp_map_list.append(map)
  33. print("temp_map_list", temp_map_list)
  34. MainGame.map_list.append(temp_map_list)
  35. print("MainGame.map_list", MainGame.map_list)
  36. # 3 将地图加载到窗口中
  37. def load_map(self):
  38. for temp_map_list in MainGame.map_list:
  39. for map in temp_map_list:
  40. map.load_map()

10.启动程序

  1. if __name__ == '__main__':
  2. game = MainGame()
  3. game.start_game()

完整代码在下一篇文章中

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

闽ICP备14008679号