国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中pygame的mouse鼠標事件用法實例

2020-01-04 17:56:42
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了Python中pygame的mouse鼠標事件用法,以完整實例形式詳細分析了pygame響應鼠標事件的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

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

pygame.mouse提供了一些方法獲取鼠標設備當前的狀態

 

 
  1. ''
  2. pygame.mouse.get_pressed - get the state of the mouse buttons get the state of the mouse buttons 
  3. pygame.mouse.get_pos - get the mouse cursor position get the mouse cursor position 
  4. pygame.mouse.get_rel - get the amount of mouse movement get the amount of mouse movement 
  5. pygame.mouse.set_pos - set the mouse cursor position set the mouse cursor position 
  6. pygame.mouse.set_visible - hide or show the mouse cursor hide or show the mouse cursor 
  7. pygame.mouse.get_focused - check if the display is receiving mouse input check if the display is receiving mouse input 
  8. pygame.mouse.set_cursor - set the image for the system mouse cursor set the image for the system mouse cursor 
  9. pygame.mouse.get_cursor - get the image for the system mouse cursor get the image for the system mouse cursor 
  10. ''

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

pygame.mouse.get_pressed()

pygame.mouse.get_pos()

展示的效果:

Python中pygame的mouse鼠標事件用法實例

游戲效果:

當鼠標經過窗口的時候,窗口背景顏色會隨著鼠標的移動而發生改變,當鼠標點擊窗口

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

 

 
  1. #pygame mouse 
  2. import os, pygame 
  3. from pygame.locals import * 
  4. from sys import exit 
  5. from random import * 
  6. __author__ = {'name' : 'Hongten'
  7. 'mail' : 'hongtenzone@foxmail.com'
  8. 'Version' : '1.0'
  9. if not pygame.font:print('Warning, Can not found font!'
  10. pygame.init() 
  11. screen = pygame.display.set_mode((255, 255), 0, 32) 
  12. screen.fill((255,255,255)) 
  13. font = pygame.font.Font('data//font//TORK____.ttf', 20) 
  14. text = font.render('Cliked Me please!!!', True, (34, 252, 43)) 
  15. mouse_x, mouse_y = 0, 0 
  16. while 1: 
  17. for event in pygame.event.get(): 
  18. if event.type == QUIT: 
  19. exit() 
  20. elif event.type == MOUSEBUTTONDOWN: 
  21. pressed_array = pygame.mouse.get_pressed() 
  22. for index in range(len(pressed_array)): 
  23. if pressed_array[index]: 
  24. if index == 0: 
  25. print('Pressed LEFT Button!'
  26. elif index == 1: 
  27. print('The mouse wheel Pressed!'
  28. elif index == 2: 
  29. print('Pressed RIGHT Button!'
  30. elif event.type == MOUSEMOTION: 
  31. #return the X and Y position of the mouse cursor 
  32. pos = pygame.mouse.get_pos() 
  33. mouse_x = pos[0] 
  34. mouse_y = pos[1] 
  35. screen.fill((mouse_x, mouse_y, 0))  
  36. screen.blit(text, (40, 100)) 
  37. pygame.display.update() 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黑龙江省| 鹤壁市| 平和县| 和硕县| 汕头市| 黄龙县| 呈贡县| 农安县| 延安市| 芜湖市| 黄大仙区| 海丰县| 昌黎县| 曲靖市| 黄龙县| 钟山县| 阆中市| 旺苍县| 桦甸市| 浦北县| 鹿邑县| 兴和县| 石嘴山市| 霍邱县| 宜君县| 榆树市| 连江县| 佛坪县| 迭部县| 钟山县| 聂荣县| 岐山县| 湄潭县| 理塘县| 淮安市| 绥棱县| 澄迈县| 固原市| 安乡县| 哈密市| 思南县|