{"id":6627,"date":"2020-07-25T08:54:50","date_gmt":"2020-07-25T06:54:50","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=6627"},"modified":"2020-07-26T08:12:31","modified_gmt":"2020-07-26T06:12:31","slug":"python-vs-snake-v-1-8-3","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/python-vs-snake-v-1-8-3\/","title":{"rendered":"Python vs. Snake &#8211; v. 1.8.3"},"content":{"rendered":"<div style=\"width: 640px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-6627-1\" width=\"640\" height=\"640\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/PYSNAKE184.mp4?_=1\" \/><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/PYSNAKE184.mp4\">https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/PYSNAKE184.mp4<\/a><\/video><\/div>\n<p>This is the updated version of <a href=\"https:\/\/formazione.itch.io\/pysnake\">Python vs. Snake<\/a>, attempt to remake the classic snake with pygame.<\/p>\n<p>Changes:<\/p>\n<ul>\n<li>the menu (some experimental visual and musical stuffs)<\/li>\n<li>the snake head (now it has some shape)<\/li>\n<li>remade many parts of the code (just to experiment cleaner and fancy code)<\/li>\n<\/ul>\n<a href=\"https:\/\/github.com\/formazione\/snake\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6600\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_2.png\" alt=\"\" width=\"208\" height=\"156\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_2.png 800w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_2-320x240.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_2-768x576.png 768w\" sizes=\"auto, (max-width: 208px) 100vw, 208px\" \/><\/a>\n<h2>The new menu and the &#8216;new&#8217; snake graphic<\/h2>\n<p>i added the snake in the menu. When you move the mouse or click some keys the snake moves and some sounds can be played. Now the music is off by default, but you can turn it on and off with m key. Press s to start.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/nemu_1_8_3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6629\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/nemu_1_8_3.png\" alt=\"\" width=\"402\" height=\"432\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/nemu_1_8_3.png 402w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/nemu_1_8_3-320x344.png 320w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/a><\/p>\n<p>The little movement of the snake in the menu is done with this code that build the snake, change position until the border and then place it back to the start and so on:<\/p>\n<pre class=\"lang:default decode:true \">xs = 5\r\nys = 6\r\ndef show_snake():\r\n    'show the snake for the menu'\r\n    global xs, ys\r\n\r\n    if xs &lt; 21:\r\n        xs += 1\r\n    else:\r\n        xs = 5\r\n    psnake = [[xs, ys],[xs-1, ys], [xs-2, ys]]\r\n    l = []\r\n    s = build_snake(l, psnake)\r\n    l.extend(s)\r\n    window.blits(blit_sequence=(l))\r\n    pygame.display.update()<\/pre>\n<p>&nbsp;<\/p>\n<h2>How the snake is made now<\/h2>\n<p>The snake is made blitting the parts of it in the coordinates of snake.body that are costantly updated by the main while loop. I created a new function just for it, putting the code outside of blit_all() to make the code more clear and to reuse it to show the snake in the menu.<\/p>\n<pre class=\"lang:default decode:true\">def build_snake(list_of_sprites, snake):\r\n    'Builds the snake getting the coordinate from snake.body and blitting \\\r\n    a square on every coordinate, it also 2 squares for the eyes'\r\n    btail = (blacktail, (snake[-1][0] * BLOCK_SIZE, snake[-1][1] * BLOCK_SIZE))\r\n    for n, pos in enumerate(snake):\r\n        # bxy = (xy, (pos[0] * BLOCK_SIZE, pos[1] * BLOCK_SIZE))\r\n        if n == 0:\r\n            bbody = (head, (pos[0] * BLOCK_SIZE, pos[1] * BLOCK_SIZE))\r\n            eye1 = (head2, (pos[0] * BLOCK_SIZE, pos[1] * BLOCK_SIZE + 1))\r\n            eye2 = (head2, (pos[0] * BLOCK_SIZE + 10, pos[1] * BLOCK_SIZE + 1))\r\n        else:\r\n            bbody = (body, (pos[0] * BLOCK_SIZE, pos[1] * BLOCK_SIZE))\r\n        list_of_sprites.append(bbody)\r\n\r\n    snake_body = [bbody, eye1, eye2, btail]\r\n    return snake_body<\/pre>\n<p>The tecnique is the same for all the surfaces. They go into the snake_body list and then the are blitted with window.blits in the blit_sequence argument<\/p>\n<pre class=\"lang:default decode:true \">def blit_all(food_pos):\r\n    \"blit xy body and tail, altogether, uses build_snake to make the snake, \\\r\n    like we did in the menu\"\r\n    global blacktail, body, fruit, bscore2\r\n\r\n    list_of_sprites = []\r\n    text_surface = write(f\"{score}\", food_pos[0] * BOARD_SIZE + 5, food_pos[1] * BOARD_SIZE + 3, color=\"Black\")\r\n    btext = (text_surface, (food_pos[0] * BOARD_SIZE + 5, food_pos[1] * BOARD_SIZE + 3))\r\n    b_score = write(f\"Score: {score}\", 0, 0)\r\n    bscore = (b_score, (0, 0))\r\n    b_score2 = (bscore2, (0, 0))\r\n    bfruit = (fruit, (food_pos[0] * BLOCK_SIZE, food_pos[1] * BLOCK_SIZE))\r\n    # Appending the snake body surfaces to the list of sprites\r\n    list_of_sprites.extend(build_snake(list_of_sprites, snake.body))\r\n    # Append the rest of the surfaces\r\n    for surface in (bfruit, btext, b_score2, bscore):\r\n        list_of_sprites.append(surface)\r\n    # Blit the sequence of surfaces and coordinates \r\n    window.blits(blit_sequence=(list_of_sprites))<\/pre>\n<p>&nbsp;<\/p>\n<p>The snake had some changes. Now the head is of a different color and got eyes.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_8_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6628\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_8_2.png\" alt=\"\" width=\"397\" height=\"420\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_8_2.png 397w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_8_2-320x339.png 320w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/a><\/p>\n<h2>The code<\/h2>\n<p>The code now is in 4 files<\/p>\n<ul>\n<li>main.py<\/li>\n<li>functions\/soundinit.py<\/li>\n<li>functions\/snake.py.init()<\/li>\n<li>functions\/costants.py<\/li>\n<\/ul>\n<h2>main.py<\/h2>\n<p>The menu is here<\/p>\n<pre class=\"lang:default decode:true \">from functions.snake import *\r\nfrom functions.soundinit import play, random_play\r\nimport random\r\n\r\n'''\r\nfuntions:\r\n    - soundinit.py\r\n        initialize sounds and let you play with play and random_play\r\n        play need the name of the file as argument\r\n        random_play does not need argument, but you can specify a number for randomness\r\n    - snake.py\r\n        the class that gives the starting position of the snake, builds the\r\n        snake.body list with the coordinates of the parts, add a new part or\r\n        moves it forward, check if it eats or goes out of the borders\r\n'''\r\n\r\n\r\ndef blit_all(food_pos):\r\n    \"blit xy body and tail, altogether, uses build_snake to make the snake, \\\r\n    like we did in the menu\"\r\n    global blacktail, body, fruit, bscore2\r\n\r\n    list_of_sprites = []\r\n    text_surface = write(f\"{score}\", food_pos[0] * BOARD_SIZE + 5, food_pos[1] * BOARD_SIZE + 3, color=\"Black\")\r\n    btext = (text_surface, (food_pos[0] * BOARD_SIZE + 5, food_pos[1] * BOARD_SIZE + 3))\r\n    b_score = write(f\"Score: {score}\", 0, 0)\r\n    bscore = (b_score, (0, 0))\r\n    b_score2 = (bscore2, (0, 0))\r\n    bfruit = (fruit, (food_pos[0] * BLOCK_SIZE, food_pos[1] * BLOCK_SIZE))\r\n    # Appending the snake body surfaces to the list of sprites\r\n    list_of_sprites.extend(build_snake(list_of_sprites, snake.body))\r\n    # Append the rest of the surfaces\r\n    for surface in (bfruit, btext, b_score2, bscore):\r\n        list_of_sprites.append(surface)\r\n    # Blit the sequence of surfaces and coordinates \r\n    window.blits(blit_sequence=(list_of_sprites))\r\n\r\ndef write(text_to_show, x=0, y=0, middle=0, color=\"Coral\"):\r\n    'To write some text on the screen for the menu and the score \\\r\n    if middle = 0, will put the text at 0,0 unless you specify coordinates \\\r\n    if middle = 1 it will put in the middle (on top)'\r\n    font = pygame.font.SysFont(text_to_show, 24)\r\n    text = font.render(text_to_show, 1, pygame.Color(color))\r\n    w = h = BOARD_SIZE * BLOCK_SIZE\r\n    if middle:\r\n        text_rect = text.get_rect(center=((w \/\/ 2, h \/\/ 2)))\r\n        text.blit(text, text_rect)\r\n    else:\r\n        text.blit(text, (x, y))\r\n    pygame.display.update()\r\n    return text\r\n\r\n\r\ndef build_snake(list_of_sprites, snake):\r\n    'Builds the snake getting the coordinate from snake.body and blitting \\\r\n    a square on every coordinate, it also 2 squares for the eyes'\r\n    btail = (blacktail, (snake[-1][0] * BLOCK_SIZE, snake[-1][1] * BLOCK_SIZE))\r\n    for n, pos in enumerate(snake):\r\n        # bxy = (xy, (pos[0] * BLOCK_SIZE, pos[1] * BLOCK_SIZE))\r\n        if n == 0:\r\n            bbody = (head, (pos[0] * BLOCK_SIZE, pos[1] * BLOCK_SIZE))\r\n            eye1 = (head2, (pos[0] * BLOCK_SIZE, pos[1] * BLOCK_SIZE + 1))\r\n            eye2 = (head2, (pos[0] * BLOCK_SIZE + 10, pos[1] * BLOCK_SIZE + 1))\r\n        else:\r\n            bbody = (body, (pos[0] * BLOCK_SIZE, pos[1] * BLOCK_SIZE))\r\n        list_of_sprites.append(bbody)\r\n\r\n    snake_body = [bbody, eye1, eye2, btail]\r\n    return snake_body\r\n\r\n\r\nxs = 5\r\nys = 6\r\ndef show_snake():\r\n    'show the snake for the menu'\r\n    global xs, ys\r\n\r\n    if xs &lt; 21:\r\n        xs += 1\r\n    else:\r\n        xs = 5\r\n    psnake = [[xs, ys],[xs-1, ys], [xs-2, ys]]\r\n    l = []\r\n    s = build_snake(l, psnake)\r\n    l.extend(s)\r\n    window.blits(blit_sequence=(l))\r\n    pygame.display.update()\r\n\r\n\r\ndef menu():\r\n    \"This is the menu that waits you to click the s key to start\"\r\n    global GAME_SPEED, score, xs, ys\r\n    xs = 5\r\n    pygame.display.set_caption(\"Python Snake v. 1.8.2\")\r\n    window.fill((0, 255, 0))\r\n    window.blit(write(\"PYTHON SNAKE 2020 - MADE WITH PYGAME\", middle=1), (0, 30))\r\n    window.blit(write(\"Press s to start\", middle=1), (0, 60))\r\n    window.blit(write(\"Press m to start \/ stop the music\", 0, 0), (0, 90))\r\n    window.blit(write(\"Use the arrow keys to move the snake in the game\", 0, 0), (0, 310))\r\n    window.blit(write(\"Move the mouse to hear some music\", 0, 0), (0, 330))\r\n    window.blit(write(\"and make the snake move in the menu\", 0, 0), (0, 350))\r\n    window.blit(write(\"Music is experimental\", 0, 0), (0, 380))\r\n    pygame.draw.line(window, (255, 255, 255), (0, 25), (400, 25), 2)\r\n    pygame.draw.line(window, (255, 255, 255), (0, 110), (400, 110), 2)\r\n    while True:\r\n        show_snake()\r\n        random_play()\r\n        event = pygame.event.wait()\r\n        if (event.type == pygame.QUIT):\r\n            break\r\n        if event.type == pygame.KEYDOWN:\r\n            press_escape = event.key == pygame.K_ESCAPE\r\n            if press_escape:\r\n                break\r\n            restart = (event.key == pygame.K_s)\r\n            if restart:\r\n                score = 0\r\n                GAME_SPEED = 8\r\n                window.fill((0, 0, 0))\r\n                snake.start()\r\n                start()\r\n                break\r\n        pygame.display.update()\r\n        clock.tick(GAME_SPEED)\r\n    pygame.quit()\r\n\r\n\r\ndef start():\r\n    \"Once you press the 's' key it runs, moves the snake a wait the user input\"\r\n    global GAME_SPEED, score, loop, music\r\n\r\n    go = \"RIGHT\"\r\n    food_pos = [random.randrange(1, BOARD_SIZE), random.randrange(1, BOARD_SIZE)]\r\n    loop = 1\r\n    while loop:\r\n        for event in pygame.event.get():\r\n            if event.type == pygame.QUIT:\r\n                loop = 0\r\n            elif event.type == pygame.KEYDOWN:\r\n                if event.key == pygame.K_ESCAPE:\r\n                    loop = 0\r\n                # You cannot move backwards\r\n                elif event.key == pygame.K_RIGHT:\r\n                    go = \"RIGHT\"\r\n                elif event.key == pygame.K_UP:\r\n                    go = \"UP\"\r\n                elif event.key == pygame.K_DOWN:\r\n                    go = \"DOWN\"\r\n                elif event.key == pygame.K_LEFT:\r\n                    go = \"LEFT\"\r\n                elif event.key == pygame.K_m:\r\n                    if music == 0:\r\n                        music = 1\r\n                    else:\r\n                        music = 0\r\n                snake.not_backwards(go)\r\n        # Moves and check if eats\r\n        if snake.move(food_pos):\r\n            play(\"click\")\r\n            score += 1\r\n            GAME_SPEED += 1\r\n            food_pos = [random.randrange(1, BOARD_SIZE), random.randrange(1, BOARD_SIZE)]\r\n        # Draw everything on\r\n        blit_all(food_pos)\r\n        if snake.check_collisions() == 1:\r\n            loop = 0\r\n            window.fill((0, 0, 0))\r\n            menu()\r\n        pygame.display.update()\r\n        clock.tick(GAME_SPEED)\r\n    pygame.quit()\r\n\r\nmenu()<\/pre>\n<h2>functions\/soundinit()<\/h2>\n<p>This manages the sound<\/p>\n<pre class=\"lang:default decode:true \">import pygame\r\nfrom glob import glob\r\nfrom pathlib import Path\r\nimport os\r\nimport random\r\n\r\ndef init(directory):\r\n    '''\r\n    How to use this module:\r\n\r\n    - THE FOLDER STRUCTURE\r\n\r\n    main.py\r\n        |\r\n        functions\r\n        |    |\r\n        |   soundinit.py\r\n        |\r\n        sounds\r\n            |\r\n            click.mp3        call this with play(\"click\")\r\n            Marker #1.mp3    call this with random_play() ... a random sound will be played for the files starting with Marker\r\n            ...\r\n\r\n    - HOW TO USE THIS\r\n\r\n    In the main.py (or other main file) import like this\r\n    ----------------------------------------------------------\r\n    from functions.soundinit import play, random_play\r\n    \r\n    play(\"click\")\r\n    random_play()\r\n    -----------------------------------------------------------\r\n    @ Giovanni Gatto 2020\r\n\r\n    '''\r\n\r\n    # This is to avoid lag\r\n    pygame.mixer.pre_init(44100, -16, 2, 512)\r\n    pygame.init()\r\n    pygame.mixer.quit()\r\n    pygame.mixer.init(44100, -16, 2, 512)\r\n    pygame.mixer.set_num_channels(32)\r\n    # Load all sounds\r\n    lsounds = glob(f\"{directory}\/*.mp3\")\r\n    # Dictionary with all sounds, keys are the name of wav\r\n    sounds = {}\r\n    random_sounds = []\r\n    for sound in lsounds:\r\n        filepath = Path(sound)\r\n        if filepath.stem.startswith(\"Marker\"):\r\n            random_sounds.append(pygame.mixer.Sound(f\"{filepath}\"))\r\n        else:\r\n            sounds[filepath.stem] = pygame.mixer.Sound(f\"{filepath}\")\r\n    return sounds, random_sounds\r\n\r\ndef play(snd):\r\n    \"Plays one of the sounds in the sounds folder using play('name')\"\r\n    print(snd)\r\n    pygame.mixer.Sound.play(sounds[snd])\r\n\r\n\r\ndef random_play(rnd=3):\r\n    \"Plays a random sounds at a randrange(1, 5) == rnd\"\r\n    if random.randrange(1, 5) == rnd:\r\n        sound = pygame.mixer.Sound(random.choice(random_sounds))\r\n        sound.set_volume(1 \/ random.randrange(1, 10))\r\n        sound.play()\r\n\r\nsounds, random_sounds = init(\"sounds\")<\/pre>\n<h2>functions\/costants.py<\/h2>\n<p>Some costants and the surfaces<\/p>\n<pre class=\"lang:default decode:true \">import pygame\r\n\r\nclock = pygame.time.Clock()\r\n# Define Constants\r\nBOARD_SIZE = 20  # Size of the board, in block\r\nBLOCK_SIZE = 20  # Size of 1 block, in pixel\r\nGAME_SPEED = 8  # Game speed (Normal = 10), The bigger, the faster\r\nwindow = pygame.display.set_mode((BOARD_SIZE * BLOCK_SIZE, BOARD_SIZE * BLOCK_SIZE))\r\npygame.display.set_caption(\"window\")\r\nscore = 0\r\nglobal music\r\nmusic = 0\r\n# SURFACES\r\n\r\nhead = pygame.Surface((20, 20))\r\nhead.fill((255, 255, 0))\r\nhead2 = pygame.Surface((5, 5))\r\nhead2.fill((255, 0, 0))\r\n\r\nbody = pygame.Surface((20, 20))\r\nbody.fill((0, 255, 0))\r\nblacktail = pygame.Surface((20, 20))\r\nblacktail.fill((0, 0, 0))\r\nfruit = pygame.Surface((20, 20))\r\nfruit.fill((255, 0, 0))\r\n\r\nbscore2 = pygame.Surface((80, 15))\r\nbscore2.fill((0, 0, 0))<\/pre>\n<h2>functions\/snake.py<\/h2>\n<a href=\"https:\/\/github.com\/formazione\/snake\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6600\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_2.png\" alt=\"\" width=\"208\" height=\"156\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_2.png 800w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_2-320x240.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/07\/snake_2-768x576.png 768w\" sizes=\"auto, (max-width: 208px) 100vw, 208px\" \/><\/a>\n<p>Finally, the class for the snake, the most important where we define the initial position of the snake<\/p>\n<ul>\n<li>snake.x<\/li>\n<li>snake.y<\/li>\n<\/ul>\n<p>the position of the 2 initial parts of the body inside the list snake.body<\/p>\n<ul>\n<li>snake.x -1, snake.y<\/li>\n<li>snake.x -2, snake.y<\/li>\n<\/ul>\n<p>With this code<\/p>\n<pre class=\"lang:default decode:true \">self.body = [[self.x - c, self.y] for c in range(3)]<\/pre>\n<p>This is a list comprehension, a way that python has to make a for loop in one line.<\/p>\n<p>There are also the function to avoid that the snake turns in the opposite direction towards which he is moving and the function that makes it move of one cell at the time. I would like to make the movement to go smooth from a cell to the other.<\/p>\n<p>At last, we have the collision check and the food eat check.<\/p>\n<pre class=\"lang:default decode:true \">from functions.costants import *\r\n\r\nclass Snake():\r\n    def __init__(self):\r\n        \"I made the method so I can call it to restart\"\r\n        self.start()\r\n\r\n    def start(self):\r\n        \"Where the snake starts and snake.body first list build\"\r\n        self.x = 5\r\n        self.y = 5\r\n        # This has the coords of the snake; head -1 -2 are the other\r\n        self.body = [[self.x - c, self.y] for c in range(3)]\r\n        self.moves_towards = \"RIGHT\"\r\n\r\n    def not_backwards(self, wanna_go):\r\n        \"Avoid going backwards\"\r\n        if wanna_go in \"LEFT RIGHT\":\r\n            if self.moves_towards in \"UP DOWN\":\r\n                self.moves_towards = wanna_go\r\n        # IF YOU GO LEFT OR RIGHT YOU CAN GO UP OR DOWN\r\n        elif self.moves_towards in \"LEFT RIGHT\":\r\n            self.moves_towards = wanna_go\r\n\r\n    def move(self, food_pos):\r\n        \"A new\"\r\n\r\n        global music\r\n        directions = {\r\n            \"RIGHT\": 1,\r\n            \"LEFT\": -1,\r\n            \"UP\": -1,\r\n            \"DOWN\": 1}\r\n        if self.moves_towards in \"RIGHT LEFT\":\r\n            self.x += directions[self.moves_towards]\r\n        else:\r\n            self.y += directions[self.moves_towards]\r\n\r\n        self.body.insert(0, [self.x, self.y])\r\n        if [self.x, self.y] == food_pos:\r\n            # not popping the last element, it grows in size\r\n            return 1\r\n        else:\r\n            \"If do not eat... same size\"\r\n            self.body.pop()\r\n            # A random not at a random time and random volume\r\n            if music:\r\n                random_play(rnd=random.randrange(3, 10))\r\n            return 0\r\n\r\n    def check_collisions(self):\r\n        \"Check if it goes out or on himself\"\r\n        game_over_points = (\r\n        self.x &gt;= 20 or self.x &lt; 0,\r\n        self.y &gt; 20 or self.y &lt; 0,\r\n        [x for x in self.body[4:] if (self.x, self.y) == x]\r\n        )\r\n        if any(game_over_points):\r\n            return 1\r\n        else:\r\n            return 0\r\n\r\n#                         2 main objects\r\nsnake = Snake()<\/pre>\n<p>&nbsp;<\/p>\n<!-- se vuoi mettere un testo scorrevole\r\n[hoops name=\"typeWriterGen\"]\r\n\r\npoi metti un id diverso per ogni testo nella stessa pagina\r\n\r\n<div id=\"div01\">\r\n<script>\r\n\r\ntypeWriterGen(\"div01\",\"Esempio di testo scorrevole\");\r\n<\/script>\r\n\r\n-->\r\n<style>\r\n.avatar {\r\n  vertical-align: middle;\r\n  width: 100px;\r\n  height: 100px;\r\n  border-radius: 50%;\r\n}\r\n<\/style>\r\n\r\n<hr>\r\n\r\n<!-- NEWSLETTER LINK -->\r\n<a href=\"https:\/\/docs.google.com\/forms\/d\/e\/1FAIpQLSf7TniIPCWHDzCSGh2dYZaCwDvi9yLKS5ovFdKuK1sdfOvwEg\/viewform\">\r\n<img decoding=\"async\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/08\/image-13.png\" class=\"avatar\">\r\nSubscribe to the <b>newsletter<\/b> for updates<\/a><br>\r\n\r\n<!-- TKINTER TEMPLATE LINK -->\r\n<a href=\"https:\/\/pythonprogramming.altervista.org\/tkinter-templates\/\">\r\n<img decoding=\"async\" src=\"https:\/\/i0.wp.com\/pythonprogramming.altervista.org\/wp-content\/uploads\/2023\/07\/image-26.png\" class=\"avatar\">\r\nTkinter templates<\/a><br>\r\n\r\n<!-- MY AVATAR PUT A LINK TO YOUTUBE CHANNEL-->\r\n<iframe loading=\"lazy\" frameborder=\"0\" src=\"https:\/\/itch.io\/embed\/711828\" width=\"552\" height=\"167\"><a href=\"https:\/\/pythonprogrammi.itch.io\/pysnake\">PySnake by PythonProgrammi<\/a><\/iframe>\r\n<br>\r\n<style>\r\n.avatar {\r\n  vertical-align: middle;\r\n  width: 100px;\r\n  height: 100px;\r\n  border-radius: 50%;\r\n}\r\n<\/style>\r\n\r\n\r\n<a href=\"https:\/\/www.youtube.com\/channel\/UCzbxq5e9gLiY-je2-br1rvg\">\r\n\t<img decoding=\"async\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/10\/avatar64x64.png\" alt=\"Avatar\" class=\"avatar\">\r\n\t My youtube channel<\/a><br>\r\n\r\n<br>\r\n\r\nTwitter: <a href=\"https:\/\/twitter.com\/pythonprogrammi\">@pythonprogrammi - python_pygame<\/a>\r\n<h3>Claude's Games<\/h3>\r\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/random-daily-game-1-arkanoid\/\">Arkanoid<\/a><br>\r\n<a href=\"https:\/\/pythonprogramming.altervista.org\/platform-2d-with-pygame-made-with-claude\/\">Platform 2d<\/a><\/p> <!-- videogames made with claude -->\r\n<a href=\"https:\/\/pythonprogramming.altervista.org\/artifacts-games-day-1-memory-game\/\">1. Memory game<\/a>\r\n<h4>Videos<\/h4>\r\n<a href=\"https:\/\/youtu.be\/ciLjWWw5pLY\">Speech recognition game<\/a>\r\n<h3>Pygame's Platform Game<\/h3>\r\n\r\n<a href=\"https:\/\/pythonprogramming.altervista.org\/pygame-platform-game-5-sounds-and-mixer\/\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/01\/climbercover.png?w=557&ssl=1\"\/ width=\"50%\"><\/a>\r\n<script>\r\nvar title = \"Platform Pygame\";\r\n\t\tvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-animation-of-a-sprite-v-1-3\/\",\"Animation 1.3\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-sprite-animation-v-2-better-coding-test-it-checking-fps-on-the-screen\/\",\"Animation 1.2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-how-to-display-the-frame-rate-fps-on-the-screen\/\",\"Display Frame rate\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-sprite-animation-update\/\",\"Animation 1.1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platformer-1\/\",\"Pygame Platform Game 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-platform-game-2\/\",\"Pygame Platform 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platform-game-3-recap-cheatsheet\/\",\"Pygame PLatform 3 - recap and some Cheat Sheet\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platform-game-4-background-and-stuffs\/\",\"Pygame Platform 4 - Background & organizing code\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platform-game-5-sounds-and-mixer\/\",\"Pygame Platform 5 - Sounds\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/platform-game-in-detail-part-1\/\",\"Game in detail part 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/map-maker-1-2\/\", \"Map maker 1.2\"]\r\n\t\t];\r\n\t\t<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\r\n\r\n<h3>Other Pygame's posts<\/h3>\r\n\r\n<script>\r\nvar title = \"Pygame's Posts\"\r\nvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-platformer-1\/\",\"Platform game 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/make-a-platform-game-with-pygame-dafluffypotato\/\",\"DaFluffyPotato Platform Tutorials\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-and-classic-arcade-games-pong\/\",\"Pong Game Full\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-draws-in-colors-app-to-draw-with-pygame\/\",\"PyGameGIF 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-draw-app-with-animation\/\",\"PyGameGIF 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pydraw-2-0-app-to-draw-gif\/\",\"PyDraw 2.0\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-drawing-2\/\",\"Draw with Pygame\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/animation-with-pygame\",\"Sprite animation 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/animation-on-pygame-2-free-characters-and-more-actions\/\",\"Sprite animation 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/starting-with-pygame\/\",\"Starting movements with Pygame\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-3-move-sprite\/\", \"Move a Sprite\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-4-fonts\/\",\"Text and Fonts\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-animate-a-sprite\/\", \"Animate a sprite\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pygame-and-mouse-events\/\",\"Mouse events\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pgp-aka-pygamepresentation-project\/\",\"Pygame presentation\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/moving-the-player-in-pygame-with-key-get_pressed\/\",\"How to use key.get_pressed()\"]\r\n]\r\n<\/script>\r\n\r\n\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"The snake game\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/python-vs-snake-v-1-8-3\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":6632,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,191],"tags":[194,520],"class_list":["post-6627","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-pygame","tag-pygame","tag-snake"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/6627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=6627"}],"version-history":[{"count":5,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/6627\/revisions"}],"predecessor-version":[{"id":6651,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/6627\/revisions\/6651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/6632"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=6627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=6627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=6627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}