Best Python code snippet using Airtest
blocks program.py
Source:blocks program.py  
1"""2    Charlotte S. Blocks Program.3    Copyright (C) 2020 Charlotte S.4    This program is free software: you can redistribute it and/or modify5    it under the terms of the GNU General Public License as published by6    the Free Software Foundation, either version 3 of the License, or7    (at your option) any later version.8    9    This program is distributed in the hope that it will be useful,10    but WITHOUT ANY WARRANTY; without even the implied warranty of11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12    GNU General Public License for more details.13    14    You can contact the author and get a copy of the original code from:15    https://github.com/xftransformers16    17    You should have received a copy of the GNU General Public License18    along with this program.  If not, see <https://www.gnu.org/licenses/>.19"""20"""21	This program is based on and extends 22	the examples from [https://simplegametutorials.github.io/]23"""24import random25import pgzrun26piece_structures = [27    [28        [29            [' ', ' ', ' ', ' '],30            ['i', 'i', 'i', 'i'],31            [' ', ' ', ' ', ' '],32            [' ', ' ', ' ', ' '],33        ],34        [35            [' ', 'i', ' ', ' '],36            [' ', 'i', ' ', ' '],37            [' ', 'i', ' ', ' '],38            [' ', 'i', ' ', ' '],39        ],40    ],41    [42        [43            [' ', ' ', ' ', ' '],44            [' ', 'o', 'o', ' '],45            [' ', 'o', 'o', ' '],46            [' ', ' ', ' ', ' '],47        ],48    ],49    [50        [51            [' ', ' ', ' ', ' '],52            ['j', 'j', 'j', ' '],53            [' ', ' ', 'j', ' '],54            [' ', ' ', ' ', ' '],55        ],56        [57            [' ', 'j', ' ', ' '],58            [' ', 'j', ' ', ' '],59            ['j', 'j', ' ', ' '],60            [' ', ' ', ' ', ' '],61        ],62        [63            ['j', ' ', ' ', ' '],64            ['j', 'j', 'j', ' '],65            [' ', ' ', ' ', ' '],66            [' ', ' ', ' ', ' '],67        ],68        [69            [' ', 'j', 'j', ' '],70            [' ', 'j', ' ', ' '],71            [' ', 'j', ' ', ' '],72            [' ', ' ', ' ', ' '],73        ],74    ],75    [76        [77            [' ', ' ', ' ', ' '],78            ['l', 'l', 'l', ' '],79            ['l', ' ', ' ', ' '],80            [' ', ' ', ' ', ' '],81        ],82        [83            [' ', 'l', ' ', ' '],84            [' ', 'l', ' ', ' '],85            [' ', 'l', 'l', ' '],86            [' ', ' ', ' ', ' '],87        ],88        [89            [' ', ' ', 'l', ' '],90            ['l', 'l', 'l', ' '],91            [' ', ' ', ' ', ' '],92            [' ', ' ', ' ', ' '],93        ],94        [95            ['l', 'l', ' ', ' '],96            [' ', 'l', ' ', ' '],97            [' ', 'l', ' ', ' '],98            [' ', ' ', ' ', ' '],99        ],100    ],101    [102        [103            [' ', ' ', ' ', ' '],104            ['t', 't', 't', ' '],105            [' ', 't', ' ', ' '],106            [' ', ' ', ' ', ' '],107        ],108        [109            [' ', 't', ' ', ' '],110            [' ', 't', 't', ' '],111            [' ', 't', ' ', ' '],112            [' ', ' ', ' ', ' '],113        ],114        [115            [' ', 't', ' ', ' '],116            ['t', 't', 't', ' '],117            [' ', ' ', ' ', ' '],118            [' ', ' ', ' ', ' '],119        ],120        [121            [' ', 't', ' ', ' '],122            ['t', 't', ' ', ' '],123            [' ', 't', ' ', ' '],124            [' ', ' ', ' ', ' '],125        ],126    ],127    [128        [129            [' ', ' ', ' ', ' '],130            [' ', 's', 's', ' '],131            ['s', 's', ' ', ' '],132            [' ', ' ', ' ', ' '],133        ],134        [135            ['s', ' ', ' ', ' '],136            ['s', 's', ' ', ' '],137            [' ', 's', ' ', ' '],138            [' ', ' ', ' ', ' '],139        ],140    ],141    [142        [143            [' ', ' ', ' ', ' '],144            ['z', 'z', ' ', ' '],145            [' ', 'z', 'z', ' '],146            [' ', ' ', ' ', ' '],147        ],148        [149            [' ', 'z', ' ', ' '],150            ['z', 'z', ' ', ' '],151            ['z', ' ', ' ', ' '],152            [' ', ' ', ' ', ' '],153        ],154    ],155]156piece_x_count = 4157piece_y_count = 4158grid_x_count = 10159grid_y_count = 18160timer_limit = 0.5161def new_sequence():162    global sequence163    sequence = list(range(len(piece_structures)))164    random.shuffle(sequence)165def new_piece():166    global piece_x167    global piece_y168    global piece_type169    global piece_rotation170    piece_x = 3171    piece_y = 0172    piece_type = sequence.pop()173    if len(sequence) == 0:174        new_sequence()175    piece_rotation = 0176def reset():177    global inert178    global timer179    inert = []180    for y in range(grid_y_count):181        inert.append([])182        for x in range(grid_x_count):183            inert[y].append(' ')184    timer = 0185    new_sequence()186    new_piece()187reset()188def can_piece_move(test_x, test_y, test_rotation):189    for y in range(piece_y_count):190        for x in range(piece_x_count):191            test_block_x = test_x + x192            test_block_y = test_y + y193            if (194                piece_structures[piece_type][test_rotation][y][x] != ' ' and (195                    test_block_x < 0196                    or test_block_x >= grid_x_count197                    or test_block_y >= grid_y_count198                    or inert[test_block_y][test_block_x] != ' '199                )200            ):201                return False202    return True203def update(dt):204    global timer205    global piece_y206    timer += dt207    if timer >= timer_limit:208        timer = 0209        test_y = piece_y + 1210        if can_piece_move(piece_x, test_y, piece_rotation):211            piece_y = test_y212        else:213            # Add piece to inert214            for y in range(piece_y_count):215                for x in range(piece_x_count):216                    block = piece_structures[piece_type][piece_rotation][y][x]217                    if block != ' ':218                        inert[piece_y + y][piece_x + x] = block219            # Find complete rows220            for y in range(grid_y_count):221                complete = True222                for x in range(grid_x_count):223                    if inert[y][x] == ' ':224                        complete = False225                        break226                if complete:227                    for ry in range(y, 1, -1):228                        for rx in range(grid_x_count):229                            inert[ry][rx] = inert[ry - 1][rx]230                    for rx in range(grid_x_count):231                        inert[0][rx] = ' '232            new_piece()233            if not can_piece_move(piece_x, piece_y, piece_rotation):234                reset()235def on_key_down(key):236    global piece_rotation237    global piece_x238    global piece_y239    global timer240    if key == keys.X:241        test_rotation = piece_rotation + 1242        if test_rotation > len(piece_structures[piece_type]) - 1:243            test_rotation = 0244        if can_piece_move(piece_x, piece_y, test_rotation):245            piece_rotation = test_rotation246    elif key == keys.Z:247        test_rotation = piece_rotation - 1248        if test_rotation < 0:249            test_rotation = len(piece_structures[piece_type]) - 1250        if can_piece_move(piece_x, piece_y, test_rotation):251            piece_rotation = test_rotation252    elif key == keys.LEFT:253        test_x = piece_x - 1254        if can_piece_move(test_x, piece_y, piece_rotation):255            piece_x = test_x256    elif key == keys.RIGHT:257        test_x = piece_x + 1258        if can_piece_move(test_x, piece_y, piece_rotation):259            piece_x = test_x260    elif key == keys.C:261        while can_piece_move(piece_x, piece_y + 1, piece_rotation):262            piece_y += 1263            timer = timer_limit264def draw():265    screen.fill((255, 255, 255))266    def draw_block(block, x, y):267        colors = {268            ' ': (222, 222, 222),269            'i': (120, 195, 239),270            'j': (236, 231, 108),271            'l': (124, 218, 193),272            'o': (234, 177, 121),273            's': (211, 136, 236),274            't': (248, 147, 196),275            'z': (169, 221, 118),276            'preview': (190, 190, 190),277        }278        color = colors[block]279        block_size = 20280        block_draw_size = block_size - 1281        screen.draw.filled_rect(282            Rect(283                x * block_size, y * block_size,284                block_draw_size, block_draw_size285            ),286            color=color287        )288    offset_x = 2289    offset_y = 5290    for y in range(grid_y_count):291        for x in range(grid_x_count):292            draw_block(inert[y][x], x + offset_x, y + offset_y)293    for y in range(piece_y_count):294        for x in range(piece_x_count):295            block = piece_structures[piece_type][piece_rotation][y][x]296            if block != ' ':297                draw_block(298                    block,299                    x + piece_x + offset_x,300                    y + piece_y + offset_y301                )302    for y in range(piece_y_count):303        for x in range(piece_x_count):304            block = piece_structures[sequence[-1]][0][y][x]305            if block != ' ':306                draw_block('preview', x + 5, y + 1)...blocks.py
Source:blocks.py  
1import random, sys, pygame2piece_structures = [3    [4        [5            [" ", " ", " ", " "],6            ["i", "i", "i", "i"],7            [" ", " ", " ", " "],8            [" ", " ", " ", " "],9        ],10        [11            [" ", "i", " ", " "],12            [" ", "i", " ", " "],13            [" ", "i", " ", " "],14            [" ", "i", " ", " "],15        ],16    ],17    [18        [19            [" ", " ", " ", " "],20            [" ", "o", "o", " "],21            [" ", "o", "o", " "],22            [" ", " ", " ", " "],23        ],24    ],25    [26        [27            [" ", " ", " ", " "],28            ["j", "j", "j", " "],29            [" ", " ", "j", " "],30            [" ", " ", " ", " "],31        ],32        [33            [" ", "j", " ", " "],34            [" ", "j", " ", " "],35            ["j", "j", " ", " "],36            [" ", " ", " ", " "],37        ],38        [39            ["j", " ", " ", " "],40            ["j", "j", "j", " "],41            [" ", " ", " ", " "],42            [" ", " ", " ", " "],43        ],44        [45            [" ", "j", "j", " "],46            [" ", "j", " ", " "],47            [" ", "j", " ", " "],48            [" ", " ", " ", " "],49        ],50    ],51    [52        [53            [" ", " ", " ", " "],54            ["l", "l", "l", " "],55            ["l", " ", " ", " "],56            [" ", " ", " ", " "],57        ],58        [59            [" ", "l", " ", " "],60            [" ", "l", " ", " "],61            [" ", "l", "l", " "],62            [" ", " ", " ", " "],63        ],64        [65            [" ", " ", "l", " "],66            ["l", "l", "l", " "],67            [" ", " ", " ", " "],68            [" ", " ", " ", " "],69        ],70        [71            ["l", "l", " ", " "],72            [" ", "l", " ", " "],73            [" ", "l", " ", " "],74            [" ", " ", " ", " "],75        ],76    ],77    [78        [79            [" ", " ", " ", " "],80            ["t", "t", "t", " "],81            [" ", "t", " ", " "],82            [" ", " ", " ", " "],83        ],84        [85            [" ", "t", " ", " "],86            [" ", "t", "t", " "],87            [" ", "t", " ", " "],88            [" ", " ", " ", " "],89        ],90        [91            [" ", "t", " ", " "],92            ["t", "t", "t", " "],93            [" ", " ", " ", " "],94            [" ", " ", " ", " "],95        ],96        [97            [" ", "t", " ", " "],98            ["t", "t", " ", " "],99            [" ", "t", " ", " "],100            [" ", " ", " ", " "],101        ],102    ],103    [104        [105            [" ", " ", " ", " "],106            [" ", "s", "s", " "],107            ["s", "s", " ", " "],108            [" ", " ", " ", " "],109        ],110        [111            ["s", " ", " ", " "],112            ["s", "s", " ", " "],113            [" ", "s", " ", " "],114            [" ", " ", " ", " "],115        ],116    ],117    [118        [119            [" ", " ", " ", " "],120            ["z", "z", " ", " "],121            [" ", "z", "z", " "],122            [" ", " ", " ", " "],123        ],124        [125            [" ", "z", " ", " "],126            ["z", "z", " ", " "],127            ["z", " ", " ", " "],128            [" ", " ", " ", " "],129        ],130    ],131]132piece_x_count = 4133piece_y_count = 4134grid_x_count = 10135grid_y_count = 18136timer_limit = 0.5137def new_sequence():138    global sequence139    sequence = list(range(len(piece_structures)))140    random.shuffle(sequence)141def new_piece():142    global piece_x143    global piece_y144    global piece_type145    global piece_rotation146    piece_x = 3147    piece_y = 0148    piece_type = sequence.pop()149    if len(sequence) == 0:150        new_sequence()151    piece_rotation = 0152def reset():153    global inert154    global timer155    inert = []156    for y in range(grid_y_count):157        inert.append([])158        for x in range(grid_x_count):159            inert[y].append(" ")160    timer = 0161    new_sequence()162    new_piece()163reset()164def can_piece_move(test_x, test_y, test_rotation):165    for y in range(piece_y_count):166        for x in range(piece_x_count):167            test_block_x = test_x + x168            test_block_y = test_y + y169            if piece_structures[piece_type][test_rotation][y][x] != " " and (170                test_block_x < 0171                or test_block_x >= grid_x_count172                or test_block_y >= grid_y_count173                or inert[test_block_y][test_block_x] != " "174            ):175                return False176    return True177def update(dt):178    global timer179    global piece_y180    timer += dt181    if timer >= timer_limit:182        timer = 0183        test_y = piece_y + 1184        if can_piece_move(piece_x, test_y, piece_rotation):185            piece_y = test_y186        else:187            # Add piece to inert188            for y in range(piece_y_count):189                for x in range(piece_x_count):190                    block = piece_structures[piece_type][piece_rotation][y][x]191                    if block != " ":192                        inert[piece_y + y][piece_x + x] = block193            # Find complete rows194            for y in range(grid_y_count):195                complete = True196                for x in range(grid_x_count):197                    if inert[y][x] == " ":198                        complete = False199                        break200                if complete:201                    for ry in range(y, 1, -1):202                        for rx in range(grid_x_count):203                            inert[ry][rx] = inert[ry - 1][rx]204                    for rx in range(grid_x_count):205                        inert[0][rx] = " "206            new_piece()207            if not can_piece_move(piece_x, piece_y, piece_rotation):208                reset()209def on_key_down(key):210    global piece_rotation211    global piece_x212    global piece_y213    global timer214    if key == keys.X:215        test_rotation = piece_rotation + 1216        if test_rotation > len(piece_structures[piece_type]) - 1:217            test_rotation = 0218        if can_piece_move(piece_x, piece_y, test_rotation):219            piece_rotation = test_rotation220    elif key == keys.Z:221        test_rotation = piece_rotation - 1222        if test_rotation < 0:223            test_rotation = len(piece_structures[piece_type]) - 1224        if can_piece_move(piece_x, piece_y, test_rotation):225            piece_rotation = test_rotation226    elif key == keys.LEFT:227        test_x = piece_x - 1228        if can_piece_move(test_x, piece_y, piece_rotation):229            piece_x = test_x230    elif key == keys.RIGHT:231        test_x = piece_x + 1232        if can_piece_move(test_x, piece_y, piece_rotation):233            piece_x = test_x234    elif key == keys.C:235        while can_piece_move(piece_x, piece_y + 1, piece_rotation):236            piece_y += 1237            timer = timer_limit238def draw():239    screen.fill((255, 255, 255))240    def draw_block(block, x, y):241        colors = {242            " ": (222, 222, 222),243            "i": (120, 195, 239),244            "j": (236, 231, 108),245            "l": (124, 218, 193),246            "o": (234, 177, 121),247            "s": (211, 136, 236),248            "t": (248, 147, 196),249            "z": (169, 221, 118),250            "preview": (190, 190, 190),251        }252        color = colors[block]253        block_size = 20254        block_draw_size = block_size - 1255        screen.draw.filled_rect(256            Rect(x * block_size, y * block_size, block_draw_size, block_draw_size),257            color=color,258        )259    offset_x = 2260    offset_y = 5261    for y in range(grid_y_count):262        for x in range(grid_x_count):263            draw_block(inert[y][x], x + offset_x, y + offset_y)264    for y in range(piece_y_count):265        for x in range(piece_x_count):266            block = piece_structures[piece_type][piece_rotation][y][x]267            if block != " ":268                draw_block(block, x + piece_x + offset_x, y + piece_y + offset_y)269    for y in range(piece_y_count):270        for x in range(piece_x_count):271            block = piece_structures[sequence[-1]][0][y][x]272            if block != " ":273                draw_block("preview", x + 5, y + 1)274WIDTH = 20 * 14...test_rotation.py
Source:test_rotation.py  
1from nose.tools import assert_equal2class TestRotation(object):3    def test_rotation(self):4        rotation = Rotation()5        assert_equal(rotation.is_rotation('o', 'oo'), False)6        assert_equal(rotation.is_rotation(None, 'foo'), False)7        assert_equal(rotation.is_rotation('', 'foo'), False)8        assert_equal(rotation.is_rotation('', ''), True)9        assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)10        print('Success: test_rotation')11def main():12    test = TestRotation()13    test.test_rotation()14if __name__ == '__main__':...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!!
