{"id":96,"date":"2026-05-01T14:09:08","date_gmt":"2026-05-01T14:09:08","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/r_najim\/?p=96"},"modified":"2026-05-01T14:09:08","modified_gmt":"2026-05-01T14:09:08","slug":"friday-may-1-2026","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/r_najim\/2026\/05\/01\/friday-may-1-2026\/","title":{"rendered":"Friday May 1, 2026"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=JkjiFPNH0Ng\">https:\/\/www.youtube.com\/watch?v=JkjiFPNH0Ng<\/a><\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>This Free Friday I am working on a project on YouTube a Tetris game.<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import pygame \nimport random\nimport sys\n\npygame.init()\n\nWIDTH, HEIGHT = 300, 500\nFPS = 35\nCELL = 20\nROWS = (HEIGHT - 120) \/\/ CELL\nCOLS = WIDTH \/\/ CELL\n\nSCREEN = pygame.display.set_mode((WIDTH, HEIGHT), pygame.NOFRAME)\nclock = pygame.time.Clock()\npygame.display.set_caption(\"Tetris\")\n\nBLACK = (0,0,0)\nWHITE = (255,255, 255)\nBG_COLOR = (31, 25, 76)\nGRID = (31, 25, 132)\nWIN = (50, 230, 50)\nLOSE = (252, 91, 122)\n\nASSETS ={\n    1: pygame.image.load(\"Assets\/1.png\"),\n    2: pygame.image.load(\"Assets\/2.png\"),\n    3: pygame.image.load(\"Assets\/3.png\"),\n    4: pygame.image.load(\"Assets\/4.png\")\n}\n\nfont = pygame.font.SysFont(\"verdana\", 50)\nfont2 = pygame.font.SysFont(\"verdana\", 15)\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Initializes Pygame and creates a <strong>300\u00d7500 window<\/strong> with no frame.<\/li>\n\n\n\n<li>Defines constants:\n<ul class=\"wp-block-list\">\n<li>Grid cell size<\/li>\n\n\n\n<li>Number of rows\/columns<\/li>\n\n\n\n<li>Colors<\/li>\n\n\n\n<li>Block images (1.png, 2.png, etc.)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Creates fonts for text.<\/li>\n\n\n\n<li>This section prepares everything the game needs before running.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Shape:\n    VERSION = {\n        'I': &#91;&#91;1,5,9,13], &#91;4,5,6,7]], \n        'Z': &#91;&#91;4,5,9,10], &#91;2,6,5,9]],\n        'S': &#91;&#91;6,7,9,10], &#91;1,5,6,10]],\n        'L': &#91;&#91;1,2,6, 10], &#91;0,4,5,6], &#91;1,5,9,8], &#91;4,5,6,10]],\n        'J': &#91;&#91;1,2,6,10], &#91;5,6,7,9], &#91;2,6,10,11], &#91;3,5,6,7]],\n        'T': &#91;&#91;1,4,5,6], &#91;1,4,5,9], &#91; 4,5,6,9], &#91;1,5,6,9]],\n        'O': &#91;&#91;1,2,5,6]]\n    }\n\n    SHAPES = &#91;'I', 'Z', 'S', 'L', 'J', 'T', 'O']\n\n    def __init__(self,x,y):\n        self.x = x\n        self.y = y\n        self.type = random.choice(self.SHAPES)\n        self.shape = self.VERSION&#91;self.type]\n        self.color = random.randint(1,4)\n        self.orientation = 0\n\n    def image(self):\n        return self.shape&#91;self.orientation]\n    \n    def rotate(self):\n        self.orientation = (self.orientation + 1) % len(self.shape)\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This class represents a single falling Tetris piece.<\/li>\n\n\n\n<li>Each shape type (<code>I, Z, S, L, J, T, O<\/code>) has predefined rotation patterns.<\/li>\n\n\n\n<li>A shape has:\n<ul class=\"wp-block-list\">\n<li><code>x, y<\/code> position on the grid<\/li>\n\n\n\n<li>A random type<\/li>\n\n\n\n<li>A random color (1\u20134)<\/li>\n\n\n\n<li>A rotation index (<code>orientation<\/code>)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>image()<\/code> returns the current rotation pattern.<\/li>\n\n\n\n<li><code>rotate()<\/code> cycles through the available rotations.<\/li>\n\n\n\n<li>This class handles the behavior of individual tetrominoes.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Tetris:\n\n    def __init__(self, rows, cols):\n        self.rows = rows\n        self.cols = cols\n        self.score = 0\n        self.level = 1\n        self.grid = &#91;&#91;0 for j in range(cols)] for i in range(rows)]\n        self.next = None\n        self.end = False\n        self.new_shape()\n\n    def make_grid(self):\n        for i in range(self.rows + 1):\n            pygame.draw.line(SCREEN, GRID, (0,CELL*i), (WIDTH,CELL*i))\n        for j in range(self.rows + 1):\n            pygame.draw.line(SCREEN, GRID, (CELL*j, 0), (CELL*j, HEIGHT-120))\n\n    def new_shape(self):\n        if not self.next:\n            self.next = Shape(5,0)\n        self.figure = self.next\n        self.next = Shape(5,0)\n\n    def collision(self) -> bool:\n        for i in range(4):\n            for j in range(4):\n                if (i*4 *j) in self.figure.image():\n                    block_row = i + self.figure.y\n                    block_col = j + self.figure.x\n                    if(block_row >= self.rows or block_col >= self.cols or block_col &lt; 0 or self.grid(block_row) (block_col) > 0):\n                        return True\n                    \n            return False\n        \n\n    def freeze(self):\n        for i in range(4):\n            for j in range(4):\n                if (i*4+j) in self.figure.image():\n                    self.grid&#91;i+self.figure.y]&#91;j+self.figure.x] = self.figure.color\n\n        self.new_shape()\n        if self.collision():\n            self.end = True\n\n\n    def move_down(self):\n        self.figure.y += 1\n        if self.collision():\n            self.figure.y -= 1\n            self.freeze()\n\n    def left(self):\n        self.figure.x -= 1\n        if self.collision():\n            self.figure.x += 1\n\n    def right(self):\n        self.figure.y += 1\n        if self.collision():\n            self.figure.y -= 1\n\n    def freefall(self):\n        while not self.collision():\n            self.figure.y += 1\n        self.figure.y -= 1\n        self.freeze()\n\n    def rotate(self):\n        orientation = self.figure.orientation\n        self.figure.rotate()\n        if self.collision():\n            self.figure.orientation = orientation\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This class manages the entire game board.<\/li>\n\n\n\n<li>It stores:<\/li>\n\n\n\n<li>The grid (2D list of rows \u00d7 columns)<\/li>\n\n\n\n<li>The current falling shape<\/li>\n\n\n\n<li>The next shape<\/li>\n\n\n\n<li>Score, level, and game-over state<\/li>\n\n\n\n<li>It handles:<\/li>\n\n\n\n<li><strong>Drawing the grid<\/strong><\/li>\n\n\n\n<li><strong>Spawning new shapes<\/strong><\/li>\n\n\n\n<li><strong>Collision detection<\/strong><\/li>\n\n\n\n<li><strong>Freezing pieces into the grid<\/strong><\/li>\n\n\n\n<li><strong>Movement<\/strong> (left, right, down)<\/li>\n\n\n\n<li><strong>Rotation<\/strong><\/li>\n\n\n\n<li><strong>Instant drop<\/strong> (space bar)<\/li>\n\n\n\n<li>This is the core engine of the game.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    tetris = Tetris(ROWS, COLS)\n    counter = 0\n    move = True\n    run = True\n    space_pressed = False\n    while run:\n        SCREEN.fill(BG_COLOR)\n\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                run = False\n                sys.exit()\n\n        keys = pygame.key.get_pressed()\n        if not tetris.end:\n            if keys&#91;pygame.K_LEFT]:\n                tetris.left()\n            elif keys&#91;pygame.K_RIGHT]:\n                tetris.right()\n            elif keys&#91;pygame.K_DOWN]:\n                tetris.move_down()\n            elif keys&#91;pygame.K_UP]:\n                tetris.rotate()\n            elif keys&#91;pygame.K_SPACE]:\n                space_pressed = True\n        if keys&#91;pygame.K_ESCAPE] or keys&#91;pygame.K_q]:\n            run = False\n\n        counter += 1\n        if counter >= 15000:\n            counter = 0 \n\n        if move:\n            if counter % (FPS \/\/(tetris.level*2)) == 0:\n                if not tetris.end:\n                    if space_pressed:\n                        tetris.freefall()\n                        space_pressed = False\n                    else:\n                        tetris.move_down()\n\n        tetris.make_grid()\n\n        for x in range(ROWS):\n            for y in range(COLS):\n                if tetris.grid&#91;x]&#91;y] > 0:\n                    value = tetris.grid&#91;x]&#91;y]\n                    image = ASSETS&#91;value]\n                    SCREEN.blit(image, (x*CELL, y*CELL))\n                    pygame.draw.rect(SCREEN, WHITE, (y*CELL, x*CELL, CELL, CELL),1)\n\n        if tetris.figure:\n            for i in range(4):\n                for j in range(4):\n                    if (i *4 + j) in tetris.figure.image():\n                        shape = ASSETS&#91;tetris.figure.color]\n                        x = CELL * (tetris.figure.x + j)\n                        y = CELL * (tetris.figure.y + i)\n                        SCREEN.blit(shape, (x,y))\n                        pygame.draw.rect(SCREEN, WHITE, (x,y,CELL,CELL),1)\n\n\n        pygame.display.update()\n        clock.tick(FPS)\n\n\nif __name__ == \"__main__\":\n    main()\n     <\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Runs continuously until the player quits.<\/li>\n\n\n\n<li>It handles:<\/li>\n\n\n\n<li>Keyboard input:\n<ul class=\"wp-block-list\">\n<li>Arrow keys to move\/rotate<\/li>\n\n\n\n<li>Space for instant drop<\/li>\n\n\n\n<li>Escape\/Q to quit<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Automatic falling based on a timer<\/li>\n\n\n\n<li>Drawing:\n<ul class=\"wp-block-list\">\n<li>Background<\/li>\n\n\n\n<li>Grid lines<\/li>\n\n\n\n<li>Frozen blocks<\/li>\n\n\n\n<li>Current falling piece<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Updating the display each frame<\/li>\n\n\n\n<li>This loop is the heartbeat of the game.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":1,"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/posts\/96\/revisions\/97"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/r_najim\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}