Best Python code snippet using Airtest
snake.py
Source:snake.py
...173 self.pause_x = cell_num * cell_size - 45174 self.pause_y = 20175 self.pause_rect = self.pause_surface.get_rect(center = (self.pause_x, self.pause_y))176 177 def check_touch(self, x, y):178 if self.pause_rect.left <= x <= self.pause_rect.right and self.pause_rect.top <= y <= self.pause_rect.bottom:179 return True180 return False181182 def draw_button(self, touch):183 if touch:184 color = (167, 70, 61)185 else:186 color = (167, 209, 61)187 pygame.draw.rect(screen, color, self.pause_rect)188 screen.blit(self.pause_surface, self.pause_rect)189 pygame.draw.rect(screen, (56, 74, 12), self.pause_rect, 2)190191class Mute():192 def __init__(self):193 self.text = 'MUTE'194 self.mute_font = pygame.font.Font('Font\\Shadow of the Deads.ttf', 15)195 self.mute_surface = self.mute_font.render(self.text, True, (56, 74, 12))196 self.mute_x = 10197 self.mute_y = 10198 self.mute_rect = self.mute_surface.get_rect(topleft = (self.mute_x, self.mute_y))199 200 def check_touch(self, x, y):201 if self.mute_rect.left <= x <= self.mute_rect.right and self.mute_rect.top <= y <= self.mute_rect.bottom:202 return True203 return False204 205 def draw_button(self, touch):206 if touch:207 color = (167, 70, 61)208 else:209 color = (167, 209, 61)210 pygame.draw.rect(screen, color, self.mute_rect)211 screen.blit(self.mute_surface, self.mute_rect)212 pygame.draw.rect(screen, (56, 74, 12), self.mute_rect, 2)213214class Resume():215 def __init__(self):216 self.text = 'RESUME'217 self.resume_font = pygame.font.Font('Font\\Shadow of the Deads.ttf', 50)218 self.resume_surface = self.resume_font.render(self.text, True, (100, 20, 80))219 self.resume_x = 300220 self.resume_y = 300221 self.resume_rect = self.resume_surface.get_rect(center = (self.resume_x, self.resume_y))222223 def check_touch(self, x, y):224 if self.resume_rect.left <= x <= self.resume_rect.right and self.resume_rect.top <= y <= self.resume_rect.bottom:225 return True226 return False227 228 def draw_button(self, touch):229 if touch:230 self.resume_surface = self.resume_font.render(self.text, True, (200, 200, 180))231 else:232 self.resume_surface = self.resume_font.render(self.text, True, (100, 20, 80))233 screen.blit(self.resume_surface, self.resume_rect)234235class Start():236 def __init__(self):237 self.text = 'GO!'238 self.start_font = pygame.font.Font('Font\\Shadow of the Deads.ttf', 30)239 self.start_surface = self.start_font.render(self.text, True, (100, 20, 80))240 self.start_x = 300241 self.start_y = 150242 self.start_rect = self.start_surface.get_rect(center = (self.start_x, self.start_y))243244 def check_touch(self, x, y):245 if self.start_rect.left <= x <= self.start_rect.right and self.start_rect.top <= y <= self.start_rect.bottom:246 return True247 return False248 249 def draw_button(self, touch):250 if touch:251 self.start_surface = self.start_font.render(self.text, True, (200, 200, 180))252 else:253 self.start_surface = self.start_font.render(self.text, True, (100, 20, 80))254 screen.blit(self.start_surface, self.start_rect)255256class Music():257 def __init__(self):258 self.text = 'MUSIC'259 self.music_font = pygame.font.Font('Font\\Shadow of the Deads.ttf', 30)260 self.music_surface = self.music_font.render(self.text, True, (100, 20, 80))261 self.music_x = 300262 self.music_y = 225263 self.music_rect = self.music_surface.get_rect(center = (self.music_x, self.music_y))264265 def check_touch(self, x, y):266 if self.music_rect.left <= x <= self.music_rect.right and self.music_rect.top <= y <= self.music_rect.bottom:267 return True268 return False269 270 def draw_button(self, touch):271 if touch:272 self.music_surface = self.music_font.render(self.text, True, (200, 200, 180))273 else:274 self.music_surface = self.music_font.render(self.text, True, (100, 20, 80))275 screen.blit(self.music_surface, self.music_rect)276277class Song():278 def __init__(self, song_name, x):279 song_name = song_name.upper()280 self.text = song_name[0:len(song_name) - 4]281 self.song_font = pygame.font.Font('Font\\Shadow of the Deads.ttf', 15)282 self.song_surface = self.song_font.render(self.text, True, (100, 20, 80))283 self.song_x = 300284 self.song_y = 150 + 50 * x285 self.song_rect = self.song_surface.get_rect(center = (self.song_x, self.song_y))286287 def check_touch(self, x, y):288 if self.song_rect.left <= x <= self.song_rect.right and self.song_rect.top <= y <= self.song_rect.bottom:289 return True290 return False291 292 def draw_button(self, touch):293 if touch:294 self.song_surface = self.song_font.render(self.text, True, (200, 200, 180))295 else:296 self.song_surface = self.song_font.render(self.text, True, (100, 20, 80))297 screen.blit(self.song_surface, self.song_rect)298299class Back():300 def __init__(self):301 self.text = 'GO BACK'302 self.back_font = pygame.font.Font('Font\\Shadow of the Deads.ttf', 30)303 self.back_surface = self.back_font.render(self.text, True, (100, 20, 80))304 self.back_x = 300305 self.back_y = 500306 self.back_rect = self.back_surface.get_rect(center = (self.back_x, self.back_y))307308 def check_touch(self, x, y):309 if self.back_rect.left <= x <= self.back_rect.right and self.back_rect.top <= y <= self.back_rect.bottom:310 return True311 return False312 313 def draw_button(self, touch):314 if touch:315 self.back_surface = self.back_font.render(self.text, True, (200, 200, 180))316 else:317 self.back_surface = self.back_font.render(self.text, True, (100, 20, 80))318 screen.blit(self.back_surface, self.back_rect)319320class Quit():321 def __init__(self):322 self.text = 'QUIT'323 self.quit_font = pygame.font.Font('Font\\Shadow of the Deads.ttf', 30)324 self.quit_surface = self.quit_font.render(self.text, True, (100, 20, 80))325 self.quit_x = 300326 self.quit_y = 300327 self.quit_rect = self.quit_surface.get_rect(center = (self.quit_x, self.quit_y))328329 def check_touch(self, x, y):330 if self.quit_rect.left <= x <= self.quit_rect.right and self.quit_rect.top <= y <= self.quit_rect.bottom:331 return True332 return False333 334 def draw_button(self, touch):335 if touch:336 self.quit_surface = self.quit_font.render(self.text, True, (200, 200, 180))337 else:338 self.quit_surface = self.quit_font.render(self.text, True, (100, 20, 80))339 screen.blit(self.quit_surface, self.quit_rect) 340 341342cell_size = 30343cell_num = 20344345pygame.mixer.pre_init(44100, -16, 2, 512)346pygame.init()347screen = pygame.display.set_mode((cell_size * cell_num, cell_size * cell_num))348clock = pygame.time.Clock()349350pygame.mixer.music.load('Music\\punch deck brazilian street fight.mp3')351pygame.mixer.music.play(-1)352353apple = pygame.image.load('Graphics\\Apple\\apple.png').convert_alpha()354apple = pygame.transform.scale(apple, (cell_size, cell_size))355356game_font = pygame.font.Font('Font\\Shadow of the Deads.ttf', 25)357358game_obj = Game()359pause_button = Pause()360mute_button = Mute()361resume_button = Resume()362start_button = Start()363music_button = Music()364back_button = Back()365quit_button = Quit()366playlist = []367for index, songfile in enumerate(os.listdir('Music')):368 playlist.append(Song(songfile, index))369370SCREEN_UPDATE = pygame.USEREVENT371pygame.time.set_timer(SCREEN_UPDATE, 150)372373game_pause = True374game_mute = False375music_button_pressed = False376377while True:378 chosen_song = -1379 for event in pygame.event.get():380 if event.type == pygame.QUIT:381 pygame.quit()382 sys.exit()383 if game_obj.game_on:384 if event.type == SCREEN_UPDATE and game_pause == False:385 game_obj.update()386 if event.type == pygame.KEYDOWN:387 if event.key == pygame.K_UP and game_obj.snake.direction != Vector2(0, 1):388 game_obj.snake.direction = Vector2(0, -1) 389 game_pause = False390 if event.key == pygame.K_RIGHT and game_obj.snake.direction != Vector2(-1, 0):391 game_obj.snake.direction = Vector2(1, 0)392 game_pause = False393 if event.key == pygame.K_DOWN and game_obj.snake.direction != Vector2(0, -1):394 game_obj.snake.direction = Vector2(0, 1)395 game_pause = False396 if event.key == pygame.K_LEFT and game_obj.snake.direction != Vector2(1, 0):397 game_obj.snake.direction = Vector2(-1, 0)398 game_pause = False399 if event.key == pygame.K_p:400 game_pause = not game_pause401 if event.key == pygame.K_m:402 game_mute = not game_mute403 if game_mute:404 pygame.mixer.music.pause()405 else:406 pygame.mixer.music.unpause()407 if event.type == pygame.MOUSEBUTTONDOWN:408 if pause_button.check_touch(mouse[0], mouse[1]):409 game_pause = not game_pause410 if resume_button.check_touch(mouse[0], mouse[1]):411 game_pause = False412 if mute_button.check_touch(mouse[0], mouse[1]):413 game_mute = not game_mute414 if game_mute:415 pygame.mixer.music.pause()416 else:417 pygame.mixer.music.unpause()418 else:419 if event.type == pygame.MOUSEBUTTONDOWN:420 if not music_button_pressed:421 if start_button.check_touch(mouse[0], mouse[1]):422 game_obj.game_on = True423 game_pause = False424 if mute_button.check_touch(mouse[0], mouse[1]):425 game_mute = not game_mute426 if game_mute:427 pygame.mixer.music.pause()428 else:429 pygame.mixer.music.unpause()430 if music_button.check_touch(mouse[0], mouse[1]):431 music_button_pressed = True 432 if quit_button.check_touch(mouse[0], mouse[1]):433 pygame.quit()434 sys.exit()435 else:436 for index, song_button in enumerate(playlist): 437 if song_button.check_touch(mouse[0], mouse[1]):438 chosen_song = index439 break440 if back_button.check_touch(mouse[0], mouse[1]):441 music_button_pressed = False442 443 if music_button_pressed and chosen_song != -1:444 pygame.mixer.music.unload()445 pygame.mixer.music.load('Music\\' + os.listdir('Music')[index])446 pygame.mixer.music.play(-1)447448 mouse = pygame.mouse.get_pos()449 screen.fill((175, 215, 70))450 game_obj.draw_elements()451 pause_button.draw_button(pause_button.check_touch(mouse[0], mouse[1]))452 mute_button.draw_button(mute_button.check_touch(mouse[0], mouse[1]))453 if game_pause and game_obj.game_on:454 resume_button.draw_button(resume_button.check_touch(mouse[0], mouse[1]))455 elif not game_obj.game_on:456 if not music_button_pressed:457 start_button.draw_button(start_button.check_touch(mouse[0], mouse[1]))458 music_button.draw_button(music_button.check_touch(mouse[0], mouse[1]))459 quit_button.draw_button(quit_button.check_touch(mouse[0], mouse[1]))460 else:461 back_button.draw_button(back_button.check_touch(mouse[0], mouse[1]))462 for song_button in playlist:463 song_button.draw_button(song_button.check_touch(mouse[0], mouse[1]))464 pygame.display.update()
...
touch_main.py
Source:touch_main.py
...27 return head_touch_pin, body_touch_pin28def cleanup_touch(head_touch_pin, body_touch_pin):29 GPIO.cleanup(head_touch_pin)30 GPIO.cleanup(body_touch_pin)31def check_touch(head_touch_pin, body_touch_pin):32 head_touch = GPIO.input(head_touch_pin)33 body_touch = GPIO.input(body_touch_pin)34 return head_touch, body_touch35def main():36 # setup pins37 body_servo, tail_servo = tail_servo_main.setup_tail(dataCenter.body_pin, dataCenter.tail_pin)38 head_touch_pin, body_touch_in = setup_touch(dataCenter.head_touch_pin, dataCenter.body_touch_pin)39 # body_servo, tail_servo = tail_servo_main.setup_tail(36, 38)40 # head_touch_pin, body_touch_pin = setup_touch(11, 13)41 while True:42 global touch_count43 global prev_head_touch, prev_body_touch44 head_touch, body_touch = check_touch(dataCenter.head_touch_pin, dataCenter.body_touch_pin)45 # head_touch, body_touch = check_touch(11, 13)46 #check_touch(input_head, input_body)47 if (prev_head_touch and (not prev_body_touch)) and body_touch:48 print("head to body")49 # touch_count += 150 # shake tail51 #tail_servo_main.shake_tail(body_servo, tail_servo)52 elif (prev_body_touch and (not prev_head_touch)) and head_touch:53 print("body to head")54 # touch_count += 1 55 elif (not prev_head_touch) and head_touch:56 print("only head")57 elif (not prev_body_touch) and body_touch:58 print("only body")59 tail_servo_main.shake_tail(body_servo, tail_servo)60 touch_count += 1...
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!!