import os
import pygame
import pickle
import random
import math
from _pickle import UnpicklingError
current_path = os.path.dirname(__file__)
resource_path = os.path.join(current_path, 'resources')
MaxFPS = 100
cheats = False
class Map:
def __init__(self, path: list, name: str, backgroundColor: (int, int, int), pathColor: (int, int, int), displayColor: (int, int, int) = None):
self.name = name
self.path = path
self.backgroundColor = backgroundColor
self.pathColor = pathColor
self.displayColor = self.backgroundColor if displayColor is None else displayColor
screen = pygame.display.set_mode((1000, 600))
pygame.init()
pygame.font.init()
clock = pygame.time.Clock()
font = pygame.font.SysFont('Ubuntu Mono', 20)
mediumFont = pygame.font.SysFont('Ubuntu Mono', 30)
largeFont = pygame.font.SysFont('Ubuntu Mono', 75)
pygame.display.set_caption('Tower Defense')
RACE_TRACK = Map([[25, 0], [25, 375], [775, 375], [775, 25], [40, 25], [40, 360], [760, 360], [760, 40], [55, 40], [55, 345], [745, 345], [745, 55], [0, 55]], "Race Track", (19, 109, 21), (189, 22, 44), (189, 22, 44))
POND = Map([[0, 25], [700, 25], [700, 375], [100, 375], [100, 75], [800, 75]], "Pond", (6, 50, 98), (0, 0, 255))
LAVA_SPIRAL = Map([[300, 225], [575, 225], [575, 325], [125, 325], [125, 125], [675, 125], [675, 425], [25, 425], [25, 0]], "Lava Spiral", (207, 16, 32), (255, 140, 0), (178, 66, 0))
PLAINS = Map([[25, 0], [25, 375], [500, 375], [500, 25], [350, 25], [350, 175], [750, 175], [750, 0]], "Plains", (19, 109, 21), (155, 118, 83))
DESERT = Map([[0, 25], [750, 25], [750, 200], [25, 200], [25, 375], [800, 375]], "Desert", (170, 108, 35), (178, 151, 5))
THE_END = Map([[0, 225], [800, 225]], "The End", (100, 100, 100), (200, 200, 200))
Maps = [RACE_TRACK, POND, LAVA_SPIRAL, PLAINS, DESERT, THE_END]
waves = [
'00' * 3,
'00' * 5 + '01' * 3,
'00' * 3 + '01' * 5 + '02' * 3,
'00' * 3 + '01' * 5 + '02' * 5 + '03' * 3,
'03' * 30,
'02' * 30 + '03' * 30,
'04' * 30,
'04' * 15 + '05' * 15,
'06' * 25,
'0A',
'06' * 30,
'07' * 25,
'07' * 50,
'08' * 25,
'0B',
'16' * 25,
'17' * 25,
'17' * 50,
'18' * 25,
'1A',
]
enemyColors = {
'0': (255, 0, 0),
'1': (0, 0, 221),
'2': (0, 255, 0),
'3': (255, 255, 0),
'4': (255, 20, 147),
'5': (68, 68, 68),
'6': (255, 255, 255),
'7': (16, 16, 16),
'8': (110, 38, 14),
'A': (146, 43, 62),
'B': (191, 64, 191)
}
damages = {
'0': 1,
'1': 2,
'2': 3,
'3': 4,
'4': 5,
'5': 6,
'6': 7,
'7': 8,
'8': 9,
'A': 30,
'B': 69
}
speed = {
'0': 1,
'1': 1,
'2': 2,
'3': 2,
'4': 3,
'5': 4,
'6': 3,
'7': 2,
'8': 2,
'A': 1,
'B': 1
}
onlyExplosiveTiers = [7, 8]
trueHP = {
'A': 1000,
'B': 1500
}
bossCoins = {
'A': 150,
'B': 250
}
defaults = {
'enemies': [],
'projectiles': [],
'piercingProjectiles': [],
'towers': [],
'HP': 100,
'FinalHP': None,
'coins': 100000 if cheats else 50,
'selected': None,
'placing': '',
'nextWave': 299,
'wave': 0,
'win': False,
'lose': False,
'MapSelect': True,
'shopScroll': 0,
'spawnleft': '',
'spawndelay': 9,
'Map': None,
'totalWaves': len(waves)
}
LOCKED = 'LOCKED'
IceCircle = pygame.transform.scale(pygame.image.load(os.path.join(resource_path, 'ice_circle.png')), (250, 250))
smallIceCircle = IceCircle.copy()
smallIceCircle.fill((255, 255, 255, 128), None, pygame.BLEND_RGBA_MULT)
IceCircle = pygame.transform.scale(pygame.image.load(os.path.join(resource_path, 'ice_circle.png')), (350, 350))
largeIceCircle = IceCircle.copy()
largeIceCircle.fill((255, 255, 255, 128), None, pygame.BLEND_RGBA_MULT)
rangeImages = []
possibleRanges = [50, 100, 125, 130, 150, 165, 175, 180, 200, 250, 400]
for possibleRange in possibleRanges:
rangeImage = pygame.transform.scale(pygame.image.load(os.path.join(resource_path, 'range.png')), (possibleRange * 2,) * 2)
alphaImage = rangeImage.copy()
alphaImage.fill((255, 255, 255, 128), None, pygame.BLEND_RGBA_MULT)
rangeImages.append(alphaImage)
SIN45 = COS45 = math.sqrt(2) / 2
class data:
def __init__(self):
self.PBs = {Map.name: (LOCKED if Map.name != 'Pond' else None) for Map in Maps}
for attr, default in defaults.items():
setattr(self, attr, default)
def reset(self):
for attr, default in defaults.items():
if attr in ['PBs', 'FinalHP', 'totalWaves']:
continue
setattr(self, attr, default if type(default) is not list else [])
class Towers:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
self.timer = 0
self.upgrades = [0, 0, 0]
self.stun = 0
self.hits = 0
def draw(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), 15)
def attack(self):
pass
def update(self):
pass
class Turret(Towers):
name = 'Turret'
color = (128, 128, 128)
req = 0
price = 50
upgradePrices = [
[30, 75, 125],
[20, 60, 275],
[75, 125, 175]
]
upgradeNames = [
['Longer Range', 'Extreme Range', 'Ultra Range'],
['More Bullets', 'Bullet Rain', 'Double Bullets'],
['Explosive Shots', 'Camo Detection', 'Boss Shred']
]
range = 100
cooldown = 75
def __init__(self, x: int, y: int):
super().__init__(x, y)
def attack(self):
if self.stun > 0:
self.stun -= 1
return
if self.timer >= self.cooldown:
try:
closest = getTarget(self)
explosiveRadius = 30 if self.upgrades[2] >= 1 else 0
info.projectiles.append(Projectile(self, self.x, self.y, closest.x, closest.y, bossDamage= 5 if self.upgrades[2] == 3 else 1, explosiveRadius=explosiveRadius))
self.timer = 0
except AttributeError:
pass
else:
self.timer += 1
def update(self):
self.range = [100, 130, 165, 200][self.upgrades[0]]
self.cooldown = [60, 35, 20, 10][self.upgrades[1]]
class IceTower(Towers):
class SnowStormCircle:
def __init__(self, parent, x, y):
self.x = x
self.y = y
self.parent = parent
self.freezeDuration = [100, 150, 150, 199][self.parent.upgrades[2]]
self.visibleTicks = 0
def draw(self):
if self.visibleTicks > 0:
self.visibleTicks -= 1
screen.blit(largeIceCircle if self.parent.upgrades[0] >= 1 else smallIceCircle, (self.x - self.parent.range, self.y - self.parent.range))
def freeze(self):
self.visibleTicks = 50
for enemy in info.enemies:
if abs(enemy.x - self.x) ** 2 + abs(enemy.y - self.y) ** 2 <= self.parent.range ** 2:
if type(enemy.tier) is int:
enemy.freezeTimer = self.freezeDuration
def update(self):
self.freezeDuration = [100, 150, 150, 199][self.parent.upgrades[2]]
name = 'Ice Tower'
color = (32, 32, 200)
req = 2
price = 30
upgradePrices = [
[30, 60, 100],
[30, 50, 85],
[30, 50, 75]
]
upgradeNames = [
['Longer Range', 'Extreme Range', 'Ultra Range'],
['Lesser Cooldown', 'Snowball Shower', 'Heavy Snowfall'],
['Longer Freeze', 'Snowstorm Circle', 'Ultra Freeze']
]
range = 125
cooldown = 100
freezeDuration = 20
def __init__(self, x: int, y: int):
super().__init__(x, y)
self.snowCircle = self.SnowStormCircle(self, self.x, self.y)
def draw(self):
super().draw()
self.snowCircle.draw()
def attack(self):
if self.stun > 0:
self.stun -= 1
return
if self.timer >= self.c
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共9个文件
py:4个
png:2个
license:1个
资源分类:Python库 所属语言:Python 资源全名:tower-defense-1.6.7.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
tower-defense-1.6.7.tar.gz (9个子文件)
tower-defense-1.6.7
PKG-INFO 435B
pyproject.toml 384B
tower_defense
main.py 58KB
resources
range.png 14KB
ice_circle.png 14KB
__main__.py 28B
LICENSE 1KB
__init__.py 22B
setup.py 743B
共 9 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功