Skip to content

Number Guessing Game

06 Nov 2023
Number Guessing Game

Mystery Number Guessing Game Project!

Table of Contents

    We always say to our friends, “Let’s guess the number.” You’ve probably heard the answers: “No, it’s bigger. No, it’s smaller. Wow, how did you know?” Wouldn’t you like to do just that with PicoBricks? We invite you to a journey full of mathematics and intelligence! Today we open the door to an exciting project: the number guessing game project. 

    This project is a fun experience designed to test your math skills, improve problem-solving skills, and hone your mental abilities.

    The guess the number game is a fun and interactive coding project that challenges players to guess a random number within a specified range. This number guessing game project not only sharpens your programming skills but also offers an entertaining experience for players as they attempt to guess the correct number. Whether you’re a beginner looking to learn coding or an enthusiast seeking an engaging game development project, Guess The Number Game is a great choice to explore and enjoy.

    Remember, you will learn much more than just to guess the number in this project…

    Guess The Number!

    Details and Algorithm

    Press the “Start” button to start the number guessing game. To update your forecast, follow the steps below. If the player presses the up arrow key from remote (if the picobrick estimate is less than the number the player selected), the estimated range is halved again and that value is added to the current picobrick estimate. 

    If the player presses the down arrow key from remote (if the picobrick estimate is greater than the number selected by the player), the estimated range is halved again and that value is subtracted from the current picobricks estimate. When the player presses the “OK” button from remote, the  number guessing  game ends and PicoBricks celebrates. This means that the number chosen by the player was guessed correctly!

    Components

    1X PicoBricks

    Wiring Diagram

    PicoBricks IDE Codes of the PicoBricks

    MicroBlocks Codes of PicoBricks

    MicroPython Codes of the PicoBricks

    `
    from time import sleep
    from machine import Pin
    from machine import I2C
    from picobricks import SSD1306_I2C
    import time
    import machine
    import math
    from machine import PWM
    from math import fabs
    from picobricks import NEC_16
    from picobricks import IR_RX
    
    i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=200000)
    oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
    pin_button = machine.Pin(10, machine.Pin.IN)
    import random
    buzzer = PWM(Pin(20))
    
    pwm_2 = PWM(Pin(22))
    pwm_2.freq(50)
    
    
    def CalculateAngle(angle):
        angle = fabs((angle * (6000 / 180)) + 2000)
        angle = round(angle)
        return angle
    
    
    def RPS():
        global sentence, choice, i_g, sentence_array, i, list2, n_r, dare_question, truth_question, truth_or_dare
        for i in range((3)):
            buzzer.freq(700)
            buzzer.duty_u16(100)
            sleep(0.25)
            buzzer.duty_u16(0)
            oled.fill(0)
            oled.text("{}".format(list2[i]), int((64) - (int(len(list2[i]))) / (2)), 23)
            oled.text("{}".format("???"), 46, 33)
            oled.show()
            time.sleep((1))
    
        choice = random.choice(["Rock", "Paper", "Scissors"])
        oled.fill(0)
        if choice == ("Rock"):
            oled.text("{}".format(">>ROCK<<"), 25, 23)
            oled.show()
            music()
            pwm_2.duty_u16(CalculateAngle(45))
        elif choice == ("Paper"):
            oled.text("{}".format(">>PAPER<<"), 25, 23)
            oled.show()
            music()
            pwm_2.duty_u16(CalculateAngle(90))
        else:
            oled.text("{}".format(">>SCISSORS<<"), 16, 23)
            oled.show()
            music()
            pwm_2.duty_u16(CalculateAngle(135))
    
    
    def music():
        global sentence, choice, i_g, sentence_array, i, list2, n_r, dare_question, truth_question, truth_or_dare
        buzzer.freq(800)
        buzzer.duty_u16(100)
        sleep(0.25)
        buzzer.duty_u16(0)
        time.sleep((0.0001))
        buzzer.freq(1000)
        buzzer.duty_u16(100)
        sleep(0.25)
        buzzer.duty_u16(0)
        time.sleep((0.0001))
        buzzer.freq(800)
        buzzer.duty_u16(100)
        sleep(0.25)
        buzzer.duty_u16(0)
        time.sleep((0.0001))
        buzzer.freq(1000)
        buzzer.duty_u16(100)
        sleep(0.25)
        buzzer.duty_u16(0)
        time.sleep((0.0001))
        buzzer.freq(1300)
        buzzer.duty_u16(100)
        sleep(0.5)
        buzzer.duty_u16(0)
    
    
    def ir_callback(data, addr, ctrl):
        global ir_data
        global ir_addr, data_rcvd
        if data > 0:
            ir_data = data
            ir_addr = addr
            print('Data {:02x} Addr {:04x}'.format(data, addr))
            data_rcvd = True
    
    
    ir = NEC_16(Pin(0, Pin.IN), ir_callback)
    ir_data = 0
    data_rcvd = False
    
    
    def up():
        global sentence, choice, i_g, sentence_array, i, list2, n_r, dare_question, truth_question, truth_or_dare
        i_g += n_r
        oled.fill(0)
        oled.text("{}".format(str("My gues:")+str(i_g)), 0, 0)
        oled.show()
        n_r = int(n_r / (2))
        time.sleep((0.5))
    
    
    def Starter():
        global sentence, choice, i_g, sentence_array, i, list2, n_r, dare_question, truth_question, truth_or_dare
        while (pin_button.value()) == (0):
            oled.text("{}".format("Rock"), 42, 10)
            oled.text("{}".format("Paper"), 40, 20)
            oled.text("{}".format("Scissors"), 30, 30)
            oled.text("{}".format(""), 6, 45)
            oled.show()
    
    
    def down():
        global sentence, choice, i_g, sentence_array, i, list2, n_r, dare_question, truth_question, truth_or_dare
        i_g += (0) - n_r
        if i_g < (0):
            i_g = 0
        else:
            oled.fill(0)
            oled.text("{}".format(str("My gues:")+str(i_g)), 0, 0)
            oled.show()
        n_r = int(n_r / (2))
        time.sleep((0.5))
    
    
    sentence = ""
    sentence_array = " "
    dare_question = "Yell out the first word that comes to your mind.Eat a snack without using your hands.Dance without music for two minutes".split(".")
    truth_question = "Do you have a hidden talent? When's the last time you apologized, What for?What is your biggest fear?".split("?")
    while True:
        oled.fill(0)
        oled.text("{}".format("Spin "), 35, 30)
        oled.text("{}".format(" Bottle"), 20, 40)
        oled.show()
        time.sleep((1))
        oled.fill(0)
        while (pin_button.value()) == (0):
            truth_or_dare = round(0 - 0) * (0 - 0) / (0 - 0) + 0
            if truth_or_dare == (0):
                oled.fill(0)
                oled.text("{}".format("Truth Or Dare"), 0, 10)
                oled.text("{}".format("truth"), 10, 40)
                oled.show()
            else:
                oled.fill(0)
                oled.text("{}".format("Truth Or Dare"), 0, 10)
                oled.text("{}".format("dare"), 10, 40)
                oled.show()
                time.sleep((0.3))
                oled.fill(0)
    
        oled.fill(0)
        oled.show()
        if truth_or_dare == (0):
            sentence = truth_question[random.randint(1, (len(truth_question)) - (1))]
            if (len(sentence)) >= (16):
                sentence_array = sentence.split(" ")
                for i in range((1) + (len(sentence_array))):
                    oled.text("{}".format(sentence_array[i - (1)]), 32, (10) * (i - (1)))
                    oled.show()
                    time.sleep((1))
            else:
                oled.text("{}".format(sentence), 0, 25)
                oled.show()
                time.sleep((1))
        else:
            sentence = dare_question[random.randint(1, (len(dare_question)) - (1))]
            if (len(sentence)) >= (16):
                sentence_array = sentence.split(" ")
                for i in range((1) + (len(sentence_array))):
                    oled.text("{}".format(sentence_array[i - (1)]), 32, (10) * (i - (1)))
                    oled.show()
                    time.sleep((1))
            else:
                oled.text("{}".format(sentence), 0, 25)
                oled.show()
                time.sleep((1))
    
    choice = 0
    list2 = ["Rock", "Paper", "Scissors"]
    Starter()
    RPS()
    while True:
        while (pin_button.value()) == (0):
            oled.fill(0)
            oled.text("{}".format("Push to play "), 15, 20)
            oled.text("{}".format("again"), 40, 30)
            oled.show()
    
        RPS()
    
    oled.fill(0)
    oled.text("{}".format("Press the Button "), 0, 20)
    oled.text("{}".format("TO START!!!"), 15, 30)
    oled.show()
    while (pin_button.value()) == (0):
        pass
    
    while True:
        oled.fill(0)
        n_r = 64
        i_g = n_r
        oled.text("{}".format(str("My gues:")+str(i_g)), 25, 0)
        oled.text("{}".format("Press \'ok\' if I guess correctly"), 0, 30)
        oled.show()
        while True:
            if data_rcvd == True:
                data_rcvd = False
                if ir_data == IR_RX.number_up:
                    up()
                if ir_data == IR_RX.number_down:
                    down()
                if ir_data == IR_RX.number_ok:
                    break
    

    Arduino Codes of the PicoBricks

    
    #ifndef ACROBOTIC_SSD1306_H
    #define ACROBOTIC_SSD1306_H
    
    #if ARDUINO >= 100
    #include "Arduino.h"
    #else
    #include "WProgram.h"
    #endif
    
    #ifdef __AVR__
    #include 
    #define OLEDFONT(name) static const uint8_t __attribute__ ((progmem)) name[]
    #elif defined(ESP8266)
    #include 
    #define OLEDFONT(name) static const uint8_t name[]
    #else
    #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
    #define OLEDFONT(name) static const uint8_t name[]
    #endif
    
    #include "Wire.h"
    #include "fonts/font8x8.h"
    #include "fonts/font5x7.h"
    
    #if !defined SSD1306_128_64 && !defined SSD1306_128_32
    #define SSD1306_128_64
    #endif
    
    #if defined SSD1306_128_64
    #define SSD1306_Max_X                 127
    #define SSD1306_Max_Y                 63
    #endif
    #if defined SSD1306_128_32
    #define SSD1306_Max_X                 127
    #define SSD1306_Max_Y                 31
    #endif
    
    #define PAGE_MODE                     01
    #define HORIZONTAL_MODE               02
    
    #define SSD1306_Address               0x3C
    #define SSD1306_Command_Mode          0x80
    #define SSD1306_Data_Mode             0x40
    #define SSD1306_Display_Off_Cmd       0xAE
    #define SSD1306_Display_On_Cmd        0xAF
    #define SSD1306_Normal_Display_Cmd    0xA6
    #define SSD1306_Inverse_Display_Cmd   0xA7
    #define SSD1306_Activate_Scroll_Cmd   0x2F
    #define SSD1306_Dectivate_Scroll_Cmd  0x2E
    #define SSD1306_Set_Brightness_Cmd    0x81
    
    #define Scroll_Left                   0x00
    #define Scroll_Right                  0x01
    
    #define Scroll_2Frames                0x7
    #define Scroll_3Frames                0x4
    #define Scroll_4Frames                0x5
    #define Scroll_5Frames                0x0
    #define Scroll_25Frames               0x6
    #define Scroll_64Frames               0x1
    #define Scroll_128Frames              0x2
    #define Scroll_256Frames              0x3
    
    class ACROBOTIC_SSD1306 {
    public:
        char addressingMode;
        void init(TwoWire& wire = Wire);
    
        void setNormalDisplay();
        void setInverseDisplay();
    
        void sendCommand(unsigned char command);
        void sendData(unsigned char Data);
    
        void setPageMode();
        void setHorizontalMode();
    
        void setTextXY(unsigned char Row, unsigned char Column);
        void clearDisplay();
        void setBrightness(unsigned char Brightness);
        bool putChar(unsigned char c);
        void putString(const char* string);
        void putString(String string);
        unsigned char putNumber(long n);
        unsigned char putFloat(float floatNumber, unsigned char decimal);
        unsigned char putFloat(float floatNumber);
        void drawBitmap(unsigned char* bitmaparray, int bytes);
    
        void setHorizontalScrollProperties(
            bool direction,
            unsigned char startPage,
            unsigned char endPage,
            unsigned char scrollSpeed);
        void activateScroll();
        void deactivateScroll();
    
        void displayOn();
        void displayOff();
    
        void setFont(const uint8_t* font, bool inverse = false);
    
    private:
        const uint8_t* m_font;      // Current font.
        uint8_t m_font_offset = 2;  // Font bytes for meta data.
        uint8_t m_font_width;       // Font width.
        uint8_t m_col;              // Cursor column.
        uint8_t m_row;              // Cursor row (RAM).
        bool m_inverse = false;     // Inverse text.
        TwoWire* m_wire;
    };
    
    extern ACROBOTIC_SSD1306 oled;  // ACROBOTIC_SSD1306 object
    
    #endif
    

    You can access other interactive projects like the “Guess the Number Game” here!

     

    
    
    Prev Post
    Next Post

    Thanks for subscribing!

    This email has been registered!

    Shop the look
    Choose Options

    Edit Option

    Back In Stock Notification

    Compare

    Product SKURatingDescription Collection Availability Product Type Other Details
    this is just a warning
    Login
    Shopping Cart
    0 items
    Same Day Shipping No Extra Costs
    Easy Returns Guarantee Return with Ease
    Secure Checkout Secure Payment