Python中pygame的mouse鼠標(biāo)事件用法實例
本文實例講述了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)文章
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-03python?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-08Python調(diào)用Matplotlib繪制振動圖、箱型圖和提琴圖
Matplotlib作為用于數(shù)據(jù)可視化的Python軟件包,能夠繪制多種2D圖像,它使用簡單、代碼清晰易懂,深受廣大技術(shù)愛好者喜愛。本文主要介紹了通過?Matplotlib繪制振動圖、箱型圖、提琴圖,需要的朋友可以參考一下2021-12-12