我想在屏幕上移動矩形讓我們垂直說,並附上了一個動態變化並代表矩形位置的數字,甚至有可能嗎?非常感謝我的整個項目取決於此。如何在Pygame中顯示與矩形位置相關的數字?
0
A
回答
1
您需要在每次重新描繪文字面的矩形的位置變化時(例如,如果一個MOUSEMOTION
事件發生),然後在當前矩形位置的blit它。
import sys
import pygame as pg
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
font = pg.font.Font(None, 32)
font_color = (100, 200, 150)
rect = pg.Rect(20, 200, 80, 50)
txt_surf = font.render(str(rect.topleft), True, font_color)
selected = None
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
if selected:
selected = None
elif rect.collidepoint(event.pos):
selected = rect
elif event.type == pg.MOUSEMOTION:
if selected:
selected.center = event.pos
txt_surf = font.render(str(rect.topleft), True, font_color)
screen.fill((30, 30, 30))
pg.draw.rect(screen, font_color, rect, 2)
screen.blit(txt_surf, (rect.right+10, rect.top))
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
sys.exit()
+0
一如既往的完美答案,仍然試圖實現它,謝謝! – Bartnick81
1
以下是顯示按住鼠標左鍵時鼠標光標位置的示例代碼。在這種情況下,「數字動態變化」是鼠標光標的位置。如你在代碼中看到的,我使用等寬字體(Courier New in Windows),所以數字中的任何前導空格都不會改變數字的大小 - 具有空格的數字12
需要相同數量的屏幕上的房地產號碼爲199
。每次通過主循環時,由於 timer.tick(60)
聲明,每次通過主循環約60次,該字符串是根據所需的數字構建的,然後從字符串構建一個名爲text
的表面,然後將表面傳送到屏幕。
是否清楚如何將此應用於您的情況?
"""A pygame "game" that shows the position of the mouse cursor while
the left mouse button is pressed."""
import pygame
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Show the Position of the Mouse while Dragging")
BLACK= (0, 0, 0)
RED = (255, 0, 0)
timer = pygame.time.Clock()
font = pygame.font.SysFont("Courier New", 24)
mouse_is_down = False
keep_going = True
while keep_going:
# Handle events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[0]: # Left mouse button
mouse_is_down = True
if event.type == pygame.MOUSEBUTTONUP:
if not pygame.mouse.get_pressed()[0]: # Left mouse button
mouse_is_down = False
# Draw the screen.
screen.fill(BLACK)
if mouse_is_down:
position = pygame.mouse.get_pos()
position_string = 'Mouse position: ({:3d}, {:3d})'.format(*position)
else:
position_string = ''
text = font.render(position_string, True, RED)
text_rect = text.get_rect()
text_rect.right = screen.get_rect().right - 10
text_rect.y = 10
screen.blit(text, text_rect)
pygame.display.update()
timer.tick(60)
pygame.quit()
+0
另一個答案更適合我的問題,但是非常感謝! – Bartnick81
相關問題
- 1. pygame的顯示位置
- 2. 將矩形移動到與矩形相同的鼠標位置?
- 3. 如何在pygame中自動移動到鼠標位置的矩形?
- 4. pygame中的透明矩形
- 5. 文本在矩形 - Pygame的
- 6. pygame的角度球(矩形)和矩形
- 7. 與渲染紋理相關的矩形
- 8. 在LineChart中顯示矩形
- 9. 用Pygame定位移動圖像/矩形
- 10. 在矩形位置顯示0.01的差異?
- 11. 如何在圖形中顯示數字?
- 12. 所有條形圖均顯示在MPAndroidChart中的相同位置
- 13. 如何讓文字顯示在我的矩形中,但我甚至無法讓矩形顯示?
- 14. Pygame繪製矩形
- 15. Pygame矩形碰撞
- 16. 如何在pygame中使矩形出現在另一個矩形之後?
- 17. 如何更改矩形的位置?
- 18. 如何在鼠標上顯示矩形?
- 19. 如何在中心設置用戶位置的地圖矩形。
- 20. 如何與超過4位小數顯示矩陣數據
- 21. QWidget/QPainter show()+ paintEvent() - >在舊位置顯示矩形
- 22. Graphics.DrawString:統計在矩形中有效顯示的字符數
- 23. 在相對佈局中使用矩形堆棧顯示金字塔形狀
- 24. iphone相機顯示焦點矩形
- 25. 如何在Pygame上顯示分數?
- 26. 不能在Python pygame中繪製矩形
- 27. pygame的矩形參數無效
- 28. 如何在矩形輸出中顯示二維數組?
- 29. 如何在Pygame中指定和測試矩形上的屬性
- 30. 如何在Pygame中產生更多的矩形?
當然這是可能的,但請添加更多的細節。例如,你想要什麼數字「代表矩形的位置」 - 它的頂部的y座標?矩形移動的頻率有多快 - 速度如此之快以至於觀衆無法閱讀到快速變化的數字?對字體是否有任何限制 - 固定寬度的字體可以更好地處理快速變化的數字,特別是如果任何文本發生更改。最後,你還做了哪些其他的研究,你嘗試了什麼?究竟你在哪裏卡住 - 你知道如何在pygame屏幕上顯示文本? –
我知道如何顯示文本我已將它附加到矩形的頂部,但作爲字體是文本對象,我無法動態更改值。我需要pygame.mousemotion的rect.y位置來顯示每當我用鼠標拖動矩形。我想我必須以某種方式將返回的y值轉換爲字符串? – Bartnick81