2014-01-27 28 views
0

我正在使用pygame和python 2.7.5創建我的繪畫程序,目前我正在添加最終的觸摸。我想添加的最後一件事是當前位置顯示。我只是想知道我會如何做到這一點。我知道如何獲得當前的鼠標位置,但我不知道如何在我的程序中顯示它。任何幫助表示讚賞。謝謝。在Python中顯示畫圖程序的當前位置

回答

1

您可以使用pygame.font模塊在窗口中顯示文本。

實施例:

import pygame 
import sys 

pygame.init() 
font = pygame.font.SysFont("monospace", 15) 
screen = pygame.display.set_mode((400,100)) 

cur_x, cur_y = 0,0 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit(0) 
     elif event.type == pygame.MOUSEMOTION: 
      cur_x, cur_y = event.pos 

    screen.fill((0,0,0)) 
    coord_message = "position: x={}, y={}:".format(cur_x, cur_y) 
    coord_label = font.render(coord_message, 1, (255,0,0)) 
    screen.blit(coord_label, (50, 10)) 
    pygame.display.flip() 

結果:

enter image description here

相關問題