首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在随机坐标系中使用对撞机

在随机坐标系中使用对撞机
EN

Stack Overflow用户
提问于 2021-05-22 15:29:29
回答 1查看 113关注 0票数 2

现在,我的游戏blits所有的图像在随机位置正确,也得到了正确的图像,但我不知道如何使用碰撞,以确保图像不重叠。它怎么能对我的代码起作用?

同时,我也在努力让第一条文字逐渐淡出,我不知道为什么它对我不起作用。

以下是代码:

代码语言:javascript
复制
class GAME1:
    def __init__(self, next_scene):
        self.background = pygame.Surface(size)
        
        # Create an array of images with their rect
        self.images = []
        self.rects = []
        self.imagenes1_array = ['autobus.png','coche.png','barco.png','autobus2.png','grua.png','bici.png']
        for i in self.imagenes1_array:
            # We divide in variables so we can then get the rect of the whole Img (i2)
            i2 = pygame.image.load(i)
            self.images.append(i2)
            s = pygame.Surface(i2.get_size())
            r = s.get_rect()
            
            # Trying to use colliderect so it doesnt overlap
            if pygame.Rect.colliderect(r,r) == True:
                x = random.randint(300,1000)
                y = random.randint(200,700)
            
                self.rects.append(r)
        

    def start(self, gamestate):
        self.gamestate = gamestate

        for rect in self.rects:
            # Give random coordinates (we limit the dimensions (x,y))
            x = random.randint(300,1000)
            y = random.randint(200,700)
            rect.x = x
            rect.y = y

    def draw(self,screen):
        self.background = pygame.Surface(size)
        font = pygame.font.SysFont("comicsansms",70)
        
        # First half (Show image to remember)
        text1 = font.render('¡A recordar!',True, PURPLE)
        text1_1 = text1.copy()
        # This surface is used to adjust the alpha of the txt_surf.
        alpha_surf = pygame.Surface(text1_1.get_size(), pygame.SRCALPHA)
        alpha = 255 # The current alpha value of the surface.

        if alpha > 0:
            alpha = max(alpha-4, 0)
            text1_1 = text1.copy()
            alpha_surf.fill((255, 255, 255, alpha))
            text1_1.blit(alpha_surf, (0,0), special_flags = pygame.BLEND_RGBA_MULT)
        
        screen.blit(text1_1, (600,50))
       

        # Second half (Show all similar images)
        text2 = font.render('¿Cuál era el dibujo?',True, PURPLE)
        #screen.blit(text2, (500,50))
        
        for i in range(len(self.images)):
            #colliding = pygame.Rect.collidelistall(self.rects)
            screen.blit(self.images[i], (self.rects[i].x, self.rects[i].y))
    
    def update(self, events, dt):
        for event in events:
            if event.type == pygame.MOUSEBUTTONDOWN:
                for rect in self.rects:
                    if rect.collidepoint(event.pos):
                        print('works!')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-22 15:37:57

使用collidelist()测试列表中的一个矩形是否相交:

代码语言:javascript
复制
for i in self.imagenes1_array:
    s = pygame.image.load(i)
    self.images.append(s)
    r = s.get_rect()
    
    position_set = False 
    while not position_set:
        r.x = random.randint(300,1000)
        r.y = random.randint(200,700)    
        
        margin = 10
        rl = [rect.inflate(margin*2, margin*2) for rect in self.rects]
        if len(self.rects) == 0 or r.collidelist(rl) < 0:
            self.rects.append(r)
            position_set = True

请参阅最小示例,该示例使用该算法生成随机而不是重叠的矩形:

代码语言:javascript
复制
import pygame
import random

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

def new_recs(rects):
    rects.clear()
    for _ in range(10):
        r = pygame.Rect(0, 0, random.randint(30, 40), random.randint(30, 50))
        
        position_set = False 
        while not position_set:
            r.x = random.randint(10, 340)
            r.y = random.randint(10, 340)    
            
            margin = 10
            rl = [rect.inflate(margin*2, margin*2) for rect in rects]
            if len(rects) == 0 or r.collidelist(rl) < 0:
                rects.append(r)
                position_set = True
rects = []
new_recs(rects)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            new_recs(rects)          

    window.fill(0)
    for r in rects:
        pygame.draw.rect(window, (255, 0, 0), r)
    pygame.display.flip()

pygame.quit()
exit()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67651237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档