为什么碰撞测试总是返回 'True' 而图像矩形的位置总是错误 (0, 0)
碰撞测试问题
原因:
碰撞测试依靠 Sprite 对象的 rect 属性来确定它们是否相交。但是,代码中的 get_rect() 方法未正确将矩形的位置设置为预期坐标。
解决方案:
使用 get_rect() 时,您可以使用关键字参数指定位置或将其分配给矩形的左上角虚拟属性。使用此更正后的代码:
self.rect = self.image.get_rect(topleft=(self.x, self.y))
不正确的矩形位置
原因:
您向精灵添加了不必要的 x 和 y 属性,而不是依赖在矩形的位置上。结果,矩形的位置始终设置为 (0, 0),因为 Surface 对象没有默认位置。
解决方案:
删除 x 和 y 属性并使用 rect 属性设置 Sprite 对象的位置。这是更正后的代码:
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
使用 Sprite Group 简化代码
您可以通过使用 pygame.sprite.Group 来管理 Sprite 对象来进一步简化代码。这将自动处理绘制和更新。
obstacle = Obstacle() ball = Ball() # Create a sprite group and add the sprites to it. all_sprites = pygame.sprite.Group([obstacle, ball]) while not crashed: # [... event handling and game logic] gameDisplay.fill((255, 255, 255)) # Draw all sprites using the group's draw method. all_sprites.draw(gameDisplay) # [... other game loop tasks]
以上是为什么我的 Pygame 碰撞检测总是返回 True,以及为什么我的图像矩形位置错误地设置为 (0, 0)?的详细内容。更多信息请关注PHP中文网其他相关文章!

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版
视觉化网页开发工具

Dreamweaver CS6
视觉化网页开发工具

SublimeText3汉化版
中文版,非常好用