python小游戏学习笔记4-4(很多个彩色小球/变色同心圆同时随意弹跳)

import pgzrun  #导入游戏库
import random
WIDTH = 800    #设置窗口宽度
HEIGHT = 600   #设置窗口高度

balls = []  #类似建立一个空文档

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(): #这段是整个框架,之后两段是在里面填充
    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():
    # 设置小球自动弹跳
    for ball in balls:
        ball[0] = ball[0] + ball[2]
        ball[1] = ball[1] + ball[3]
        if ball[0] > WIDTH - r or ball[0]  HEIGHT - r or ball[1] < r:
            ball[3] = - ball[3]

pgzrun.go()

x的变式决定了同心圆(关键在r = ball[4]-x):

import pgzrun  #导入游戏库
import random
WIDTH = 800    #设置窗口宽度
HEIGHT = 600   #设置窗口高度

balls = []  #类似建立一个空文档

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(): #这段是整个框架,之后两段是在里面填充
    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():
    # 设置小球自动弹跳

    for ball in balls:
        ball[0] = ball[0] + ball[2]
        ball[1] = ball[1] + ball[3]
        if ball[0] > WIDTH - r or ball[0]  HEIGHT - r or ball[1] < r:
            ball[3] = - ball[3]

pgzrun.go()

鼠标点按的同心圆:

import pgzrun  #导入游戏库
import random
WIDTH = 800    #设置窗口宽度
HEIGHT = 600   #设置窗口高度

balls = []  #类似建立一个空文档


def draw(): #这段是整个框架,之后两段是在里面填充
    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):#当鼠标移动时
    if mouse.LEFT in buttons:      #当鼠标左键按下时
        x = pos[0]      #鼠标的x坐标,设为小球的x坐标
        y = pos[1]      #鼠标的y坐标,设为小球的y坐标
        r = random.randint(10,30)       #小球半径
        colorR = random.randint(10,255) #小球三个颜色分量
        colorG = random.randint(10,255)
        colorB = random.randint(10,255)
        ball = [x,y,r,colorG,colorR,colorB]
        balls.append(ball)


pgzrun.go()

你可能感兴趣的:(python小游戏学习笔记4-4(很多个彩色小球/变色同心圆同时随意弹跳))