The position of the mouse is represented by the coordinates of other pygame programs. The values of coordinates are often represented by x and Y variables. The coordinate values in the upper-left corner are the values of 0,0,x and Y, which increase as the mouse moves to the right and downward.
Print left mouse button click location
Import Pygamepygame.init () Windowsize=[400,300]screen=pygame.display.set_mode (windowsize) Clock=pygame.time.Clock () Done=falsewhile not done:for event in Pygame.event.get (): If Event.type==pygame. Mousebuttondown:pos=pygame.mouse.get_pos () Print pos if event.type==pygame. Quit:done=truepygame.quit ()
Change the background color by clicking inside the rectangle
Import pygamepygame.init () Windowsize=[400,300]screen=pygame.display.set_mode (windowSize) clock= Pygame.time.Clock () Black=pygame.color.color ("#000000") White=pygame.color.color ("#FFFFFF") btncolour= Pygame.color.Color ("#A45C8F") btnwidth=50btnlength=20btnx= (windowsize[0]-btnwidth)/2btny= (windowsize[1]- Btnlength)/2toggled=falsepos= (0,0) done=falsewhile not done: if toggled: screen.fill (Black) else: screen.fill (White) pygame.draw.rect (screen, Btncolour,[btnx,btny,btnwidth,btnlength]) if btnx<=pos[0]<=btnx+btnwidth and btny<=pos[1]<=btny+btnlength: toggled=not toggled pos=[0,0] for event in pygame.event.get (): &Nbsp; if event.type==pygame. Mousebuttondown: pos=pygame.mouse.get_ POS () if event.type==pygame. quit: done=true Pygame.display.flip () clock.tick () Pygame.quit ()
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1863198
Python Learning gui (Pygame mouse)