Question

I am currently adding my final touch to my paint program I have been creating using pygame and python 2.7.5. The final thing I would like to add is a current position display. I am just wondering about how I would do this. I know how to get the current mouse position but I just do not know how to display it in my program. Any help is appreciated. Thanks.

Was it helpful?

Solution

You can use the pygame.font module to display text in your window.

Example:

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()

Result:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top