Best Python code snippet using Airtest
turn_state.py
Source:turn_state.py
...12 self.utility_buttons = {}13 self.hovered_ability = None14 self.zone_indicator = pygame.image.load("resources/img/wbsprite.png").convert()15 self.zone_indicator.set_colorkey((0, 0, 0))16 self.ability_panel = pygame.Rect([director.screen.get_rect().midleft[0], director.screen.get_rect().midleft[1] + 280, 450, 200])17 self.name_panel = pygame.Rect([director.screen.get_rect().midleft[0], director.screen.get_rect().midleft[1] + 280, 225, 50])18 self.ap_panel = pygame.Rect([director.screen.get_rect().midleft[0] + 225, director.screen.get_rect().midleft[1] + 280, 225, 50])19 self.first_player_panel = pygame.Rect([director.screen.get_rect().midleft[0], director.screen.get_rect().midleft[1] - 150, 225, 125])20 self.second_player_panel = pygame.Rect([director.screen.get_rect().midleft[0], director.screen.get_rect().midleft[1] - 25, 225, 125])21 self.third_player_panel = pygame.Rect([director.screen.get_rect().midleft[0], director.screen.get_rect().midleft[1] + 100, 225, 125])22 self.first_enemy_panel = pygame.Rect([director.screen.get_rect().topright[0] - 225, director.screen.get_rect().topright[1], 225, 125])23 self.second_enemy_panel = pygame.Rect([director.screen.get_rect().topright[0] - 225, director.screen.get_rect().topright[1] + 125, 225, 125])24 self.third_enemy_panel = pygame.Rect([director.screen.get_rect().topright[0] - 225, director.screen.get_rect().topright[1] + 250, 225, 125])25 self.fourth_enemy_panel = pygame.Rect([director.screen.get_rect().topright[0] - 225, director.screen.get_rect().topright[1] + 375, 225, 125])26 self.player_info_panel = pygame.Rect([director.screen.get_rect().midleft[0], director.screen.get_rect().midleft[1] - 150, 225, 375])27 self.enemy_info_panel = pygame.Rect([director.screen.get_rect().topright[0] - 225, director.screen.get_rect().topright[1], 225, 475])28 self.font = pygame.font.Font("resources/font/PressStart2P-vaV7.ttf", 25)29 def enter(self):30 print("Entered Turn State")31 self.ability_buttons.clear()32 for i in range(len(self.scene.current_character.abilities)):33 self.ability_buttons[f"{i}"] = Button((0,0,70), self.director.screen.get_rect().midleft[0], self.director.screen.get_rect().midleft[1] + 330 + (i * 30), 120, 35, self.scene.current_character.abilities[i])34 self.utility_buttons[1] = Button((0,0,70), self.director.screen.get_rect().midleft[0] + 120, self.director.screen.get_rect().midleft[1] + 330, 120, 35, self.scene.director.wait)35 36 def exit(self):37 print("Exited Turn State")38 pass39 def update(self):40 for btn in self.ability_buttons.values():41 if btn.isOver((self.scene.x_world, self.scene.y_world)):42 self.hovered_ability = btn.ability43 for btn in self.utility_buttons.values():44 if btn.isOver((self.scene.x_world, self.scene.y_world)):45 self.hovered_ability = btn.ability46 def render(self):47 pygame.draw.rect(self.director.screen,(100, 100, 100), self.ability_panel)48 pygame.draw.rect(self.director.screen,(100, 100, 100), self.player_info_panel)49 pygame.draw.rect(self.director.screen,(100, 100, 100), self.enemy_info_panel)50 pygame.draw.rect(self.director.screen,(0, 0, 0), self.first_player_panel, 3)51 pygame.draw.rect(self.director.screen,(0, 0, 0), self.second_player_panel, 3)52 pygame.draw.rect(self.director.screen,(0, 0, 0), self.third_player_panel, 3)53 pygame.draw.rect(self.director.screen,(0, 0, 0), self.first_enemy_panel, 3)54 pygame.draw.rect(self.director.screen,(0, 0, 0), self.second_enemy_panel, 3)55 pygame.draw.rect(self.director.screen,(0, 0, 0), self.third_enemy_panel, 3)56 pygame.draw.rect(self.director.screen,(0, 0, 0), self.fourth_enemy_panel, 3)57 pygame.draw.rect(self.director.screen,(0, 0, 0), self.name_panel, 3)58 pygame.draw.rect(self.director.screen,(0, 0, 0), self.ap_panel, 3)59 #Blit text into player info boxes60 ui_module.draw_character_info(self.director.screen, self.font, self.scene.group_manager.player_party, (255, 255, 255), self.director.screen.get_rect().midleft[0], self.director.screen.get_rect().midleft[1] - 130, 5, 120, 25)61 #Blit text into enemy info boxes62 ui_module.draw_character_info(self.director.screen,self.font, self.scene.group_manager.enemy_party, (255, 255, 255), self.director.screen.get_rect().topright[0], self.director.screen.get_rect().topright[1], -220, 125, 25)63 64 self.director.screen.blit(self.font.render(self.scene.current_character.name, True, (255, 255, 255)), (self.director.screen.get_rect().midleft[0], self.director.screen.get_rect().midleft[1] + 290))65 self.director.screen.blit(self.font.render(f"AP: {self.scene.current_character.action_points}", True, (255, 255, 255)), (self.director.screen.get_rect().midleft[0] + 230, self.director.screen.get_rect().midleft[1] + 290))66 for btn in self.ability_buttons.values():67 btn.draw(self.director.screen)68 for btn in self.utility_buttons.values():69 btn.draw(self.director.screen)...
Enemy.py
Source:Enemy.py
...10 self.font = pygame.font.Font(None, 30)11 self.ship = self.font.render("o", True, self.color)12 self.speed = 5013 self.position = Position()14 self.position.Y = - self.ship.get_rect().height / 215 self.position.X = random.randint(16 self.ship.get_rect().width / 2,17 self.screen.get_rect().width - self.ship.get_rect().width18 )19 self.startTime = time.time()20 self.player = player21 self.shotTime = time.time()22 def draw(self):23 self.position.Y += self.speed * (time.time() - self.startTime)24 self.screen.blit(25 self.ship,26 (self.position.X - self.ship.get_rect().width / 2, self.position.Y - self.ship.get_rect().height / 2)27 )28 self.startTime = time.time()29 def pause(self):30 self.screen.blit(31 self.ship,32 (self.position.X - self.ship.get_rect().width / 2, self.position.Y - self.ship.get_rect().height / 2)33 )34 self.startTime = time.time()35 def check_valid(self):36 if self.position.Y >= self.screen.get_rect().height + self.ship.get_rect().height / 2:37 return False38 else:39 return True40 def check_crash(self, bullet):41 if bullet.position.Y <= self.position.Y + self.ship.get_rect().height / 2 + 3:42 if bullet.position.Y >= self.position.Y - self.ship.get_rect().height / 2 - 3:43 if bullet.position.X <= self.position.X + self.ship.get_rect().width / 2 + 3:44 if bullet.position.X >= self.position.X - self.ship.get_rect().width / 2 - 3:45 return True46 else:47 return False48 def will_shot(self):49 return self.player.get_position().X - 0.5 <= self.position.X <= self.player.get_position().X + 0.550 def is_crash_with_player(self):51 left = self.player.get_position().X - self.player.ship_width / 2 - self.ship.get_rect().width / 2 - 152 right = self.player.get_position().X + self.player.ship_width / 2 + self.ship.get_rect().width / 2 + 153 up = self.player.get_position().Y - self.player.ship_height / 2 - self.ship.get_rect().width / 2 - 154 down = self.player.get_position().Y + self.player.ship_height / 2 + self.ship.get_rect().height / 2 + 155 if left <= self.position.X <= right:56 if up <= self.position.Y <= down:57 return True58 else:59 return False60 def get_position(self):61 return self.position62 def is_permitted_to_shot(self):63 if time.time() - self.shotTime >= 1:64 self.shotTime = time.time()65 return True66 else:...
Settings.py
Source:Settings.py
...3 pygame.init()4 screen = pygame.display.get_surface() 5 screen = pygame.display.set_mode((800, 600), pygame.RESIZABLE) 6 level = pygame.image.load("background/back2.jpg").convert() 7 levelRect = level.get_rect(center=(400, 300)) 8 myfont = pygame.font.Font("fonts/moonhouse.ttf", 60) 9 myfonta = pygame.font.Font(None, 30) 10 myfontd = pygame.font.Font(None, 30) 11 myfonts = pygame.font.Font(None, 30) 12 myfontj = pygame.font.Font(None, 30) 13 winner = "How to play" 14 a = 'A - This key moves your player left'15 d = 'D - This key moves your player right'16 space = 'Spacebar - This key makes your character jump'17 j = 'J - This key makes your character shoot'18 label = myfont.render(winner, 1, (0, 255, 0))19 labela = myfonta.render(a, 1, (255, 255, 0))20 labeld = myfontd.render(d, 1, (255, 255, 0))21 labels = myfonts.render(space, 1, (255, 255, 0))22 labelj = myfontj.render(j, 1, (255, 255, 0))23 textpos = label.get_rect() 24 textposa = labela.get_rect() 25 textposd = labeld.get_rect() 26 textposs = labels.get_rect() 27 textposj = labelj.get_rect() 28 textpos.centerx = level.get_rect().centerx 29 textposa.centerx = level.get_rect().centerx 30 textposd.centerx = level.get_rect().centerx 31 textposs.centerx = level.get_rect().centerx 32 textposj.centerx = level.get_rect().centerx 33 textposa.centery = 20034 textposd.centery = 25035 textposs.centery = 30036 textposj.centery = 35037 while 1: 38 for event in pygame.event.get(): 39 if event.type == pygame.KEYDOWN: 40 keypressed = pygame.key.name(event.key) 41 if keypressed == pygame.key.name(pygame.K_ESCAPE): 42 Menu.main() 43 sys.exit(0) 44 screen.blit(level, (0,0)) 45 screen.blit(label, textpos)46 screen.blit(labela, textposa)...
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!!