Best Python code snippet using robotframework-appiumlibrary_python
ManualControl.py
Source:ManualControl.py
1# -*- coding: utf-8 -*-2"""3Created on Tue Nov 17 23:35:59 202045@author: CUHKSZ67ip: 192.168.3.38port: 809"""1011import pygame12import sys13import time14import copy15from OASES import OASES16from UDPserver import DockingSystem1718# create the boat19USV = OASES()20print("Init boat.")2122# create the docking system23DS = DockingSystem('BOAT_1', 1)24time.sleep(5)2526# initial pygame27pygame.display.set_caption("PyGame")28pygame.display.set_mode((640, 480))29pygame.init()30print("Init Pygame.")3132# catch keyboard event33Flag = True34long_press = {'W': False, 'S': False, 'A': False, 'D': False, 35 'I': False, 'J': False, 'K': False, 'L': False} # For movement and action36long_press_last = copy.copy(long_press) # remember the last time37count = 038reset_waittime = 103940while Flag:41 42 for event in pygame.event.get():43 # if event.type == QUIT:44 # sys.exit()45 #print("event get")4647 # press key48 if event.type == pygame.KEYDOWN:49 # quit50 # print("key down, key: ", event.key == pygame.K_w)51 if event.key == pygame.K_q:52 print("Quit manual control.")53 Flag = False54 USV.stop()55 pygame.quit()56 sys.exit()57 break58 59 if event.key == pygame.K_u:60 print("Update servo motor position.")61 USV.update_servo_position()62 63 if event.key == pygame.K_w:64 long_press['W'] = True65 if event.key == pygame.K_a:66 long_press['A'] = True67 if event.key == pygame.K_s:68 long_press['S'] = True69 if event.key == pygame.K_d:70 long_press['D'] = True71 if event.key == pygame.K_i:72 long_press['I'] = True73 if event.key == pygame.K_j:74 long_press['J'] = True75 if event.key == pygame.K_k:76 long_press['K'] = True77 if event.key == pygame.K_l:78 long_press['L'] = True79 if event.key == pygame.K_r:80 USV.reset()81 print("Reset boat.")82 if event.key == pygame.K_y:83 DS.ON('BOAT_1_DOCK_1')84 DS.ON('BOAT_1_DOCK_1')85 if event.key == pygame.K_n:86 DS.OFF('BOAT_1_DOCK_1')87 DS.OFF('BOAT_1_DOCK_1')88 # release key89 elif event.type == pygame.KEYUP:90 # print("key up, key: ", event.key == pygame.K_w)91 if event.key == pygame.K_w:92 long_press['W'] = False93 if event.key == pygame.K_a:94 long_press['A'] = False95 if event.key == pygame.K_s:96 long_press['S'] = False97 if event.key == pygame.K_d:98 long_press['D'] = False99 if event.key == pygame.K_i:100 long_press['I'] = False101 if event.key == pygame.K_j:102 long_press['J'] = False103 if event.key == pygame.K_k:104 long_press['K'] = False105 if event.key == pygame.K_l:106 long_press['L'] = False107 108 # handle the keyboard event109 if long_press['W'] == True and long_press_last['W'] == False: 110 USV.forward()111 print("Forward.")112 if long_press["S"] == True and long_press_last['S'] == False:113 USV.backward()114 print("Backward.")115 if long_press["A"] == True and long_press_last['A'] == False:116 USV.leftward()117 print("Leftward.")118 if long_press["D"] == True and long_press_last['D'] == False:119 USV.rightward()120 print("Rightward.")121 122 if long_press['J'] == True and long_press_last['J'] == False:123 USV.turnleft()124 print("Turn left.")125 if long_press["L"] == True and long_press_last['L'] == False:126 USV.turnright()127 print("Turn right.")128129 if long_press["I"] == True and long_press_last['I'] == False:130 USV.extend()131 print("Extend.")132 if long_press["K"] == True and long_press_last['K'] == False:133 USV.contract()134 print("Contract.")135 136 # no key pressed, wait for several rounds 137 if all(value == 0 for value in long_press.values()):138 if long_press != long_press_last:139 count += 1140 #print(long_press)141 #print(long_press_last)142 if count > reset_waittime:143 USV.stop()144 print("Reset boat.")145 count = 0146 long_press_last = copy.copy(long_press)147 148 else:149 long_press_last = copy.copy(long_press)150 151 #pygame.time.delay(0) # delay 10ms
...
obj.py
Source:obj.py
1# -*- coding: utf-8 -*-2"""3Created on Sun Dec 12 13:12:42 20214@author: 785285"""6import pygame7import tools8from constant import w, h9class obj(pygame.sprite.Sprite):10 size = w /16/1611 # speed12 speed_x = 013 speed_y = 014 def __init__(self,x,y,pic_group,pic):15 super().__init__()16 self.PIC = tools.load_pictures(pic_group)17 self.pic_count = 018 self.x, self.y = x, y19 self.long_press = {'up': False, 'down': False, 'right': False, 'left': False} 20 self.speed = w/16/3221 self.image = tools.get_image(self.PIC[pic],0 , 0, 1920, 1280, (255,255,255), self.size)22 self.rect = pygame.Rect(self.x, self.y, self.image.get_width(), self.image.get_height())23 24 def read_event(self, event):25 if event.type == pygame.KEYDOWN:26 if event.key == pygame.K_w:27 self.long_press['up'] = True28 if event.key == pygame.K_s:29 self.long_press['down'] = True30 if event.key == pygame.K_a:31 self.long_press['left'] = True32 33 if event.key == pygame.K_d:34 self.long_press['right'] =True35 36 if event.type == pygame.KEYUP:37 if event.key == pygame.K_w:38 self.long_press['up'] = False39 40 if event.key == pygame.K_s:41 self.long_press['down'] = False42 43 if event.key == pygame.K_d:44 self.long_press['right'] = False45 46 if event.key == pygame.K_a:47 self.long_press['left'] = False48 self.speed_x, self.speed_y = 0, 049 50 if self.long_press['up'] == True:51 self.speed_y = self.speed * 1.252 if self.long_press['down'] == True:53 self.speed_y = -self.speed * 1.254 if self.long_press['right'] == True:55 self.speed_x = -self.speed * 1.256 if self.long_press['left'] == True:57 self.speed_x = self.speed * 1.258 59 def update_pos(self):60 self.x += self.speed_x61 self.y += self.speed_y62 63 64 def undo_update_pos(self):65 self.x -= self.speed_x66 self.y -= self.speed_y67 68 def update(self,pos1):69 70 self.update_pos()71 if not pos1[0] == []:72 if (tools.caculate_distance(pos1[1],self.rect.center)) > (tools.caculate_distance(pos1[1],(self.x+self.rect.width/2,self.y + self.rect.height/2))):73 74 self.undo_update_pos()75 self.rect.x = self.x...
platformtrigger.py
Source:platformtrigger.py
...39 def continuous_callback(self):40 if not self._platform_continuous_callback:41 return False42 return self._platform_continuous_callback()43 def long_press(self):44 start_time = time.time()45 while self._platform_continuous_callback():46 if (time.time() - start_time > self._tconfig['long_press']['duration']):47 if ('audio_file' in self._tconfig['long_press']) and self._tconfig['long_press']['audio_file']:48 pass49 # play_audio(self._pconfig['long_press']['audio_file'].replace('{resources_path}', resources_path))50 logger.info("-- " + str(self._tconfig['long_press']['duration']) + " second button press detected. Running specified command.")51 os.system(self._tconfig['long_press']['command'])52 break53 time.sleep(.1)54 def enable(self):55 self._enabled = True56 def disable(self):57 self._enabled = False
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!