Pygame Mouse Mouse Event usage instance in Python

Source: Internet
Author: User
The example in this article describes the mouse mouse event usage of pygame in Python. Share to everyone for your reference, as follows:

Pygame.mouse provides some ways to get the current state of a mouse device

"Pygame.mouse.get_pressed-get the state of the mouse buttons get the state of the  mouse Buttonspygame.mouse.get_po S-get the mouse cursor position  get the mouse cursor positionpygame.mouse.get_rel-get The amount of mouse movement  get the amount of mouse movementpygame.mouse.set_pos-set the mouse cursor position  set the mouse cursor Positio Npygame.mouse.set_visible-hide or show the mouse cursor  hide or show the mouse CURSORPYGAME.MOUSE.GET_FOCUSED-CHEC K If the display is receiving mouse input  check if the display is receiving mouse Inputpygame.mouse.set_cursor-set t He image for the system mouse cursor  set the image for the system mouse cursorpygame.mouse.get_cursor-get the image For the system mouse cursor  get the image for the system mouse cursor '

In the following demo, the main use of:

Pygame.mouse.get_pressed ()

Pygame.mouse.get_pos ()

The effect of the show:

Game effect:

When the mouse passes through the window, the window background color will change as the mouse moves, when the mouse clicks the window

The button that will print the mouse is clicked on the console: left, right, scroll wheel

#pygame mouseimport OS, pygamefrom pygame.locals import *from sys import exitfrom random import *__author__ = {' name ': ' H Ongten ', ' Mail ': ' hongtenzone@foxmail.com ', ' Version ': ' 1.0 '}if not Pygame.font:print (' Warning, Can not Foun D font! ') Pygame.init () screen = Pygame.display.set_mode ((255, 255), 0, +) Screen.fill ((255,255,255)) font = Pygame.font.Font (' Data\\font\\tork____.ttf ', ') Text = Font.render (' cliked Me please!!! ', True, (252,)) mouse_x, mouse_y = 0, 0while 1 : For event in Pygame.event.get (): if Event.type = = Quit:exit () elif Event.type = = Mousebuttondown:pres          Sed_array = pygame.mouse.get_pressed () for index in range (len (pressed_array)): If Pressed_array[index]:          If index = = 0:print (' Pressed left button! ')          Elif index = = 1:print (' The Mouse Wheel pressed! ')    Elif index = = 2:print (' Pressed right button! ') elif Event.type = = mousemotion: #return The X and Y position of the mouse cursor pos = pygame.mouse.get_pos () mouse_x = pos[0] mouse_y = pos[1] Screen.fill ((mouse_x, mouse_y, 0)) Screen.blit (text, (+)) Pygame.display.update ()

I hope this article is helpful for Python program design.

  • Related Article

    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.