0

Well, I pretty much just copied this code that gets the x and y position of the cursor, and 'I'd like to ask what each line of command does so I can get a grip of it.

Thanks in advance !

print('Type Ctrl-C to stop the program')


try:
    while True:
        x, y = pg.position() 
        coordinates = 'X: ' + str(x).ljust(4) + ' Y: ' + str(y).ljust(4)


        print(coordinates, end='')
        print('\b' * len(coordinates), end='', flush = True)

except KeyboardInterrupt():
    print('\n See you next time!')
Littm
  • 4,923
  • 4
  • 30
  • 38
cvcvka5
  • 57
  • 1
  • 7

1 Answers1

1

You have defined a while loop which will never end as True is universal truth.

After that you have used pg.position() to get the live cursor position which will give you the x and and y coordinate of the cursor on the screen in two tuples defined here x and y. You can get your screen size with pg.size()

Then you are defining a variable named coordinates where you are using ljust() method that returns the left-justified string within the given minimum width. str.ljust(width[, fillchar]) If fillchar is defined, it also fills the remaining space with the defined character.

In next line you have printed the coordinates and again printing length of coordinates with flush() method whose only work is to flush the internal buffer. \b is used to backspace one character before it.

And in the last you are using KeyboardInterrupt() which is raised when you try to stop a running program by pressing ctrl+c or ctrl+z in a command line . This is a summary but to get grip I recommend to see the whole documentation.

Hope it helps!

Ayush Raj
  • 294
  • 3
  • 14