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

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与列表元素结合实现有趣的堆栈效果,进来看看咯。

二、来个矩形框吧

pygame.draw.rect(screen,(255,0,0),(200,200,300,100),2)

pygame与列表元素结合实现有趣的堆栈效果,进来看看咯。
    pygame.draw.rect(screen,(255,0,0),(200,200,300,100))

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

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

三、来一箩筐的矩形框吧

用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])

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

发现红色的第一个框显示在最下面了,因此我们需要倒序处理以下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])

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

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随着鼠标的点击,越来越少元素了,最后没有了。

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

; (四)在删除第一个元素的同时,增加新元素,进行补给

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

    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

如果出界,置为None

五、实现效果

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

; 六、完整代码

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

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

(二)运行效果

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

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

Original: https://blog.csdn.net/dhjabc_1/article/details/117191146
Author: dhjabc_1
Title: pygame与列表元素结合实现有趣的堆栈效果,进来看看咯。

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783878/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球