6574| 1
|
[项目分享] 之前PS2手柄还写了python代码,有mind+里面的模块,和自己另加 |
非常非常大,400多行报错还有时钟的信息 ''' Suitable for PS2_gamepad on Arduino_Uno motherboards. ''' ''' View the original picture please run the same folder with another program ''' '''======================================================================================''' from pgzero.runner import prepare_mod from pgzero.runner import run_mod from pygame.color import THECOLORS from os import * import warnings import sys import pygame import math import threading import time import struct mod = sys.modules['__main__'] if not getattr(sys, '_PS2gamepad_', None): if not getattr(mod, '__file__', None): raise ImportError( "You are running from an interactive interpreter.\n" "'import PS2gamepad' only works when you are running a Python file." ) prepare_mod(mod) try: from pinpong.board import Board Board('uno','Arduino Uno').begin() #Only applicable to Arduino_Uno motherboards except: def go(): if getattr(sys, '_PS2gamepad', None): return go run_mod(mod) finally: pass '''======================================================================================''' '''Module program''' _all_=['__init__','vibrate','update','vibrate','press','getxy','screen','print_thecolors_'] def _init_(self, x=None): self.seed(x) self.gauss_next = None return self._init_ class PS2gamepad: def __init__(self, DAT=2, CMD=3, CS=4, CLK=5, vibrate=True): self.DAT = ('2','3','4','5','6', '7','8','9', '10','11','12','13', 'A0','A1','A2','A3', 'A4','A5','A6','A7') self.CMD = ('3','2','4','5','6', '7','8','9', '10','11','12','13', 'A0','A1','A2','A3', 'A4','A5','A6','A7') self.CS = ('4','2','3','5','6', '7','8','9', '10','11','12','13', 'A0','A1','A2','A3', 'A4','A5','A6','A7') self.CLK = ('5','2','3','4','6', '7','8','9', '10','11','12','13', 'A0','A1','A2','A3', 'A4','A5','A6','A7') self.vibrate = vibrate self.pressdict = {'TR':0, 'CL':0, 'X':0, 'SQ':0, 'lEFT1':0, 'lEFT2':0, 'lEFT3':0, 'RIGHT1':0, 'RIGHT2':0, 'RIGHT3':0, 'WU':0,'WD':0,'WL':0,'WR':0, 'START':0,'SELECT':0} self.xydict={'lx':0, 'ly':0, 'rx':0, 'ry':0} self.rumble_f=0 self.rumble_v=0 return self.__init__ def vibrate(self, shake=False, strength=0): self.shake=shake self.strength=strength return vibrate def update(self): if self.rumble.f==0: return (False, 0x00) else: return (True,self.rumble_v) def press(self, buttonname='3', ispress=True): return self.pressdict[buttonname]==int(ispress) def getxy(self, rockername='rx'): return self.xydict[rockername] class __ctrl__(): def ctrl (self,_ctrl_='TR|CL'): self._ctrl_={'TR|CL':__init__,'lx|ly':vibrate, 'TR|SQ':update,'TR|x':press, 'CL|x':getxy} self._shake_=_shake_ self._ctrl_=_ctrl_ self.rumble_f=0 self.rumble_v=0 return self.ctrl def screen(self, switch = True): try: pygame.init() if pygame.K_F12 == True: pygame.image.save(Screen,"Screen.jpg") return self.screen except: pygame.quit() return self.screen def print_thecolors_(self,switch_=True,colors='green'): self.colors=('green','blue','black','gray','white', 'pink','orange','red','yellow') self.vibrate_ = vibrate_ if switch_==True: print(THECOLORS[colors]) else: pass return print_thecolors_ #Settings window ' Event '↓ ↓ ↓ class Event: try: def DianJiYuanXing(x, y): global ShiFuXuanZe global ShiFuZhongHui if ((math.sqrt((((abs((x - 100)))**2) + ((abs((y - 100)))**2)))) <= 15): ShiFuXuanZe = (1 - ShiFuXuanZe) ShiFuZhongHui = 0 return DianJiYuanXing def XuanXiang(): if ShiFuXuanZe == 1: pygame.draw.circle(win, (0,0,255), (100,100), 15, 2) pygame.draw.circle(win, (0,0,0), (100,100), 7, 0) ctrl=PS2gamepad() ctrl._ctrl_() else: pygame.draw.circle(win, (0,0,255), (100,100), 15, 2) return XuanXiang pygame.font.init() pygame.init() pygame.display.init() if pygame.key.get_mods() & (pygame.KMOD_LSHIFT | pygame.KMOD_LCTRL): win = pygame.display.set_mode(size=(800,600), flags=0, depth=0) pygame.display.set_caption("Event") run = True ShiFuXuanZe = 0 ShiFuZhongHui = 0 open_ = 0 while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if ((event.type == pygame.MOUSEBUTTONDOWN) or (event.type == pygame.K_ESCAPE)): DianJiYuanXing((event.pos[0]), (event.pos[1])) else: pass if event.type == pygame.KEYDOWN: if event.type == pygame.K_F6: open_ = 1 else: pass if open_ == 1: if ShiFuZhongHui == 0: win.fill((255,255,255)) win.blit(pygame.font.Font('C:/Windows/Fonts/Deng.ttf',20).render('开启快捷键\n Open shortcut keys', True, (0,0,0,0)) , (120,90)) XuanXiang() pygame.display.update() ShiFuZhongHui = ShiFuZhongHui+1 else: pass else: win.fill((255,255,255)) pygame.display.update() pygame.quit() pygame.display.quit() pygame.font.quit() else: pass except: try: import binascii pygame.init() time = pygame.time.Clock() pygame.mixer.init() mixer_ = pygame.mixer.Sound("Error sound.mp3") pygame.mixer.music.set_volume(7.5) pygame.mixer.music.play(0,start=1) if pygame.mixer.get_busy(): pygame.mixer.music.stop() print('It has a radius of',truct.pack('I', abs(-15)),'and a thickness of',truct.pack('I',abs(-2)),',(blue)\n') print() print('It has a radius of',truct.pack('I', abs(-7)),'and a thickness of',truct.pack('I', abs(-0)),',(black)\n') print() print(binascii.a2b_base64(abs(-15)),'\n') print() print(binascii.a2b_base64(abs(-2)),'\n') print() print(binascii.a2b_base64(abs(-7)),'\n') print() print(binascii.a2b_base64(abs(-0)),'\n') time_ = pygame.time.get_ticks() fps_ = time.get_fps() print('Using time',time,'(ms)',sep=' ') print('*\b'*12) print('The clock information') print('The clock frame rate:',fps_) pygame.mixer.quit() pygame.quit() except: ImportError( "The source program did not run successfully.\n" ) mixer_ = pygame.mixer.Sound("Error Sound.mp3") pygame.mixer.music.set_volume(7.5) pygame.mixer.music.play(0,start=1) if pygame.mixer.get_busy(): pygame.mixer.music.stop() finally: pass # Thanks for supporting pygame. Without support now, there won't be PS2gamepad later. import os import sys if 'PS2GAMEPAD_HIDE_SUPPORT_PROMPT' not in os.environ: print('*** Hello from the PS2gamepad cuontry. ***') '''warn''' def warn(self): msg_type = 'import' if self.urgent else 'use' message = '%s %s: %s\n(%s)' % (msg_type, self.name, self.info, self.reason) try: import warnings level = 4 if self.urgent else 3 warnings.warn(message, RuntimeWarning, level) except ImportError: print(message) return self.warn # cleanup namespace del PS2gamepad del os,sys '''======================================================================================''' '''COMMENTS''' ''' Buttonname in press function: # Triangle key :'TR', # Circle key :'CL', # X key :'x', # Square key :'SQ', # Left1,Left2,Left3: lEFT1,lEFT2,lEFT3 # (Right key format same as above) # Up arrow key:WU, # Down arrow key:WD, # Left arrow key:WL, # Right arrow key:WR, # SELECT key :SELECT, # START key :START The ispress of press has four states: # press and hold: 1 , # press: 2 , # loosen: 3 , # change: 4 Rockername in getxy: # 'lx': left x value, # 'ly': left y value, # 'rx': right x value, # 'ry': right y value _ctrl_ function in ctrl: #'TR|CL' = __init__ , #'lx|ly' = vibrate , #'TR|SQ'= update , #'TR|x' = press , #'CL|x' = getxy , Colors in print_thecolors_: #The colors in the tuple correspond to various colors in reality ''' ''' Typical usage:: >>>import PS2gamepad >>>a.PS2gamepad() >>>a.vibrate(shake==Ture,strength=2) → #vibration strength [0~255](int) ↓ (bool) #Set vibration switch >>>a.update() #Refresh the state >>>a.press(buttonname='TR', ispress=1) #Set the TR status of the key to 1[0~3] ↓ ↓ (str) (int) >>>a.getxy(rockername='lx') #Control rocker[left and right](str) ↓ (str) >>>a.ctrl(_shake_ True,ctrl='TR|CL') → #Enter the shortcut key name(str) ↓ (bool) #Set the shortcut key switch #Press 'KMOD_LSHIFT and KMOD_LCTRL' to set the on shortcut key. (Function:ctrl) >>>a.screen(switch=True) ↓ (bool)#If the Boolean value is True, press F12 to take a screenshot. If the Boolean value is False, the function is disabled .(Function:screen) >>>a.print_colors_(switch_=True,colors='green') ↓ ↓ (bool) (str) When switch=Ture and import this function, will print out the RGB value of the set color (in the tuple) (Function:print_colors_) ''' ''' # Thanks for supporting pygame. Without support now, there won't be pygame later. import os import sys if 'PS2GAMEPAD_HIDE_SUPPORT_PROMPT' not in os.environ: print('*** Hello from the PS2gamepad ***') # cleanup namespace del PS2gamepad, os, sys ''' '''warn''' ''' def warn(self): msg_type = 'import' if self.urgent else 'use' message = '%s %s: %s\n(%s)' % (msg_type, self.name, self.info, self.reason) try: import warnings level = 4 if self.urgent else 3 warnings.warn(message, RuntimeWarning, level) except ImportError: print(message) ''' |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed