Skip to content

Instantly share code, notes, and snippets.

@lhchavez
Created March 1, 2021 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhchavez/50f4c31fc3faf811c3a572148479ef1b to your computer and use it in GitHub Desktop.
Save lhchavez/50f4c31fc3faf811c3a572148479ef1b to your computer and use it in GitHub Desktop.
Demo of the cursor grab
#!/usr/bin/python3
import base64
import io
# import the pygame module, so you can use it
import pygame
MARK_PNG = '''
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAMJJREFUWIXt
lksOgDAIRMF48N4cN9VEpTDYX0w6S4Od1xFpiZaWJovBOmm8XugFEUnYYvyqc9f3CmBzB6joYwFU
mSsgqtfWxMFR3ojaR6UEmu3+ZqYk0TwBpRHteuVZcffMiWqTeaYQSqDHZxnShFMBvJ74TwLR7j7f
8foGBhBJIQjNvHoOIBDMKfS77hEABMKaIaTMnSGj2DqMup+GlrkHUA3hmSMARPkYjYIg5ijABYJA
ILegrwBE2OU0fDFdWpqqAzx5SqoAoleYAAAAAElFTkSuQmCC
'''
# define a main function
def main():
# initialize the pygame module
pygame.init()
# load and set the logo
logo = pygame.image.load(io.BytesIO(base64.b64decode(MARK_PNG)))
pygame.display.set_icon(logo)
pygame.display.set_caption("OHIMARK")
# create a surface on screen that has the size of 240 x 180
w = 240
h = 180
screen = pygame.display.set_mode((w, h))
# define a variable to control the main loop
running = True
mark_x = 0
mark_y = 0
# main loop
while running:
# event handling, gets all event from the event queue
for event in pygame.event.get():
# only do something if the event is of type QUIT
if event.type == pygame.QUIT:
# change the value to False, to exit the main loop
running = False
elif event.type == pygame.MOUSEMOTION:
print(event)
mark_x += event.rel[0] / 10.0
mark_y += event.rel[1] / 10.0
#mark_x = event.pos[0]
#mark_y = event.pos[1]
mark_x = (w + (mark_x % w)) % w
mark_y = (h + (mark_y % h)) % h
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.mouse.set_visible(True)
pygame.event.set_grab(False)
elif event.type == pygame.MOUSEBUTTONDOWN:
pygame.mouse.set_visible(False)
pygame.event.set_grab(True)
else:
print(event)
pygame.draw.rect(screen, (0, 0, 0), (0, 0, w, h))
screen.blit(logo, (mark_x, mark_y))
pygame.display.flip()
# run the main function only if this module is executed as the main script
# (if you import this as a module then nothing is executed)
if __name__=="__main__":
# call the main function
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment