赞
踩
1.引入需要的模块
- import pygame
- import random
2.配置图片地址及页面宽高等
- IMAGE_PATH = 'D:\桌面\练习\python\植物大战僵尸\imgs'
- scrrr_width = 800
- scrrr_height = 560
- # 1 创建控制游戏结束的状态
- GAMEOVER = False
- # 4 图片加载报错处理
- LOG = '文件:{}中的方法:{}出错'.format(__file__, __name__)
3.创建地图类
- class Map():
- # 3 存储两张不同颜色的图片名称
- map_names_list = [IMAGE_PATH + 'map1.png', IMAGE_PATH + 'map2.png']
-
- def __init__(self, x, y, img_index):
- self.image = pygame.image.load(Map.map_names_list[img_index])
- self.position = (x, y)
- self.can_grow = True
-
- # 3 加载地图
- def load_map(self):
- MainGame.window.blit(self.image, self.position)
4.植物类
- class Plant(pygame.sprite.Sprite):
- def __init__(self):
- super(Plant, self).__init__()
- self.live = True
-
- # 加载图片
- def load_image(self):
- if hasattr(self, 'image') and hasattr(self, 'rect'):
- MainGame.window.blit(self.image, self.rect)
- else:
- print(LOG)
-
-
- class Sunflower(Plant):
- def __init__(self, x, y):
- super(Sunflower, self).__init__()
- self.image = pygame.image.load('imgs/sunflower.png')
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.rect.y = y
- self.price = 50
- self.hp = 100
- self.time_count = 0
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
5.增加射击方法
- def shot(self):
- should_fire = False
- for zombie in MainGame.zombie_list:
- if zombie.rect.y == self.rect.y and zombie.rect.x < 800 and zombie.rect.x > self.rect.x:
- should_fire = True
- # 6 如果活着
- if self.live and should_fire:
- self.shot_count += 1
- # 6 计数器到25发射一次
- if self.shot_count == 25:
7.豌豆子弹
- class PeaBullet(pygame.sprite.Sprite):
- def __init__(self, peashooter):
- self.live = True
- self.image = pygame.image.load('imgs/peabullet.png')
- self.damage = 50
- self.speed = 10
- self.rect = self.image.get_rect()
- self.rect.x = peashooter.rect.x + 60
- self.rect.y = peashooter.rect.y + 15
8.僵尸类
- class Zombie(pygame.sprite.Sprite):
- def __init__(self, x, y):
- super(Zombie, self).__init__()
- self.image = pygame.image.load('imgs/zombie.png')
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.rect.y = y
- self.hp = 1000
- self.damage = 2
- self.speed = 1
- self.live = True
- self.stop = False
9.以及主程序部分
- class MainGame():
- # 1 加载游戏窗口
- def init_window(self):
- # 1 调用显示模块的初始化
- pygame.display.init()
- # 1 创建窗口
- MainGame.window = pygame.display.set_mode([scrrr_width, scrrr_height])
-
- # 2 文本绘制
- def draw_text(self, content, size, color):
- pygame.font.init()
- font = pygame.font.SysFont('kaiti', size)
- text = font.render(content, True, color)
- return text
-
- def init_plant_points(self):
- for y in range(1, 7):
- points = []
- for x in range(10):
- point = (x, y)
- points.append(point)
- MainGame.map_points_list.append(points)
- print("MainGame.map_points_list", MainGame.map_points_list)
- def init_map(self):
- for points in MainGame.map_points_list:
- temp_map_list = list()
- for point in points:
- # map = None
- if (point[0] + point[1]) % 2 == 0:
- map = Map(point[0] * 80, point[1] * 80, 0)
- else:
- map = Map(point[0] * 80, point[1] * 80, 1)
- # 将地图块加入到窗口中
- temp_map_list.append(map)
- print("temp_map_list", temp_map_list)
- MainGame.map_list.append(temp_map_list)
- print("MainGame.map_list", MainGame.map_list)
-
- # 3 将地图加载到窗口中
- def load_map(self):
- for temp_map_list in MainGame.map_list:
- for map in temp_map_list:
- map.load_map()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
10.启动程序
- if __name__ == '__main__':
- game = MainGame()
- game.start_game()
完整代码在下一篇文章中
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。