{"id":75,"date":"2024-04-12T14:17:53","date_gmt":"2024-04-12T14:17:53","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/a_reynolds\/?p=75"},"modified":"2024-04-12T14:17:53","modified_gmt":"2024-04-12T14:17:53","slug":"free-friday-4-12-24","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/a_reynolds\/2024\/04\/12\/free-friday-4-12-24\/","title":{"rendered":"Free Friday (4\/12\/24)"},"content":{"rendered":"\n<p>Today I started a Tetris clone in python using pygame. I started off by creating a repository using git so I could work on this project from home. I then found a good tutorial online (<a href=\"https:\/\/www.techwithtim.net\/tutorials\/game-development-with-python\/tetris-pygame\/tutorial-1\">https:\/\/www.techwithtim.net\/tutorials\/game-development-with-python\/tetris-pygame\/tutorial-1<\/a>) To help me and I followed the first lesson.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here&#8217;s the window so far but underneath there are already over 150 lines of code:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"799\" height=\"736\" src=\"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-content\/uploads\/2024\/04\/Capture-3.png\" alt=\"\" class=\"wp-image-76\" srcset=\"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-content\/uploads\/2024\/04\/Capture-3.png 799w, https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-content\/uploads\/2024\/04\/Capture-3-300x276.png 300w, https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-content\/uploads\/2024\/04\/Capture-3-768x707.png 768w\" sizes=\"(max-width: 799px) 100vw, 799px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">import pygame<br>import random<br><br>#https:\/\/www.techwithtim.net\/tutorials\/game-development-with-python\/tetris-pygame\/tutorial-1<br># creating the data structure for pieces<br># setting up global vars<br># functions<br># - create_grid<br># - draw_grid<br># - draw_window<br># - rotating shape in main<br># - setting up the main<br><br>\"\"\"<br>10 x 20 square grid<br>shapes: S, Z, I, O, J, L, T<br>represented in order by 0 - 6<br>\"\"\"<br><br>pygame.font.init()<br><br># <sup data-fn=\"dd813132-172a-40ce-943a-3018ae930058\" class=\"fn\"><a href=\"#dd813132-172a-40ce-943a-3018ae930058\" id=\"dd813132-172a-40ce-943a-3018ae930058-link\">1<\/a><\/sup>GLOBALS VARS<br>screen_width = 800<br>screen_height = 700<br>play_width = 300  # meaning 300 \/\/ 10 = 30 width per block<br>play_height = 600  # meaning 600 \/\/ 20 = 20 height per block<br>block_size = 30<br><br>top_left_x = (screen_width - play_width) \/\/ 2<br>top_left_y = screen_height - play_height<br><br><br># SHAPE FORMATS<br><br>S = [['.....',<br>      '......',<br>      '..00..',<br>      '.00...',<br>      '.....'],<br>     ['.....',<br>      '..0..',<br>      '..00.',<br>      '...0.',<br>      '.....']]<br><br>Z = [['.....',<br>      '.....',<br>      '.00..',<br>      '..00.',<br>      '.....'],<br>     ['.....',<br>      '..0..',<br>      '.00..',<br>      '.0...',<br>      '.....']]<br><br>I = [['..0..',<br>      '..0..',<br>      '..0..',<br>      '..0..',<br>      '.....'],<br>     ['.....',<br>      '0000.',<br>      '.....',<br>      '.....',<br>      '.....']]<br><br>O = [['.....',<br>      '.....',<br>      '.00..',<br>      '.00..',<br>      '.....']]<br><br>J = [['.....',<br>      '.0...',<br>      '.000.',<br>      '.....',<br>      '.....'],<br>     ['.....',<br>      '..00.',<br>      '..0..',<br>      '..0..',<br>      '.....'],<br>     ['.....',<br>      '.....',<br>      '.000.',<br>      '...0.',<br>      '.....'],<br>     ['.....',<br>      '..0..',<br>      '..0..',<br>      '.00..',<br>      '.....']]<br><br>L = [['.....',<br>      '...0.',<br>      '.000.',<br>      '.....',<br>      '.....'],<br>     ['.....',<br>      '..0..',<br>      '..0..',<br>      '..00.',<br>      '.....'],<br>     ['.....',<br>      '.....',<br>      '.000.',<br>      '.0...',<br>      '.....'],<br>     ['.....',<br>      '.00..',<br>      '..0..',<br>      '..0..',<br>      '.....']]<br><br>T = [['.....',<br>      '..0..',<br>      '.000.',<br>      '.....',<br>      '.....'],<br>     ['.....',<br>      '..0..',<br>      '..00.',<br>      '..0..',<br>      '.....'],<br>     ['.....',<br>      '.....',<br>      '.000.',<br>      '..0..',<br>      '.....'],<br>     ['.....',<br>      '..0..',<br>      '.00..',<br>      '..0..',<br>      '.....']]<br><br>shapes = [S, Z, I, O, J, L, T]<br>shape_colors = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0), (255, 165, 0), (0, 0, 255), (128, 0, 128)]<br># index 0 - 6 represent shape<br><br><br>class Piece(object):<br>    def __init__(self, x, y, shape):<br>        self.x = x<br>        self.y = y<br>        self.shape = shape<br>        self.color = shape_colors[shapes.index(shape)]<br>        self.rotiation = 0<br><br>def create_grid(locked_positions={}):<br>    grid = [[(0,0,0) for x in range(10)] for x in range(20)]<br><br>    for i in range(len(grid)):<br>        for j in range(len(grid[i])):<br>            if (j, i) in locked_positions:<br>                c = locked_positions[(j, i)]<br>                grid[i][c] = c<br>    return grid<br><br><br>def convert_shape_format(shape):<br>    pass<br><br>def valid_space(shape, grid):<br>    pass<br><br>def check_lost(positions):<br>    pass<br><br>def get_shape():<br>    return Piece(5, 0, random.choice(shapes))<br><br>def draw_text_middle(text, size, color, surface):  <br>    pass<br>   <br>def draw_grid(surface, grid):<br>    for i in range(len(grid)):<br>        for j in range(len(grid[i])):<br>            pygame.draw.rect(surface, grid[i][j], (top_left_x + j*block_size, top_left_y + i*block_size, block_size, block_size), 0)<br><br>    pygame.draw.rect(surface, (255, 0, 0), (top_left_x, top_left_y, play_width, play_height), 4)<br><br>def clear_rows(grid, locked):<br>    pass<br><br>def draw_next_shape(shape, surface):<br>    pass<br><br>def draw_window(surface, grid):<br>    surface.fill((0,0,0))<br><br>    pygame.font.init()<br>    font = pygame.font.SysFont('comicsans', 60)<br>    label = font.render('Tetris', 1, (255,255,255))<br><br>    surface.blit(label, (top_left_x + play_width \/ 2 - (label.get_width()\/2), 30))<br>    draw_grid(surface, grid)<br>    pygame.display.update()<br><br><br>def main(win):<br>    <br>    locked_positions = {}<br>    grid = create_grid(locked_positions)<br><br>    change_piece = False<br>    run = True<br>    current_piece = get_shape()<br>    next_piece = get_shape()<br>    clock = pygame.time.Clock()<br>    fall_time = 0<br><br>    while run:<br>        for event in pygame.event.get():<br>            if event.type == pygame.QUIT:<br>                run = False<br>            if event.type == pygame.KEYDOWN:<br>                if event.key == pygame.K_LEFT:<br>                    current_piece.x -= 1<br>                    if not(valid_space(current_piece, grid)):<br>                        current_piece += 1<br>                if event.key == pygame.K_RIGHT:<br>                    current_piece.x += 1<br>                    if not(valid_space(current_piece, grid)):<br>                        current_piece -= 1<br>                if event.key == pygame.K_DOWN:<br>                    current_piece.y += 1<br>                    if not(valid_space(current_piece, grid)):<br>                        current_piece.y -= 1<br>                if event.key == pygame.K_UP:<br>                    current_piece.rotiation += 1<br>                    if not(valid_space(current_piece, grid)):<br>                        current_piece -= 1<br>        <br>        draw_window(win, grid)<br>                        <br><br>def main_menu(win):<br>    main(win)<br>    pass<br><br>win = pygame.display.set_mode((screen_width, screen_height))<br>pygame.display.set_caption('Tetris')<br>main_menu(win)  # start game<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today I started a Tetris clone in python using pygame. I started off by creating a repository using git so I could work on this project from home. I then found a good tutorial online (https:\/\/www.techwithtim.net\/tutorials\/game-development-with-python\/tetris-pygame\/tutorial-1) To help me and I followed the first lesson. Here&#8217;s the window so far but underneath there are already [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"[{\"id\":\"dd813132-172a-40ce-943a-3018ae930058\",\"content\":\"\"}]"},"categories":[1],"tags":[],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":1,"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/posts\/75\/revisions\/77"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_reynolds\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}