python小游戏学习笔记4-1(for 循环嵌套,内容列表【】,.append)

1,for循环嵌套; (x,y从坐标中心不断变大)

import pgzrun
n = 50
def draw():
    for y in range(n,600,2*n):50 150 250 350
        for x in range(n,600,2*n):50 150 250 350
            screen.draw.filled_circle((x,y),n,'blue')

pgzrun.go()

2,用for循环嵌套产生很多矩阵排列随机颜色同心圆:

import pgzrun
import random
n = 100
def draw():
    screen.fill('white')
    for y in range(n,600,2*n):
        for x in range(n,600,2*n):
            for r in range (n,0,-10):
                screen.draw.filled_circle((x,y),r,\
                (random.randint(0,255),random.randint(0,255),\
                 random.randint(0,255)))
pgzrun.go()

3,列表【】基本用法

列表内容和序号的区别:

color = ['red','white','blue']
col = color[0]
print(col)
col = color[2]
print(col)

以上关于颜色的列表用于随机颜色就是:

import pgzrun
WIDTH = 800
HEIGHT = 400

colors = ['red','orange','yellow','green','blue','purple','white','cyan'] #有8个随机颜色
def draw():

    screen.fill('white')
    for r in range (8):
        screen.draw.filled_circle((400,400),400-r*30,colors[r]) #r是随机8个颜色之一,r的数值只能取‘0-7’

pgzrun.go()
xlist = [] #空列表 范围在从0开始 0,1,2,3,4,5----中取
for i in range (1,6):   # 1  2  3 4 5 
    xlist.append(i) # 给列表增加内容 1,2,3,4,5
print(xlist)
xlist = [1,2,3,4,5]
for i in range(1,5):  #i = 1,2,3,4
    xlist[i] = 2*xlist[i] #1属于序号0,所以1不动,其他数值内容变化

print(xlist)

x数值的单列写法:

xlist = [1,2,3,4,5]
for x in xlist:
    print(x)

关于len(长度)的用法:(结果为:5)

xlist = [2,6,3,4,8]
print(len(xlist))

你可能感兴趣的:(python小游戏学习笔记4-1(for 循环嵌套,内容列表【】,.append))