python 升级_processing-python-泡泡龙(升级真实版)

def setup():
    size(600,600)
    global x, y, vx, vy, theta, vtheta, bx, by, curBall, aliveBall
    global COLOR, c, bc
    global R, L
    R = 20
    L = 3
    theta = -PI/2
    vtheta = 0.01
    #球需要有速度,位置,颜色三个属性
    #对应x,y坐标、x,y速度、c
    
    #上方球由于固定,可以只有位置和颜色属性
    #对应bx,by和bc

    x, y = width/2, height
    #初始化球在中下位置,速度为0,颜色随机给一个
    vx, vy = 0, 0
    bx, by = [], []
    curBall = []
    aliveBall = []
    COLOR = [color(227,41,54),color(41,188,227),color(41,227,48),color(250,239,13)]
    #COLOR颜色列表,c和bc表示列表中的第几个颜色,而不是直接表示颜色
    c = int(random(len(COLOR)))
    bc = []
    for i in range(int(width/R/2)):
        for j in range(-int(height/R/2),int(height/R/2/5)):
            bx.append(i*2*R)
            by.append( j*2*R)
            curBall.append(len(curBall))#显示的球
            aliveBall.append(len(aliveBall))#活着的球
            bc.append(int(random(len(COLOR))))#死了的球
            
def draw():
    global x, y, vx, vy, theta, vtheta, bx, by, curBall, aliveBall
    global COLOR, R, L, c, bc
    
    background(255)
    def findDead(i):
        d = [i]#打中了第i号球,d[]记录接下来找到的应该死掉的球
        def tmp(i):
            for j in curBall:
            #找和i相邻且同色的球,
            #首先排除掉已经找到的球,然后需要颜色编号相同,其次需要距离小于两球半径之和
                if j not in d and bc[j]==bc[i] and dist(bx[i],by[i],bx[j],by[j])<2.1*R:
                    d.append(j)#确认过眼神,找到对的球j,用小本本记下来
                    #接下来再找刚刚找到的球的下一个应该死掉的球
                    tmp(j)
        tmp(i)
        #这样一直找下一个该死的泡泡龙
        #就得到了所有该死的球 (逃
        return d
    
    noFill()
    ellipse(width/2,height,4*R,4*R)
    strokeWeight(2*R)
    line(width/2,height,width/2+20*L*cos(theta),height+20*L*sin(theta))
    strokeWeight(1)
    #画会动的球
    fill(COLOR[c])
    ellipse(x,y,2*R,2*R)
    
    for i in curBall:
      #画每个还没死的球
        fill(COLOR[bc[i]])
        ellipse(bx[i], by[i], 2*R, 2*R)
        #检查有没有被撞到
        if dist(bx[i], by[i], x, y)<2*R:
            if bc[i] == c:
              #某个同色球被撞到
              #找它旁边该死的球,以及旁边该死的球的旁边的该死的球,以及*******
                tmp = findDead(i)
                #找到了这一次所有该死的球
                #把他们从生死簿上重新做标记
                #地狱+1
                #人间-1
                for t in tmp:
                    aliveBall.remove(t)
            #不管有没有撞到该死的球,都应该飞回原点 (逃
            else:
                nearx = [bx[i]-2*R,bx[i],bx[i],bx[i]+2*R]#附近的空位
                neary = [by[i],by[i]+2*R,by[i]-2*R,by[i]]
                for n in range(4):
                    if dist(nearx[n],neary[n],x,y)<2*R:
                        bx.append(nearx[n])#接近就粘贴
                        by.append(neary[n])#实际上是添加新球
                        bc.append(c)
                        aliveBall.append(len(bx)-1)
                        break
                
            
            x, y  = width/2, height
            vx, vy = 0, 0
            #顺便换个马甲再来
            c = int(random(len(COLOR)))
    curBall = aliveBall[:]
    if frameCount%200==0:
        for i in curBall:
            by[i] += 1
    if keyPressed:
        if keyCode == LEFT:
            theta -= vtheta
        elif keyCode == RIGHT:
            theta += vtheta
        if key == 's' or key == 'S':
            vx = L*cos(theta)
            vy = L*sin(theta)
    #更新一下,现在显示的球全是没死的球
    x += vx
    y += vy
    #左右碰壁就反弹
    if x>width-R or x<R:
      vx = -vx
    # 上面碰壁也反弹
    if y<R:
      vy = -vy
    #下面碰壁就还原
    if y>height+R:
        x, y = width/2, height
        vx, vy = 0, 0
        #换个马甲
        c = int(random(len(COLOR)))
        
    
def mousePressed():
    global vx, vy, theta, L
    vx = L*cos(theta)
    vy = L*sin(theta)
    
def mouseMoved():
    global theta
    px = mouseX-width/2.0
    if px==0:
        px=0.00001
    py = mouseY-height
    theta = -PI+atan(py/px) if px<0 else atan(py/px)

泡泡龙这次真的over


欢迎关注我的公众号:processing与python与arduino(p5-py3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值