|
|
import pygame,random,math
pygame.init()
W,H=800,600
sc=pygame.display.set_mode((W,H))
clk=pygame.time.Clock()
pygame.display.set_caption("CYBER GRAVITY")
#游戏全局变量
cx,cy=W/2,H/2
particles=[]
lasers=[]
score=0
timer=0
level=1
run=True
shot_cd=0
#关卡目标分数
level_target=[80,180,320]
while run:
sc.fill((6,8,18))
timer+=1
shot_cd=max(0,shot_cd-1)
#事件监听
for e in pygame.event.get():
if e.type==pygame.QUIT:
run=False
#鼠标左键发射激光
if e.type==pygame.MOUSEBUTTONDOWN and e.button==1 and shot_cd==0:
mx,my=pygame.mouse.get_pos()
ang=math.atan2(my-cy,mx-cx)
lasers.append([cx,cy,math.cos(ang)*9,math.sin(ang)*9])
shot_cd=12
#飞船平滑跟随鼠标
mx,my=pygame.mouse.get_pos()
cx+=(mx-cx)*0.16
cy+=(my-cy)*0.16
#根据关卡调整粒子生成难度
spawn_rate=0.065+level*0.008
danger_rate=0.22+level*0.03
if random.random()<spawn_rate:
type_ok=1 if random.random()>danger_rate else 0
vx=random.uniform(-2.5,2.5)
vy=random.uniform(1.8,3.2)
particles.append([random.randint(0,W),-15,vx,vy,type_ok])
#更新粒子
for p in particles[:]:
p[0]+=p[2]
p[1]+=p[3]
x,y=p[0],p[1]
dist=math.hypot(x-cx,y-cy)
#引力拉扯
if dist<150:
p[0]-=(x-cx)*0.04
p[1]-=(y-cy)*0.04
#飞船碰撞危险粒子=游戏结束
if dist<22 and p[4]==0:
run=False
#吸收能量粒子加分
if dist<22 and p[4]==1:
score+=5
particles.remove(p)
#边界清除
elif y>H+30 or x<-30 or x>W+30:
particles.remove(p)
#绘制粒子
color=(0,235,210) if p[4] else (190,20,110)
pygame.draw.circle(sc,color,(int(x),int(y)),6)
#更新激光
for la in lasers[:]:
la[0]+=la[2]
la[1]+=la[3]
#激光击中危险粒子
for p in particles[:]:
d=math.hypot(la[0]-p[0],la[1]-p[1])
if d<14 and p[4]==0:
particles.remove(p)
score+=8
lasers.remove(la)
break
#激光超出屏幕删除
if la[0]<0 or la[0]>W or la[1]<0 or la[1]>H:
lasers.remove(la)
pygame.draw.circle(sc,(60,220,255),(int(la[0]),int(la[1])),4)
#绘制飞船
ship=[(cx,cy-16),(cx-12,cy+12),(cx+12,cy+12)]
pygame.draw.polygon(sc,(140,220,255),ship)
pygame.draw.polygon(sc,(80,200,255),ship,2)
#关卡判定:达到分数进入下一关,最多3关
if level<=3 and score>=level_target[level-1]:
level+=1
particles.clear()
pygame.display.flip()
clk.tick(60)
pygame.quit()
|
|