如何在Pygame中正确使用colliderect?
How to use colliderect properly in Pygame?
我不明白为什么每当我执行程序时,我的角色与星星相撞时,它不执行以下代码?发生的情况是,即使它们没有发生碰撞,它也已经执行了代码。
class FinishStar:
def __init__(self):
self.x = screenWidth + 100
self.y = 290
self.image = Finish
self.width = self.image.get_width()
self.rect = self.image.get_rect()
def update(self):
self.x -= gameSpeed
if self.x < -self.width:
self.x = screenWidth + 2500
self.y = 50
def draw(self, screen):
screen.blit(self.image, (self.x, self.y))
这是 main() 中的代码:
if scorePoints >= 100:
star.draw(screen)
star.update()
if player.character_rect.colliderect(star.rect):
font = pygame.font.Font('Fonts/Bubblebody.ttf', 22)
font1 = pygame.font.Font('Fonts/Affogato-Bold.otf', 20)
font2 = pygame.font.Font('Fonts/Billgis - Personal Use.ttf', 38)
font3 = pygame.font.Font('Fonts/Affogato-Medium.otf', 31)
background_image = pygame.image.load(os.path.join("Images/Start",
"Level1Complete.png"))
screen.blit(background_image, [0, 0])
text = font1.render("PRESS ANY KEY TO CONTINUE", True, (122, 166, 185))
screen.blit(text, [411, 400])
text = font2.render("level one completed", True, (163, 213, 176))
screen.blit(text, [467, 292])
text = font.render("Congratulations!", True, (163, 213, 176))
screen.blit(text, [473, 321])
text = font3.render("SCORE: 400/400", True, (122, 166, 185))
screen.blit(text, [450, 349])
pygame.display.update()
TriviaLvl1.mainTrivia()
pygame.Surface.get_rect.get_rect()
returns 具有 Surface 对象大小的矩形,始终从 (0, 0) 开始,因为 Surface 对象没有位置。您必须更新 rect
属性的位置:
class FinishStar:
def __init__(self):
self.x = screenWidth + 100
self.y = 290
self.image = Finish
self.width = self.image.get_width()
self.rect = self.image.get_rect(topleft = (self.x, self.y))
def update(self):
self.x -= gameSpeed
if self.x < -self.width:
self.x = screenWidth + 2500
self.y = 50
self.rect.topleft = (self.x, self.y)
def draw(self, screen):
screen.blit(self.image, (self.x, self.y))
实际上您根本不需要 x
、y
和 width
属性。使用 rect
属性的位置和大小。见 pygame.Rect
:
class FinishStar:
def __init__(self):
self.image = Finish
self.rect = self.image.get_rect(topleft = (screenWidth + 100, 290))
def update(self):
self.rect.x -= gameSpeed
if self.rect.x < -self.rect.width:
self.rect.x = screenWidth + 2500
self.rect.y = 50
def draw(self, screen):
screen.blit(self.image, self.rect)
我不明白为什么每当我执行程序时,我的角色与星星相撞时,它不执行以下代码?发生的情况是,即使它们没有发生碰撞,它也已经执行了代码。
class FinishStar:
def __init__(self):
self.x = screenWidth + 100
self.y = 290
self.image = Finish
self.width = self.image.get_width()
self.rect = self.image.get_rect()
def update(self):
self.x -= gameSpeed
if self.x < -self.width:
self.x = screenWidth + 2500
self.y = 50
def draw(self, screen):
screen.blit(self.image, (self.x, self.y))
这是 main() 中的代码:
if scorePoints >= 100:
star.draw(screen)
star.update()
if player.character_rect.colliderect(star.rect):
font = pygame.font.Font('Fonts/Bubblebody.ttf', 22)
font1 = pygame.font.Font('Fonts/Affogato-Bold.otf', 20)
font2 = pygame.font.Font('Fonts/Billgis - Personal Use.ttf', 38)
font3 = pygame.font.Font('Fonts/Affogato-Medium.otf', 31)
background_image = pygame.image.load(os.path.join("Images/Start",
"Level1Complete.png"))
screen.blit(background_image, [0, 0])
text = font1.render("PRESS ANY KEY TO CONTINUE", True, (122, 166, 185))
screen.blit(text, [411, 400])
text = font2.render("level one completed", True, (163, 213, 176))
screen.blit(text, [467, 292])
text = font.render("Congratulations!", True, (163, 213, 176))
screen.blit(text, [473, 321])
text = font3.render("SCORE: 400/400", True, (122, 166, 185))
screen.blit(text, [450, 349])
pygame.display.update()
TriviaLvl1.mainTrivia()
pygame.Surface.get_rect.get_rect()
returns 具有 Surface 对象大小的矩形,始终从 (0, 0) 开始,因为 Surface 对象没有位置。您必须更新 rect
属性的位置:
class FinishStar:
def __init__(self):
self.x = screenWidth + 100
self.y = 290
self.image = Finish
self.width = self.image.get_width()
self.rect = self.image.get_rect(topleft = (self.x, self.y))
def update(self):
self.x -= gameSpeed
if self.x < -self.width:
self.x = screenWidth + 2500
self.y = 50
self.rect.topleft = (self.x, self.y)
def draw(self, screen):
screen.blit(self.image, (self.x, self.y))
实际上您根本不需要 x
、y
和 width
属性。使用 rect
属性的位置和大小。见 pygame.Rect
:
class FinishStar:
def __init__(self):
self.image = Finish
self.rect = self.image.get_rect(topleft = (screenWidth + 100, 290))
def update(self):
self.rect.x -= gameSpeed
if self.rect.x < -self.rect.width:
self.rect.x = screenWidth + 2500
self.rect.y = 50
def draw(self, screen):
screen.blit(self.image, self.rect)