圆月山庄资源网 Design By www.vgjia.com
利用python3的pygame模块基本实现塔防游戏的基本功能,包括血量和分数显示,bgm,防御塔建造,防御塔攻击范围内的敌军,暂停和加速功能。由于实在没有素材,用的都是自己截图P的,所以美化不好。但基本保证功能,其中有一个BUG,但不影响游戏效果。
1.运行主类
"""主程序""" from pygame.locals import * from TowerDefend.enemy import * from TowerDefend.towerposSet import * from TowerDefend.tower import * import pygame def run(): """运行函数""" pygame.init() size = width, height = 1200, 600 screen = pygame.display.set_mode(size) background_img = pygame.image.load(r'image/background.png').convert_alpha() background_img = pygame.transform.scale(background_img, (width, height)) # 创建分数和血量 health_count = 5 score_count = 0 score = pygame.font.Font('font/score_health.ttf', 30) health = pygame.font.Font('font/score_health.ttf', 30) # 创建背景音乐 bg_music = pygame.mixer.music bg_music.load('media/bg.mp3') bg_music.set_volume(2) # 创建敌军类 enemies = pygame.sprite.Group() ENEMY_NUM = 5 position = [[258, 600], [258, 670], [258, 740], [258, 810], [258, 880]] for i in range(ENEMY_NUM): enemies.add(Enemy(position[i])) # 创建炮塔 towers = pygame.sprite.Group() # 加载暂停键 pause_img = pygame.image.load('image/pause.png').convert_alpha() pause_rect = pause_img.get_rect() pause_rect.left, pause_rect.top = 1145, 0 # 加载血量和金币显示 health_money_img = pygame.image.load('image/health_money.png').convert_alpha() health_money_rect = health_money_img.get_rect() health_money_rect.left, health_money_rect.top = 0, 0 # 加载加速键 speed_img = pygame.image.load('image/speed.png').convert_alpha() speed_rect = speed_img.get_rect() speed_rect.left, speed_rect.top = 1090, 0 # 设置炮塔位置 towers_pos = pygame.sprite.Group() position_list = [[225, 495], [264, 428], [312, 428], [362, 428], [410, 428], [460, 428], [508, 428], [561, 428], [561, 373], [377, 373]] for i in range(len(position_list)): towers_pos.add(Position(position_list[i])) # 设置循环条件 running = True clock = pygame.time.Clock() paused = False # 播放音乐 if not bg_music.get_busy(): bg_music.play(-1) while running: clock.tick(100) for event in pygame.event.get(): if event.type == QUIT: running = False if event.type == MOUSEBUTTONDOWN: if event.button == 1: if speed_rect.collidepoint(event.pos): for each in enemies: each.accelerate *= 2 if pause_rect.collidepoint(event.pos): paused = not paused for each in towers_pos: if each.rect.collidepoint(event.pos): tower = Tower([each.rect.left, each.rect.top]) towers.add(tower) towers_pos.remove(each) if not paused: for enemy in enemies: if enemy.active: enemy.move() else: if enemy.rect.top <= 180: health_count -= 1 if enemy.rect.top > 180: score_count += 20 enemies.remove(enemy) # 绘制界面设置 screen.blit(background_img, (0, 0)) screen.blit(health.render(str(health_count), True, (255, 255, 255)), (60, 0.3)) screen.blit(score.render(str(score_count), True, (255, 255, 255)), (130, 0.5)) screen.blit(health_money_img, health_money_rect) screen.blit(pause_img, pause_rect) screen.blit(speed_img, speed_rect) # 绘制炮塔 for each in towers: each.draw(screen, enemies) each.hit(enemies) # 绘制炮塔位置 towers_pos.draw(screen) # 敌军若存活则绘制其和血量 for enemy in enemies: if enemy.active: screen.blit(enemy.img, enemy.rect) enemy.drawhealth(screen) pygame.display.flip() pygame.quit() if __name__ == "__main__": run() 2.炮塔类 """TOWER""" import pygame import math class Tower(pygame.sprite.Sprite): """tower""" def __init__(self, pos): pygame.sprite.Sprite.__init__(self) self.img0 = pygame.image.load('image/tower0.png') self.img1 = pygame.image.load('image/tower1.png') self.img2 = pygame.image.load('image/tower2.png') self.rect = self.img0.get_rect() self.rect.left, self.rect.top = pos self.count = 1 def draw(self, screen, enemies): """绘制""" if self.count > 90: self.count = 1 if 1 <= self.count < 30: screen.blit(self.img0, self.rect) elif 30 <= self.count < 60: screen.blit(self.img1, self.rect) else: screen.blit(self.img2, self.rect) for enemy in enemies: distance = math.sqrt( math.pow((self.rect.left - enemy.rect.left), 2) + math.pow((self.rect.top - enemy.rect.top), 2)) if distance < 50 and enemy.active is True: self.count += 1 def hit(self, enemies): """攻击""" for enemy in enemies: distance = math.sqrt( math.pow((self.rect.left - enemy.rect.left), 2) + math.pow((self.rect.top - enemy.rect.top), 2)) if distance < 50: enemy.health -= 1 if enemy.health == 0: enemy.active = False
3.敌军类
import pygame class Enemy(pygame.sprite.Sprite): """小兵类""" def __init__(self, position): pygame.sprite.Sprite.__init__(self) self.img = pygame.image.load(r'image/enemy.png').convert_alpha() self.rect = self.img.get_rect() self.init_pos = position self.rect.left, self.rect.top = self.init_pos self.accelerate = 1 self.speed = [0, -self.accelerate] self.active = True self.health = 500 # 竖直 self.status = 'UP' def move(self): """移动""" self.rect = self.rect.move(self.speed) if self.rect.top <= 448 and self.rect.left == 258: self.rect.top = 448 self.status = 'R' self.img = pygame.transform.rotate(self.img, 270) self.speed = [self.accelerate, 0] if self.rect.top == 448 and self.rect.left >= 597: self.rect.left = 597 self.status = 'UP' self.img = pygame.transform.rotate(self.img, 90) self.speed = [0, -self.accelerate] if 320 < self.rect.top <= 335 and self.rect.left == 597: self.rect.top = 335 self.status = 'L' self.img = pygame.transform.rotate(self.img, 90) self.speed = [-self.accelerate, 0] if self.rect.top == 335 and self.rect.left <= 370: self.rect.left = 370 self.status = 'UP' self.img = pygame.transform.rotate(self.img, 270) self.speed = [0, -self.accelerate] if self.rect.top <= 216 and self.rect.left == 370: self.rect.top = 216 self.status = 'R' self.img = pygame.transform.rotate(self.img, 270) self.speed = [self.accelerate, 0] if self.rect.top == 216 and 800 > self.rect.left >= 746: self.rect.left = 746 self.status = 'DW' self.img = pygame.transform.rotate(self.img, 270) self.speed = [0, self.accelerate] if self.rect.top >= 330 and self.rect.left == 746: self.rect.top = 330 self.status = 'R' self.img = pygame.transform.rotate(self.img, 90) self.speed = [self.accelerate, 0] if self.rect.top == 330 and self.rect.left >= 930: self.rect.left = 930 self.status = 'UP' self.img = pygame.transform.rotate(self.img, 90) self.speed = [0, -self.accelerate] if self.rect.top < 180: self.active = False def drawhealth(self, screen): """绘制血量""" BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) health_percentage = float(self.health) / 500 if self.status == 'UP': start = self.rect.left + 8 pygame.draw.line(screen, BLACK, (start, self.rect.top - 3), (start + 32, self.rect.top - 3), 4) if health_percentage > 0.5: pygame.draw.line(screen, GREEN, (start, self.rect.top - 3), (start + 32 * health_percentage, self.rect.top - 3), 4) else: pygame.draw.line(screen, RED, (start, self.rect.top - 3), (start + 32 * health_percentage, self.rect.top - 3), 4) elif self.status == 'DW': start = self.rect.left + 8 pygame.draw.line(screen, BLACK, (start, self.rect.bottom + 3), (start + 32, self.rect.bottom + 3), 4) if health_percentage > 0.5: pygame.draw.line(screen, GREEN, (start, self.rect.bottom + 3), (start + 32 * health_percentage, self.rect.bottom + 3), 4) else: pygame.draw.line(screen, RED, (start, self.rect.bottom + 3), (start + 32 * health_percentage, self.rect.bottom + 3), 4) elif self.status == 'R': start = self.rect.bottom - 8 pygame.draw.line(screen, BLACK, (self.rect.right + 3, start), (self.rect.right + 3, start - 32), 4) if health_percentage > 0.5: pygame.draw.line(screen, GREEN, (self.rect.right + 3, start), (self.rect.right + 3, start - 32 * health_percentage), 4) else: pygame.draw.line(screen, RED, (self.rect.right + 3, start), (self.rect.right + 3, start - 32 * health_percentage), 4) else: start = self.rect.bottom - 8 pygame.draw.line(screen, BLACK, (self.rect.left - 3, start), (self.rect.left - 3, start - 30), 4) if health_percentage > 0.5: pygame.draw.line(screen, GREEN, (self.rect.left - 3, start), (self.rect.left - 3, start - 30 * health_percentage), 4) else: pygame.draw.line(screen, RED, (self.rect.left - 3, start), (self.rect.left - 3, start - 30 * health_percentage), 4) def reset(self): """reset the position""" self.rect.left, self.rect.top = self.init_pos
4.炮塔建造位置类
"""初始化炮塔可占据的位置""" import pygame class Position(pygame.sprite.Sprite): """pos""" def __init__(self, pos): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load('image/pt.png') self.rect = self.image.get_rect() self.rect.left, self.rect.top = pos
5.说明
本游戏通过鼠标左键点击炮塔可以建造的位置俩建造炮塔。
6.效果图
可以看到图确实不好看,但美术功底不够,所以不怎么好看。当然需要素材的也可以给你。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月03日
2024年11月03日
- 明达年度发烧碟MasterSuperiorAudiophile2021[DSF]
- 英文DJ 《致命的温柔》24K德国HD金碟DTS 2CD[WAV+分轨][1.7G]
- 张学友1997《不老的传说》宝丽金首版 [WAV+CUE][971M]
- 张韶涵2024 《不负韶华》开盘母带[低速原抓WAV+CUE][1.1G]
- lol全球总决赛lcs三号种子是谁 S14全球总决赛lcs三号种子队伍介绍
- lol全球总决赛lck三号种子是谁 S14全球总决赛lck三号种子队伍
- 群星.2005-三里屯音乐之男孩女孩的情人节【太合麦田】【WAV+CUE】
- 崔健.2005-给你一点颜色【东西音乐】【WAV+CUE】
- 南台湾小姑娘.1998-心爱,等一下【大旗】【WAV+CUE】
- 【新世纪】群星-美丽人生(CestLaVie)(6CD)[WAV+CUE]
- ProteanQuartet-Tempusomniavincit(2024)[24-WAV]
- SirEdwardElgarconductsElgar[FLAC+CUE]
- 田震《20世纪中华歌坛名人百集珍藏版》[WAV+CUE][1G]
- BEYOND《大地》24K金蝶限量编号[低速原抓WAV+CUE][986M]
- 陈奕迅《准备中 SACD》[日本限量版] [WAV+CUE][1.2G]