產品協同設計第4組

手足球雙人鍵盤對打 << Previous Next >> 手足球電腦與電腦對打

手足球手控與電腦對打

檔案:new football

V-REP模擬結果

PYTHON程式碼

import vrep
import keyboard
import  time 
import sys, math
vrep.simxFinish(-1) 
clientID = vrep.simxStart("127.0.0.1", 19997, True, True, 5000, 5)  
KickBoV = 360
L_KickBoV = (math.pi/180)*KickBoV
R_KickBoV = -(math.pi/180)*KickBoV
if clientID!= -1:  
    print('connect successfully')
else:
    print('connect failed')
    vrep.simxFinish(clientID)
print('program ended')
    
    
    
    
err,Sphere_handle=vrep.simxGetObjectHandle(clientID,'Sphere',vrep.simx_opmode_oneshot_wait)
err,Pole_handle=vrep.simxGetObjectHandle(clientID,'Pole',vrep.simx_opmode_oneshot_wait)
err,Revolute_handle=vrep.simxGetObjectHandle(clientID,'Revolute',vrep.simx_opmode_oneshot_wait)
err,Prismatic_handle=vrep.simxGetObjectHandle(clientID,'Prismatic',vrep.simx_opmode_oneshot_wait)
err,Revolute2_handle=vrep.simxGetObjectHandle(clientID,'Revolute2',vrep.simx_opmode_oneshot_wait)
err,Prismatic2_handle=vrep.simxGetObjectHandle(clientID,'Prismatic2',vrep.simx_opmode_oneshot_wait)
err,Pole2_handle=vrep.simxGetObjectHandle(clientID,'Pole2',vrep.simx_opmode_oneshot_wait)
 


if err == -1:
    print('Can not find left or right motor')
    sys.exit()
    
    
    
def speed(handle,speed):
    err = vrep.simxSetJointTargetVelocity(clientID,handle,speed,vrep.simx_opmode_oneshot_wait)
def start():
    err=vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot_wait)
    while True:
        err,position_LR=vrep.simxGetObjectPosition(clientID,Revolute2_handle,-1,vrep.simx_opmode_oneshot)
        err,position_S=vrep.simxGetObjectPosition(clientID,Sphere_handle,-1,vrep.simx_opmode_oneshot)
        err,position_RR=vrep.simxGetObjectPosition(clientID,Revolute_handle,-1,vrep.simx_opmode_oneshot)
        LP =position_S[1] -position_LR[1] 
        LPP =position_S[0] - position_LR[0]
        print(position_S)
        if LPP >-0.015:
            speed(Revolute2_handle,L_KickBoV)
        elif LPP <= -0.015:
            speed(Revolute2_handle,R_KickBoV)
        else:
            pass
        try:
            if keyboard.is_pressed('r'):
                vrep.simxSetJointTargetVelocity(clientID,Revolute_handle,R_KickBoV,vrep.simx_opmode_oneshot_wait)
            elif keyboard.is_pressed('t'):
                vrep.simxSetJointTargetVelocity(clientID,Revolute_handle,L_KickBoV,vrep.simx_opmode_oneshot_wait)
            elif keyboard.is_pressed('q'):
                vrep.simxSetJointTargetVelocity(clientID,Prismatic_handle,0.1,vrep.simx_opmode_oneshot_wait)
            elif keyboard.is_pressed('w'):
                vrep.simxSetJointTargetVelocity(clientID,Prismatic_handle,0,vrep.simx_opmode_oneshot_wait)
            elif keyboard.is_pressed('e'):
                vrep.simxSetJointTargetVelocity(clientID,Prismatic_handle,-0.1,vrep.simx_opmode_oneshot_wait)
            else:
                pass
        except:
            break
        Pv = LP*2.5
        vrep.simxSetJointTargetVelocity(clientID,Prismatic2_handle,Pv,vrep.simx_opmode_oneshot_wait)
      
        
vrep.simxSetJointTargetVelocity(clientID,Prismatic_handle,0,vrep.simx_opmode_oneshot_wait)       
vrep.simxSetJointTargetVelocity(clientID,Prismatic2_handle,0,vrep.simx_opmode_oneshot_wait)

        
        

start()


手足球雙人鍵盤對打 << Previous Next >> 手足球電腦與電腦對打