python小游戲?qū)W習(xí)筆記4-4(很多個彩色小球/變色同心圓同時隨意彈跳)

import pgzrun  #導(dǎo)入游戲庫
import random
WIDTH = 800    #設(shè)置窗口寬度
HEIGHT = 600   #設(shè)置窗口高度

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():
    # 設(shè)置小球自動彈跳
    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)入游戲庫
import random
WIDTH = 800    #設(shè)置窗口寬度
HEIGHT = 600   #設(shè)置窗口高度

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():
    # 設(shè)置小球自動彈跳

    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)點按的同心圓:

import pgzrun  #導(dǎo)入游戲庫
import random
WIDTH = 800    #設(shè)置窗口寬度
HEIGHT = 600   #設(shè)置窗口高度

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):#當(dāng)鼠標(biāo)移動時
    if mouse.LEFT in buttons:      #當(dāng)鼠標(biāo)左鍵按下時
        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) #小球三個顏色分量
        colorG = random.randint(10,255)
        colorB = random.randint(10,255)
        ball = [x,y,r,colorG,colorR,colorB]
        balls.append(ball)


pgzrun.go()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容