[Python] 纯文本查看 复制代码
import pygame
import sys
import random
import math
import time # 引入 time 模块
# 初始化 pygame
pygame.init()
# 窗口参数
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("乱飞子弹打怪升级游戏")
# 颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0) # 添加黄色
# 玩家参数
PLAYER_SIZE = 40
PLAYER_X = WIDTH // 2 - PLAYER_SIZE // 2
PLAYER_Y = HEIGHT - PLAYER_SIZE - 20
PLAYER_SPEED = 5
PLAYER_HEALTH = 100
PLAYER_MAX_HEALTH = 100
# 子弹参数
BULLET_SIZE = 10
BULLET_SPEED = 7
BULLET_COLOR = GREEN
bullets = []
BULLET_INTERVAL = 5
BULLET_TIMER = 0
# 怪物参数
MONSTER_SIZE = 30
MONSTER_SPEED = 2
MONSTERS = []
MONSTER_SPAWN_RATE = 60
MONSTER_SPAWN_TIMER = 0
MONSTERS_KILLED = 0
MONSTERS_TO_SPAWN_BOSS = 5 # 修改为 5 以便测试
# Boss 参数
BOSS_SIZE = 80
BOSS_X = WIDTH // 2 - BOSS_SIZE // 2
BOSS_Y = 50
BOSS_HEALTH = 100
BOSS_MAX_HEALTH = 100
BOSS_ACTIVE = False # 初始设置为False
BOSS = None
BOSS_BULLETS = []
BOSS_BULLET_SPEED = 5
BOSS_ATTACK_INTERVAL = 120 # BOSS攻击频率
BOSS_ATTACK_TIMER = 0
# 游戏状态
PLAYER_LEVEL = 1
PLAYER_EXP = 0
EXP_PER_LEVEL = 5
BULLET_ROWS = 1
GAME_STATE = "PLAYING"
# 其他参数
FPS = 60
clock = pygame.time.Clock()
def draw_player(x, y):
pygame.draw.rect(screen, WHITE, (x, y, PLAYER_SIZE, PLAYER_SIZE))
def draw_bullet(x, y):
pygame.draw.rect(screen, BULLET_COLOR, (x, y, BULLET_SIZE, BULLET_SIZE))
def draw_monster(x, y):
pygame.draw.rect(screen, RED, (x, y, MONSTER_SIZE, MONSTER_SIZE))
def draw_boss(x, y):
pygame.draw.rect(screen, BLUE, (x, y, BOSS_SIZE, BOSS_SIZE))
def draw_boss_bullet(x, y):
pygame.draw.rect(screen, YELLOW, (x, y, BULLET_SIZE,
BULLET_SIZE)) # 使用黄色绘制BOSS子弹
def create_bullet(player_x, player_y):
global bullets
start_y = player_y
for row in range(BULLET_ROWS):
y = start_y - row * BULLET_SIZE * 1.5
angle = random.uniform(0, 2 * math.pi)
bullet = {
"x": player_x + PLAYER_SIZE // 2 - BULLET_SIZE // 2,
"y": y,
"angle": angle,
"speed": BULLET_SPEED
}
bullets.append(bullet)
def create_monster():
x = random.randint(0, WIDTH - MONSTER_SIZE)
y = 0
MONSTERS.append({"x": x, "y": y})
def create_boss():
global BOSS_ACTIVE, BOSS
BOSS = {
"x": BOSS_X,
"y": BOSS_Y,
"health": BOSS_HEALTH,
"speed": 1, # 初始化BOSS的速度
"bullets": [] # 初始化 BOSS 的子弹列表
}
BOSS_ACTIVE = True
def create_boss_bullet(boss_x, boss_y):
global BOSS_BULLETS
angle = math.pi/2
bullet = {
"x": boss_x + BOSS_SIZE // 2 - BULLET_SIZE // 2,
"y": boss_y + BOSS_SIZE,
"angle": angle,
"speed": BOSS_BULLET_SPEED
}
BOSS_BULLETS.append(bullet)
def handle_bullets():
global bullets
bullets_to_remove = []
for bullet in bullets:
bullet["x"] += bullet["speed"] * math.cos(bullet["angle"])
bullet["y"] += bullet["speed"] * math.sin(bullet["angle"])
if bullet["x"] < 0 or bullet["x"] > WIDTH or bullet["y"] < 0 or bullet["y"] > HEIGHT:
bullets_to_remove.append(bullet)
for bullet in bullets_to_remove:
if bullet in bullets:
bullets.remove(bullet)
def handle_monsters():
global MONSTERS
monsters_to_remove = []
for monster in MONSTERS:
monster["y"] += MONSTER_SPEED
if monster["y"] > HEIGHT:
monsters_to_remove.append(monster)
for monster in monsters_to_remove:
if monster in MONSTERS:
MONSTERS.remove(monster)
# 每次刷新怪物后增加计数,确保BOSS可以正常出现
global MONSTERS_KILLED
MONSTERS_KILLED += 1
def handle_boss():
global BOSS, BOSS_ACTIVE, BOSS_ATTACK_TIMER, BOSS_BULLETS
if BOSS_ACTIVE and BOSS: # 确保 BOSS 对象存在
BOSS["x"] += BOSS["speed"]
if BOSS["x"] <= 0 or BOSS["x"] >= WIDTH - BOSS_SIZE:
BOSS["speed"] *= -1
BOSS_ATTACK_TIMER += 1
if BOSS_ATTACK_TIMER >= BOSS_ATTACK_INTERVAL:
create_boss_bullet(BOSS["x"], BOSS["y"])
BOSS_ATTACK_TIMER = 0
bullets_to_remove = []
for bullet in BOSS_BULLETS:
bullet["y"] += bullet["speed"]
if bullet["y"] < 0 or bullet["y"] > HEIGHT:
bullets_to_remove.append(bullet)
for bullet in bullets_to_remove:
if bullet in BOSS_BULLETS:
BOSS_BULLETS.remove(bullet)
def check_collisions():
# 添加 player_x, player_y 为全局变量
global MONSTERS, PLAYER_EXP, PLAYER_LEVEL, BULLET_ROWS, MONSTERS_KILLED, BOSS, BOSS_ACTIVE, GAME_STATE, PLAYER_HEALTH, BOSS_BULLETS, player_x, player_y
bullets_to_remove = []
monsters_to_remove = []
if GAME_STATE == "PLAYING":
for bullet in bullets:
for monster in MONSTERS:
if (bullet["x"] < monster["x"] + MONSTER_SIZE and
bullet["x"] + BULLET_SIZE > monster["x"] and
bullet["y"] < monster["y"] + MONSTER_SIZE and
bullet["y"] + BULLET_SIZE > monster["y"]):
bullets_to_remove.append(bullet)
monsters_to_remove.append(monster)
PLAYER_EXP += 1
break
for monster in MONSTERS:
if (player_x < monster["x"] + MONSTER_SIZE and
player_x + PLAYER_SIZE > monster["x"] and
player_y < monster["y"] + MONSTER_SIZE and
player_y + PLAYER_SIZE > monster["y"]):
PLAYER_HEALTH -= 5 # 玩家与怪物碰撞减血
for bullet in bullets_to_remove:
if bullet in bullets:
bullets.remove(bullet)
for monster in monsters_to_remove:
if monster in MONSTERS:
MONSTERS.remove(monster)
elif GAME_STATE == "BOSS_FIGHT":
for bullet in bullets:
if (bullet["x"] < BOSS["x"] + BOSS_SIZE and
bullet["x"] + BULLET_SIZE > BOSS["x"] and
bullet["y"] < BOSS["y"] + BOSS_SIZE and
bullet["y"] + BULLET_SIZE > BOSS["y"]):
bullets_to_remove.append(bullet)
BOSS["health"] -= 1
for bullet in BOSS_BULLETS:
if (player_x < bullet["x"] + BULLET_SIZE and
player_x + PLAYER_SIZE > bullet["x"] and
player_y < bullet["y"] + BULLET_SIZE and
player_y + PLAYER_SIZE > bullet["y"]):
PLAYER_HEALTH -= 10
for bullet in bullets_to_remove:
if bullet in bullets:
bullets.remove(bullet)
if BOSS["health"] <= 0:
GAME_STATE = "WIN"
BOSS_ACTIVE = False
if PLAYER_EXP >= EXP_PER_LEVEL:
PLAYER_LEVEL += 1
PLAYER_EXP = 0
BULLET_ROWS += 1
print(f"Level up to {PLAYER_LEVEL}, Bullet Rows: {BULLET_ROWS}")
def draw_text(text, x, y, color, font_size):
font = pygame.font.Font(None, font_size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(topleft=(x, y))
screen.blit(text_surface, text_rect)
def draw_health_bar(x, y, health, max_health):
BAR_WIDTH = 100
BAR_HEIGHT = 10
fill = (health / max_health) * BAR_WIDTH
outline_rect = pygame.Rect(x, y, BAR_WIDTH, BAR_HEIGHT)
fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
pygame.draw.rect(screen, RED, fill_rect)
pygame.draw.rect(screen, WHITE, outline_rect, 2)
def draw_player_health(x, y, health, max_health):
BAR_WIDTH = 150
BAR_HEIGHT = 15
fill = (health / max_health) * BAR_WIDTH
outline_rect = pygame.Rect(x, y, BAR_WIDTH, BAR_HEIGHT)
fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
pygame.draw.rect(screen, GREEN, fill_rect)
pygame.draw.rect(screen, WHITE, outline_rect, 2)