我正在調試我的一個程序,我試圖將自訂變數分配給矩形以更新其位置。
這是我的程式碼:
import os ; os.environ['PYGAME_HIDE_SUPPORT_PROMPT']='False' import pygame, random pygame.init() display = pygame.display.set_mode((401, 401)) display.fill("white") ; pygame.display.flip() class MyRect(pygame.Rect): def __setattr__(self, attr, value): # Sets a custom attribute to a rectangle super().__setattr__(attr, value) if attr == 'xValue': pygame.Rect.move(self, (value-self.centerx), 0) # Move the rectangle according to the xValue def contains(self, coords): return self.collidepoint(coords) square = MyRect(175, 175, 50, 50) pygame.draw.rect(display, 'steelBlue', square) # Draw the rectangle to the screen square.xValue = 200 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() ; exit() elif event.type == pygame.MOUSEBUTTONDOWN: if square.contains(pygame.mouse.get_pos()): square.xValue = random.randint(0, display.get_width()) # Update the square.xValue property pygame.display.flip() # Update the screen
當我執行程式時,square.xvalue 屬性正在改變,但螢幕上正方形的位置沒有改變。
我錯過了什麼?
正確答案
你必須重新繪製場景。更改後必須重新繪製矩形。清除顯示並在應用程式循環中繪製矩形:
while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() ; exit() elif event.type == pygame.MOUSEBUTTONDOWN: if square.contains(pygame.mouse.get_pos()): square.xValue = random.randint(0, display.get_width()) # Update the square.xValue property display.fill("white"); pygame.draw.rect(display, 'steelBlue', square) pygame.display.flip() # Update the screen
以上是在 Pygame 中使用自訂屬性更新矩形時出現問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

本文解釋瞭如何使用美麗的湯庫來解析html。 它詳細介紹了常見方法,例如find(),find_all(),select()和get_text(),以用於數據提取,處理不同的HTML結構和錯誤以及替代方案(SEL)

本文比較了Tensorflow和Pytorch的深度學習。 它詳細介紹了所涉及的步驟:數據準備,模型構建,培訓,評估和部署。 框架之間的關鍵差異,特別是關於計算刻度的

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途

本文指導Python開發人員構建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等庫詳細介紹,強調輸入/輸出處理,並促進用戶友好的設計模式,以提高CLI可用性。

文章討論了虛擬環境在Python中的作用,重點是管理項目依賴性並避免衝突。它詳細介紹了他們在改善項目管理和減少依賴問題方面的創建,激活和利益。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

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