|
|
# 全局状态
current_level = 0
px = 0
py = 0
vx = 0
vy = 0
on_ground = False
game_over = False
win_level = False
game_complete = False
frame_count = 0
platform_list = []
ground_mob_list = []
fly_mob_list = []
bounce_mob_list = []
laser_list = []
def load_level(lv_idx):
global px,py,vx,vy,on_ground,game_over,win_level
global platform_list,ground_mob_list,fly_mob_list,bounce_mob_list,laser_list
lv = levels[lv_idx]
px = lv["start_x"]
py = lv["start_y"]
vx = 0
vy = 0
on_ground = False
game_over = False
win_level = False
platform_list = []
for pd in lv["platforms"]:
x,y,w,h = pd["rect"]
plat = {
"x":x,"y":y,"w":w,"h":h,
"origin_x":x,"origin_y":y,
"type":pd["type"],
"mr":pd.get("mr",0),
"msp":pd.get("msp",0),
"dir":1,
"timer":0,
"timer_max":pd.get("timer_max",0),
"visible":True
}
platform_list.append(plat)
ground_mob_list = []
for m in lv["ground_monsters"]:
ground_mob_list.append({"x":m["x"],"y":m["y"],"ox":m["x"],"range":m["range"],"spd":m["speed"],"dir":1,"w":26,"h":26})
fly_mob_list = []
for m in lv["fly_monsters"]:
fly_mob_list.append({"x":m["x"],"y":m["y"],"ox":m["x"],"range":m["range"],"spd":m["speed"],"dir":1,"w":26,"h":26})
bounce_mob_list = []
for m in lv["bounce_monsters"]:
bounce_mob_list.append({"x":m["x"],"y":m["y"],"ox":m["x"],"range":m["range"],"bspd":m["bounce_spd"],"vy":-m["bounce_spd"],"w":28,"h":28})
laser_list = []
for ls in lv["lasers"]:
laser_list.append({"x":ls["x"],"y":ls["y"],"h":ls["height"],"cycle":ls["cycle"],"timer":0,"active":False})
load_level(current_level)
clock = pygame.time.Clock()
running = True
while running:
frame_count += 1
# 地狱渐变背景
grad_surf = pygame.Surface((WIDTH, HEIGHT))
grad_surf.fill(BG_DARK)
pygame.draw.rect(grad_surf, BG_RED, (0, HEIGHT//2, WIDTH, HEIGHT//2))
screen.blit(grad_surf, (0,0))
clock.tick(60)
keys = pygame.key.get_pressed()
lv_data = levels[current_level]
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
if game_complete:
current_level = 0
game_complete = False
load_level(current_level)
else:
load_level(current_level)
if not game_over and not win_level and not game_complete:
if event.key == pygame.K_UP and on_ground:
vy = jump_power
on_ground = False
if not game_over and not win_level and not game_complete:
# 玩家移动与重力
vx = 0
if keys[pygame.K_LEFT]: vx = -speed
if keys[pygame.K_RIGHT]: vx = speed
vy += gravity
px += vx
py += vy
player_rect = pygame.Rect(px, py, pw, ph)
on_ground = False
# 更新所有平台
for plat in platform_list:
if not plat["visible"]:
continue
if plat["type"] == "lift":
plat["y"] += plat["msp"] * plat["dir"]
if plat["y"] > plat["origin_y"] + plat["mr"]: plat["dir"] = -1
if plat["y"] < plat["origin_y"] - plat["mr"]: plat["dir"] = 1
|
|