DEV Community

Cover image for I built my first game with Python - Guess the Dog
Erika Kacelnik
Erika Kacelnik

Posted on

I built my first game with Python - Guess the Dog

Hey everyone. Really happy to have built my first game! It’s a simple dog guessing game, but I learnt a lot important Python logic building it.

Apart from the whole loop to create the turn and point systems, at each turn the game makes a request to this cool Dog API to get a new photo. Since the breed’s name is in the image URL, I had to understand how to parse the URL, change the strings’ order and replace characters.

Oh, I also put in 2 little cheats that will immediately give you a point. There’s one in English and one in Portuguese for the other Brazillians out there hehe :)

Might not be a big accomplishment to everyone, but I’m happy w it!

Click here to play

Important disclaimer: I’m interning at Abstra Cloud. I used it to generate the UI and hosting for me bc right now I’m more focused on learning logic. I’ve been making most of my projects over there, but wanted to share bc it also would be cool for someone to make this elsewhere. Hope that’s okay!

Full code (with the cheat hidden!):

import requests
from urllib.parse import unquote, urlparse
from pathlib import PurePosixPath

points = 0
tries = 0

def request():
    x = requests.get('https://dog.ceo/api/breeds/image/random')
    response = x.json()
    url = response["message"]
    return url

def urlparser(url):
    name = PurePosixPath(
            unquote(
                urlparse(
                    url
                ).path
            )
        ).parts[2]
    name = name.replace('-', ' ')
    name = name.split()
    name = list(reversed(name))
    return " ".join(name)

while tries < 5:
    selected_url = request()
    selected_name = urlparser(selected_url)
    print(selected_name)

    guess = Page().display_image(selected_url)\
                .read("What is this dog's breed?")\
                .run()
    guess_answer = guess["What is this dog's breed?"].lower()
    cheat_options = [???]

    if guess_answer in selected_name or guess_answer in cheat_options:
        points += 1

    tries += 1

if points > 2:
    display(f"Congrats dawg! You scored {points} points 😎")
else:
    display(f"Woof... You only scored {points} points.")
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
dmahely profile image
Doaa Mahely

Well done Erika, the game is pretty fun! I already played 4 rounds 😆

Collapse
 
ekacelnik profile image
Erika Kacelnik

thank you! so happy to hear this ☺️

Collapse
 
codetutorials profile image
marlenastarling

Very Well and funny game.

Collapse
 
ekacelnik profile image
Erika Kacelnik

thanks!