欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中pygame的mouse鼠標(biāo)事件用法實例

 更新時間:2015年11月11日 14:34:20   作者:Hongten  
這篇文章主要介紹了Python中pygame的mouse鼠標(biāo)事件用法,以完整實例形式詳細(xì)分析了pygame響應(yīng)鼠標(biāo)事件的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Python中pygame的mouse鼠標(biāo)事件用法。分享給大家供大家參考,具體如下:

pygame.mouse提供了一些方法獲取鼠標(biāo)設(shè)備當(dāng)前的狀態(tài)

'''
pygame.mouse.get_pressed - get the state of the mouse buttons  get the state of the mouse buttons
pygame.mouse.get_pos - get the mouse cursor position  get the mouse cursor position
pygame.mouse.get_rel - get the amount of mouse movement  get the amount of mouse movement
pygame.mouse.set_pos - set the mouse cursor position  set the mouse cursor position
pygame.mouse.set_visible - hide or show the mouse cursor  hide or show the mouse cursor
pygame.mouse.get_focused - check if the display is receiving mouse input  check if the display is receiving mouse input
pygame.mouse.set_cursor - set the image for the system mouse cursor  set the image for the system mouse cursor
pygame.mouse.get_cursor - get the image for the system mouse cursor  get the image for the system mouse cursor
'''

在下面的demo中,主要用到了:

pygame.mouse.get_pressed()

pygame.mouse.get_pos()

展示的效果:

游戲效果:

當(dāng)鼠標(biāo)經(jīng)過窗口的時候,窗口背景顏色會隨著鼠標(biāo)的移動而發(fā)生改變,當(dāng)鼠標(biāo)點擊窗口

會在控制臺打印出是鼠標(biāo)的那個鍵被點擊了:左,右,滾輪

#pygame mouse
import os, pygame
from pygame.locals import *
from sys import exit
from random import *
__author__ = {'name' : 'Hongten',
       'mail' : 'hongtenzone@foxmail.com',
       'Version' : '1.0'}
if not pygame.font:print('Warning, Can not found font!')
pygame.init()
screen = pygame.display.set_mode((255, 255), 0, 32)
screen.fill((255,255,255))
font = pygame.font.Font('data\\font\\TORK____.ttf', 20)
text = font.render('Cliked Me please!!!', True, (34, 252, 43))
mouse_x, mouse_y = 0, 0
while 1:
  for event in pygame.event.get():
    if event.type == QUIT:
      exit()
    elif event.type == MOUSEBUTTONDOWN:
      pressed_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, (40, 100))
  pygame.display.update()

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • Python進(jìn)行區(qū)間取值案例講解

    Python進(jìn)行區(qū)間取值案例講解

    這篇文章主要介紹了Python進(jìn)行區(qū)間取值案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim())

    matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim())

    這篇文章主要介紹了matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim()),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python?request要求接口參數(shù)必須是json數(shù)據(jù)的處理方式

    python?request要求接口參數(shù)必須是json數(shù)據(jù)的處理方式

    這篇文章主要介紹了python?request要求接口參數(shù)必須是json數(shù)據(jù)的處理方式,Reqeusts支持以form表單形式發(fā)送post請求,只需要將請求的參數(shù)構(gòu)造成一個字典,然后傳給requests.post()的data參數(shù)即可,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-08-08
  • Python作用域與名字空間原理詳解

    Python作用域與名字空間原理詳解

    這篇文章主要介紹了python作用域與名字空間原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Python技能樹共建之python?urllib?模塊

    Python技能樹共建之python?urllib?模塊

    這篇文章介紹了Python技能樹共建之python?urllib?模塊,urllib模塊是?Python?標(biāo)準(zhǔn)庫,更多相關(guān)介紹需要的小伙伴可以參考下面文章的詳細(xì)內(nèi)容
    2022-05-05
  • pytorch 彩色圖像轉(zhuǎn)灰度圖像實例

    pytorch 彩色圖像轉(zhuǎn)灰度圖像實例

    今天小編就為大家分享一篇pytorch 彩色圖像轉(zhuǎn)灰度圖像實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python正則表達(dá)式re模塊詳解(建議收藏!)

    Python正則表達(dá)式re模塊詳解(建議收藏!)

    正則表達(dá)式是用來匹配與查找字符串的,從網(wǎng)上爬取數(shù)據(jù)自然或多或少會用到正則表達(dá)式,python的正則表達(dá)式要先引入re模塊,這篇文章主要給大家介紹了關(guān)于Python正則表達(dá)式re模塊的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Python類和方法注釋規(guī)范說明

    Python類和方法注釋規(guī)范說明

    這篇文章主要介紹了Python類和方法注釋規(guī)范說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Python調(diào)用Matplotlib繪制振動圖、箱型圖和提琴圖

    Python調(diào)用Matplotlib繪制振動圖、箱型圖和提琴圖

    Matplotlib作為用于數(shù)據(jù)可視化的Python軟件包,能夠繪制多種2D圖像,它使用簡單、代碼清晰易懂,深受廣大技術(shù)愛好者喜愛。本文主要介紹了通過?Matplotlib繪制振動圖、箱型圖、提琴圖,需要的朋友可以參考一下
    2021-12-12
  • Python+Selenium實現(xiàn)自動填寫問卷

    Python+Selenium實現(xiàn)自動填寫問卷

    本文主要介紹了Python+Selenium實現(xiàn)自動填寫問卷,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評論