Python Learning gui (Pygame mouse)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.