Best Python code snippet using yandex-tank
StarQuake-Game.py
Source:StarQuake-Game.py
1import pygame2import random3from room import Room1, Room2, Room3, Room4, Room5, Room6, Room7, Room8, Room9, Room10, Room11, Room12, Room13, Room14, Room154from Player import Player5from enemy import enemy6from Bullet import Bullet78BLACK = (0, 0, 0)9WHITE = (255, 255, 255)10BLUE = (0, 0, 255)11GREEN = (0, 255, 0)12RED = (255, 0, 0)13PURPLE = (255, 0, 255)14YELLOW = (246, 255, 0)1516_TILE = 4017SPD = None1819# Call this function so the Pygame library can initialize itself20pygame.init()2122Enemy_img_list = ["blue_monster.png", "yellow_monster.png", "green_monster.png", "red_monster.png"]2324movingsprites = pygame.sprite.Group()2526bullet_list = pygame.sprite.Group()27# List of each block in the game28wall_list = pygame.sprite.Group()29# all_sprites_list = pygame.sprite.Group()30Enemies_list = pygame.sprite.Group()313233def game_intro():34 intro = True3536 while intro:37 for event in pygame.event.get():38 if event.type == pygame.KEYDOWN:39 if event.key == pygame.K_SPACE:40 intro = False41 elif event.type == pygame.QUIT:42 pygame.quit()4344 intro_image = pygame.image.load('intro.png')45 screen.blit(intro_image, (0, 0))46 pygame.display.update()47 clock.tick(15)484950def monsters():51 for i in range(5):52 # This represents a enemy53 Enemy_img = random.randint(0, 3)54 Enemies = enemy(BLACK, 40, 40, Enemy_img_list[Enemy_img])55 # Set a random location for the enemy56 Enemies.rect.x = 70057 Enemies.rect.y = 50058 Enemies.change_x = random.randrange(-3, 4)59 Enemies.change_y = random.randrange(-3, 4)60 Enemies.left_boundary = 4061 Enemies.top_boundary = 16062 Enemies.right_boundary = 76063 Enemies.bottom_boundary = 56064 Enemies_list.add(Enemies)65 # Add the block to the list of objects66 movingsprites.add(Enemies)676869moving_right = False70moving_left = False71stay_right = True7273""" Main Program """7475# Create an 800x600 sized screen76screen = pygame.display.set_mode([800, 600])77# Hide the mouse cursor78pygame.mouse.set_visible(0)7980# This is a font we use to draw text on the screen (size 36)81font = pygame.font.Font(None, 30)82font1 = pygame.font.Font(None, 25)8384# Use this boolean variable to trigger if the game is over.85game_over = False86win = False8788# Insert the scoreboard89sb = pygame.image.load('score_board.png')90screen.blit(sb, (0, 0))9192momentum = 093air_timer = 09495# Set the title of the window96pygame.display.set_caption('ZX_SPECTRUM--STARQUAKE')9798# Create the player paddle object99player = Player(40, 150)100monsters()101102movingsprites.add(player)103104rooms = []105106room1 = Room1()107rooms.append(room1)108109room2 = Room2()110rooms.append(room2)111112room3 = Room3()113rooms.append(room3)114115room4 = Room4()116rooms.append(room4)117118room5 = Room5()119rooms.append(room5)120121room6 = Room6()122rooms.append(room6)123124room7 = Room7()125rooms.append(room7)126127room8 = Room8()128rooms.append(room8)129130room9 = Room9()131rooms.append(room9)132133room10 = Room10()134rooms.append(room10)135136room11 = Room11()137rooms.append(room11)138139room12 = Room12()140rooms.append(room12)141142room13 = Room13()143rooms.append(room13)144145room14 = Room14()146rooms.append(room14)147148room15 = Room15()149rooms.append(room15)150151current_room_no = 0152current_room = rooms[current_room_no]153154clock = pygame.time.Clock()155156pygame.mixer.music.load('Starquake.ogg')157pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)158pygame.mixer.music.play(-1)159160loop = 1161score = 0162life = 10163ezises = False164165game_intro()166while loop:167168 screen.fill(BLACK)169 # Tiles are blitted ==========================170 tile_rects = []171 y = 0172 for line_of_symbols in current_room.game_map:173 x = 0174 for symbol in line_of_symbols:175 if symbol in current_room.tl:176 # draw the symbol for image177 screen.blit(178 current_room.tl[symbol], (x * _TILE, y * _TILE))179 # draw a rectangle for every symbol except for the empty one180 if symbol != "-":181 tile_rects.append(pygame.Rect(x * _TILE, y * _TILE, _TILE, _TILE))182 x += 1183 y += 1184 # ================================================185186 # --- Event Processing ---187188 # MOVEMENT OF THE PLAYER189 player_movement = [0, 0]190 if moving_right:191 player_movement[0] += 3192 if moving_left:193 player_movement[0] -= 3194 player_movement[1] += momentum195 momentum += 0.3196 if momentum > 3:197 momentum = 3198199 player.rect, collisions = player.move(player.rect, player_movement, tile_rects)200201 if collisions['bottom']:202 air_timer = 0203 momentum = 0204 else:205 air_timer += 0.5206207 for event in pygame.event.get():208 if event.type == pygame.QUIT:209 loop = 0210 if event.type == pygame.KEYDOWN:211 if event.key == pygame.K_ESCAPE:212 loop = 0213 if event.key == pygame.K_RIGHT:214 moving_right = True215 stay_right = True216 if event.key == pygame.K_LEFT:217 moving_left = True218 stay_right = False219 if event.key == pygame.K_SPACE:220 if air_timer < 6:221 momentum = -7222 if event.key == pygame.K_a:223 # Fire a bullet if the user clicks the mouse button224 bullet = Bullet(stay_right)225 # Set the bullet so it is where the player is226 bullet.rect.x = player.rect.x227 bullet.rect.y = player.rect.y228 # Add the bullet to the lists229 bullet_list.add(bullet)230 movingsprites.add(bullet)231 if event.type == pygame.KEYUP:232 if event.key == pygame.K_RIGHT:233 moving_right = False234 if event.key == pygame.K_LEFT:235 moving_left = False236237 if stay_right:238 player.image = pygame.image.load("player_right.png").convert()239 player.image.set_colorkey(BLACK)240 else:241 player.image = pygame.image.load("player_left.png").convert()242 player.image.set_colorkey(BLACK)243244 # --- Game Logic ---245 # Right246 # player.move(current_room.wall_list)247 if player.rect.x > 790:248 if current_room_no == 0:249 current_room_no = 1250 for Enemies in Enemies_list:251 Enemies.kill()252 for bullet in bullet_list:253 movingsprites.remove(bullet)254 bullet_list.remove(bullet)255 monsters()256 print("ROOM = ", current_room_no + 1)257 current_room = rooms[current_room_no]258 player.rect.x = 0259260 elif current_room_no == 1:261 current_room_no = 2262 for Enemies in Enemies_list:263 Enemies.kill()264 for bullet in bullet_list:265 movingsprites.remove(bullet)266 bullet_list.remove(bullet)267 monsters()268 print("ROOM = ", current_room_no + 1)269 current_room = rooms[current_room_no]270 player.rect.x = 0271272 elif current_room_no == 2:273 current_room_no = 3274 for Enemies in Enemies_list:275 Enemies.kill()276 for bullet in bullet_list:277 movingsprites.remove(bullet)278 bullet_list.remove(bullet)279 monsters()280 print("ROOM = ", current_room_no + 1)281 current_room = rooms[current_room_no]282 player.rect.x = 0283284 elif current_room_no == 3:285 current_room_no = 4286 for Enemies in Enemies_list:287 Enemies.kill()288 for bullet in bullet_list:289 movingsprites.remove(bullet)290 bullet_list.remove(bullet)291 monsters()292 print("ROOM = ", current_room_no + 1)293 current_room = rooms[current_room_no]294 player.rect.x = 0295296 elif current_room_no == 7:297 current_room_no = 8298 ezises = True299 for Enemies in Enemies_list:300 Enemies.kill()301 for bullet in bullet_list:302 movingsprites.remove(bullet)303 bullet_list.remove(bullet)304 monsters()305 print("ROOM = ", current_room_no + 1)306 current_room = rooms[current_room_no]307 player.rect.x = 0308309 elif current_room_no == 11:310 current_room_no = 10311 ezises = True312 for Enemies in Enemies_list:313 Enemies.kill()314 for bullet in bullet_list:315 movingsprites.remove(bullet)316 bullet_list.remove(bullet)317 monsters()318 print("ROOM = ", current_room_no + 1)319 current_room = rooms[current_room_no]320 player.rect.x = 0321 elif current_room_no == 12:322 current_room_no = 11323 ezises = True324 for Enemies in Enemies_list:325 Enemies.kill()326 for bullet in bullet_list:327 movingsprites.remove(bullet)328 bullet_list.remove(bullet)329 monsters()330 print("ROOM = ", current_room_no + 1)331 current_room = rooms[current_room_no]332 player.rect.x = 0333 elif current_room_no == 14:334 current_room_no = 7335 ezises = True336 for Enemies in Enemies_list:337 Enemies.kill()338 for bullet in bullet_list:339 movingsprites.remove(bullet)340 bullet_list.remove(bullet)341 monsters()342 print("ROOM = ", current_room_no + 1)343 current_room = rooms[current_room_no]344 player.rect.x = 0345346 elif current_room_no == 13:347 current_room_no = 12348 ezises = True349 for Enemies in Enemies_list:350 Enemies.kill()351 for bullet in bullet_list:352 movingsprites.remove(bullet)353 bullet_list.remove(bullet)354 monsters()355 print("ROOM = ", current_room_no + 1)356 current_room = rooms[current_room_no]357 player.rect.x = 0358 #left movement359 if player.rect.x < -15:360 if current_room_no == 1:361 current_room_no = 0362 for Enemies in Enemies_list:363 Enemies.kill()364 for bullet in bullet_list:365 movingsprites.remove(bullet)366 bullet_list.remove(bullet)367 print("ROOM = ", current_room_no + 1)368 current_room = rooms[current_room_no]369 player.rect.x = 790370371 elif current_room_no == 2:372 current_room_no = 1373 for Enemies in Enemies_list:374 Enemies.kill()375 for bullet in bullet_list:376 movingsprites.remove(bullet)377 bullet_list.remove(bullet)378 monsters()379 print("ROOM = ", current_room_no + 1)380 current_room = rooms[current_room_no]381 player.rect.x = 790382 elif current_room_no == 3:383 current_room_no = 2384 for Enemies in Enemies_list:385 Enemies.kill()386 for bullet in bullet_list:387 movingsprites.remove(bullet)388 bullet_list.remove(bullet)389 monsters()390 print("ROOM = ", current_room_no + 1)391 current_room = rooms[current_room_no]392 player.rect.x = 790393 elif current_room_no == 4:394 current_room_no = 3395 for Enemies in Enemies_list:396 Enemies.kill()397 for bullet in bullet_list:398 movingsprites.remove(bullet)399 bullet_list.remove(bullet)400 monsters()401 print("ROOM = ", current_room_no + 1)402 current_room = rooms[current_room_no]403 player.rect.x = 790404 elif current_room_no == 8:405 current_room_no = 7406 for Enemies in Enemies_list:407 Enemies.kill()408 for bullet in bullet_list:409 movingsprites.remove(bullet)410 bullet_list.remove(bullet)411 monsters()412 print("ROOM = ", current_room_no + 1)413 current_room = rooms[current_room_no]414 player.rect.x = 790415 elif current_room_no == 7:416 current_room_no = 14417 for Enemies in Enemies_list:418 Enemies.kill()419 for bullet in bullet_list:420 movingsprites.remove(bullet)421 bullet_list.remove(bullet)422 monsters()423 print("ROOM = ", current_room_no + 1)424 current_room = rooms[current_room_no]425 player.rect.x = 790426 elif current_room_no == 10:427 current_room_no = 11428 for Enemies in Enemies_list:429 Enemies.kill()430 for bullet in bullet_list:431 movingsprites.remove(bullet)432 bullet_list.remove(bullet)433 monsters()434 print("ROOM = ", current_room_no + 1)435 current_room = rooms[current_room_no]436 player.rect.x = 790437 elif current_room_no == 11:438 current_room_no = 12439 for Enemies in Enemies_list:440 Enemies.kill()441 for bullet in bullet_list:442 movingsprites.remove(bullet)443 bullet_list.remove(bullet)444 monsters()445 print("ROOM = ", current_room_no + 1)446 current_room = rooms[current_room_no]447 player.rect.x = 790448449 elif current_room_no == 12:450 current_room_no = 13451 for Enemies in Enemies_list:452 Enemies.kill()453 for bullet in bullet_list:454 movingsprites.remove(bullet)455 bullet_list.remove(bullet)456 monsters()457 print("ROOM = ", current_room_no + 1)458 current_room = rooms[current_room_no]459 player.rect.x = 790460461 # Bottom movement462 if player.rect.y > 540:463 if current_room_no == 2:464 current_room_no = 5465 for Enemies in Enemies_list:466 Enemies.kill()467 for bullet in bullet_list:468 movingsprites.remove(bullet)469 bullet_list.remove(bullet)470 monsters()471 print("ROOM = ", current_room_no + 1)472 current_room = rooms[current_room_no]473 player.rect.y = 0474475 elif current_room_no == 5:476 current_room_no = 6477 for Enemies in Enemies_list:478 Enemies.kill()479 for bullet in bullet_list:480 movingsprites.remove(bullet)481 bullet_list.remove(bullet)482 monsters()483 print("ROOM = ", current_room_no + 1)484 current_room = rooms[current_room_no]485 player.rect.y = 0486487 elif current_room_no == 6:488 current_room_no = 7489 for Enemies in Enemies_list:490 Enemies.kill()491 for bullet in bullet_list:492 movingsprites.remove(bullet)493 bullet_list.remove(bullet)494 monsters()495 print("ROOM = ", current_room_no + 1)496 current_room = rooms[current_room_no]497 player.rect.y = 0498499 elif current_room_no == 13:500 current_room_no = 12501 for Enemies in Enemies_list:502 Enemies.kill()503 for bullet in bullet_list:504 movingsprites.remove(bullet)505 bullet_list.remove(bullet)506 monsters()507 print("ROOM = ", current_room_no + 1)508 current_room = rooms[current_room_no]509 player.rect.y = 0510511 elif current_room_no == 14:512 current_room_no = 13513 for Enemies in Enemies_list:514 Enemies.kill()515 for bullet in bullet_list:516 movingsprites.remove(bullet)517 bullet_list.remove(bullet)518 monsters()519 print("ROOM = ", current_room_no + 1)520 current_room = rooms[current_room_no]521 player.rect.y = 0522 elif current_room_no == 8:523 current_room_no = 9524 for Enemies in Enemies_list:525 Enemies.kill()526 for bullet in bullet_list:527 movingsprites.remove(bullet)528 bullet_list.remove(bullet)529 monsters()530 print("ROOM = ", current_room_no + 1)531 current_room = rooms[current_room_no]532 player.rect.y = 0533 elif current_room_no == 9:534 current_room_no = 10535 for Enemies in Enemies_list:536 Enemies.kill()537 for bullet in bullet_list:538 movingsprites.remove(bullet)539 bullet_list.remove(bullet)540 monsters()541 print("ROOM = ", current_room_no + 1)542 current_room = rooms[current_room_no]543 player.rect.y = 0544545 # up movement546 if player.rect.y < -15:547 if current_room_no == 12:548 current_room_no = 13549 for Enemies in Enemies_list:550 Enemies.kill()551 for bullet in bullet_list:552 movingsprites.remove(bullet)553 bullet_list.remove(bullet)554 monsters()555 print("ROOM = ", current_room_no + 1)556 current_room = rooms[current_room_no]557 player.rect.y = 500558 elif current_room_no == 13:559 current_room_no = 14560 for Enemies in Enemies_list:561 Enemies.kill()562 for bullet in bullet_list:563 movingsprites.remove(bullet)564 bullet_list.remove(bullet)565 monsters()566 print("ROOM = ", current_room_no + 1)567 current_room = rooms[current_room_no]568 player.rect.y = 500569 elif current_room_no == 7:570 current_room_no = 6571 for Enemies in Enemies_list:572 Enemies.kill()573 for bullet in bullet_list:574 movingsprites.remove(bullet)575 bullet_list.remove(bullet)576 monsters()577 print("ROOM = ", current_room_no + 1)578 current_room = rooms[current_room_no]579 player.rect.y = 500580 elif current_room_no == 6:581 current_room_no = 5582 for Enemies in Enemies_list:583 Enemies.kill()584 for bullet in bullet_list:585 movingsprites.remove(bullet)586 bullet_list.remove(bullet)587 monsters()588 print("ROOM = ", current_room_no + 1)589 current_room = rooms[current_room_no]590 player.rect.y = 500591 elif current_room_no == 5:592 current_room_no = 2593 for Enemies in Enemies_list:594 Enemies.kill()595 for bullet in bullet_list:596 movingsprites.remove(bullet)597 bullet_list.remove(bullet)598 monsters()599 print("ROOM = ", current_room_no + 1)600 current_room = rooms[current_room_no]601 player.rect.y = 500602 elif current_room_no == 10:603 current_room_no = 9604 for Enemies in Enemies_list:605 Enemies.kill()606 for bullet in bullet_list:607 movingsprites.remove(bullet)608 bullet_list.remove(bullet)609 monsters()610 print("ROOM = ", current_room_no + 1)611 current_room = rooms[current_room_no]612 player.rect.y = 500613 elif current_room_no == 9:614 current_room_no = 8615 for Enemies in Enemies_list:616 Enemies.kill()617 for bullet in bullet_list:618 movingsprites.remove(bullet)619 bullet_list.remove(bullet)620 monsters()621 print("ROOM = ", current_room_no + 1)622 current_room = rooms[current_room_no]623 player.rect.y = 500624625 # --- Drawing ---626627 current_room.wall_list.draw(screen)628629 blocks_hit_list = pygame.sprite.spritecollide(player, Enemies_list, True)630 # Check the list of collisions.631 for block in blocks_hit_list:632 life -= 1633 if life > 0:634 print("life=", life)635 else:636 print("GAME OVER")637638 screen.blit(sb, (0, 0))639640 # See if it hit a block641 for bullet in bullet_list:642 block_hit_list = pygame.sprite.spritecollide(bullet, Enemies_list, True)643 # For each block hit, remove the bullet and add to the score644 for block in block_hit_list:645 bullet_list.remove(bullet)646 movingsprites.remove(bullet)647 score += 500648 print("score=", score)649650 # Remove the bullet if it goes right off the screen651 if bullet.rect.x > 800:652 bullet_list.remove(bullet)653 movingsprites.remove(bullet)654655 text = font.render(str(score), True, WHITE)656 screen.blit(text, [505, 40])657658 text = font1.render("0" + str(life), True, YELLOW)659 screen.blit(text, [350, 70])660661 if life == 0:662 game_over = True663664 elif not Enemies_list and ezises == True:665 win = True666667 if game_over:668 # If game over is true, draw game over669 go = pygame.image.load('game_over.png')670 screen.blit(go, (0, 0))671 movingsprites.remove(player)672 for enemies in Enemies_list:673 enemies.kill()674 for bullet in bullet_list:675 movingsprites.remove(bullet)676677 if win:678 # If game over is true, draw game over679 go = pygame.image.load('win.png')680 screen.blit(go, (0, 0))681 movingsprites.remove(player)682 for bullet in bullet_list:683 movingsprites.remove(bullet)684685 movingsprites.update()686 movingsprites.draw(screen)687 pygame.display.flip()688689 pygame.display.update()690 clock.tick(60)691
...
test_environment_toctree.py
Source:test_environment_toctree.py
1"""2 test_environment_toctree3 ~~~~~~~~~~~~~~~~~~~~~~~~4 Test the sphinx.environment.managers.toctree.5 :copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS.6 :license: BSD, see LICENSE for details.7"""8import pytest9from docutils import nodes10from docutils.nodes import bullet_list, list_item, caption, comment, reference11from sphinx import addnodes12from sphinx.addnodes import compact_paragraph, only13from sphinx.builders.html import StandaloneHTMLBuilder14from sphinx.environment.adapters.toctree import TocTree15from sphinx.testing.util import assert_node16@pytest.mark.sphinx('xml', testroot='toctree')17@pytest.mark.test_params(shared_result='test_environment_toctree_basic')18def test_process_doc(app):19 app.build()20 # tocs21 toctree = app.env.tocs['index']22 assert_node(toctree,23 [bullet_list, ([list_item, (compact_paragraph, # [0][0]24 [bullet_list, (addnodes.toctree, # [0][1][0]25 only, # [0][1][1]26 list_item)])], # [0][1][2]27 [list_item, (compact_paragraph, # [1][0]28 [bullet_list, (addnodes.toctree, # [1][1][0]29 addnodes.toctree)])], # [1][1][1]30 list_item)])31 assert_node(toctree[0][0],32 [compact_paragraph, reference, "Welcome to Sphinx Testsâs documentation!"])33 assert_node(toctree[0][0][0], reference, anchorname='')34 assert_node(toctree[0][1][0], addnodes.toctree,35 caption="Table of Contents", glob=False, hidden=False,36 titlesonly=False, maxdepth=2, numbered=999,37 entries=[(None, 'foo'), (None, 'bar'), (None, 'http://sphinx-doc.org/')],38 includefiles=['foo', 'bar'])39 # only branch40 assert_node(toctree[0][1][1], addnodes.only, expr="html")41 assert_node(toctree[0][1][1],42 [only, list_item, ([compact_paragraph, reference, "Section for HTML"],43 [bullet_list, addnodes.toctree])])44 assert_node(toctree[0][1][1][0][0][0], reference, anchorname='#section-for-html')45 assert_node(toctree[0][1][1][0][1][0], addnodes.toctree,46 caption=None, glob=False, hidden=False, entries=[(None, 'baz')],47 includefiles=['baz'], titlesonly=False, maxdepth=-1, numbered=0)48 assert_node(toctree[0][1][2],49 ([compact_paragraph, reference, "subsection"],50 [bullet_list, list_item, compact_paragraph, reference, "subsubsection"]))51 assert_node(toctree[1][0],52 [compact_paragraph, reference, "Test for issue #1157"])53 assert_node(toctree[1][0][0], reference, anchorname='#test-for-issue-1157')54 assert_node(toctree[1][1][0], addnodes.toctree,55 caption=None, entries=[], glob=False, hidden=False,56 titlesonly=False, maxdepth=-1, numbered=0)57 assert_node(toctree[1][1][1], addnodes.toctree,58 caption=None, glob=False, hidden=True,59 titlesonly=False, maxdepth=-1, numbered=0,60 entries=[('Latest reference', 'http://sphinx-doc.org/latest/'),61 ('Python', 'http://python.org/')])62 assert_node(toctree[2][0],63 [compact_paragraph, reference, "Indices and tables"])64 # other collections65 assert app.env.toc_num_entries['index'] == 666 assert app.env.toctree_includes['index'] == ['foo', 'bar', 'baz']67 assert app.env.files_to_rebuild['foo'] == set(['index'])68 assert app.env.files_to_rebuild['bar'] == set(['index'])69 assert app.env.files_to_rebuild['baz'] == set(['index'])70 assert app.env.glob_toctrees == set()71 assert app.env.numbered_toctrees == set(['index'])72 # qux has no section title73 assert len(app.env.tocs['qux']) == 074 assert_node(app.env.tocs['qux'], nodes.bullet_list)75 assert app.env.toc_num_entries['qux'] == 076 assert 'qux' not in app.env.toctree_includes77@pytest.mark.sphinx('dummy', testroot='toctree-glob')78def test_glob(app):79 includefiles = ['foo', 'bar/index', 'bar/bar_1', 'bar/bar_2',80 'bar/bar_3', 'baz', 'qux/index']81 app.build()82 # tocs83 toctree = app.env.tocs['index']84 assert_node(toctree,85 [bullet_list, list_item, (compact_paragraph, # [0][0]86 [bullet_list, (list_item, # [0][1][0]87 list_item)])]) # [0][1][1]88 assert_node(toctree[0][0],89 [compact_paragraph, reference, "test-toctree-glob"])90 assert_node(toctree[0][1][0],91 [list_item, ([compact_paragraph, reference, "normal order"],92 [bullet_list, addnodes.toctree])]) # [0][1][0][1][0]93 assert_node(toctree[0][1][0][1][0], addnodes.toctree, caption=None,94 glob=True, hidden=False, titlesonly=False,95 maxdepth=-1, numbered=0, includefiles=includefiles,96 entries=[(None, 'foo'), (None, 'bar/index'), (None, 'bar/bar_1'),97 (None, 'bar/bar_2'), (None, 'bar/bar_3'), (None, 'baz'),98 (None, 'qux/index'),99 ('hyperref', 'https://sphinx-doc.org/?q=sphinx')])100 assert_node(toctree[0][1][1],101 [list_item, ([compact_paragraph, reference, "reversed order"],102 [bullet_list, addnodes.toctree])]) # [0][1][1][1][0]103 assert_node(toctree[0][1][1][1][0], addnodes.toctree, caption=None,104 glob=True, hidden=False, titlesonly=False,105 maxdepth=-1, numbered=0, includefiles=includefiles,106 entries=[(None, 'qux/index'), (None, 'baz'), (None, 'bar/bar_3'),107 (None, 'bar/bar_2'), (None, 'bar/bar_1'), (None, 'bar/index'),108 (None, 'foo')])109 includefiles = ['foo', 'bar/index', 'bar/bar_1', 'bar/bar_2',110 'bar/bar_3', 'baz', 'qux/index']111 # other collections112 assert app.env.toc_num_entries['index'] == 3113 assert app.env.toctree_includes['index'] == includefiles + includefiles114 for file in includefiles:115 assert 'index' in app.env.files_to_rebuild[file]116 assert 'index' in app.env.glob_toctrees117 assert app.env.numbered_toctrees == set()118@pytest.mark.sphinx('xml', testroot='toctree')119@pytest.mark.test_params(shared_result='test_environment_toctree_basic')120def test_get_toc_for(app):121 app.build()122 toctree = TocTree(app.env).get_toc_for('index', app.builder)123 assert_node(toctree,124 [bullet_list, ([list_item, (compact_paragraph, # [0][0]125 [bullet_list, (addnodes.toctree, # [0][1][0]126 comment, # [0][1][1]127 list_item)])], # [0][1][2]128 [list_item, (compact_paragraph, # [1][0]129 [bullet_list, (addnodes.toctree,130 addnodes.toctree)])],131 [list_item, compact_paragraph])]) # [2][0]132 assert_node(toctree[0][0],133 [compact_paragraph, reference, "Welcome to Sphinx Testsâs documentation!"])134 assert_node(toctree[0][1][2],135 ([compact_paragraph, reference, "subsection"],136 [bullet_list, list_item, compact_paragraph, reference, "subsubsection"]))137 assert_node(toctree[1][0],138 [compact_paragraph, reference, "Test for issue #1157"])139 assert_node(toctree[2][0],140 [compact_paragraph, reference, "Indices and tables"])141@pytest.mark.sphinx('xml', testroot='toctree')142@pytest.mark.test_params(shared_result='test_environment_toctree_basic')143def test_get_toc_for_only(app):144 app.build()145 builder = StandaloneHTMLBuilder(app)146 toctree = TocTree(app.env).get_toc_for('index', builder)147 assert_node(toctree,148 [bullet_list, ([list_item, (compact_paragraph, # [0][0]149 [bullet_list, (addnodes.toctree, # [0][1][0]150 list_item, # [0][1][1]151 list_item)])], # [0][1][2]152 [list_item, (compact_paragraph, # [1][0]153 [bullet_list, (addnodes.toctree,154 addnodes.toctree)])],155 [list_item, compact_paragraph])]) # [2][0]156 assert_node(toctree[0][0],157 [compact_paragraph, reference, "Welcome to Sphinx Testsâs documentation!"])158 assert_node(toctree[0][1][1],159 ([compact_paragraph, reference, "Section for HTML"],160 [bullet_list, addnodes.toctree]))161 assert_node(toctree[0][1][2],162 ([compact_paragraph, reference, "subsection"],163 [bullet_list, list_item, compact_paragraph, reference, "subsubsection"]))164 assert_node(toctree[1][0],165 [compact_paragraph, reference, "Test for issue #1157"])166 assert_node(toctree[2][0],167 [compact_paragraph, reference, "Indices and tables"])168@pytest.mark.sphinx('xml', testroot='toctree')169@pytest.mark.test_params(shared_result='test_environment_toctree_basic')170def test_get_toc_for_tocdepth(app):171 app.build()172 toctree = TocTree(app.env).get_toc_for('tocdepth', app.builder)173 assert_node(toctree,174 [bullet_list, list_item, (compact_paragraph, # [0][0]175 bullet_list)]) # [0][1]176 assert_node(toctree[0][0],177 [compact_paragraph, reference, "level 1"])178 assert_node(toctree[0][1],179 [bullet_list, list_item, compact_paragraph, reference, "level 2"])180@pytest.mark.sphinx('xml', testroot='toctree')181@pytest.mark.test_params(shared_result='test_environment_toctree_basic')182def test_get_toctree_for(app):183 app.build()184 toctree = TocTree(app.env).get_toctree_for('index', app.builder, collapse=False)185 assert_node(toctree,186 [compact_paragraph, ([caption, "Table of Contents"],187 bullet_list,188 bullet_list,189 bullet_list)])190 assert_node(toctree[1],191 ([list_item, ([compact_paragraph, reference, "foo"],192 bullet_list)],193 [list_item, compact_paragraph, reference, "bar"],194 [list_item, compact_paragraph, reference, "http://sphinx-doc.org/"]))195 assert_node(toctree[1][0][1],196 ([list_item, compact_paragraph, reference, "quux"],197 [list_item, compact_paragraph, reference, "foo.1"],198 [list_item, compact_paragraph, reference, "foo.2"]))199 assert_node(toctree[1][0][0][0], reference, refuri="foo", secnumber=[1])200 assert_node(toctree[1][0][1][0][0][0], reference, refuri="quux", secnumber=[1, 1])201 assert_node(toctree[1][0][1][1][0][0], reference, refuri="foo#foo-1", secnumber=[1, 2])202 assert_node(toctree[1][0][1][2][0][0], reference, refuri="foo#foo-2", secnumber=[1, 3])203 assert_node(toctree[1][1][0][0], reference, refuri="bar", secnumber=[2])204 assert_node(toctree[1][2][0][0], reference, refuri="http://sphinx-doc.org/")205 assert_node(toctree[2],206 [bullet_list, list_item, compact_paragraph, reference, "baz"])207 assert_node(toctree[3],208 ([list_item, compact_paragraph, reference, "Latest reference"],209 [list_item, compact_paragraph, reference, "Python"]))210 assert_node(toctree[3][0][0][0], reference, refuri="http://sphinx-doc.org/latest/")211 assert_node(toctree[3][1][0][0], reference, refuri="http://python.org/")212@pytest.mark.sphinx('xml', testroot='toctree')213@pytest.mark.test_params(shared_result='test_environment_toctree_basic')214def test_get_toctree_for_collapse(app):215 app.build()216 toctree = TocTree(app.env).get_toctree_for('index', app.builder, collapse=True)217 assert_node(toctree,218 [compact_paragraph, ([caption, "Table of Contents"],219 bullet_list,220 bullet_list,221 bullet_list)])222 assert_node(toctree[1],223 ([list_item, compact_paragraph, reference, "foo"],224 [list_item, compact_paragraph, reference, "bar"],225 [list_item, compact_paragraph, reference, "http://sphinx-doc.org/"]))226 assert_node(toctree[1][0][0][0], reference, refuri="foo", secnumber=[1])227 assert_node(toctree[1][1][0][0], reference, refuri="bar", secnumber=[2])228 assert_node(toctree[1][2][0][0], reference, refuri="http://sphinx-doc.org/")229 assert_node(toctree[2],230 [bullet_list, list_item, compact_paragraph, reference, "baz"])231 assert_node(toctree[3],232 ([list_item, compact_paragraph, reference, "Latest reference"],233 [list_item, compact_paragraph, reference, "Python"]))234 assert_node(toctree[3][0][0][0], reference, refuri="http://sphinx-doc.org/latest/")235 assert_node(toctree[3][1][0][0], reference, refuri="http://python.org/")236@pytest.mark.sphinx('xml', testroot='toctree')237@pytest.mark.test_params(shared_result='test_environment_toctree_basic')238def test_get_toctree_for_maxdepth(app):239 app.build()240 toctree = TocTree(app.env).get_toctree_for('index', app.builder,241 collapse=False, maxdepth=3)242 assert_node(toctree,243 [compact_paragraph, ([caption, "Table of Contents"],244 bullet_list,245 bullet_list,246 bullet_list)])247 assert_node(toctree[1],248 ([list_item, ([compact_paragraph, reference, "foo"],249 bullet_list)],250 [list_item, compact_paragraph, reference, "bar"],251 [list_item, compact_paragraph, reference, "http://sphinx-doc.org/"]))252 assert_node(toctree[1][0][1],253 ([list_item, compact_paragraph, reference, "quux"],254 [list_item, ([compact_paragraph, reference, "foo.1"],255 bullet_list)],256 [list_item, compact_paragraph, reference, "foo.2"]))257 assert_node(toctree[1][0][1][1][1],258 [bullet_list, list_item, compact_paragraph, reference, "foo.1-1"])259 assert_node(toctree[1][0][0][0], reference, refuri="foo", secnumber=[1])260 assert_node(toctree[1][0][1][0][0][0], reference, refuri="quux", secnumber=[1, 1])261 assert_node(toctree[1][0][1][1][0][0], reference, refuri="foo#foo-1", secnumber=[1, 2])262 assert_node(toctree[1][0][1][1][1][0][0][0],263 reference, refuri="foo#foo-1-1", secnumber=[1, 2, 1])264 assert_node(toctree[1][0][1][2][0][0], reference, refuri="foo#foo-2", secnumber=[1, 3])265 assert_node(toctree[1][1][0][0], reference, refuri="bar", secnumber=[2])266 assert_node(toctree[1][2][0][0], reference, refuri="http://sphinx-doc.org/")267 assert_node(toctree[2],268 [bullet_list, list_item, compact_paragraph, reference, "baz"])269 assert_node(toctree[3],270 ([list_item, compact_paragraph, reference, "Latest reference"],271 [list_item, compact_paragraph, reference, "Python"]))272 assert_node(toctree[3][0][0][0], reference, refuri="http://sphinx-doc.org/latest/")273 assert_node(toctree[3][1][0][0], reference, refuri="http://python.org/")274@pytest.mark.sphinx('xml', testroot='toctree')275@pytest.mark.test_params(shared_result='test_environment_toctree_basic')276def test_get_toctree_for_includehidden(app):277 app.build()278 toctree = TocTree(app.env).get_toctree_for('index', app.builder, collapse=False,279 includehidden=False)280 assert_node(toctree,281 [compact_paragraph, ([caption, "Table of Contents"],282 bullet_list,283 bullet_list)])284 assert_node(toctree[1],285 ([list_item, ([compact_paragraph, reference, "foo"],286 bullet_list)],287 [list_item, compact_paragraph, reference, "bar"],288 [list_item, compact_paragraph, reference, "http://sphinx-doc.org/"]))289 assert_node(toctree[1][0][1],290 ([list_item, compact_paragraph, reference, "quux"],291 [list_item, compact_paragraph, reference, "foo.1"],292 [list_item, compact_paragraph, reference, "foo.2"]))293 assert_node(toctree[1][0][0][0], reference, refuri="foo", secnumber=[1])294 assert_node(toctree[1][0][1][0][0][0], reference, refuri="quux", secnumber=[1, 1])295 assert_node(toctree[1][0][1][1][0][0], reference, refuri="foo#foo-1", secnumber=[1, 2])296 assert_node(toctree[1][0][1][2][0][0], reference, refuri="foo#foo-2", secnumber=[1, 3])297 assert_node(toctree[1][1][0][0], reference, refuri="bar", secnumber=[2])298 assert_node(toctree[1][2][0][0], reference, refuri="http://sphinx-doc.org/")299 assert_node(toctree[2],...
space_Invaders2.py
Source:space_Invaders2.py
1import pygame2import sys3from pygame.locals import *4import logging5import random6import time7"""8Notes:9Triple quote string actually was10not meant to be for comments11Control + 3 = Comment out region (IDLE ide only)12Control + 4 = vice versa (Same)13"""14#Starting up pygame15pygame.init()16#Set up font17pygame.font.init()18#Setting screen to the values in the tuple19screen = pygame.display.set_mode((1200,660))20#Set title of screen21pygame.display.set_caption('Space Invaders')22#Setting up logger23logging.basicConfig(filename = 'problems.log',24 level = logging.INFO)25 26logger = logging.getLogger(__name__)27#formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')28#Classes:29 #Ship30class Ship:31 def __init__(self):32 self.health = 2033 self.x = 15034 self.y = 60035 self.speed_x = 1836 self.bullet_list = []37 #ship.png38 self.ship_image = pygame.image.load('ship.png').convert()39 self.ship_image = pygame.transform.scale(self.ship_image,(70,70))40 self.rect = self.ship_image.get_rect()41 def update(self):42 for i in self.bullet_list:43 i.ship_bullet()44 if i.y < 0:45 self.bullet_list.remove(i)46 screen.blit(self.ship_image, (self.x, 600))47 self.rect.x = self.x48 self.rect.y = 60049 50 #Invader(s)51class Invader:52 def __init__(self, x, y, image_preference):53 self.time_stamp = random.randint(0,20)54 self.x = x55 self.y = y56 self.speed_x = 257 self.invader_image = pygame.image.load(image_preference).convert()58 self.invader_image = pygame.transform.scale(self.invader_image, (30, 30))59 self.bullet_list = []60 self.rect = self.invader_image.get_rect()61 pygame.time.Clock()62 def update(self):63 delay = pygame.time.get_ticks()//100064 if delay%10 == self.time_stamp:65 self.time_stamp = random.randint(0,7)66 bullet = Bullets(self.x,self.y,'bullet.png')67 self.bullet_list.append(bullet)68 for i in self.bullet_list:69 i.invader_bullet()70 screen.blit(self.invader_image, (self.x, self.y))71 self.x += self.speed_x72 if self.x > 1000:73 self.speed_x = -(self.speed_x)74 self.y += 3575 if self.x < 0:76 self.speed_x = -(self.speed_x)77 self.y += 3578 self.rect.x = self.x79 self.rect.y = self.y80 #Bullets81class Bullets:82 def __init__(self, x, y, image_preference):83 self.x = x84 self.y = y85 self.bullet_image = pygame.image.load(image_preference).convert()86 self.bullet_image = pygame.transform.scale(self.bullet_image, (20,20))87 self.rect = self.bullet_image.get_rect()88 def ship_bullet(self):89 screen.blit(self.bullet_image, (self.x, self.y))90 sound = pygame.mixer.Sound('shoot.wav')91 sound.set_volume(0.05)92 sound.play()93 sound.fadeout(1000)94 self.y -= 4595 96 self.rect.x = self.x97 self.rect.y = self.y98 def invader_bullet(self):99 screen.blit(self.bullet_image,(self.x, self.y))100 self.y += 4101 if self.y > 640:102 self.is_alive = False103 self.rect.x = self.x104 self.rect.y = self.y 105 #Blocks106class Blocks:107 def __init__(self, x, y):108 self.x = x109 self.y = y110 self.surface = pygame.Surface((7,7))111 self.rect = pygame.draw.rect(self.surface, (60, 179, 113), (0, 0, 7, 7)) 112 def update(self):113 screen.blit(self.surface,(self.x,self.y))114 self.rect.x = self.x115 self.rect.y = self.y116 #Blockers117class Blocker:118 def __init__(self, x, y):119 self.x = x120 self.y = y121 self.shape_list = [[0,1,1,1,1,1,1,1,1,1,1,1,1,1,0],122 [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],123 [1,1,1,1,1,1,0,0,0,1,1,1,1,1,1],124 [1,1,1,1,1,0,0,0,0,0,1,1,1,1,1],125 [1,1,1,1,1,0,0,0,0,0,1,1,1,1,1],126 [1,1,1,1,1,0,0,0,0,0,1,1,1,1,1]]127 self.block_list = []128 for m in range(15):129 for n in range(6):130 if self.shape_list[n][m] == 1:131 b = Blocks(x+m*7, y+n*7)132 self.block_list.append(b)133 def update(self):134 for var in self.block_list:135 var.update()136 137#Intro Screen138def game_Intro():139 background = pygame.image.load('Menu.png').convert()140 background = pygame.transform.scale(background, (640, 640))141 while True:142 screen.blit(background, (0,0))143 for event in pygame.event.get():144 if event.type == QUIT:145 pygame.quit()146 sys.exit()147 if event.type == MOUSEBUTTONDOWN:148 game_Loop()149 pygame.display.update()150 151#Game Loop152def game_Loop():153 score = 0154 155 #Variables & Functions156 invader_list = [[],[],[],[],[],[]]157 blocker_list = []158 myfont = pygame.font.SysFont('Comic Sans MS', 30)159 #Creating Objects:160 #Ship 161 s = Ship()162 #Invaders163 for x in range(80, 1000, 60):164 for y in range(30, 100, 70):165 #2166 i = Invader(x, y, 'invader2.png')167 invader_list[0].append(i)168 for y in range(95, 165, 70):169 #4170 i = Invader(x, y , 'invader4.png')171 invader_list[1].append(i)172 for y in range(160, 230, 70):173 i = Invader(x, y , 'invader1.png')174 invader_list[2].append(i)175 for y in range(225, 295, 70):176 i = Invader(x, y , 'invader5.png')177 invader_list[3].append(i)178 for y in range(290, 360, 70):179 i = Invader(x, y , 'invader3.png')180 invader_list[4].append(i)181 for y in range(355, 425, 70):182 i = Invader(x, y , 'invader6.png')183 invader_list[5].append(i)184 #Blockers185 for x in range(30, 1170, 170):186 blocker = Blocker(x,450)187 blocker_list.append(blocker)188 s.health = 20189 while (1):190 pygame.display.update()191 screen.fill((0,0,0))192 #Game Over / Win193 if s.health == 0:194 break195 if (len(invader_list[0]) == 0 and len(invader_list[1]) == 0 and 196 len(invader_list[2]) == 0 and len(invader_list[3]) == 0 and197 len(invader_list[4]) == 0 and len(invader_list[5]) == 0):198 pygame.quit()199 sys.exit()200 #Updates201 s.update()202 for n in range(5):203 for invader in invader_list[n]:204 invader.update()205 for blockers in blocker_list:206 blockers.update()207 #Collisions208 #Ship's bullets and invaders209 for bullet in s.bullet_list:210 for invader in invader_list[0]:211 if bullet.rect.colliderect(invader.rect):212 if bullet not in s.bullet_list:213 continue214 invader_list[0].remove(invader)215 pygame.mixer.music.load('invaderkilled.wav')216 pygame.mixer.music.play(0)217 s.bullet_list.remove(bullet)218 score += 30219 for invader in invader_list[1]:220 if bullet.rect.colliderect(invader.rect):221 if bullet not in s.bullet_list:222 continue223 invader_list[1].remove(invader)224 sound1 = pygame.mixer.Sound('invaderkilled.wav')225 sound1.set_volume(0.1)226 sound1.play()227 sound1.fadeout(1000)228 s.bullet_list.remove(bullet)229 score += 30230 for invader in invader_list[2]:231 if bullet.rect.colliderect(invader.rect):232 if bullet not in s.bullet_list:233 continue234 invader_list[2].remove(invader)235 sound1 = pygame.mixer.Sound('invaderkilled.wav')236 sound1.set_volume(0.1)237 sound1.play()238 sound1.fadeout(1000)239 s.bullet_list.remove(bullet)240 score += 20241 for invader in invader_list[3]:242 if bullet.rect.colliderect(invader.rect):243 if bullet not in s.bullet_list:244 continue245 invader_list[3].remove(invader)246 sound1 = pygame.mixer.Sound('invaderkilled.wav')247 sound1.set_volume(0.1)248 sound1.play()249 sound1.fadeout(1000)250 s.bullet_list.remove(bullet)251 score += 20252 for invader in invader_list[4]:253 if bullet.rect.colliderect(invader.rect):254 if bullet not in s.bullet_list:255 continue256 invader_list[4].remove(invader)257 sound1 = pygame.mixer.Sound('invaderkilled.wav')258 sound1.set_volume(0.1)259 sound1.play()260 sound1.fadeout(1000)261 s.bullet_list.remove(bullet)262 score += 10263 for invader in invader_list[5]:264 265 if bullet.rect.colliderect(invader.rect):266 if bullet not in s.bullet_list:267 continue268 invader_list[5].remove(invader)269 sound1 = pygame.mixer.Sound('invaderkilled.wav')270 sound1.set_volume(0.1)271 sound1.play()272 sound1.fadeout(1000)273 s.bullet_list.remove(bullet)274 score += 10275 for blockers in blocker_list:276 for block in blockers.block_list:277 for bullet in s.bullet_list:278 if bullet.rect.colliderect(block.rect):279 if block not in blockers.block_list:280 continue281 blockers.block_list.remove(block)282 s.bullet_list.remove(bullet)283 #Invader bullets and ship284 for invader in invader_list:285 for invader_bullet in invader[0].bullet_list:286 if s.rect.colliderect(invader_bullet.rect):287 invader[0].bullet_list.remove(invader_bullet)288 s.health -= 1289 290 for invader_bullet in invader[1].bullet_list:291 if s.rect.colliderect(invader_bullet.rect):292 invader[1].bullet_list.remove(invader_bullet)293 s.health -= 1294 295 for invader_bullet in invader[2].bullet_list:296 if s.rect.colliderect(invader_bullet.rect):297 invader[2].bullet_list.remove(invader_bullet)298 s.health -= 1299 300 for invader_bullet in invader[3].bullet_list:301 if s.rect.colliderect(invader_bullet.rect):302 invader[3].bullet_list.remove(invader_bullet)303 s.health -= 1304 305 for invader_bullet in invader[4].bullet_list:306 if s.rect.colliderect(invader_bullet.rect):307 invader[4].bullet_list.remove(invader_bullet)308 s.health -= 1309 310 for invader_bullet in invader[5].bullet_list:311 if s.rect.colliderect(invader_bullet.rect):312 invader[5].bullet_list.remove(invader_bullet)313 s.health -= 1314 315 #Ship "Control" keys316 keys = pygame.key.get_pressed()317 318 if keys[pygame.K_RIGHT]:319 s.x += s.speed_x320 if keys[pygame.K_LEFT]:321 s.x -= s.speed_x322 323 for event in pygame.event.get():324 if event.type == QUIT:325 pygame.quit()326 sys.exit()327 if event.type == MOUSEBUTTONDOWN:328 b = Bullets(s.x+25, s.y, 'greenbullet.png')329 b.ship_bullet()330 s.bullet_list.append(b)331 textsurface = myfont.render('Score: %s'%score, False, (255, 255, 255))332 screen.blit(textsurface, (0,0))333#Calling home screen & game loop334game_Intro()...
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!!