pygame与列表元素结合实现有趣的堆栈效果,进来看看咯。

本文通过Python的pygame库展示了如何结合列表创建一个动态的堆栈效果,当鼠标点击时,堆栈中的矩形框会依次移除并补充新的元素,同时实现矩形框的移动。详细步骤包括初始化pygame环境、绘制矩形框、存储和展示矩形框列表、处理鼠标事件以及使用zip函数优化代码。最终实现了一个交互式的堆栈视觉效果。
摘要由CSDN通过智能技术生成

pygame与列表元素结合实现有趣的堆栈效果,进来看看咯。

一、先把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对象存储起来

(一)初始化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

(二)展示list元素

for myrect in rect_list:
   pygame.draw.rect(screen,myrect[0],myrect[1])
   pygame.draw.rect(screen,myrect[0],myrect[1],myrect[2])

在这里插入图片描述

发现红色的第一个框显示在最下面了,因此我们需要倒序处理以下list对象。

(三)实现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)

(七)实现move函数

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()


七、使用zip函数优化

(一)完整代码

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()

(二)运行效果

在这里插入图片描述

好吧,写完,不一样的体验,不一样的效果,大家各自感受一下吧

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值