為什麼碰撞測試總是回傳'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,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

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

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

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

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
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

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

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。