import pygame
import time
class Fighter:
def __init__(self, x, y, image_path, attack_image_path):
self.x = x
self.y = y
self.speed = 2
self.image = pygame.image.load(image_path)
self.attack_image = pygame.image.load(attack_image_path)
self.is_jumping = False
self.jump_height = 15
self.jump_count = self.jump_height
self.health = 100
self.is_attacking = False
self.last_attack_time = 0
self.attack_delay = 0.3
def draw(self, screen):
if self.is_attacking:
screen.blit(self.attack_image, (self.x, self.y))
else:
screen.blit(self.image, (self.x, self.y))
def move(self, keys):
pass
def attack(self, enemy):
current_time = time.time()
if current_time - self.last_attack_time >= self.attack_delay:
if abs(self.x - enemy.x) <= 50:
enemy.health -= 2
if enemy.health < 0:
enemy.health = 0
self.is_attacking = True
self.last_attack_time = current_time
else:
self.is_attacking = False
class Fighter1(Fighter):
def move(self, keys):
if keys[pygame.K_a] and self.x > 0:
self.x -= self.speed
if keys[pygame.K_d] and self.x < 750:
self.x += self.speed
if keys[pygame.K_k] and not self.is_jumping:
self.is_jumping = True
if self.is_jumping:
if self.jump_count >= -self.jump_height:
neg = 1
if self.jump_count < 0:
neg = -1
self.y -= (self.jump_count ** 2) * 0.5 * neg
self.jump_count -= 1
else:
self.is_jumping = False
self.jump_count = self.jump_height
if keys[pygame.K_j]:
self.attack(fighter2)
else:
self.is_attacking = False
class Fighter2(Fighter):
def __init__(self, x, y, image_path, attack_image_path):
super().__init__(x, y, image_path, attack_image_path)
self.image = pygame.transform.scale(self.image, (int(self.image.get_width() * 0.5), int(self.image.get_height() * 0.5)))
self.attack_image = pygame.transform.scale(self.attack_image, (int(self.attack_image.get_width() * 0.5), int(self.attack_image.get_height() * 0.5)))
def move(self, keys):
if keys[pygame.K_LEFT] and self.x > 0:
self.x -= self.speed
if keys[pygame.K_RIGHT] and self.x < 750:
self.x += self.speed
if keys[pygame.K_KP2] and not self.is_jumping:
self.is_jumping = True
if self.is_jumping:
if self.jump_count >= -self.jump_height:
neg = 1
if self.jump_count < 0:
neg = -1
self.y -= (self.jump_count ** 2) * 0.5 * neg
self.jump_count -= 1
else:
self.is_jumping = False
self.jump_count = self.jump_height
if keys[pygame.K_KP1]:
self.attack(fighter1)
else:
self.is_attacking = False
def draw_health_bars(screen, fighter1, fighter2):
BAR_LENGTH = 300
BAR_HEIGHT = 20
# 角色 1 的血条
fill1 = (fighter1.health / 100) * BAR_LENGTH
outline_rect1 = pygame.Rect(20, 20, BAR_LENGTH, BAR_HEIGHT)
fill_rect1 = pygame.Rect(20, 20, fill1, BAR_HEIGHT)
pygame.draw.rect(screen, (255, 0, 0), outline_rect1, 2)
pygame.draw.rect(screen, (0, 255, 0), fill_rect1)
# 角色 2 的血条
fill2 = (fighter2.health / 100) * BAR_LENGTH
outline_rect2 = pygame.Rect(480, 20, BAR_LENGTH, BAR_HEIGHT)
fill_rect2 = pygame.Rect(480, 20, fill2, BAR_HEIGHT)
pygame.draw.rect(screen, (255, 0, 0), outline_rect2, 2)
pygame.draw.rect(screen, (0, 255, 0), fill_rect2)
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("格斗游戏")
background_image = pygame.image.load("background.jpg")
background_image = pygame.transform.scale(background_image, (width, height))
fighter1 = Fighter1(100, 500, "webwxgetmsgimg.jpg", "attack1.jpg")
fighter2 = Fighter2(600, 500, "_cgi-bin_mmwebwx-bin_webwxgetmsgimg__&MsgID=4151463399841909084&skey=@crypt_3b46ed7a_7efc25287b2eeaad8d2392b1642162ed&mmweb_appid=wx_webfilehelper.jpg", "attack2.jpg")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
fighter1.move(keys)
fighter2.move(keys)
screen.blit(background_image, (0, 0))
draw_health_bars(screen, fighter1, fighter2)
fighter1.draw(screen)
fighter2.draw(screen)
pygame.display.flip()
pygame.quit()
我要让角色的跳跃高度降低
最新发布