赞
踩
当问你塔防游戏时,大多数人第一时间想到的可能是《植物大战僵尸》,这是一款经典的塔防游戏,在其中你需要种植植物来抵御僵尸的入侵。但除了这款经典之作,是否还能有其他更多的塔防游戏呢?今天我们就来介绍一款基于 Python 和 Pygame 开发的塔防游戏(demo)——《僵尸大战僵尸》。
首先,让我们来了解一下这款游戏的背景和玩法。《僵尸大战僵尸》是一款以僵尸为主题的塔防游戏,玩家需要在游戏中布置不同种类的僵尸来抵御敌方的进攻。游戏分为左右两方,每一方都有自己的阳光资源和僵尸种类,玩家需要合理利用资源,选择不同的僵尸种类,并根据敌方的情况进行布局,最终击败对方获得胜利。下面是僵尸类的代码片段:
- # 僵尸类
- class Zombie(pygame.sprite.Sprite):
- def __init__(self, kind, camp, ai, ny):
- super().__init__()
- # 阵营分配
- self.camp = camp
- self.ai = ai
- self.kind = kind
- if camp == 0:
- self.x = random.randint(1300, 1600) # 默认出生位置
- else:
- self.x = random.randint(-300, 0)
- if not self.ai:
- self.y = ny
- else:
- self.y = random.choice(random_y)
- # 僵尸种类
- if self.kind == 0: # 普通僵尸
- self.speed = 0.5 # 基础速度
- self.HP = 500
- self.image = 0 # 普通僵尸对应的图片组
- elif self.kind == 1: # 路障僵尸(更快)
- self.speed = 1 #
- self.HP = 600
- self.image = 1
- elif self.kind == 2: # 铁桶僵尸(更肉)
- self.speed = 0.5 #
- self.HP = 1200
- self.image = 2
- # 进入房间的僵尸数量计数一个僵尸只记一次的标志位
- self.cont_added = False
- # 两种帧
- self.frame = None
- self.frames = []
- self.currentFrame = 0
- self.frame1 = None
- self.frames1 = []
- self.currentFrame1 = 0
- if self.camp == 0:
- if self.image == 0:
- for i in range(1, 23):
- self.frame = pygame.image.load(f'Zombies/Zombie/Zombie_{i}.png').convert_alpha()
- self.frames.append(self.frame)
- elif self.image == 1:
- for i in range(1, 22):
- self.frame = pygame.image.load(f'Zombies/ConeheadZombie/ConeheadZombie_{i}.png').convert_alpha()
- self.frames.append(self.frame)
- elif self.image == 2:
- for i in range(1, 16):
- self.frame = pygame.image.load(f'Zombies/BucketheadZombie/BucketheadZombie_{i}.png').convert_alpha()
- self.frames.append(self.frame)
- elif self.camp == 1:
- if self.image == 0:
- for i in range(1, 23):
- self.frame = pygame.image.load(f'Zombies/Zombie/Zombie_{i}.png').convert_alpha()
- self.frame = pygame.transform.flip(self.frame, True, False)
- self.frames.append(self.frame)
- elif self.image == 1:
- for i in range(1, 22):
- self.frame = pygame.image.load(f'Zombies/ConeheadZombie/ConeheadZombie_{i}.png').convert_alpha()
- self.frame = pygame.transform.flip(self.frame, True, False)
- self.frames.append(self.frame)
- elif self.image == 2:
- for i in range(1, 16):
- self.frame = pygame.image.load(f'Zombies/BucketheadZombie/BucketheadZombie_{i}.png').convert_alpha()
- self.frame = pygame.transform.flip(self.frame, True, False)
- self.frames.append(self.frame)
- if self.camp == 0:
- if self.image == 0:
- for i in range(1, 22):
- self.frame1 = pygame.image.load(f'Zombies/Zombie/ZombieAttack_{i}.png').convert_alpha()
- self.frames1.append(self.frame1)
- elif self.image == 1:
- for i in range(1, 12):
- self.frame1 = pygame.image.load(
- f'Zombies/ConeheadZombie/ConeheadZombieAttack_{i}.png').convert_alpha()
- self.frames1.append(self.frame1)
- elif self.image == 2:
- for i in range(1, 12):
- self.frame1 = pygame.image.load(
- f'Zombies/BucketheadZombie/BucketheadZombieAttack_{i}.png').convert_alpha()
- self.frames1.append(self.frame1)
- elif self.camp == 1:
- if self.image == 0:
- for i in range(1, 22):
- self.frame1 = pygame.image.load(f'Zombies/Zombie/ZombieAttack_{i}.png').convert_alpha()
- self.frame1 = pygame.transform.flip(self.frame1, True, False)
- self.frames1.append(self.frame1)
- elif self.image == 1:
- for i in range(1, 12):
- self.frame1 = pygame.image.load(
- f'Zombies/ConeheadZombie/ConeheadZombieAttack_{i}.png').convert_alpha()
- self.frame1 = pygame.transform.flip(self.frame1, True, False)
- self.frames1.append(self.frame1)
- elif self.image == 2:
- for i in range(1, 12):
- self.frame1 = pygame.image.load(
- f'Zombies/BucketheadZombie/BucketheadZombieAttack_{i}.png').convert_alpha()
- self.frame1 = pygame.transform.flip(self.frame1, True, False)
- self.frames1.append(self.frame1)
-
- # 僵尸移动
- def walking(self):
- if self.camp == 0:
- self.x = self.x - self.speed
- screen.blit(self.frames[self.currentFrame],
- (self.x, self.y))
- # 更新当前帧
- self.currentFrame = (self.currentFrame + 1) % len(self.frames)
- else:
- self.x = self.x + self.speed
- screen.blit(self.frames[self.currentFrame],
- (self.x, self.y))
- # 更新当前帧
- self.currentFrame = (self.currentFrame + 1) % len(self.frames)
- if self.camp == 0:
- if self.kind == 0:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 10, 10))
- elif self.kind == 1:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 100, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 100, self.y, self.HP / 12, 10))
- elif self.kind == 2:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 24, 10))
- elif self.camp == 1:
- if self.kind == 0:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 10, 10))
- elif self.kind == 1:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 12, 10))
- elif self.kind == 2:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 24, 10))
-
- def eating(self):
- screen.blit(self.frames1[self.currentFrame1],
- (self.x, self.y))
- # 更新当前帧
- self.currentFrame1 = (self.currentFrame1 + 1) % len(self.frames1)
- if self.camp == 0:
- if self.kind == 0:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 10, 10))
- elif self.kind == 1:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 100, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 100, self.y, self.HP / 12, 10))
- elif self.kind == 2:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 24, 10))
- elif self.camp == 1:
- if self.kind == 0:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 10, 10))
- elif self.kind == 1:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 12, 10))
- elif self.kind == 2:
- pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
- pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 24, 10))
-
- def game(self):
- global rcont
- global lcont
- if self.camp == 0:
- if not self.cont_added:
- rcont = rcont + 1
- self.cont_added = True
- elif self.camp == 1:
- if not self.cont_added:
- lcont = lcont + 1
- self.cont_added = True
在游戏中,玩家可以通过点击不同的按钮来选择不同种类的僵尸,比如普通僵尸、路障僵尸和铁桶僵尸等。每个僵尸种类都有自己的特点和属性,需要玩家根据实际情况进行选择。另外,游戏还提供了不同的难度选择,玩家可以根据自己的实力来选择简单、普通或困难模式,增加了游戏的可玩性。目前僵尸种类不多,在后续会加入更多的僵尸,实现更加复杂的交互。
游戏中的僵尸移动、攻击等行为都经过碰撞算法进行设计,通过 Pygame 实现了生动的动画效果(能动),给玩家带来了更加真实的游戏体验。同时,游戏中还加入了音乐和阳光资源等元素,丰富了游戏的内容,为玩家带来更加丰富的游戏体验。(使用阳光召唤僵尸,定时生产阳光,消灭敌方僵尸获取阳光)
除此之外,游戏还设置了五条不同的路径供玩家进行选择,这样玩家可以根据实际情况进行布局,增加了游戏的策略性和趣味性。(目前是初版,后续会改成卡片拖拽指定分路生成)
总的来说,这款《僵尸大战僵尸》是一款非常有趣的塔防游戏,通过精心设计的僵尸种类、动画效果和游戏元素,为玩家带来了不错的游戏体验。如果你对塔防游戏感兴趣,不妨来试试这款游戏吧
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。