2017-07-18 89 views
0

我想在屏幕上移動矩形讓我們垂直說,並附上了一個動態變化並代表矩形位置的數字,甚至有可能嗎?非常感謝我的整個項目取決於此。如何在Pygame中顯示與矩形位置相關的數字?

+1

當然這是可能的,但請添加更多的細節。例如,你想要什麼數字「代表矩形的位置」 - 它的頂部的y座標?矩形移動的頻率有多快 - 速度如此之快以至於觀衆無法閱讀到快速變化的數字?對字體是否有任何限制 - 固定寬度的字體可以更好地處理快速變化的數字,特別是如果任何文本發生更改。最後,你還做了哪些其他的研究,你嘗試了什麼?究竟你在哪裏卡住 - 你知道如何在pygame屏幕上顯示文本? –

+0

我知道如何顯示文本我已將它附加到矩形的頂部,但作爲字體是文本對象,我無法動態更改值。我需要pygame.mousemotion的rect.y位置來顯示每當我用鼠標拖動矩形。我想我必須以某種方式將返回的y值轉換爲字符串? – Bartnick81

回答

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