大致规则:你是红球,射击黑球扣分,碰到黑球加分;射击其他球加分,碰到其他球扣分。
黑球碰地也会扣分。
代码不短,但思路很简单,很清楚,你可以自己修改数据。不过,需要先安装pygame
操作方式:按一下a跟着鼠标走,按一下d停止移动。按s射击,按w瞬移,但都要消耗分数。
如果不修改数据的话,可以忽略下面注释内的规则。只要记住前20s难度小,后面难度大
没有结束的标准,不过如果你愿意,可以自己设一个。
'''游戏规则:
你是红球。初始分数为0,运动速度为0.5
碰到黑球+5分(30s后+15),黄球-15分,绿球-25分
按s射击,消耗分数。消耗的分与射击距离成正比。
射到黑球-10分,黄球+10分(30s后+20),绿球+15分(30s后+25)
一定范围内,你的运动速度与分数成正比。
分越低,速度越快。最快1.5,最慢0.2
黑球垂直下落,黄色乱动,绿色朝着你动
黑球:30s前2s一个,v=0.5,30s后1s一个,v=0.7。
黄球:20s前1s刷新,方向随机且不变,20-35s0.7s刷新,方向随机且改变,35s后0.5s刷新。v最快,但随机。
50s后在红球横纵200范围内刷新,速度1,但会改变,且刷新时距离大于80
绿球:v=1.初始方向指向玩家,30s前不改变,3s刷新,30s后不断调整.30-45s,2s刷新,45s后1s刷新
移动:方向为鼠标方向。按a解锁,d锁定。w瞬移,但要消耗分数。
'''
#导入
import pygame,random,time
from pygame.locals import *
from sys import exit
#设置界面与参数
pygame.init()
screen=pygame.display.set_mode((1000,800),0,32)
pygame.display.set_caption('games')
ball_x=[random.randint(15,985)]#黑球位置
ball_y=[0]
yellow_x=[]#黄球位置
yellow_y=[]
yellow_pos_x=[]#黄球运动方向
yellow_pos_y=[]
green_x=[]#绿球位置
green_y=[]
green_pos_x=[]#绿球运动方向
green_pos_y=[]
def new_yellow():#前期创建新黄球
a=random.randint(0,3)
if a==0:
yellow_x.append(0)
yellow_y.append(random.randint(200,600))
yellow_pos_x.append(random.randint(6,12)/10)
yellow_pos_y.append(random.randint(-12,12)/10)
elif a==1:
yellow_x.append(1000)
yellow_y.append(random.randint(200,600))
yellow_pos_x.append(random.randint(-12,-6)/10)
yellow_pos_y.append(random.randint(-12,12)/10)
elif a==2:
yellow_y.append(0)
yellow_x.append(random.randint(300,700))
yellow_pos_y.append(random.randint(6,12)/10)
yellow_pos_x.append(random.randint(-12,12)/10)
else:
yellow_y.append(800)
yellow_x.append(random.randint(300,700))
yellow_pos_y.append(random.randint(-12,-6)/10)
yellow_pos_x.append(random.randint(-12,12)/10)
def new_yellow2():#后期创建新黄球
d=0
x1,y1=self_x,self_y
while d <=80 or not(0<x1<1000 and 0<y1<800):
x1,y1=self_x+random.randint(-200,200),self_y+random.randint(-200,200)
dx=-(x1-self_x)
dy=-(y1-self_y)
d=(dx**2+dy**2)**0.5
yellow_x.append(x1)
yellow_y.append(y1)
k=1/d
yellow_pos_x.append(dx*k+random.randint(-10,10)/10)
yellow_pos_y.append(dy*k+random.randint(-10,10)/10)
def new_green(i):#创建新绿球
a=random.randint(0,3)
if a==0:
green_x.append(0)
green_y.append(random.randint(200,600))
elif a==1:
green_x.append(1000)
green_y.append(random.randint(200,600))
elif a==2:
green_y.append(0)
green_x.append(random.randint(300,700))
else:
green_y.append(800)
green_x.append(random.randint(300,700))
green_position(i)
def green_position(i):#改变或设置绿球运动方向
global green_x,green_y,self_x,self_y,green_pos_x,green_pos_y
dx=-(green_x[i]-self_x)
dy=-(green_y[i]-self_y)
d=(dx**2+dy**2)**0.5
k=1/d
try:
green_pos_x[i]=k*dx
green_pos_y[i]=k*dy
except:
green_pos_x.append(k*dx)
green_pos_y.append(k*dy)
new_yellow()
self_x=500#红球位置
self_y=750
target_x,target_y=self_x,self_y
position_x,position_y=self_x,self_y
time_start=time.time()#获取时间
time2=time.time()
time3=time.time()
time4=time.time()
point=0#分数
shoot=False#是否正在射击
go=False#是否正在运动
speed=0.5
#main
while True:
#处理事件
time_now=time.time()
t=time_now-time3
for event in pygame.event.get():
if event.type==QUIT:#退出
exit()
elif event.type==KEYDOWN:#键盘事件
if event.key==K_a:#运动
go=True
elif event.key==K_d:#停止运动
go=False
elif event.key==K_w:#瞬移
target_x,target_y=pygame.mouse.get_pos()
dx=target_x-self_x
dy=target_y-self_y
d=(dx**2+dy**2)**0.5
point-=round(d/30,1)
self_x=target_x
self_y=target_y
elif event.key==K_s:#射击
position_x,position_y=pygame.mouse.get_pos()
shoot_x,shoot_y=self_x,self_y
dx=position_x-shoot_x
dy=position_y-shoot_y
d=(dx**2+dy**2)**0.5
if t<=30:
point-=round(d/150,2)
else:
point-=round(d/250,2)
shoot=True
if go==True:#获取鼠标位置
target_x,target_y=pygame.mouse.get_pos()
screen.fill((255,255,255))
font=pygame.font.SysFont('宋体',30)#文字
text1=font.render('point=%d'%point,True,(0,0,0),(255,255,255))
text2=font.render('time=%d'%round(t,1),True,(0,0,0),(255,255,255))
screen.blit(text1,(30,30))
screen.blit(text2,(30,50))
#玩家的移动
if -500 <point<150:
speed=-0.002*point+0.5
dx=target_x-self_x
dy=target_y-self_y
if dx !=0 or dy!=0:
k=speed/(dx**2+dy**2)**0.5
self_x+=dx*k
self_y+=dy*k
#子弹的移动
if shoot==True:
dx=position_x-shoot_x
dy=position_y-shoot_y
d=(dx**2+dy**2)**0.5
if d<5:
shoot=False
else:
k=10/d
shoot_x+=dx*k
shoot_y+=dy*k
#设置黄球
for i in range(len(yellow_y)):
if yellow_y[i] >830 or yellow_y[i]<-30 or yellow_x[i] >1030 or yellow_x[i]<-30:#碰墙则删除
yellow_y[i]=-100
yellow_x[i]=-100
yellow_pos_y[i]=-100
yellow_pos_x[i]=-100
else:#未碰墙则移动
yellow_y[i]+=yellow_pos_y[i]
yellow_x[i]+=yellow_pos_x[i]
if t>=20:
yellow_pos_x[i]+=random.randint(-1,1)/10
yellow_pos_y[i]+=random.randint(-1,1)/10
while -100 in yellow_y:#删除(避免循环过程中删除导致报错)
yellow_y.remove(-100)
while -100 in yellow_x:
yellow_x.remove(-100)
while -100 in yellow_pos_x:
yellow_pos_x.remove(-100)
while -100 in yellow_pos_y:
yellow_pos_y.remove(-100)
#设置绿球
for i in range(len(green_y)):
if green_y[i] >820 or green_y[i]<-20 or green_x[i] >1020 or green_x[i]<-20:
green_y[i]=-100
green_x[i]=-100
green_pos_y[i]=-100
green_pos_x[i]=-100
else:
green_y[i]+=green_pos_y[i]
green_x[i]+=green_pos_x[i]
if t>=30:
green_position(i)
while -100 in green_y:
green_y.remove(-100)
while -100 in green_x:
green_x.remove(-100)
while -100 in green_pos_x:
green_pos_x.remove(-100)
while -100 in green_pos_y:
green_pos_y.remove(-100)
#设置黑球
for i in range(len(ball_y)):
if ball_y[i] >=825:
ball_y[i]=-1
point-=10
del ball_x[i]
else:
if t<30:
ball_y[i]+=0.5
if t>=30:
ball_y[i]+=0.7
while -1 in ball_y:
ball_y.remove(-1)
#球刷新
time_end=time.time()
if (time_end-time_start>=2 and t<30) or (time_end-time_start>=1 and t>=30):#黑
time_start=time_end
ball_y.append(0)
ball_x.append(random.randint(15,985))
if (time_end-time2>=1 and t<20) or (time_end-time2>=0.7 and t>=20)or (time_end-time2>=0.5 and t>=35):#黄
time2=time_end
if t<50:
new_yellow()
else:
new_yellow2()
if (time_end-time4>=3 and t<30) or (time_end-time4>=2 and 30<=t<45)or (time_end-time4>=1 and t>=45):#绿
time4=time_end
new_green(len(green_y))
#绘制球
for i in range(len(ball_y)):#黑
pygame.draw.circle(screen,(0,0,0),(ball_x[i],ball_y[i]),25,25)
for i in range(len(yellow_y)):#黄
pygame.draw.circle(screen,(255,255,0),(yellow_x[i],yellow_y[i]),30,30)
for i in range(len(green_y)):#绿
pygame.draw.circle(screen,(0,255,0),(green_x[i],green_y[i]),20,20)
pygame.draw.circle(screen,(255,0,0),(self_x,self_y),15,15)
if shoot==True:
pygame.draw.circle(screen,(255,0,0),(shoot_x,shoot_y),5,5)
#检测玩家是否被碰到
for i in range(len(ball_y)):#黑
d=((self_x-ball_x[i])**2+(self_y-ball_y[i])**2)**0.5
if d<=40:
ball_y[i]=-10
ball_x[i]=-10
if t<30:
point+=5
else:
point+=15
while -10 in ball_y:
ball_y.remove(-10)
ball_x.remove(-10)
for i in range(len(yellow_y)):#黄
d=((self_x-yellow_x[i])**2+(self_y-yellow_y[i])**2)**0.5
if d<=45:
yellow_y[i]=-100
yellow_x[i]=-100
yellow_pos_y[i]=-100
yellow_pos_x[i]=-100
point-=15
while -100 in yellow_y:
yellow_y.remove(-100)
while -100 in yellow_x:
yellow_x.remove(-100)
while -100 in yellow_pos_x:
yellow_pos_x.remove(-100)
while -100 in yellow_pos_y:
yellow_pos_y.remove(-100)
for i in range(len(green_y)):#绿
d=((self_x-green_x[i])**2+(self_y-green_y[i])**2)**0.5
if d<=35:
green_y[i]=-100
green_x[i]=-100
green_pos_y[i]=-100
green_pos_x[i]=-100
point-=25
while -100 in green_y:
green_y.remove(-100)
while -100 in green_x:
green_x.remove(-100)
while -100 in green_pos_x:
green_pos_x.remove(-100)
while -100 in green_pos_y:
green_pos_y.remove(-100)
#检测球是否被击中
if shoot==True:
for i in range(len(ball_y)):#黑
d=((shoot_x-ball_x[i])**2+(shoot_y-ball_y[i])**2)**0.5
if d<=30:
ball_y[i]=-10
ball_x[i]=-10
point-=10
while -10 in ball_y:
ball_y.remove(-10)
ball_x.remove(-10)
for i in range(len(yellow_y)):#黄
d=((shoot_x-yellow_x[i])**2+(shoot_y-yellow_y[i])**2)**0.5
if d<=35:
yellow_y[i]=-100
yellow_x[i]=-100
yellow_pos_y[i]=-100
yellow_pos_x[i]=-100
if t<=30:
point+=10
else:
point+=20
while -100 in yellow_y:
yellow_y.remove(-100)
while -100 in yellow_x:
yellow_x.remove(-100)
while -100 in yellow_pos_x:
yellow_pos_x.remove(-100)
while -100 in yellow_pos_y:
yellow_pos_y.remove(-100)
for i in range(len(green_y)):#绿
d=((shoot_x-green_x[i])**2+(shoot_y-green_y[i])**2)**0.5
if d<=25:
green_y[i]=-100
green_x[i]=-100
green_pos_y[i]=-100
green_pos_x[i]=-100
if t<=30:
point+=15
else:
point+=25
while -100 in green_y:
green_y.remove(-100)
while -100 in green_x:
green_x.remove(-100)
while -100 in green_pos_x:
green_pos_x.remove(-100)
while -100 in green_pos_y:
green_pos_y.remove(-100)
#加载
pygame.display.update()
这游戏,到负几千分很容易:
但找到策略后,就越玩越简单。
我的策略:
固定在一个位置,专心打绿球。其他虽然会扣分,但扣得少,不用管。而且黄球很难击中,不动的话它碰到你的概率也比较小。