好的,下面是一个简单的火柴人格斗游戏的Python代码:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小和标题
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("火柴人格斗游戏")
# 火柴人的大小
stickman_size = 50
# 火柴人1的初始位置、速度和血量
stickman1_x = 100
stickman1_y = height - stickman_size
stickman1_speed_x = 0
stickman1_speed_y = 0
stickman1_hp = 100
# 火柴人2的初始位置、速度和血量
stickman2_x = width - 100 - stickman_size
stickman2_y = height - stickman_size
stickman2_speed_x = 0
stickman2_speed_y = 0
stickman2_hp = 100
# 游戏循环
running = True
while running:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
# 火柴人1的控制
if event.key == pygame.K_a:
stickman1_speed_x = -10
elif event.key == pygame.K_d:
stickman1_speed_x = 10
elif event.key == pygame.K_w:
stickman1_speed_y = -10
elif event.key == pygame.K_s:
stickman1_speed_y = 10
elif event.key == pygame.K_SPACE:
# 火柴人1攻击
if abs(stickman1_x - stickman2_x) < stickman_size:
stickman2_hp -= 10
elif event.type == pygame.KEYUP:
# 火柴人1的控制
if event.key == pygame.K_a or event.key == pygame.K_d:
stickman1_speed_x = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
stickman1_speed_y = 0
# 移动火柴人
stickman1_x += stickman1_speed_x
stickman1_y += stickman1_speed_y
stickman2_x += stickman2_speed_x
stickman2_y += stickman2_speed_y
# 检测火柴人是否碰到边界,如果碰到,则停止移动
if stickman1_x < 0:
stickman1_x = 0
stickman1_speed_x = 0
elif stickman1_x + stickman_size > width:
stickman1_x = width - stickman_size
stickman1_speed_x = 0
if stickman1_y < 0:
stickman1_y = 0
stickman1_speed_y = 0
elif stickman1_y + stickman_size > height:
stickman1_y = height - stickman_size
stickman1_speed_y = 0
if stickman2_x < 0:
stickman2_x = 0
stickman2_speed_x = 0
elif stickman2_x + stickman_size > width:
stickman2_x = width - stickman_size
stickman2_speed_x = 0
if stickman2_y < 0:
stickman2_y = 0
stickman2_speed_y = 0
elif stickman2_y + stickman_size > height:
stickman2_y = height - stickman_size
stickman2_speed_y = 0
# 绘制游戏界面
screen.fill((255, 255, 255)) # 白色背景
pygame.draw.rect(screen, (255, 0, 0), (stickman1_x, stickman1_y, stickman_size, stickman_size)) # 火柴人1
pygame.draw.rect(screen, (0, 0, 255), (stickman2_x, stickman2_y, stickman_size, stickman_size)) # 火柴人2
pygame.draw.rect(screen, (255, 0, 0), (10, 10, stickman1_hp, 20)) # 火柴人1的血条
pygame.draw.rect(screen, (0, 0, 255), (width - 10 - stickman2_hp, 10, stickman2_hp, 20)) # 火柴人2的血条
pygame.display.flip() # 更新屏幕显示
# 控制游戏帧率
pygame.time.delay(10)
# 检测火柴人1和火柴人2是否碰撞
if abs(stickman1_x - stickman2_x) < stickman_size and abs(stickman1_y - stickman2_y) < stickman_size:
stickman1_hp -= 1
# 检测游戏是否结束
if stickman1_hp <= 0 or stickman2_hp <= 0:
running = False
# 显示游戏结束界面
screen.fill((255, 255, 255)) # 白色背景
if stickman1_hp <= 0:
text = "火柴人2获胜!"
else:
text = "火柴人1获胜!"
font = pygame.font.SysFont(None, 48)
text_surface = font.render(text, True, (0, 0, 0))
text_rect = text_surface.get_rect()
text_rect.center = (width // 2, height // 2)
screen.blit(text_surface, text_rect)
pygame.display.flip()
# 等待用户退出游戏
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
```
这个游戏实现了两个火柴人之间的格斗,其中火柴人1由玩家控制,火柴人2由电脑控制。玩家可以使用键盘控制火柴人1的移动和攻击,火柴人2会随机移动。游戏结束后会显示获胜方的信息。你可以根据自己的需求对游戏进行进一步的改进和扩展。