Python中pygame的mouse鼠標(biāo)事件用法實(shí)例
本文實(shí)例講述了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)過(guò)窗口的時(shí)候,窗口背景顏色會(huì)隨著鼠標(biāo)的移動(dòng)而發(fā)生改變,當(dāng)鼠標(biāo)點(diǎn)擊窗口
會(huì)在控制臺(tái)打印出是鼠標(biāo)的那個(gè)鍵被點(diǎn)擊了:左,右,滾輪
#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()
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim())
這篇文章主要介紹了matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim()),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03python?request要求接口參數(shù)必須是json數(shù)據(jù)的處理方式
這篇文章主要介紹了python?request要求接口參數(shù)必須是json數(shù)據(jù)的處理方式,Reqeusts支持以form表單形式發(fā)送post請(qǐng)求,只需要將請(qǐng)求的參數(shù)構(gòu)造成一個(gè)字典,然后傳給requests.post()的data參數(shù)即可,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-08-08Python技能樹(shù)共建之python?urllib?模塊
這篇文章介紹了Python技能樹(shù)共建之python?urllib?模塊,urllib模塊是?Python?標(biāo)準(zhǔn)庫(kù),更多相關(guān)介紹需要的小伙伴可以參考下面文章的詳細(xì)內(nèi)容2022-05-05pytorch 彩色圖像轉(zhuǎn)灰度圖像實(shí)例
今天小編就為大家分享一篇pytorch 彩色圖像轉(zhuǎn)灰度圖像實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python類(lèi)和方法注釋規(guī)范說(shuō)明
這篇文章主要介紹了Python類(lèi)和方法注釋規(guī)范說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Python調(diào)用Matplotlib繪制振動(dòng)圖、箱型圖和提琴圖
Matplotlib作為用于數(shù)據(jù)可視化的Python軟件包,能夠繪制多種2D圖像,它使用簡(jiǎn)單、代碼清晰易懂,深受廣大技術(shù)愛(ài)好者喜愛(ài)。本文主要介紹了通過(guò)?Matplotlib繪制振動(dòng)圖、箱型圖、提琴圖,需要的朋友可以參考一下2021-12-12Python+Selenium實(shí)現(xiàn)自動(dòng)填寫(xiě)問(wèn)卷
本文主要介紹了Python+Selenium實(shí)現(xiàn)自動(dòng)填寫(xiě)問(wèn)卷,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07