import pgzrun #導(dǎo)入游戲庫(kù)
import random
WIDTH = 800 #設(shè)置窗口寬度
HEIGHT = 600 #設(shè)置窗口高度
balls = [] #類似建立一個(gè)空文檔
for i in range(25):
x = random.randint(100,WIDTH-100)
y = random.randint(100,HEIGHT-100)
speedx = random.randint(1,4)
speedy = random.randint(1, 4)
r = random.randint(5,50)
colorR = random.randint(10,255)
colorG = random.randint(10, 255)
colorB = random.randint(10, 255)
ball = [x,y,speedx,speedy,r,colorB,colorR,colorG]
balls.append(ball)
def draw(): #這段是整個(gè)框架,之后兩段是在里面填充
screen.fill('white')
for ball in balls: #繪制所有的圓
screen.draw.filled_circle((ball[0],ball[1]),ball[4],(ball[5],ball[6],ball[7]))
def update():
# 設(shè)置小球自動(dòng)彈跳
for ball in balls:
ball[0] = ball[0] + ball[2]
ball[1] = ball[1] + ball[3]
if ball[0] > WIDTH - r or ball[0] <r:
ball[2] = - ball[2]
if ball[1] > HEIGHT - r or ball[1] < r:
ball[3] = - ball[3]
pgzrun.go()
x的變式?jīng)Q定了同心圓(關(guān)鍵在r = ball[4]-x):
import pgzrun #導(dǎo)入游戲庫(kù)
import random
WIDTH = 800 #設(shè)置窗口寬度
HEIGHT = 600 #設(shè)置窗口高度
balls = [] #類似建立一個(gè)空文檔
for i in range(25):
x = random.randint(100, WIDTH - 100)
y = random.randint(100, HEIGHT - 100)
speedx = random.randint(1, 4)
speedy = random.randint(1, 4)
r = random.randint(5, 50)
colorR = random.randint(10, 255)
colorG = random.randint(10, 255)
colorB = random.randint(10, 255)
ball = [x, y, speedx, speedy, r, colorB, colorR, colorG]
balls.append(ball)
def draw(): #這段是整個(gè)框架初澎,之后兩段是在里面填充
screen.fill('white')
for ball in balls: #繪制所有的圓
#screen.draw.filled_circle((ball[0],ball[1]),ball[4],(ball[5],ball[6],ball[7]))
for x in range(1,ball[4],3):#用同心圓填充
screen.draw.filled_circle((ball[0],ball[1]),ball[4]-x,(random.randint(
ball[5],255),random.randint(ball[6],255),random.randint(ball[7],255)))
def update():
# 設(shè)置小球自動(dòng)彈跳
for ball in balls:
ball[0] = ball[0] + ball[2]
ball[1] = ball[1] + ball[3]
if ball[0] > WIDTH - r or ball[0] <r:
ball[2] = - ball[2]
if ball[1] > HEIGHT - r or ball[1] < r:
ball[3] = - ball[3]
pgzrun.go()
鼠標(biāo)點(diǎn)按的同心圓:
import pgzrun #導(dǎo)入游戲庫(kù)
import random
WIDTH = 800 #設(shè)置窗口寬度
HEIGHT = 600 #設(shè)置窗口高度
balls = [] #類似建立一個(gè)空文檔
def draw(): #這段是整個(gè)框架,之后兩段是在里面填充
screen.fill('white')
for ball in balls: #繪制所有的圓
screen.draw.filled_circle((ball[0],ball[1]),ball[2],(ball[3],ball[4],ball[5]))
for x in range(1,ball[2],3):#用同心圓填充
screen.draw.filled_circle((ball[0],ball[1]),ball[2]-x,(random.randint(
ball[3],255),random.randint(ball[4],255),random.randint(ball[5],255)))
def on_mouse_move(pos,rel,buttons):#當(dāng)鼠標(biāo)移動(dòng)時(shí)
if mouse.LEFT in buttons: #當(dāng)鼠標(biāo)左鍵按下時(shí)
x = pos[0] #鼠標(biāo)的x坐標(biāo)碑宴,設(shè)為小球的x坐標(biāo)
y = pos[1] #鼠標(biāo)的y坐標(biāo),設(shè)為小球的y坐標(biāo)
r = random.randint(10,30) #小球半徑
colorR = random.randint(10,255) #小球三個(gè)顏色分量
colorG = random.randint(10,255)
colorB = random.randint(10,255)
ball = [x,y,r,colorG,colorR,colorB]
balls.append(ball)
pgzrun.go()