Giter Club home page Giter Club logo

color-game's Introduction

Brain challenge

This project is a game which has 2 different screens :

  • one is called the loading_screen or welcome_screen, it is here that the rules are explained
  • the other is the game_screen where you can find all of the game logic.

These pages are linked in the run fonction of the class/file Game/game

Here are the main features of this game :

File handling

We use a text file to chose what colors we vant to add to the game.

input.txt :

PURPLE
BROWN
GREEN
ORANGE
GRAY
BLUE
YELLOW
BLACK
RED

We read this file using this simple block

color_dico = dict()
with open("input.txt", 'r') as file:
    for line in file.readlines():
        color_string = line.rstrip()  # we remove spaces and newlines
        color_dico[color_string] = globals()[color_string]  # we add the color to our color dicitonary

Data structure

One of the most important data structure used in this project is the one of color_dico. I chose to use the builtin type dictionnary in python because it makes in really easy and smooth to use in the game class !

The format of the dictionnary is (key,value) = (str, (int,int,int)) str is the color name, and (int,int,int) is the rgb encoding of the color.

Here is the code to verify if the color entered by the player is the color of the word shown on the screen:

if val.upper() == color_dico_reversed[self.txt_color]:
    self.score += 100
  • txt.color is the rgb encoding of the word shown on the screen

  • val is the text that the player just wrote and submitted.

We can get the name of the color of the word by reversing our dictionnary which mean that we input the rgb value and it gives us the corresponding color !

Looping

in the run fonction of game.py we have an example of a game loop :

while self.controler[0]:
    clock.tick(FPS)  # slows down the loop to get a fixed frame rate
    self.win.fill(BLACK)
    for event in pygame.event.get():
        pass
        # checks for user input
    self.loading_window.update(self.win)
    pygame.display.flip()

This is the basic implementation of a window in any kind of langage. Indeed, you have a pseudo infinite while loop and every turn you:

  • Check for user interaction and use specific fonction if he clicks somwhere for exampe
  • Update the screen and all of its components
  • Refresh the display

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.