如何确定对象在 Colliderect 中相互穿过的原因 [英] How to determine why objects pass through each other in Colliderect

查看:32
本文介绍了如何确定对象在 Colliderect 中相互穿过的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,Colliderect 无法正常工作,并且雨水会穿过人行道.这真的很烦人,因为所有这些未使用的精灵都会造成大量延迟.

For some reason the Colliderect won't work and the rain passes through pavement. This is really annoying because all of those unused sprites create tons of lag.

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

推荐答案

你必须检测所有雨滴与路面的碰撞.

You have to detect the collision of all the raindrops with the pavement.

为雨滴添加一个组:

rain = pygame.sprite.Group()

将每个雨滴添加到组中:

Add each rain drop to the 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():

And remove a drop if it hits the ground, by pygame.sprite.Sprite.kill():

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

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

使用pygame.sprite.spritecollide() 并将True 传递给参数dokill:

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

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

<小时>

完整示例代码:


Complete example code:

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

这篇关于如何确定对象在 Colliderect 中相互穿过的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆