pygame.colliderect 尽管彼此没有接触,但返回 True
pygame.colliderect returning True despite not touching each other
我在修修补补PyGame,看文章,遇到了这个奇怪的现象。出于某种原因,pygame.colliderect returns True
当两个精灵不是 touching.Code:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
running = True
spriteX = 100
spriteY = 100
spriteVY = 3
clock = pygame.time.Clock()
class Sprite(pygame.sprite.Sprite):
def __init__(self):
super(Sprite, self).__init__()
self.vel = (0, 0)
self.surf = pygame.Surface((100, 200))
self.surf.fill((255, 255, 255))
self.rectangle = self.surf.get_rect()
class Platform(pygame.sprite.Sprite):
def __init__(self):
super(Platform, self).__init__()
self.surf = pygame.Surface((200, 10))
self.surf.fill((142, 212, 25))
self.rectangle = self.surf.get_rect()
on_ground = True
while running:
platform_group = pygame.sprite.Group()
platform_group.add(Platform())
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if pygame.key.get_pressed()[pygame.K_SPACE]: spriteY -= 10
if pygame.key.get_pressed()[pygame.K_d]: spriteX += 10
if pygame.key.get_pressed()[pygame.K_a]: spriteX -= 10
print((Platform().rectangle.colliderect(Sprite().rectangle)))
screen.fill((0, 0, 0))
if spriteX >= 300:
spriteX = 300
if spriteX <= 0:
spriteX = 0
if not on_ground:
spriteY += spriteVY
spriteVY -= 3
screen.blit(Sprite().surf, ((spriteX, spriteY)))
screen.blit(Platform().surf, ((50, 20)))
pygame.display.flip()
clock.tick(24)
A pygame.Surface
has no position. Hence the position of the pygame.Rect
object which is returned by get_rect()
是 (0, 0).
请注意,当 Surface 为 blit()
.
时,您必须指定位置
当从表面获取矩形时,通过关键字参数指定位置。
在主应用程序循环之前创建对象的实例(参见组的 Instance Objects) and add them to a pygame.sprite.Group
. Each pygame.sprite.Sprite
should have the attributes .rect
and .image
which are used by the method draw()
。
摆脱全局变量 spriteX
和 spriteY
并使用精灵的 .rect
属性。
例如:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
running = True
spriteVY = 3
clock = pygame.time.Clock()
class MySprite(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.vel = (0, 0)
self.image = pygame.Surface((100, 200))
self.image.fill((255, 255, 255))
self.rect = self.image.get_rect(topleft = (x, y))
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((200, 10))
self.image.fill((142, 212, 25))
self.rect = self.image.get_rect(topleft = (x, y))
sprite = MySprite(100, 100)
platform = Platform(50, 20)
group = pygame.sprite.Group([sprite, platform])
on_ground = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if pygame.key.get_pressed()[pygame.K_SPACE]: sprite.rect.y -= 10
if pygame.key.get_pressed()[pygame.K_d]: sprite.rect.x += 10
if pygame.key.get_pressed()[pygame.K_a]: sprite.rect.x -= 10
if platform.rect.colliderect(sprite.rect):
print("hit")
if sprite.rect.x >= 300:
sprite.rect.x = 300
if sprite.rect.x <= 0:
sprite.rect.x = 0
if not on_ground:
sprite.rect.y += spriteVY
spriteVY -= 3
screen.fill((0, 0, 0))
group.draw(screen)
pygame.display.flip()
clock.tick(24)
我在修修补补PyGame,看文章,遇到了这个奇怪的现象。出于某种原因,pygame.colliderect returns True
当两个精灵不是 touching.Code:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
running = True
spriteX = 100
spriteY = 100
spriteVY = 3
clock = pygame.time.Clock()
class Sprite(pygame.sprite.Sprite):
def __init__(self):
super(Sprite, self).__init__()
self.vel = (0, 0)
self.surf = pygame.Surface((100, 200))
self.surf.fill((255, 255, 255))
self.rectangle = self.surf.get_rect()
class Platform(pygame.sprite.Sprite):
def __init__(self):
super(Platform, self).__init__()
self.surf = pygame.Surface((200, 10))
self.surf.fill((142, 212, 25))
self.rectangle = self.surf.get_rect()
on_ground = True
while running:
platform_group = pygame.sprite.Group()
platform_group.add(Platform())
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if pygame.key.get_pressed()[pygame.K_SPACE]: spriteY -= 10
if pygame.key.get_pressed()[pygame.K_d]: spriteX += 10
if pygame.key.get_pressed()[pygame.K_a]: spriteX -= 10
print((Platform().rectangle.colliderect(Sprite().rectangle)))
screen.fill((0, 0, 0))
if spriteX >= 300:
spriteX = 300
if spriteX <= 0:
spriteX = 0
if not on_ground:
spriteY += spriteVY
spriteVY -= 3
screen.blit(Sprite().surf, ((spriteX, spriteY)))
screen.blit(Platform().surf, ((50, 20)))
pygame.display.flip()
clock.tick(24)
A pygame.Surface
has no position. Hence the position of the pygame.Rect
object which is returned by get_rect()
是 (0, 0).
请注意,当 Surface 为 blit()
.
当从表面获取矩形时,通过关键字参数指定位置。
在主应用程序循环之前创建对象的实例(参见组的 Instance Objects) and add them to a pygame.sprite.Group
. Each pygame.sprite.Sprite
should have the attributes .rect
and .image
which are used by the method draw()
。
摆脱全局变量 spriteX
和 spriteY
并使用精灵的 .rect
属性。
例如:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
running = True
spriteVY = 3
clock = pygame.time.Clock()
class MySprite(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.vel = (0, 0)
self.image = pygame.Surface((100, 200))
self.image.fill((255, 255, 255))
self.rect = self.image.get_rect(topleft = (x, y))
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((200, 10))
self.image.fill((142, 212, 25))
self.rect = self.image.get_rect(topleft = (x, y))
sprite = MySprite(100, 100)
platform = Platform(50, 20)
group = pygame.sprite.Group([sprite, platform])
on_ground = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if pygame.key.get_pressed()[pygame.K_SPACE]: sprite.rect.y -= 10
if pygame.key.get_pressed()[pygame.K_d]: sprite.rect.x += 10
if pygame.key.get_pressed()[pygame.K_a]: sprite.rect.x -= 10
if platform.rect.colliderect(sprite.rect):
print("hit")
if sprite.rect.x >= 300:
sprite.rect.x = 300
if sprite.rect.x <= 0:
sprite.rect.x = 0
if not on_ground:
sprite.rect.y += spriteVY
spriteVY -= 3
screen.fill((0, 0, 0))
group.draw(screen)
pygame.display.flip()
clock.tick(24)