请用python帮我写一个简单的第三人称射击小游戏
时间: 2023-06-01 08:04:24 浏览: 95
抱歉,我是一名AI语言模型,无法直接为您编写游戏程序。但我可以为您提供一个游戏的框架,供您参考和修改。您可以根据自己的需求进行修改或添加,祝您编写成功!
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 定义游戏窗口大小
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
# 创建游戏窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# 设置窗口标题
pygame.display.set_caption("Third Person Shooter")
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# 加载玩家和敌人的图片
player_img = pygame.image.load("player.png").convert()
enemy_img = pygame.image.load("enemy.png").convert()
# 定义玩家和敌人的初始位置
player_x = 100
player_y = 100
enemy_x = random.randint(0, WINDOW_WIDTH - enemy_img.get_width())
enemy_y = random.randint(0, WINDOW_HEIGHT - enemy_img.get_height())
# 定义玩家和敌人的移动速度
player_speed = 5
enemy_speed = 3
# 定义玩家和敌人的血量
player_health = 100
enemy_health = 50
# 定义字体
font = pygame.font.SysFont(None, 30)
# 游戏循环
running = True
while running:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 获取键盘按键状态
keys = pygame.key.get_pressed()
# 移动玩家
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < WINDOW_WIDTH - player_img.get_width():
player_x += player_speed
if keys[pygame.K_UP] and player_y > 0:
player_y -= player_speed
if keys[pygame.K_DOWN] and player_y < WINDOW_HEIGHT - player_img.get_height():
player_y += player_speed
# 移动敌人
if enemy_x < player_x:
enemy_x += enemy_speed
elif enemy_x > player_x:
enemy_x -= enemy_speed
if enemy_y < player_y:
enemy_y += enemy_speed
elif enemy_y > player_y:
enemy_y -= enemy_speed
# 绘制游戏场景
screen.fill(WHITE)
screen.blit(player_img, (player_x, player_y))
screen.blit(enemy_img, (enemy_x, enemy_y))
# 绘制血量条
pygame.draw.rect(screen, RED, (player_x, player_y - 10, player_health, 5))
pygame.draw.rect(screen, RED, (enemy_x, enemy_y - 10, enemy_health, 5))
# 判断是否碰撞
if pygame.Rect(player_x, player_y, player_img.get_width(), player_img.get_height()).colliderect(
pygame.Rect(enemy_x, enemy_y, enemy_img.get_width(), enemy_img.get_height())):
player_health -= 10
enemy_health -= 10
# 判断是否游戏结束
if player_health <= 0 or enemy_health <= 0:
running = False
# 绘制血量文本
player_health_text = font.render("Player Health: " + str(player_health), True, BLACK)
enemy_health_text = font.render("Enemy Health: " + str(enemy_health), True, BLACK)
screen.blit(player_health_text, (10, 10))
screen.blit(enemy_health_text, (WINDOW_WIDTH - enemy_health_text.get_width() - 10, 10))
# 更新游戏窗口
pygame.display.update()
# 游戏结束
if player_health <= 0:
print("You Lose!")
else:
print("You Win!")
# 退出pygame
pygame.quit()
```
阅读全文