Best Python code snippet using molecule_python
main.py
Source:main.py
12import pygame, sys, time, random345# Window size6frame_size_x = 7207frame_size_y = 48089# Difficulty settings :10 # Basic -> 1311 # Easy -> 1812 # Medium -> 2513 # Hard -> 3014 # Pro -> 3815difficulty_level_select = [13,18,24,30,38]16difficulty=151718score = 01920# Colours (R, G, B)21black = pygame.Color(0, 0, 0)22white = pygame.Color(255, 255, 255)23red = pygame.Color(255, 0, 0)24green = pygame.Color(0, 255, 0)25blue = pygame.Color(0, 0, 255)2627# Initialise game window28game_window = pygame.display.set_mode((frame_size_x, frame_size_y))293031# Game Over32def game_over():33 my_font = pygame.font.SysFont('times new roman', 90)34 game_over_surface = my_font.render('YOU DIED', True, red)35 game_over_rectangle = game_over_surface.get_rect()36 game_over_rectangle.midtop = (frame_size_x/2, frame_size_y/4)37 game_window.fill(black)38 game_window.blit(game_over_surface, game_over_rectangle)39 show_score(1, red, 'times', 45)40 pygame.display.flip()41 time.sleep(1.75)42 pygame.quit()43 sys.exit()444546# Score47def show_score(out, colour, font, size):48 score_font = pygame.font.SysFont(font, size)49 score_surface = score_font.render('Score : ' + str(score), True, colour)50 score_rectangle = score_surface.get_rect()51 if out == 0:52 score_rectangle.midtop = (frame_size_x/10, 15)53 else:54 score_rectangle.midtop = (frame_size_x/2, frame_size_y/1.8)55 game_window.blit(score_surface, score_rectangle)565758def mainGame():59 global difficulty60 global score61 # Game variables62 snake_position = [100, 50]63 snake_body = [[100, 50], [100-10, 50], [100-(2*10), 50]]6465 food_position = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]66 food_spawn = True6768 direction = 'RIGHT'69 change_dir_to = direction7071 # Main logic72 while True:73 for action in pygame.event.get():74 #If user clicks on cross button or press escape, close the game75 if action.type == pygame.QUIT or (action.type == pygame.KEYDOWN and action.key == pygame.K_ESCAPE):76 pygame.quit()77 sys.exit()78 # Whenever a key is pressed down79 elif action.type == pygame.KEYDOWN:80 # W -> Up; S -> Down; A -> Left; D -> Right81 if action.key == pygame.K_UP or action.key == ord('w'):82 change_dir_to = 'UP'83 if action.key == pygame.K_DOWN or action.key == ord('s'):84 change_dir_to = 'DOWN'85 if action.key == pygame.K_LEFT or action.key == ord('a'):86 change_dir_to = 'LEFT'87 if action.key == pygame.K_RIGHT or action.key == ord('d'):88 change_dir_to = 'RIGHT'8990 # Making sure the snake cannot move in the opposite direction instantaneously91 if change_dir_to == 'UP' and direction != 'DOWN':92 direction = 'UP'93 if change_dir_to == 'DOWN' and direction != 'UP':94 direction = 'DOWN'95 if change_dir_to == 'LEFT' and direction != 'RIGHT':96 direction = 'LEFT'97 if change_dir_to == 'RIGHT' and direction != 'LEFT':98 direction = 'RIGHT'99100 # Changing the direction of snake101 if direction == 'UP':102 snake_position[1] -= 10103 if direction == 'DOWN':104 snake_position[1] += 10105 if direction == 'LEFT':106 snake_position[0] -= 10107 if direction == 'RIGHT':108 snake_position[0] += 10109110 # Snake body growing mechanism and increasing slight difficulty after every 4 scores111 snake_body.insert(0, list(snake_position))112 if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:113 score += 1114 food_spawn = False115 if int(score%4) == 0:116 difficulty += 1117 else:118 snake_body.pop()119120 # Spawning food on the screen121 if not food_spawn:122 food_position = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]123 food_spawn = True124125 # Showing snake on the screen126 game_window.fill(black)127 for pos in snake_body:128 pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))129 # .draw.rect(play_surface, colour, xy-coordinate)130 # xy-coordinate -> .Rect(x, y, size_x, size_y)131132 # Snake food133 pygame.draw.rect(game_window, blue, pygame.Rect(food_position[0], food_position[1], 10, 10))134135 # Game Over conditions :136137 # Getting out of bounds138 if snake_position[0] < 0 or snake_position[0] > frame_size_x-10:139 game_over()140 if snake_position[1] < 0 or snake_position[1] > frame_size_y-10:141 game_over()142 # Touching the snake body143 for block in snake_body[1:]:144 if snake_position[0] == block[0] and snake_position[1] == block[1]:145 game_over()146147 show_score(0, white, 'consolas', 20)148 # Refresh game screen149 pygame.display.update()150 151 # Refresh rate152 fps_controller.tick(difficulty)153154155def welcome_screen():156157 global difficulty158 while True:159160 welcome_font = pygame.font.SysFont('times new roman', 75)161 welcome_surface = welcome_font.render('SNAKE GAME', True, pygame.Color(229,9,20))162 welcome_rectangle = welcome_surface.get_rect()163 welcome_rectangle.midtop = (frame_size_x/2, frame_size_y/6.7)164 165 choose_font = pygame.font.SysFont('times new roman', 40)166 choose_surface = choose_font.render('select difficulty level by typing :', True, white)167 choose_rectangle = choose_surface.get_rect()168 choose_rectangle.midtop = (frame_size_x/2, frame_size_y/2.4)169170 difficulty_font = pygame.font.SysFont('times new roman', 25)171 d1_surface = difficulty_font.render('1 for BASIC', True, white)172 d1_rectangle = d1_surface.get_rect()173 d1_rectangle.midtop = (frame_size_x/2, frame_size_y/1.88)174 d2_surface = difficulty_font.render('2 for EASY', True, white)175 d2_rectangle = d2_surface.get_rect()176 d2_rectangle.midtop = (frame_size_x/2, frame_size_y/1.7)177 d3_surface = difficulty_font.render('3 for MEDIUM', True, white)178 d3_rectangle = d3_surface.get_rect()179 d3_rectangle.midtop = (frame_size_x/2, frame_size_y/1.55)180 d4_surface = difficulty_font.render('4 for HARD', True, white)181 d4_rectangle = d4_surface.get_rect()182 d4_rectangle.midtop = (frame_size_x/2, frame_size_y/1.42)183 d5_surface = difficulty_font.render('5 for PRO', True, white)184 d5_rectangle = d5_surface.get_rect()185 d5_rectangle.midtop = (frame_size_x/2, frame_size_y/1.31)186187 game_window.fill(black)188 game_window.blit(welcome_surface, welcome_rectangle)189 game_window.blit(choose_surface, choose_rectangle)190 game_window.blit(d1_surface, d1_rectangle)191 game_window.blit(d2_surface, d2_rectangle)192 game_window.blit(d3_surface, d3_rectangle)193 game_window.blit(d4_surface, d4_rectangle)194 game_window.blit(d5_surface, d5_rectangle)195196 for action in pygame.event.get():197 #If user clicks on cross button or press escape, close the game198 if action.type == pygame.QUIT or (action.type == pygame.KEYDOWN and action.key == pygame.K_ESCAPE):199 pygame.quit()200 sys.exit()201 # 202 elif action.type == pygame.KEYDOWN:203 # user clicks 1,2,3,4 or 5 for choosing difficulty level204 if action.key == pygame.K_1:205 difficulty=difficulty_level_select[0]206 time.sleep(1)207 mainGame()208 if action.key == pygame.K_2:209 difficulty=difficulty_level_select[1]210 time.sleep(1)211 mainGame()212 if action.key == pygame.K_3:213 difficulty=difficulty_level_select[2]214 time.sleep(1)215 mainGame()216 if action.key == pygame.K_4:217 difficulty=difficulty_level_select[3]218 time.sleep(1)219 mainGame()220 if action.key == pygame.K_5:221 difficulty=difficulty_level_select[4]222 time.sleep(1)223 mainGame()224 225 # Refresh screen226 pygame.display.update()227 # Refresh rate228 fps_controller.tick(difficulty)229230231232if __name__ =="__main__":233234 #initialize all pygame's modules235 pygame.init() 236 237 pygame.display.set_caption('Snake Game by Aviral')238 print("Game successfully initialised")239240 # FPS (frames per second) controller241 fps_controller = pygame.time.Clock()242243 while True:244245 welcome_screen()
...
conftest.py
Source:conftest.py
...20 os.path.pardir,21 "scenarios",22 scenario_to_test,23 )24 with change_dir_to(scenario_directory):25 yield26 if scenario_name:27 msg = "CLEANUP: Destroying instances for all scenario(s)"28 LOG.info(msg)29 cmd = ["molecule", "destroy", "--driver-name", driver_name, "--all"]30 pytest.helpers.run_command(cmd)31@pytest.fixture32def skip_test(request, driver_name):33 msg_tmpl = (34 "Ignoring '{}' tests for now"35 if driver_name == "delegated"36 else "Skipped '{}' not supported"37 )38 support_checks_map = {39 "hetznercloud": lambda: min_ansible("2.8") and supports_hetznercloud()40 }41 try:42 check_func = support_checks_map[driver_name]43 if not check_func():44 pytest.skip(msg_tmpl.format(driver_name))45 except KeyError:46 pass47@pytest.helpers.register48def idempotence(scenario_name):49 cmd = ["molecule", "create", "--scenario-name", scenario_name]50 pytest.helpers.run_command(cmd)51 cmd = ["molecule", "converge", "--scenario_name", scenario_name]52 pytest.helpers.run_command(cmd)53 cmd = ["molecule", "--scenario_name", scenario_name]54 pytest.helpers.run_command(cmd)55@pytest.helpers.register56def init_role(temp_dir, driver_name):57 cmd = ["molecule", "init", "role", "test-init", "--driver-name", driver_name]58 pytest.helpers.run_command(cmd)59 role_directory = os.path.join(temp_dir.strpath, "test-init")60 pytest.helpers.metadata_lint_update(role_directory)61 with change_dir_to(role_directory):62 cmd = ["molecule", "test", "--all"]63 pytest.helpers.run_command(cmd)64@pytest.helpers.register65def init_scenario(temp_dir, driver_name):66 cmd = ["molecule", "init", "role", "test-init", "--driver-name", driver_name]67 pytest.helpers.run_command(cmd)68 role_directory = os.path.join(temp_dir.strpath, "test-init")69 pytest.helpers.metadata_lint_update(role_directory)70 with change_dir_to(role_directory):71 molecule_directory = pytest.helpers.molecule_directory()72 scenario_directory = os.path.join(molecule_directory, "test-scenario")73 cmd = [74 "molecule",75 "init",76 "scenario",77 "test-scenario",78 "--role-name",79 "test-init",80 "--driver-name",81 driver_name,82 ]83 pytest.helpers.run_command(cmd)84 assert os.path.isdir(scenario_directory)85 cmd = ["molecule", "test", "--scenario-name", "test-scenario", "--all"]86 pytest.helpers.run_command(cmd)87@pytest.helpers.register88def metadata_lint_update(role_directory):89 ansible_lint_src = os.path.join(90 os.path.dirname(util.abs_path(__file__)), ".ansible-lint"91 )92 shutil.copy(ansible_lint_src, role_directory)93 with change_dir_to(role_directory):94 cmd = ["ansible-lint", "."]95 pytest.helpers.run_command(cmd)96@pytest.helpers.register97def list(x):98 cmd = ["molecule", "list"]99 out = pytest.helpers.run_command(cmd, log=False)100 out = out.stdout.decode("utf-8")101 out = util.strip_ansi_color(out)102 for l in x.splitlines():103 assert l in out104@pytest.helpers.register105def list_with_format_plain(x):106 cmd = ["molecule", "list", "--format", "plain"]107 result = util.run_command(cmd)...
test_azure.py
Source:test_azure.py
...29 role_directory = os.path.join(temp_dir.strpath, "test-init")30 cmd = ["molecule", "init", "role", "test-init"]31 result = run_command(cmd)32 assert result.returncode == 033 with change_dir_to(role_directory):34 molecule_directory = pytest.helpers.molecule_directory()35 scenario_directory = os.path.join(molecule_directory, "test-scenario")36 cmd = [37 "molecule",38 "init",39 "scenario",40 "test-scenario",41 "--role-name",42 "test-init",43 "--driver-name",44 "azure",45 ]46 result = run_command(cmd)47 assert result.returncode == 0...
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!!