单调小球
前一篇文章中,我们写了一个小球的碰壁检测反弹的程序
import pgzrun
import random
HEIGHT=400
WIDTH=600
x1=WIDTH/2
y1=HEIGHT/2
speed_x1=3
speed_y1=3
r=30
d=60
colors=['red','blue','green','pink','purple','yellow']
c1=random.choice(colors)
def draw():
screen.fill('white')
screen.draw.filled_circle((x1,y1),r,c1)
def update(): #定义update函数,更新模块
global x1,y1,speed_x1
x1=x1+speed_x1
x2=x2+speed_x2
if x1>=WIDTH-r or x1<=r
speed_x1=-speed_x1
if y1>=HEIGHT-r or y1<=r
speed_y1=-speed_y1
pgzrun.go()
这样你就可以得到一个随机颜色的小球,当小球撞到四壁时就会自动反弹
但是这样比较单调
- 颜色的选择太少
- 小球的个数太少
- 没什么操做,弹来弹去很无聊
所以咱们对代码进行一点点修改,变成究极无敌闪烁小球与碰壁反弹程序
由于gif的制作的次数用完了,做不出来,所以大家脑补一下,小球动来动去,颜色闪烁变换的效果(doge)
开始施法!!!
升级小球
一、首先想一下,当我们点击鼠标的时候就随机出现一个大小随机的彩色小球,如果按照之前的写法,我们只能指定小球的个数,不够;那什么结构能够不限制长度呢?没错,那就是列表!
balls=[]
二、生成随机小球。定义一个小球的列表,那么列表里面的元素是什么呢?没错就是小球的属性!根据circle的参数列表,我能要提供小球的圆心坐标,半径,颜色,要出现随机颜色的小球,我们采用RGB的颜色
for i in range(5):
x=random.randint(100, WIDTH-100)
y=random.randint(100,HEIGHT-100)
speedx=random.randint(1, 5)
speedy=random.randint(1, 5)
r=random.randint(5, 10)
color_r=random.randint(10, 255)
color_g=random.randint(10, 255)
color_b=random.randint(10, 255)
ball=[x,y,speedx,speedy,r,color_r,color_g,color_b]
balls.append(ball)
但以我是这个圆的颜色只有一个,但是究极版的小球是彩色的小球,所们画几个不同颜色,不同半径的同心圆,通过覆盖的方法变成彩色的小球
for i in range(5):
x=random.randint(100, WIDTH-100)
y=random.randint(100,HEIGHT-100)
speedx=random.randint(1, 5)
speedy=random.randint(1, 5)
r=random.randint(5, 10)
color_r=random.randint(10, 255)
color_g=random.randint(10, 255)
color_b=random.randint(10, 255)
color_r2=random.randint(10, 255)
color_g2=random.randint(10, 255)
color_b2=random.randint(10, 255)
color_r3=random.randint(10, 255)
color_g3=random.randint(10, 255)
color_b3=random.randint(10, 255)
r2=random.randint(10, 20)
r3=random.randint(20, 25)
ball=[x,y,speedx,speedy,r,color_r,color_g,color_b,r2,r3,color_r2,color_g2,color_b2,color_r3,color_g3,color_b3]
balls.append(ball)
三、draw的画图函数
def draw():
screen.fill('white')
for ball in balls:
screen.draw.filled_circle((ball[0],ball[1]),ball[9],(ball[13],ball[14],ball[15]))
screen.draw.filled_circle((ball[0],ball[1]),ball[8],(ball[10],ball[11],ball[12]))
screen.draw.filled_circle((ball[0],ball[1]),ball[4],(ball[5],ball[6],ball[7]))
但是,这样画出来的圆虽然颜色是随机的,但是这不符合究极无敌小球,他不会一直变色,所以我们改一点点
def draw():
screen.fill('white')
for ball in balls:
screen.draw.filled_circle((ball[0],ball[1]),ball[9],(random.randint(10, 255),random.randint(10, 255),random.randint(10, 255)))
screen.draw.filled_circle((ball[0],ball[1]),ball[8],(random.randint(10, 255),random.randint(10, 255),random.randint(10, 255)))
screen.draw.filled_circle((ball[0],ball[1]),ball[4],(random.randint(10, 255),random.randint(10, 255),random.randint(10, 255)))
四、update更新函数,碰撞检测
def update():
for ball in balls:
ball[0]+=ball[2]
ball[1]+=ball[3]
if ball[0]>=WIDTH-ball[4] or ball[0]<=ball[4]:
ball[2]=-ball[2]
if ball[1]>=HEIGHT-ball[4] or ball[1]<=ball[4]:
ball[3]=-ball[3]
五、鼠标点击生成小球的代码
def on_mouse_down(pos):
x=pos[0]
y=pos[1]
speedx=random.randint(1, 5)
speedy=random.randint(1, 5)
r=random.randint(5, 10)
color_r=random.randint(10, 255)
color_g=random.randint(10, 255)
color_b=random.randint(10, 255)
color_r2=random.randint(10, 255)
color_g2=random.randint(10, 255)
color_b2=random.randint(10, 255)
color_r3=random.randint(10, 255)
color_g3=random.randint(10, 255)
color_b3=random.randint(10, 255)
r2=random.randint(10, 20)
r3=random.randint(20, 25)
ball=[x,y,speedx,speedy,r,color_r,color_g,color_b,r2,r3,color_r2,color_g2,color_b2,color_r3,color_g3,color_b3]
balls.append(ball)
完整代码
import pgzrun
import random
HEIGHT=600
WIDTH=800
balls=[]
for i in range(5):
x=random.randint(100, WIDTH-100)
y=random.randint(100,HEIGHT-100)
speedx=random.randint(1, 5)
speedy=random.randint(1, 5)
r=random.randint(5, 10)
color_r=random.randint(10, 255)
color_g=random.randint(10, 255)
color_b=random.randint(10, 255)
color_r2=random.randint(10, 255)
color_g2=random.randint(10, 255)
color_b2=random.randint(10, 255)
color_r3=random.randint(10, 255)
color_g3=random.randint(10, 255)
color_b3=random.randint(10, 255)
r2=random.randint(10, 20)
r3=random.randint(20, 25)
ball=[x,y,speedx,speedy,r,color_r,color_g,color_b,r2,r3,color_r2,color_g2,color_b2,color_r3,color_g3,color_b3]
balls.append(ball)
def draw():
screen.fill('white')
for ball in balls:
screen.draw.filled_circle((ball[0],ball[1]),ball[9],(random.randint(10, 255),random.randint(10, 255),random.randint(10, 255)))
screen.draw.filled_circle((ball[0],ball[1]),ball[8],(random.randint(10, 255),random.randint(10, 255),random.randint(10, 255)))
screen.draw.filled_circle((ball[0],ball[1]),ball[4],(random.randint(10, 255),random.randint(10, 255),random.randint(10, 255)))
def update():
for ball in balls:
ball[0]+=ball[2]
ball[1]+=ball[3]
if ball[0]>=WIDTH-ball[4] or ball[0]<=ball[4]:
ball[2]=-ball[2]
if ball[1]>=HEIGHT-ball[4] or ball[1]<=ball[4]:
ball[3]=-ball[3]
def on_mouse_down(pos):
x=pos[0]
y=pos[1]
speedx=random.randint(1, 5)
speedy=random.randint(1, 5)
r=random.randint(5, 10)
color_r=random.randint(10, 255)
color_g=random.randint(10, 255)
color_b=random.randint(10, 255)
color_r2=random.randint(10, 255)
color_g2=random.randint(10, 255)
color_b2=random.randint(10, 255)
color_r3=random.randint(10, 255)
color_g3=random.randint(10, 255)
color_b3=random.randint(10, 255)
r2=random.randint(10, 20)
r3=random.randint(20, 25)
ball=[x,y,speedx,speedy,r,color_r,color_g,color_b,r2,r3,color_r2,color_g2,color_b2,color_r3,color_g3,color_b3]
balls.append(ball)
pgzrun.go()
但是,这个代码没有碰撞检测的功能,小球相互碰撞后弹开的效果没有写,想加上的可以参考