为什么我的碰撞测试总是返回“true”,并且为什么图像矩形的位置总是错误的(0,0)?

2024-11-28 08:37:00
admin
原创
135
摘要:问题描述:我的 collide_rect 函数无法正常工作。它总是返回 True,但实际上不应该返回。我尝试在互联网上查找,但没有任何效果。我认为 collide rect 不知何故没有使用两个精灵的实际坐标。有人能帮忙吗?import pygame import pygame.sprite import s...

问题描述:

我的 collide_rect 函数无法正常工作。它总是返回 True,但实际上不应该返回。我尝试在互联网上查找,但没有任何效果。我认为 collide rect 不知何故没有使用两个精灵的实际坐标。有人能帮忙吗?

import pygame
import pygame.sprite
import sys


gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption("test_collision")
clock = pygame.time.Clock()
crashed = False


class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("ball.png")
        self.rect = self.image.get_rect()
        self.x = 280
        self.y = 475
        self.col = False
    def update(self):
        gameDisplay.blit(self.image, (self.x,self.y))
        self.rect = self.image.get_rect()
    def test_collisions(self,sprite):
        self.col = pygame.sprite.collide_rect(self,sprite)
class Obstacle(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.x = 1000
        self.y = 483
        self.image = pygame.image.load("obstacle.png")
        self.time = pygame.time.get_ticks()
        self.rect = self.image.get_rect()
    def change_x(self):
        self.time = pygame.time.get_ticks()
        self.x = -(self.time/5) + 800
    def update(self):
        self.rect = self.image.get_rect()
        gameDisplay.blit(self.image,(self.x,self.y))


obstacle = Obstacle()
ball = Ball()      
while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
    gameDisplay.fill((255,255,255))
    ball.update()
    obstacle.change_x()
    obstacle.update()
    ball.test_collisions(obstacle)
    if ball.col:
        print("colided")
    pygame.display.flip()
    clock.tick(1000)


pygame.quit()
sys.exit()

PS 这是我的第一篇帖子:)


解决方案 1:

pygame.Surface.get_rect.get_rect()返回一个与Surface对象大小相同的矩形,但由于Surface对象没有位置,因此它返回的矩形始终从 (0, 0) 开始。使用该函数将 Surface放置

在显示屏上的某个位置。blit

您必须通过关键字参数设置矩形的位置,例如:

self.rect = self.image.get_rect(topleft = (self.x, self.y))

或者分配给虚拟属性(参见pygame.Rect),例如:

self.rect = self.image.get_rect()
self.rect.topleft = (self.x, self.y)

完全没有必要添加一些额外的属性self.xself.y。而是使用矩形的位置。例如:

class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("ball.png")
        self.rect = self.image.get_rect(topleft = (280, 475))
        self.col = False
    def update(self):
        gameDisplay.blit(self.image, self.rect)
    def test_collisions(self,sprite):
        self.col = pygame.sprite.collide_rect(self,sprite)

class Obstacle(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("obstacle.png")
        self.time = pygame.time.get_ticks()
        self.rect = self.image.get_rect(topleft = (1000, 483))
    def change_x(self):
        self.time = pygame.time.get_ticks()
        self.rect.x = -(self.time/5) + 800
    def update(self):
        gameDisplay.blit(self.image, self.rect)

进一步请注意,如果您使用和调用,您可以Ball.update()分别摆脱这些方法(您可以删除它们),它使用所包含精灵的和属性来绘制它们。例如:Obstacle.update()`pygame.sprite.Group.draw().image`.rect

obstacle = Obstacle()
ball = Ball()      
all_sprites = pygame.sprite.Group([obstacle, ball])

while not crashed:

    # [...]
  
    gameDisplay.fill((255,255,255))
    
    all_sprites.draw(gameDisplay)
    
    pygame.display.flip()
    clock.tick(1000)
相关推荐
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1259  
  IPD(Integrated Product Development)流程管理作为一种先进的产品开发管理理念和方法,在提升企业创新能力方面发挥着至关重要的作用。它打破了传统产品开发过程中部门之间的壁垒,通过整合资源、优化流程,实现产品的快速、高效开发,为企业在激烈的市场竞争中赢得优势。IPD流程管理的核心概念IPD流程...
IPD流程中PDCP是什么意思   11  
  IPD(Integrated Product Development)流程管理作为一种先进的产品开发管理模式,旨在通过整合各种资源,实现产品的高效、高质量开发。在这一过程中,团队协作无疑是成功的关键。有效的团队协作能够打破部门壁垒,促进信息共享,提升决策效率,从而确保产品开发项目顺利推进。接下来,我们将深入探讨IPD流...
IPD培训课程   9  
  IPD(Integrated Product Development)研发管理体系作为一种先进的产品开发理念和方法,在众多企业中得到了广泛应用。它旨在打破部门壁垒,整合资源,实现产品开发的高效、协同与创新。在项目周期方面,IPD研发管理体系有着深远且多维度的影响,深入剖析这些影响,对于企业优化产品开发流程、提升市场竞争...
华为IPD流程   11  
  IPD(Integrated Product Development)流程管理是一种先进的产品开发管理模式,旨在通过整合企业的各种资源,实现产品的高效、高质量开发。它涵盖了从产品概念提出到产品退市的整个生命周期,对企业的发展具有至关重要的意义。接下来将详细阐述IPD流程管理的五个阶段及其重要性。概念阶段概念阶段是IPD...
IPD概念阶段   12  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用