Best Python code snippet using autotest_python
play_screen.py
Source:play_screen.py
1from func import *2#Hà m hiá»n thá»3def display(font_size, status, font_color, X1, X2, Y1, relative = False, posxInpt = 0, posyInpt = 0):4 status = str(status)5 font = pygame.font.SysFont('consolas', font_size)6 surface = font.render(status, True, font_color)7 size = surface.get_size()8 if relative:9 posx = posxInpt10 posy = posyInpt11 else:12 posx = (X1 - size[0]) / 2 + X213 posy = Y1 - size[1] / 214 SCREEN.blit(surface, (posx, posy))15 return [posx, posy]16def displayBack(name_file, scaleX, scaleY, posx, posy):17 image = pygame.image.load('image/' + str(name_file))18 image = pygame.transform.scale(image, (scaleX, scaleY))19 # surface_back = pygame.surface.Surface((scaleX, scaleY), flags=0)20 # SCREEN.blit(surface_back, (posx, posy))21 SCREEN.blit(image, (posx, posy))22 return [posx, posy]23def displayImage(name_file, scaleX, scaleY, posx, posy):24 image = pygame.image.load('image/icon/' + str(name_file))25 image = pygame.transform.scale(image, (scaleX, scaleY))26 # surface_back = pygame.surface.Surface((scaleX, scaleY), flags=0)27 # SCREEN.blit(surface_back, (posx, posy))28 SCREEN.blit(image, (posx, posy))29 return [posx, posy]30#Các hà m xá» lý sá» liá»u31def changeTime(realtime):32 second_after_process = int(realtime)33 minute = int(second_after_process / 60)34 second = second_after_process - minute * 6035 if minute < 10:36 if second < 10:37 time = '0' + str(minute) + ':' + '0' + str(second)38 else:39 time = '0' + str(minute) + ':' + str(second)40 else:41 if second < 10:42 time = str(minute) + ':' + '0' + str(second)43 else:44 time = str(minute) + ':' + str(second)45 return time46def pos_mouse(pos):47 icon_position = [48 [45, 95, 500, 550, 'choose_song_next'],49 [115, 165, 500, 550, 'prep'],50 [175, 225, 500, 550, 'run_pause'],51 [235, 285, 500, 550, 'next'],52 [305, 355, 500, 550, 'like'],53 [70, WINDOWWIDTH - 70, 445, 455, 'change_start_point_music'],54 [355, 369, 120, 340, 'change_volumn'],55 [25, 75, 20, 70, 'file'],56 [125, 175, 20, 70, 'search'],57 [225, 275, 20, 70, 'play'],58 [325, 375, 20, 70, 'setting'],59 ]60 for i in icon_position:61 if pos[0] >= i[0] and pos[0] <= i[1] and pos[1] >= i[2] and pos[1] <= i[3]:62 return icon_position.index(i)63def takequeue():64 list = []65 for name in glob.glob('music/*.mp3'):66 list.append(name)67 return list68def playMusic(number):69 list = []70 for name in glob.glob('music/*.mp3'):71 list.append(name)72 mixer.init()73 mixer.music.load(str(list[number]))74 song = MP3(str(list[number]))75 songLength = song.info.length76 mixer.music.play()77 return songLength78#Các class Äặc trÆ°ng79class Disc():80 def __init__(self):81 self.angle = 082 def update(self, run_pause, speed):83 if run_pause == -1:84 self.angle += speed * 885 def draw(self):86 disc = pygame.image.load('image/discS.png')87 disc = pygame.transform.smoothscale(disc, (250, 250))88 rotate_image = pygame.transform.rotate(disc, self.angle)89 rect = rotate_image.get_rect()90 pos_disc = (((WINDOWWIDTH - rect.width) / 2), ((WINDOWHEIGHT - rect.height) / 2 - 70))91 SCREEN.blit(rotate_image, pos_disc)92class Volume():93 def __init__(self):94 self.volume = 195 self.display = 13096 self.icon = 'max.png'97 def draw(self):98 pygame.draw.line(SCREEN, WHITE, (362, 130), (362, self.display), 3)99 pygame.draw.line(SCREEN, BLACK, (362, self.display), (362, 330), 3)100 pygame.draw.circle(SCREEN, BLACK, (362, self.display), 5) # Hình tròn101 def update(self, point):102 ratio = int((point - 130) / 200 * 10) / 10103 self.volume = 1 - ratio104 if self.volume <= 0.05:105 self.volume = 0106 elif self.volume >= 0.95:107 self.volume = 1108 self.display = 130 + ratio * 200109 mixer.music.set_volume(self.volume)110 def out(self):111 return self.volume112class Song():113 def __init__(self, number):114 self.number = number115 self.list = takequeue()116 self.song = MP3(str(self.list[number]))117 self.length = self.song.info.length118 self.realtime = 70119 self.point = 70120 self.time = 0121 self.store = 0122 self.min_value = 1000123 def draw(self, time, check):124 display(20, str(self.list[self.number][6:]), WHITE, WINDOWWIDTH, 0, 400)125 display(15, str(changeTime(self.length)), WHITE, 70, WINDOWWIDTH - 70, 450)126 self.realtime = (time / (self.length * 1000)) * (WINDOWWIDTH - 140) + self.point127 if self.realtime <= WINDOWWIDTH - 70:128 if check:129 self.store = self.time + time / 1000130 else:131 pass132 display(15, str(changeTime(self.store)), WHITE, 70, 0, 450)133 pygame.draw.line(SCREEN, RED, (70, 450), (self.realtime, 450), 3)134 pygame.draw.line(SCREEN, WHITE, (self.realtime, 450), (WINDOWWIDTH - 70, 450), 3)135 pygame.draw.circle(SCREEN, RED, (self.realtime, 450), 5) # Hình tròn136 else:137 self.realtime = WINDOWWIDTH - 70138 display(15, str(changeTime(self.length)), WHITE, 70, 0, 450)139 pygame.draw.line(SCREEN, RED, (70, 450), (self.realtime, 450), 3)140 pygame.draw.line(SCREEN, WHITE, (self.realtime, 450), (WINDOWWIDTH - 70, 450), 3)141 pygame.draw.circle(SCREEN, RED, (self.realtime, 450), 5) # Hình tròn142 if self.min_value > int((self.length - self.store) * 100):143 self.min_value = int((self.length - self.store) * 100)144 elif self.min_value == int((self.length - self.store) * 100):145 self.min_value = 0146 def update(self, point):147 if int((point - 70) / (WINDOWWIDTH - 140)) < 1:148 self.point = point149 self.time = (self.point - 70) / (WINDOWWIDTH - 140) * self.length150 mixer.music.set_pos(self.time)151 else:152 pass153 def out(self):154 if self.min_value == 0:155 print('ok')156 return True157 else:158 return False159class Icon():160 def __init__(self):161 self.how_to_change = 'continue.png'162 self.run = 'pause.png'163 def draw(self, run_pause):164 if run_pause == -1:165 self.run = 'pause.png'166 else:167 self.run = 'run.png'168 displayImage(self.how_to_change, 50, 50, 45, 500)169 displayImage('prep.png', 50, 50, 115, 500)170 displayImage(self.run, 50, 50, 175, 500)171 displayImage('next.png', 50, 50, 235, 500)172 displayImage('like.png', 50, 50, 305, 500)173 def update(self, change_queue):174 if change_queue == 0:175 self.how_to_change = 'continue.png'176 elif change_queue == 1:177 self.how_to_change = 'random.png'178 elif change_queue == 2:179 self.how_to_change = 'replay-all.png'180 elif change_queue == 3:181 self.how_to_change = 'replay-one.png'182class Page():183 def __init__(self):184 self.file = 'file.png'185 self.search = 'search.png'186 self.play = 'play_c.png'187 self.setting = 'setting.png'188 self.file_c = 1189 self.search_c = 1190 self.play_c = -1191 self.setting_c = 1192 def draw(self):193 if self.file_c == 1:194 self.file = 'file.png'195 else:196 self.file = 'file_c.png'197 if self.search_c == 1:198 self.search = 'search.png'199 else:200 self.search = 'search_c.png'201 if self.play_c == 1:202 self.play = 'play.png'203 else:204 self.play = 'play_c.png'205 if self.setting_c == 1:206 self.setting = 'setting.png'207 else:208 self.setting = 'setting_c.png'209 displayImage(self.file, 50, 50, 25, 20)210 displayImage(self.search, 50, 50, 125, 20)211 displayImage(self.play, 50, 50, 225, 20)212 displayImage(self.setting, 50, 50, 325, 20)213 def update(self, status):214 if status == 0:215 self.file_c *= -1216 self.search_c = 1217 self.play_c = 1218 self.setting_c = 1219 if status == 1:220 self.search_c *= -1221 self.file_c = 1222 self.play_c = 1223 self.setting_c = 1224 if status == 2:225 self.play_c *= -1226 self.file_c = 1227 self.search_c = 1228 self.setting_c = 1229 if status == 3:230 self.setting_c *= -1231 self.file_c = 1232 self.search_c = 1233 self.play_c = 1234#Hà m chạy235disc = Disc()236volume = Volume()237song = Song(number=0)238icon = Icon()239page = Page()240def play_screen():241 number = 0242 playMusic(number)243 song.__init__(number)244 mute_volumn = -1245 run_pause = -1246 change_queue = 0247 start_realtime = 0248 unpause_realtime = 0249 pause_realtime = 0250 while True:251 for event in pygame.event.get():252 if event.type == QUIT:253 pygame.quit()254 sys.exit()255 if event.type == MOUSEBUTTONDOWN:256 pos = pygame.mouse.get_pos()257 status = pos_mouse(pos)258 # Các trạng thái chuyá»n bà i259 if status == 0:260 if change_queue == 3:261 change_queue = 0262 else:263 change_queue += 1264 icon.update(change_queue)265 if status == 1:266 number -= 1267 song.__init__(number)268 start_realtime = pygame.time.get_ticks()269 playMusic(number)270 if status == 3:271 if number == len(takequeue()) - 1:272 number = 0273 else:274 number += 1275 song.__init__(number)276 start_realtime = pygame.time.get_ticks()277 playMusic(number)278 # Các trạng thái ảnh hÆ°á»ng Äến bà i hát279 if status == 2:# Trạng thái của nút pause/run280 run_pause *= -1281 if run_pause == -1:282 unpause_realtime = pygame.time.get_ticks()283 else:284 pause_realtime = pygame.time.get_ticks()285 if status == 5:# Trạng thái bà i hát286 start_realtime = pygame.time.get_ticks()287 song.update(pos[0])288 # Các trạng thái ảnh hÆ°á»ng Äến âm lượng289 if status == 6:# Trạng thái Äiá»u khiá»n thanh âm lượng290 volume.update(pos[1])291 # Chuyá»n trang292 if status == 7:293 page.update(0)294 if status == 8:295 page.update(1)296 if status == 9:297 page.update(2)298 if status == 10:299 page.update(3)300 if event.type == MOUSEBUTTONUP:301 status = -1302 if event.type == KEYDOWN:303 if event.key == K_RETURN:304 run_pause *= -1305 if run_pause == -1:306 unpause_realtime = pygame.time.get_ticks()307 else:308 pause_realtime = pygame.time.get_ticks()309 if event.key == K_m:310 mute_volumn *= -1311 if mute_volumn == -1:312 volume.update(330)313 else:314 volume.update(230)315 if song.out():316 if change_queue == 0:317 number += 1318 if number > len(takequeue()) - 1:319 number = None320 if change_queue == 1:321 number = random.randrange(0, len(takequeue()) - 1, 1)322 if change_queue == 2:323 number += 1324 if number > len(takequeue()) - 1:325 number = 0326 if change_queue == 3:327 number = number328 if number != None:329 song.__init__(number)330 start_realtime = pygame.time.get_ticks()331 playMusic(number)332 if run_pause != -1:333 mixer.music.pause()334 else:335 mixer.music.unpause()336 stop_realtime = pygame.time.get_ticks()337 realtime = stop_realtime - start_realtime338 pause = unpause_realtime - pause_realtime339 displayBack('background1.png', WINDOWWIDTH, WINDOWHEIGHT, 0, 0)340 disc.update(run_pause, volume.out())341 disc.draw()342 volume.draw()343 page.draw()344 if mixer.music.get_busy():345 song.draw(realtime - pause, True)346 else:347 song.draw(realtime, False)348 icon.draw(run_pause)349 pygame.display.update()350 fpsClock.tick(FPS)...
main.py
Source:main.py
1"""ÐÑновной модÑлÑ, коÑÐµÐ½Ñ Ð¿ÑоекÑа Ñ Ð»Ð¾Ð³Ð¸ÐºÐ¾Ð¹"""2from objects import *3from options import *4from datetime import datetime5def generate_level(world_map):6 """Создание ÑÑовнÑ"""7 def maybe(x, y):8 """ÐÑовеÑка, еÑÑÑ Ð»Ð¸ вокÑÑг данной ÑоÑки еÑÑ ÑвободнÑе пÑÑи"""9 return any([world_map[x - 1][y], world_map[x + 1][y],10 world_map[x][y - 1], world_map[x][y + 1]])11 def double_maybe(x, y):12 """ÐÑовеÑка, еÑÑÑ Ð»Ð¸ вокÑÑг данной ÑоÑки еÑÑ ÑвободнÑе пÑÑи + диагонали"""13 ans = [(x - 1, y - 1), (x - 1, y), (x - 1, y + 1),14 (x, y - 1), (x, y), (x, y + 1),15 (x + 1, y - 1), (x + 1, y), (x + 1, y + 1)]16 isit = [world_map[x - 1][y - 1], world_map[x - 1][y], world_map[x - 1][y + 1],17 world_map[x][y - 1], world_map[x][y], world_map[x][y + 1],18 world_map[x + 1][y - 1], world_map[x + 1][y], world_map[x + 1][y + 1]]19 return list(filter(lambda x: isit[ans.index(x)], ans))20 potential_start, potential_end = [], []21 for x in range(len(world_map)):22 for y in range(len(world_map[x])):23 if not world_map[x][y]:24 cell = Wall(x, y)25 if x == 0:26 if world_map[x + 1][y]:27 potential_start.append(cell)28 elif x == len(world_map) - 1:29 if world_map[x - 1][y]:30 potential_end.append(cell)31 elif y != 0 and y != len(world_map[x]) - 1:32 if random.random() <= 0.1 and maybe(x, y):33 world_map[x, y] = 134 cell.kill()35 start, end = random.choice(potential_start), random.choice(potential_end)36 x_s, y_s, x_e, y_e = start.rect.x, start.rect.y, end.rect.x, end.rect.y37 world_map[x_e // CELL_W, y_e // CELL_W], world_map[x_s // CELL_W, y_s // CELL_W] = 2, 238 start.kill(), end.kill()39 sgs = []40 sgs.append(SG(random.choice(double_maybe(2, 2)), ((x_e, y_e), (x_s, y_s))))41 sgs.append(SG(random.choice(double_maybe((MAZE_S * 2 + 1) - 3, (MAZE_S * 2 + 1) - 3)), ((x_e, y_e), (x_s, y_s))))42 sgs.append(SG(random.choice(double_maybe(2, (MAZE_S * 2 + 1) - 3)), ((x_e, y_e), (x_s, y_s))))43 sgs.append(SG(random.choice(double_maybe((MAZE_S * 2 + 1) - 3, 2)), ((x_e, y_e), (x_s, y_s))))44 sgs.append(SG(random.choice(double_maybe(MAZE_S + 1, MAZE_S + 1)), ((x_e, y_e), (x_s, y_s))))45 sg_handler = SGHandler(sgs)46 return (x_s, y_s), (x_e, y_e), world_map, sg_handler47def generate_entity():48 """Создание оÑновнÑÑ
обÑекÑов"""49 pos_p, pos_e, wmap, sg_handler = generate_level(Maze(MAZE_S, MAZE_S).get_maze())50 exit_doors = Door(pos_p[0] // CELL_W, pos_p[1] // CELL_W), Door(pos_e[0] // CELL_W, pos_e[1] // CELL_W)51 player = Player(pos_p, wmap, exit_doors)52 monster = Enemy(pos_e, player)53 sg_handler.set_monster(monster)54 player.set_monster(monster)55 player.set_sg_handler(sg_handler)56 return player, monster, exit_doors, sg_handler57def restart():58 """ÐеÑезапÑÑк вÑей ÑеÑÑии"""59 # ÐÑиÑÑка вÑеÑ
ÑпÑайÑовÑÑ
гÑÑпп60 all_groups.empty()61 walls_groups.empty()62 doors_groups.empty()63 player_group.empty()64 enemy_group.empty()65 sg_group.empty()66 item_group.empty()67 meat_group.empty()68 # СоздаÑм ÑÑÑноÑÑи69 return generate_entity()70# Ðкно Pygame71player = None72menu = True73run_pause = False74run_game = True75while menu:76 if run_pause:77 random.seed(SEED_BAR.seed)78 player, monster, exit_doors, sg_handler = restart()79 play_doorsound = True80 while run_pause:81 if run_game:82 pygame.mouse.set_visible(False)83 while run_game:84 for event in pygame.event.get():85 if event.type == pygame.QUIT:86 menu = False87 run_pause = False88 run_game = False89 if event.type == PATHTIME:90 monster.update_goal()91 elif event.type == HEARTBEAT:92 player.heartbeat((monster.rect.x + monster.rect.w // 2,93 monster.rect.y + monster.rect.h // 2))94 elif event.type == ITEMSPAWN:95 player.item_spawner.spawn()96 elif event.type == pygame.KEYDOWN:97 if event.key == pygame.K_RETURN:98 monster.change_behave()99 elif event.key == pygame.K_BACKSPACE:100 # Ðини-каÑÑа Ð´Ð»Ñ Ð´ÐµÐ±Ð°Ð³Ð°101 DO2D = not DO2D102 elif event.key == pygame.K_ESCAPE:103 run_game = False104 elif event.key == pygame.K_KP_PLUS:105 player.score_bar.update(1)106 monster.change_speed()107 elif event.key == pygame.K_KP_MINUS:108 player.score_bar.update(-1)109 monster.change_speed()110 elif event.key == pygame.K_F2:111 # СкÑинÑоÑ112 filename = f'data/screenshots/{str(datetime.now()).split(".")[0].replace(":", "-")}.png'113 pygame.image.save(screen, filename)114 elif event.type == pygame.MOUSEWHEEL:115 player.inventory.update(event.y)116 elif event.type == pygame.MOUSEBUTTONDOWN:117 if event.button == 1:118 player.inventory.use()119 player.inventory.update()120 # ÐÑкÑÑваем двеÑÑ Ð¿Ð¾ доÑÑÐ¸Ð¶ÐµÐ½Ð¸Ñ 5 оÑков121 if player.score_bar.score == 5:122 for door in exit_doors:123 door.is_open = True124 player.w_map[door.pos] = 3125 if play_doorsound:126 DOOR_SOUND.play()127 play_doorsound = False128 else:129 for door in exit_doors:130 door.is_open = False131 player.w_map[door.pos] = 2132 play_doorsound = True133 # ÐенÑем Ñгол напÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ Ð²Ð·Ð³Ð»Ñда Ñ Ð¿Ð¾Ð¼Ð¾ÑÑÑ Ð¼ÑÑки134 mouse_pos = pygame.mouse.get_pos()135 if mouse_pos != CENTER and 0 < mouse_pos[0] < WIDTH - 1 and 0 < mouse_pos[1] < HEIGHT - 1:136 player.change_angle(mouse_pos)137 pygame.mouse.set_pos(CENTER)138 # РеÑÑаÑÑ ÑÑовнÑ139 if player.win or player.lost:140 run_game = False141 run_pause = False142 all_groups.update()143 if DO2D:144 screen.fill((0, 0, 0))145 all_groups.draw(screen)146 doors_groups.draw(screen)147 player_group.draw(screen)148 x = player.x + CELL_W * math.cos(player.angle)149 y = player.y + CELL_W * math.sin(player.angle)150 pygame.draw.line(screen, (255, 255, 255), player.pos, (x, y))151 else:152 # ÐÑÑиÑовка ÐÑевдо 3д153 screen.fill((45, 45, 45))154 player.draw_world()155 # ÐÑÑиÑовка инвенÑоÑÑ156 player.draw_inventory()157 update_fps()158 if player.is_interacting:159 screen.blit(player.interact_text(), (0, 0))160 pygame.display.flip()161 clock.tick(FPS)162 ########## GAME PAUSED ##########163 pygame.mouse.set_visible(True)164 for event in pygame.event.get():165 if event.type == pygame.QUIT:166 menu = False167 run_pause = False168 elif event.type == pygame.KEYDOWN:169 if event.key == pygame.K_ESCAPE:170 run_game = True171 pygame.mouse.set_pos(CENTER)172 elif event.key == pygame.K_F2:173 # СкÑинÑоÑ174 filename = f'data/screenshots/{str(datetime.now()).split(".")[0].replace(":", "-")}.png'175 pygame.image.save(screen, filename)176 elif event.type == pygame.MOUSEBUTTONDOWN:177 if RECT_PLAY.collidepoint(event.pos):178 BTN_SOUND.play()179 run_game = True180 pygame.mouse.set_pos(CENTER)181 elif RECT_SETTINGS.collidepoint(event.pos):182 BTN_SOUND.play()183 args = settings()184 if not args:185 menu = False186 elif args[0] is None:187 pass188 else:189 with open("settings.txt", 'w') as f:190 f.write(f'SENSITIVITY={args[0]}\n')191 f.write(f'BTN_F={args[1]}\n')192 f.write(f'BTN_L={args[2]}\n')193 f.write(f'BTN_R={args[3]}\n')194 f.write(f'BTN_B={args[4]}\n')195 f.write(f'BTN_INTERACT={args[5]}\n')196 f.write(f'WIDTH={args[6][1]}\n')197 f.write(f'HEIGHT={args[6][0]}\n')198 elif RECT_EXIT.collidepoint(event.pos):199 BTN_SOUND.play()200 run_pause = False201 screen.fill((0, 0, 0))202 player.draw_world()203 # ÐÑÑиÑовÑваем pause-баннеÑ204 [screen.blit(banner, (0, 0)) for banner in pause_banners()]205 # оÑÑиÑовка менÑÑки206 work_with_menu('game')207 SEED_BAR.draw()208 pygame.display.flip()209 clock.tick(FPS)210 ########## GAME MENU ##########211 screen.fill((0, 0, 0))212 # ÐаÑÑинка Ñ Ð»Ð¾Ð³Ð¾213 screen.blit(pygame.transform.scale(LOGO, (RECT_GAME_WINDOW.w, RECT_GAME_WINDOW.h)), (0, 0))214 work_with_menu('menu')215 player = game_over_message(player)216 for event in pygame.event.get():217 if event.type == pygame.QUIT:218 menu = False219 elif event.type == pygame.MOUSEBUTTONDOWN:220 if RECT_PLAY.collidepoint(event.pos):221 BTN_SOUND.play()222 menu, run_pause, seed = choose_session(SEED_BAR)223 if seed:224 SEED_BAR.seed = seed225 elif RECT_SETTINGS.collidepoint(event.pos):226 BTN_SOUND.play()227 args = settings()228 if not args:229 menu = False230 elif args[0] is None:231 pass232 else:233 with open("settings.txt", 'w') as f:234 f.write(f'SENSITIVITY={args[0]}\n')235 f.write(f'BTN_F={args[1]}\n')236 f.write(f'BTN_L={args[2]}\n')237 f.write(f'BTN_R={args[3]}\n')238 f.write(f'BTN_B={args[4]}\n')239 f.write(f'BTN_INTERACT={args[5]}\n')240 f.write(f'WIDTH={args[6][1]}\n')241 f.write(f'HEIGHT={args[6][0]}\n')242 elif RECT_EXIT.collidepoint(event.pos):243 menu = False244 BTN_SOUND.play()245 pygame.display.flip()246 clock.tick(FPS)...
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!!