如何确定对象在Colliderect中相互穿过的原因

2024-06-05 23:01:38 发布

您现在位置:Python中文网/ 问答频道 /正文

由于某种原因,碰撞器无法工作,雨水穿过人行道。这真的很烦人,因为所有这些未使用的精灵都会产生大量的延迟

import pygame
import random

class Square(pygame.sprite.Sprite):
    def __init__(self, x, y, size1, size2, speedx, speedy, colour):
        super().__init__()
        self.image = pygame.Surface([size1, size2])
        self.image.fill(colour)

        self.speedx = speedx
        self.speedy = speedy

        self.rect=self.image.get_rect()
        self.rect.x=x
        self.rect.y=y
    def update(self):
        square_colour = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
        self.rect.x = self.rect.x + self.speedx
        self.rect.y = self.rect.y + self.speedy


my_square = Square(0, 705, 20, 30, 1, 0, (0, 0, 0))
pavement = Square(0, 735, 750, 15, 0 , 0, (100, 100, 100))

allspriteslist = pygame.sprite.Group()
allspriteslist.add(my_square)
allspriteslist.add(pavement)


pygame.init()
screen = pygame.display.set_mode([750,750])
pygame.display.set_caption('Snake Example')
clock = pygame.time.Clock()

background_colour = (150, 150, 150)
done = False
while not done:
    r = Square(random.randint(0, 747), 0, 3, 7, 0, 5, (137, 200, 230))
    allspriteslist.add(r)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
              done = True

    if my_square.rect.x > 750:
        my_square.rect.x = - 10
    if my_square.rect.x < - 50:
        my_square.rect.x = 800
    if r.rect.colliderect(pavement.rect): 
        allspriteslist.remove(r)

    screen.fill(background_colour)
    allspriteslist.draw(screen)
    allspriteslist.update()    
    pygame.display.flip()

Tags: rectselfeventifinitmyrandompygame
1条回答
网友
1楼 · 发布于 2024-06-05 23:01:38

你必须检测所有雨滴与人行道的碰撞

为雨滴添加一个组:

rain = pygame.sprite.Group()

将每个雨滴添加到组中:

done = False
while not done:
    r = Square(random.randint(0, 747), 0, 3, 7, 0, 5, (137, 200, 230))
    allspriteslist.add(r)
    rain.add(r)

如果落在地上,通过pygame.sprite.Sprite.kill()将其移除:

done = False
while not done:
    # [...]

    for r in rain: 
        if r.rect.colliderect(pavement.rect): 
            r.kill()

通过使用^{}并将True传递给参数dokill,可以简化删除雨滴的过程:

done = False
while not done:
    # [...]

    pygame.sprite.spritecollide(pavement, rain, True)

完整的示例代码:

import pygame
import random

class Square(pygame.sprite.Sprite):
    def __init__(self, x, y, size1, size2, speedx, speedy, colour):
        super().__init__()
        self.image = pygame.Surface([size1, size2])
        self.image.fill(colour)

        self.speedx = speedx
        self.speedy = speedy

        self.rect=self.image.get_rect()
        self.rect.x=x
        self.rect.y=y
    def update(self):
        square_colour = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
        self.rect.x = self.rect.x + self.speedx
        self.rect.y = self.rect.y + self.speedy

my_square = Square(0, 705, 20, 30, 1, 0, (0, 0, 0))
pavement = Square(0, 735, 750, 15, 0 , 0, (100, 100, 100))

allspriteslist = pygame.sprite.Group()
allspriteslist.add(my_square)
allspriteslist.add(pavement)
rain = pygame.sprite.Group()

pygame.init()
screen = pygame.display.set_mode([750,750])
pygame.display.set_caption('Snake Example')
clock = pygame.time.Clock()

background_colour = (150, 150, 150)
done = False
while not done:
    r = Square(random.randint(0, 747), 0, 3, 7, 0, 5, (137, 200, 230))
    allspriteslist.add(r)
    rain.add(r)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                done = True

    if my_square.rect.x > 750:
        my_square.rect.x = - 10
    if my_square.rect.x < - 50:
        my_square.rect.x = 800
    pygame.sprite.spritecollide(pavement, rain, True)

    screen.fill(background_colour)
    allspriteslist.draw(screen)
    allspriteslist.update()    
    pygame.display.flip()

相关问题 更多 >