pygame与列表元素结合实现有趣的堆栈效果,进来看看咯。
import pygame,sys
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption('pygame和列表元素有趣的碰撞V1.0')
clock=pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0,0,0))
clock.tick(300)
pygame.display.update()
pygame.draw.rect(screen,(255,0,0),(200,200,300,100),2)
pygame.draw.rect(screen,(255,0,0),(200,200,300,100))
import pygame,sys
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption('pygame和列表元素有趣的碰撞V1.0')
clock=pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0,0,0))
pygame.draw.rect(screen,(0,255,0),(200,200,300,100))
pygame.draw.rect(screen,(255,0,0),(200,200,300,100),2)
clock.tick(300)
pygame.display.update()
用list对象存储起来
rect_list =[((255,0,0),(95,95,300,100),2)]
i = 0
j = 5
while i < 10:
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
a_rect = (100+i*j,100+i*j,300,100)
width = random.randint(1,3)
rect_list.append((color,a_rect,width))
i += 1
for myrect in rect_list:
pygame.draw.rect(screen,myrect[0],myrect[1])
pygame.draw.rect(screen,myrect[0],myrect[1],myrect[2])
发现红色的第一个框显示在最下面了,因此我们需要倒序处理以下list对象。
for myrect in rect_list[::-1]:
pygame.draw.rect(screen,myrect[0],myrect[1])
pygame.draw.rect(screen,myrect[0],myrect[1],myrect[2])
OK,红色的显示在最前面了。
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
rect_list.pop(0)
import pygame,sys
import random
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption('pygame和列表元素有趣的碰撞V1.0')
rect_list =[((255,0,0),(95,95,300,100),2)]
i = 0
j = 5
while i < 10:
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
a_rect = (100+i*j,100+i*j,300,100)
width = random.randint(1,3)
rect_list.append((color,a_rect,width))
i += 1
clock=pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
rect_list.pop(0)
screen.fill((0,0,0))
for myrect in rect_list[::-1]:
pygame.draw.rect(screen,myrect[0],myrect[1])
pygame.draw.rect(screen,myrect[0],myrect[1],myrect[2])
clock.tick(300)
pygame.display.update()
发现list随着鼠标的点击,越来越少元素了,最后没有了。
if event.type == pygame.MOUSEBUTTONDOWN:
rect_list.pop(0)
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
a_rect = (100+i*j,100+i*j,300,100)
width = random.randint(1,3)
rect_list.append((color,a_rect,width))
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if cur_rect is None:
cur_rect = rect_list[0]
rect_list.pop(0)
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
a_rect = (100+i*j,100+i*j,300,100)
width = random.randint(1,3)
rect_list.append((color,a_rect,width))
flag = random.randint(0,3)
if cur_rect is not None:
move(screen,flag)
step = 0
def move(screen,i):
global step,cur_rect
if i == 0:
newrect = (cur_rect[1][0] - step,cur_rect[1][1],cur_rect[1][2],cur_rect[1][3])
elif i == 1:
newrect = (cur_rect[1][0] + step,cur_rect[1][1],cur_rect[1][2],cur_rect[1][3])
# cur_rect[1][0] += step
elif i == 2:
newrect = (cur_rect[1][0] ,cur_rect[1][1]+ step,cur_rect[1][2],cur_rect[1][3])
# cur_rect[1][1] += step
elif i == 3:
newrect = (cur_rect[1][0],cur_rect[1][1]- step,cur_rect[1][2],cur_rect[1][3])
# cur_rect[1][1] += step
pygame.draw.rect(screen, cur_rect[0], newrect)
pygame.draw.rect(screen, cur_rect[0], newrect, cur_rect[2])
step += 5
if newrect[0]<0 or newrect[0]>600 or newrect[1] <0 or newrect[1] > 500:
step = 0
cur_rect = None
如果出界,置为None
import pygame,sys
import random
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption('pygame和列表元素有趣的碰撞V1.0')
rect_list =[((255,0,0),(95,95,300,100),2)]
i = 0
j = 5
while i < 10:
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
a_rect = (100+i*j,100+i*j,300,100)
width = random.randint(1,3)
rect_list.append((color,a_rect,width))
i += 1
step = 0
def move(screen,i):
global step,cur_rect
if i == 0:
newrect = (cur_rect[1][0] - step,cur_rect[1][1],cur_rect[1][2],cur_rect[1][3])
elif i == 1:
newrect = (cur_rect[1][0] + step,cur_rect[1][1],cur_rect[1][2],cur_rect[1][3])
# cur_rect[1][0] += step
elif i == 2:
newrect = (cur_rect[1][0] ,cur_rect[1][1]+ step,cur_rect[1][2],cur_rect[1][3])
# cur_rect[1][1] += step
elif i == 3:
newrect = (cur_rect[1][0],cur_rect[1][1]- step,cur_rect[1][2],cur_rect[1][3])
# cur_rect[1][1] += step
pygame.draw.rect(screen, cur_rect[0], newrect)
pygame.draw.rect(screen, cur_rect[0], newrect, cur_rect[2])
step += 5
if newrect[0]<0 or newrect[0]>600 or newrect[1] <0 or newrect[1] > 500:
step = 0
cur_rect = None
cur_rect = None
clock=pygame.time.Clock()
flag = 0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if cur_rect is None:
cur_rect = rect_list[0]
rect_list.pop(0)
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
a_rect = (100+i*j,100+i*j,300,100)
width = random.randint(1,3)
rect_list.append((color,a_rect,width))
flag = random.randint(0,3)
screen.fill((0,0,0))
for myrect in rect_list[::-1]:
pygame.draw.rect(screen,myrect[0],myrect[1])
pygame.draw.rect(screen,myrect[0],myrect[1],myrect[2])
if cur_rect is not None:
move(screen,flag)
clock.tick(300)
pygame.display.update()
import pygame,sys
import random
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption('pygame和列表元素有趣的碰撞V1.0')
rect_list =[]
color_list = []
i = 0
j = 5
while i < 10:
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
a_rect = (100+i*j,100+i*j,300,100)
width = random.randint(1,3)
color_list.append(color)
rect_list.append(a_rect)
i += 1
step = 0
def move(screen,i):
global step,cur_rect
if i == 0:
newrect = (cur_rect[1][0] - step,cur_rect[1][1],cur_rect[1][2],cur_rect[1][3])
elif i == 1:
newrect = (cur_rect[1][0] + step,cur_rect[1][1],cur_rect[1][2],cur_rect[1][3])
elif i == 2:
newrect = (cur_rect[1][0] ,cur_rect[1][1]+ step,cur_rect[1][2],cur_rect[1][3])
elif i == 3:
newrect = (cur_rect[1][0],cur_rect[1][1]- step,cur_rect[1][2],cur_rect[1][3])
pygame.draw.rect(screen, cur_rect[0], newrect)
pygame.draw.rect(screen, cur_rect[0], newrect, cur_rect[2])
step += 5
if newrect[0]<0 or newrect[0]>600 or newrect[1] <0 or newrect[1] > 500:
step = 0
cur_rect = None
cur_rect = None
clock=pygame.time.Clock()
flag = 0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if cur_rect is None:
cur_rect = (color_list[0],rect_list[0],2)
color_list.pop(0)
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
color_list.append(color)
flag = random.randint(0,3)
screen.fill((0,0,0))
new_list = zip(color_list[::-1],rect_list[::-1])
for myrect in new_list:
pygame.draw.rect(screen,myrect[0],myrect[1])
pygame.draw.rect(screen,myrect[0],myrect[1],2)
if cur_rect is not None:
move(screen,flag)
clock.tick(300)
pygame.display.update()
好吧,写完,不一样的体验,不一样的效果,大家各自感受一下吧