|
|
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(GRAY)
self.rect = self.image.get_rect(topleft=(x, y))
def create_walls():
walls = pygame.sprite.Group()
# 创建一些障碍物
wall_positions = [
(WIDTH//2 - 50, HEIGHT//4, 100, 20),
(WIDTH//2 - 50, HEIGHT//2 - 10, 100, 20),
(WIDTH//2 - 50, HEIGHT*3//4 - 20, 100, 20),
(WIDTH//4, HEIGHT//2 - 50, 20, 100),
(WIDTH*3//4 - 20, HEIGHT//2 - 50, 20, 100)
]
for x, y, w, h in wall_positions:
wall = Wall(x, y, w, h)
walls.add(wall)
all_sprites.add(wall)
return walls
def draw_health_bar(surface, x, y, percentage, color):
bar_length = 100
bar_height = 10
fill = (percentage / 100) * bar_length
border = pygame.Rect(x, y, bar_length, bar_height)
fill_rect = pygame.Rect(x, y, fill, bar_height)
pygame.draw.rect(surface, color, fill_rect)
pygame.draw.rect(surface, WHITE, border, 2)
def main():
global all_sprites, bullets, player1, player2
# 创建精灵组
all_sprites = pygame.sprite.Group()
bullets = pygame.sprite.Group()
# 创建玩家
player1 = Player(1)
player2 = Player(2)
all_sprites.add(player1)
all_sprites.add(player2)
# 创建墙壁
walls = create_walls()
running = True
while running:
# 控制游戏帧率
clock.tick(FPS)
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# 更新所有精灵
all_sprites.update()
# 碰撞检测:子弹击中玩家
for bullet in bullets:
if bullet.owner_id == 1 and player2.alive:
if pygame.sprite.collide_circle(bullet, player2):
player2.take_damage(BULLET_DAMAGE)
bullet.kill()
explosion = Explosion(bullet.rect.center)
all_sprites.add(explosion)
elif bullet.owner_id == 2 and player1.alive:
if pygame.sprite.collide_circle(bullet, player1):
player1.take_damage(BULLET_DAMAGE)
bullet.kill()
explosion = Explosion(bullet.rect.center)
all_sprites.add(explosion)
# 子弹碰撞墙壁
if pygame.sprite.spritecollide(bullet, walls, False):
bullet.kill()
explosion = Explosion(bullet.rect.center)
all_sprites.add(explosion)
# 玩家碰撞墙壁
if player1.alive:
player1.collides = pygame.sprite.spritecollide(player1, walls, False)
if player1.collides:
# 简单的碰撞处理,将玩家推回
for wall in player1.collides:
if player1.rect.right > wall.rect.left and player1.rect.left < wall.rect.left:
player1.rect.right = wall.rect.left
if player1.rect.left < wall.rect.right and player1.rect.right > wall.rect.right:
player1.rect.left = wall.rect.right
if player1.rect.bottom > wall.rect.top and player1.rect.top < wall.rect.top:
player1.rect.bottom = wall.rect.top
if player1.rect.top < wall.rect.bottom and player1.rect.bottom > wall.rect.bottom:
player1.rect.top = wall.rect.bottom
if player2.alive:
player2.collides = pygame.sprite.spritecollide(player2, walls, False)
if player2.collides:
# 简单的碰撞处理,将玩家推回
for wall in player2.collides:
if player2.rect.right > wall.rect.left and player2.rect.left < wall.rect.left:
player2.rect.right = wall.rect.left
if player2.rect.left < wall.rect.right and player2.rect.right > wall.rect.right:
player2.rect.left = wall.rect.right
if player2.rect.bottom > wall.rect.top and player2.rect.top < wall.rect.top:
player2.rect.bottom = wall.rect.top
if player2.rect.top < wall.rect.bottom and player2.rect.bottom > wall.rect.bottom:
player2.rect.top = wall.rect.bottom
# 绘制
screen.fill(BLACK)
all_sprites.draw(screen)
# 显示玩家1信息
p1_score = font.render(f"玩家1分数: {player1.score}", True, RED)
screen.blit(p1_score, (10, 10))
draw_health_bar(screen, 10, 50, player1.health, RED)
# 显示玩家2信息
p2_score = font.render(f"玩家2分数: {player2.score}", True, BLUE)
screen.blit(p2_score, (WIDTH - p2_score.get_width() - 10, 10))
draw_health_bar(screen, WIDTH - 110, 50, player2.health, BLUE)
# 显示操作说明
help_text1 = small_font.render("玩家1: WASD移动,空格射击", True, WHITE)
help_text2 = small_font.render("玩家2: 方向键移动,右Ctrl射击,ESC退出", True, WHITE)
screen.blit(help_text1, (10, HEIGHT - 30))
screen.blit(help_text2, (WIDTH - help_text2.get_width() - 10, HEIGHT - 30))
# 更新屏幕
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
|
|