{"id":6949,"date":"2020-08-14T09:35:09","date_gmt":"2020-08-14T07:35:09","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=6949"},"modified":"2020-08-14T11:41:35","modified_gmt":"2020-08-14T09:41:35","slug":"chimp-memory-game-beta-1","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/chimp-memory-game-beta-1\/","title":{"rendered":"Chimp Memory Game beta 1"},"content":{"rendered":"<p>Test your memory with this new free game made with pygame by me. This is the code. You got it also on github with also sounds and graphic. This one tests your working memory, that is the one that you use to store informations temporarly.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/coder.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6956 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/coder.png\" alt=\"\" width=\"402\" height=\"432\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/coder.png 402w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/coder-320x344.png 320w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">import pygame\r\nimport random\r\nfrom glob import glob\r\n\r\n# all the possible positions for the numbers\r\npos = [(x, y) for x in range(1, 8) for y in range(1, 8)] \r\n# print(pos)\r\n# print(pos.pop(pos.index(random.choice(pos))))\r\n# print(pos)\r\n\r\ndef game_init():\r\n    global screen, font\r\n\r\n    pygame.init()\r\n    size = w, h = 400, 400\r\n    screen = pygame.display.set_mode((size))\r\n    pygame.display.set_caption(\"Chimp Memory Game\")\r\n    font = pygame.font.SysFont(\"Arial\", 20)\r\n\r\n\r\nclass Square(pygame.sprite.Sprite):\r\n    def __init__(self, number):\r\n        super(Square, self).__init__()\r\n        self.number = number\r\n        self.make_image()\r\n\r\n    def make_image(self):\r\n        global font\r\n\r\n        self.x, self.y = self.random_pos()\r\n        self.image = pygame.Surface((50, 50))\r\n        r, g, b = [random.randrange(128, 256) for x in range(3)]\r\n        self.image.fill((r, g, b))\r\n        self.rect = self.image.get_rect()\r\n        self.rect.center = self.x, self.y\r\n        self.number = str(self.number)\r\n        # text_rect = self.text.get_rect(center=(50 \/\/ 2, 50 \/\/ 2))\r\n        self.text = font.render(self.number, 1, (0, 0, 0))\r\n        text_rect = self.text.get_rect(center=(50 \/\/ 2, 50 \/\/ 2))\r\n        self.image.blit(self.text, text_rect)\r\n\r\n    def update(self):\r\n        self.make_image()\r\n        screen.blit(self.image, (self.x, self.y))\r\n\r\n    def random_pos(self):\r\n        position = random.choice(pos)\r\n        x, y = position\r\n        pos.pop(pos.index(position))\r\n        # x = random.randrange(0, 16)\r\n        # y = random.randrange(0, 16)\r\n        x = x * 50\r\n        y = y * 50\r\n\r\n        return x, y\r\n\r\n\r\n\r\ng = pygame.sprite.Group()\r\nnum_order = []\r\nscore = 0\r\n# This covers the numbers...\r\nbgd = pygame.Surface((50, 50))\r\nbgd.fill((255, 0, 0))\r\n\r\n\r\ndef hide_cards():\r\n    for sprite in g:\r\n        bgd.fill((0, 255, 0))\r\n        sprite.image.blit(bgd, (0, 0))\r\n\r\n\r\ndef mouse_collision(sprite):\r\n    global num_order, score, counter_on, max_count, cards_visible\r\n    global bonus\r\n\r\n\r\n    # Check the collision only when conter is off\r\n    x, y = pygame.mouse.get_pos()\r\n    if sprite.rect.collidepoint(x, y):\r\n        play(\"click\")\r\n        print(\"touched\")\r\n        print(sprite.rect.collidepoint(x, y))\r\n        print(sprite.number)\r\n        # bgd.fill((0, 255, 0))\r\n        # sprite.image.blit(bgd, (0, 0))\r\n        num_order.append(sprite.number)\r\n        sprite.rect = pygame.Rect(-50, -50, 50, 50)\r\n\r\n        # Check if you are wrong as you type\r\n        if sprite.number != str(len(num_order)):\r\n            num_order = []\r\n            counter_on = 1\r\n            # pygame.mouse.set_visible(False)\r\n            g.update()\r\n            screen.fill((0,0,0))\r\n            bgd.fill((255, 0, 0))\r\n\r\n    if len(num_order) == len(g):\r\n        print(\"fine\")\r\n        print(num_order)\r\n        # =========================== YOU GUESSED ========== Score: add bonus for quick click\r\n        win = num_order == [str(s.number) for s in g]\r\n        if win:\r\n            # bonus is for clicking before counter stops\r\n            # when you click... it memories the bonus in main()\r\n            score += 100 + bonus\r\n            print(\"You won - Score: \" + str(score))\r\n            num_order = []\r\n            g.add(Square(len(g) + 1))\r\n            counter_on = 1\r\n            max_count = max_count + 10\r\n            cards_visible = 1\r\n            # pygame.mouse.set_visible(False)\r\n        else:\r\n            num_order = []\r\n            counter_on = 1\r\n            cards_visible = 1\r\n            # pygame.mouse.set_visible(False)\r\n        g.update()\r\n        screen.fill((0,0,0))\r\n        bgd.fill((255, 0, 0))\r\n\r\n\r\n######################\r\n#     sound          #\r\n######################\r\ndef init():\r\n    \"Initializing pygame and mixer\"\r\n    pygame.mixer.pre_init(44100, -16, 1, 512)\r\n    pygame.init()\r\n    pygame.mixer.quit()\r\n    pygame.mixer.init(22050, -16, 2, 512)\r\n    pygame.mixer.set_num_channels(32)\r\n    # Load all sounds\r\n    lsounds = glob(\"sounds\\\\*.mp3\")\r\n    print(lsounds)\r\n    # Dictionary with all sounds, keys are the name of wav\r\n    sounds = {}\r\n    for sound in lsounds:\r\n        sounds[sound.split(\"\\\\\")[1][:-4]] = pygame.mixer.Sound(f\"{sound}\")\r\n    return sounds\r\n# =========================== ([ sounds ]) ============\r\n\r\nsounds = init()\r\n\r\nbase = pygame.mixer.music\r\ndef soundtrack(filename, stop=0):\r\n    \"This load a base from sounds directory\"\r\n    base.load(filename)\r\n    if stop == 1:\r\n        base.stop()\r\n    else:\r\n        base.play(-1)\r\n\r\ndef play(sound):\r\n    pygame.mixer.Sound.play(sounds[sound])\r\n\r\ndef squares_init():\r\n    for i in range(1, 4):\r\n        g.add(Square(i))\r\n\r\ncounter = 0\r\ncounter_on = 1\r\nmax_count = 100\r\ncards_visible = 1\r\ndef main():\r\n    global counter_on, counter, max_count, cards_visible\r\n    global bonus\r\n\r\n    game_init()\r\n    squares_init()\r\n    clock = pygame.time.Clock()\r\n    loop = 1\r\n    # pygame.mouse.set_visible(False)\r\n    soundtrack(\"sounds\/soave.ogg\")\r\n    while loop:\r\n        screen.fill((0, 0, 0))\r\n        text = font.render(\"Livello: \" + str(score), 1, (255, 244, 0))\r\n        screen.blit(text, (0, 0))\r\n        if counter_on:\r\n            text = font.render(\"time: \" + str(max_count - counter), 1, (255, 244, 0))\r\n            screen.blit(text, (200, 0))\r\n            counter += 1\r\n            if counter % 4 == 0:\r\n                play(\"click\")\r\n        for event in pygame.event.get():\r\n            # ========================================= QUIT\r\n            if event.type == pygame.QUIT:\r\n                loop = 0\r\n            if event.type == pygame.KEYDOWN:\r\n                if event.key == pygame.K_s:\r\n                    loop = 0\r\n                if event.key == pygame.K_s:\r\n                    g.update()\r\n                    screen.fill((0,0,0))\r\n            if event.type == pygame.MOUSEBUTTONDOWN:\r\n                # Click mouse and stop the timer and hide the cards\r\n                if cards_visible:\r\n                    hide_cards()\r\n                    # bonus to click early: add to score if win\r\n                    bonus = max_count - counter\r\n                    cards_visible = 0\r\n                    counter_on = 0\r\n                    counter = 0\r\n                # This checks the cards you hit\r\n                for s in g:\r\n                    mouse_collision(s)    \r\n\r\n\r\n        g.draw(screen)\r\n        # Hides the number...\r\n        if counter == max_count:\r\n            hide_cards()\r\n            counter = 0\r\n            counter_on = 0\r\n\r\n                # pygame.mouse.set_visible(True)\r\n        pygame.display.update()\r\n        clock.tick(20)\r\n\r\n    pygame.quit()\r\n\r\n\r\nmain()\r\n\r\n<\/pre>\n<p>The sounds arein the sounds folder and are this. The first is click.mp3 and the second is soave.ogg. This are very basic and I will add new more exciting ones in a future version of this new game. Also new stuffs, like memorizing the position instead of numbers order.<\/p>\n<audio class=\"wp-audio-shortcode\" id=\"audio-6949-1\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/click.mp3?_=1\" \/><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/click.mp3\">https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/click.mp3<\/a><\/audio>\n<audio class=\"wp-audio-shortcode\" id=\"audio-6949-2\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/ogg\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/soave.ogg?_=2\" \/><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/soave.ogg\">https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/soave.ogg<\/a><\/audio>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/chimp_help.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6952\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/chimp_help.png\" alt=\"\" width=\"800\" height=\"600\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/chimp_help.png 800w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/chimp_help-320x240.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2020\/08\/chimp_help-768x576.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n<p>Maybe I&#8217;ll do a video about this game soon.<\/p>\n<p><iframe loading=\"lazy\" title=\"Chimp Memory Game with Pygame... coming soon\" width=\"747\" height=\"560\" src=\"https:\/\/www.youtube.com\/embed\/KAn1f16Cl1I?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"A new memory game: Chimp.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/chimp-memory-game-beta-1\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":6953,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[191],"tags":[],"class_list":["post-6949","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pygame"],"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\/6949","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=6949"}],"version-history":[{"count":2,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/6949\/revisions"}],"predecessor-version":[{"id":6957,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/6949\/revisions\/6957"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/6953"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=6949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=6949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=6949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}