demo boss_fight

This commit is contained in:
Guanforever 2025-06-30 16:34:04 +08:00
parent ecb9f0531b
commit 9ababe18f1
3 changed files with 135 additions and 0 deletions

BIN
boss.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 231 KiB

BIN
skill.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

135
wan.py Normal file
View File

@ -0,0 +1,135 @@
import pygame
import sys
import time
# 配置
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
BOSS_IMG = 'boss.png' # 替换为你的boss图片路径
SKILL_IMG = 'skill.png' # 替换为你的技能图片路径
FPS = 60
# 震动参数
def shake_pos(x, y, frame):
if frame % 2 == 0:
return x + 5, y
else:
return x - 5, y
def draw_health_bar(screen, x, y, w, h, hp, max_hp):
pygame.draw.rect(screen, (180, 0, 0), (x, y, w, h))
pygame.draw.rect(screen, (0, 220, 0), (x, y, int(w * hp / max_hp), h))
pygame.draw.rect(screen, (0, 0, 0), (x, y, w, h), 2)
class Skill:
def __init__(self, idx, dmg, img):
self.idx = idx
self.dmg = dmg
self.img = img
self.x = 0
self.y = SCREEN_HEIGHT // 2
self.active = True
def update(self):
self.x += 20
if self.x > SCREEN_WIDTH:
self.active = False
def draw(self, screen):
screen.blit(self.img, (self.x, self.y))
class Boss:
def __init__(self, img, hp):
self.img = img
self.hp = hp
self.max_hp = hp
self.x = SCREEN_WIDTH - img.get_width() - 50
self.y = SCREEN_HEIGHT // 2 - img.get_height() // 2
self.shake_frame = 0
self.shake = False
def hit(self, dmg):
self.hp = max(0, self.hp - dmg)
self.shake = True
self.shake_frame = 10
def update(self):
if self.shake:
self.shake_frame -= 1
if self.shake_frame <= 0:
self.shake = False
def draw(self, screen):
if self.shake:
x, y = shake_pos(self.x, self.y, self.shake_frame)
else:
x, y = self.x, self.y
screen.blit(self.img, (x, y))
draw_health_bar(screen, x, y - 30, 200, 20, self.hp, self.max_hp)
def main(boss_hp, skill_seq):
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('自动打Boss')
clock = pygame.time.Clock()
boss_img = pygame.image.load(BOSS_IMG).convert_alpha()
skill_img = pygame.image.load(SKILL_IMG).convert_alpha()
boss = Boss(boss_img, boss_hp)
skills = []
skill_idx = 0
running = True
skill_cooldown = 0
font = pygame.font.SysFont(None, 36)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((220, 220, 255))
boss.update()
boss.draw(screen)
# 技能释放
if skill_idx < len(skill_seq) and skill_cooldown == 0:
s = skill_seq[skill_idx]
skills.append(Skill(s['first'], s['second'], skill_img))
skill_cooldown = 30 # 帧数间隔
skill_idx += 1
if skill_cooldown > 0:
skill_cooldown -= 1
# 技能动画
for skill in skills:
if skill.active:
skill.update()
skill.draw(screen)
# 判断是否击中boss
if skill.x + skill.img.get_width() > boss.x and boss.hp > 0:
boss.hit(skill.dmg)
skill.active = False
# 移除无效技能
skills = [s for s in skills if s.active]
# 显示血量数字
hp_text = font.render(f'Boss HP: {boss.hp}/{boss.max_hp}', True, (0,0,0))
screen.blit(hp_text, (50, 30))
pygame.display.flip()
clock.tick(FPS)
if boss.hp <= 0 and not skills:
time.sleep(1)
running = False
pygame.quit()
if __name__ == '__main__':
# 示例boss血量1000技能序列
boss_hp = 1000
skill_seq = [
{'first': 0, 'second': 120},
{'first': 1, 'second': 200},
{'first': 2, 'second': 150},
{'first': 3, 'second': 300},
{'first': 4, 'second': 250},
]
main(boss_hp, skill_seq)