<repo_name>gwen23/P3<file_sep>/game/settings.py
""" Different constants of the game """

# String to identify each character in level structure
PLAYER_STRIPE = "P"
BAD_GUY_STRIPE = "B"
ETHER_STRIPE = "E"
TUBE_STRIPE = "T"
SERINGE_STRIPE = "S"
WALL_STRIPE = "X"
FLOOR_STRIPE = " "

WIDTH = 300
HEIGHT = 350
SIZE_SPRITE = 20
FOND = "./images/floor-tiles-20x20.png"

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 255)

FL = "./images/floor-tiles-20x20.png"
WA = "./images/structures.png"
E = "./images/ether.png"
T = "./images/tube_plastique.png"
S = "./images/seringue.png"
BG = "./images/Gardien.png"
PL = "./images/MacGyver.png"
<file_sep>/README.md
# The McGyverMaze .

Short python game where we guide a character through a maze.
To win the game we need to pick up three items randomly placed in the maze and beat the guardian.

### Prerequisites :

- Python3 (https://www.python.org/)
- PIP to download the dependencies (https://pypi.org/project/pip/)
- Virtualenv to generate a local env to run the game (https://virtualenv.pypa.io/en/latest/installation.html)
- Pygame library : https://www.pygame.org/wiki/GettingStarted

## install the game :

1st step - Download the repo :
```
https://github.com/gwen23/P3
```

2nd step - Create a virtual env and activate it
```
python -m venv env
source env/Scripts/activate
```

3rd step - Download the dependencies :
```
pip install -r requirements.txt
```

the 4th step - Launch the game :
```
cd pythonProject3
python -m game.py
```

### To play the game :

KEYBOARD : 
    use the directional keys : up , down , left and right 

### Modification of the map :

in maze.txt:

    _the walls are represented by the letter "X"
    _the ways are represented by an " "
    _the player is represented by the letter "P"
    _the guardian is represented by the letter "B"

for any modification in the maze you have to replace the letter you want by another one of the list.





<file_sep>/game/the_maze.py
import random
from game.guardian import Guardian
from game.player import Macgyver


class Maze:

    def __init__(self):
        self.tiles = {}
        self.paths = []
        self.mac_gyver = ()
        self.guardian = ()
        self.test()
        self.create_objects()
        self.numb_objects = 0

    def test(self):
        x, y = -1, -1
        with open("game/maze.txt", "r") as maze:
            for line in maze:
                x += 1
                for cell in line:
                    if cell != "\n":
                        y += 1
                        self.tiles[(x, y)] = cell
                    if cell == " ":
                        self.paths.append((x, y))
                    if cell == "P":
                        self.mac_gyver = Macgyver(x, y)
                    if cell == "B":
                        self.guardian = Guardian(x, y)
                y = -1

    def move_macgyver(self, direction):
        coo_destination = (self.mac_gyver.x, self.mac_gyver.y)
        if direction == "up":
            coo_destination = (self.mac_gyver.x - 1, self.mac_gyver.y)
        if direction == "down":
            coo_destination = (self.mac_gyver.x + 1, self.mac_gyver.y)
        if direction == "right":
            coo_destination = (self.mac_gyver.x, self.mac_gyver.y + 1)
        if direction == "left":
            coo_destination = (self.mac_gyver.x, self.mac_gyver.y - 1)
        self.conditions(coo_destination)
        if self.tiles[coo_destination] != "X":
            return True
        else:
            return False

    def conditions(self, coo_destination):
        if self.tiles[coo_destination]:
            if self.tiles[coo_destination] != "X":
                self.tiles[(self.mac_gyver.x, self.mac_gyver.y)] = " "
                self.mac_gyver.x = coo_destination[0]
                self.mac_gyver.y = coo_destination[1]
                if self.tiles[coo_destination] in "TSE":
                    self.numb_objects += 1
                    print("object + 1")
                if self.tiles[coo_destination] == "B":
                    if self.numb_objects == 3:
                        print("you win")
                    else:
                        print("you lose")
                self.tiles[(self.mac_gyver.x, self.mac_gyver.y)] = "P"
                return True

    def create_objects(self):
        objects = ["E", "T", "S"]
        random.shuffle(self.paths)
        for i in range(0, 3):
            coo_new_obj = self.paths[i]
            self.tiles[coo_new_obj] = objects[i]
<file_sep>/game.py
import pygame as pg
from pygame.locals import KEYDOWN as Kd

from game.settings import E, T, S, FL, WA, BG, PL, WIDTH, HEIGHT, SIZE_SPRITE
from game.level import Level
from game.the_maze import Maze

clock = pg.time.Clock()
pg.display.set_caption('The Maze')
level = Level()
SCREEN = pg.display.set_mode((WIDTH, HEIGHT))


class Game:

    def __init__(self):
        pg.init()
        self.maze = Maze()
        pg.display.set_mode((WIDTH, HEIGHT))
        pg.image.load(FL).convert()
        pg.image.load(WA).convert()
        pg.image.load(E).convert_alpha()
        pg.image.load(T).convert_alpha()
        pg.image.load(S).convert_alpha()
        pg.image.load(BG).convert_alpha()
        pg.image.load(PL).convert_alpha()
        pg.display.set_caption('The Maze')
        self.clock = pg.time.Clock()
        self.is_running = True
        self.SCREEN = pg.display.set_mode((WIDTH, HEIGHT))
        self.fond = pg.image.load("./images/floor-tiles-20x20.png").convert()
        self.level = Level()
        self.run()

    def run(self):
        self.SCREEN.blit(self.fond, (0, 0))
        self.level.display(self.maze.tiles)
        self.count_display()
        self.main_loop()

    def count_display(self):
        # create a black surface and display it
        s = pg.Surface((300, 100))
        s.fill((0, 0, 0))
        self.SCREEN.blit(s, (20, 300))
        # write object count
        white = (255, 255, 255)
        phrase = "nombre d'objets ramassés : " + str(self.maze.numb_objects)
        font = pg.font.SysFont("arial", 24)
        text = font.render(phrase, True, white)
        self.SCREEN.blit(text, (20, 310))

    def main_loop(self):
        x = 0
        y = 20
        is_running = True
        while is_running:
            clock.tick(5)
            for event in pg.event.get():
                if event.type == Kd and event.key == pg.K_RIGHT:
                    if self.maze.move_macgyver("right"):
                        SCREEN.blit(pg.image.load(FL).convert(), (x, y))
                        x += 1 * SIZE_SPRITE
                        SCREEN.blit(pg.image.load(PL).convert_alpha(), (x, y))
                elif event.type == Kd and event.key == pg.K_LEFT:
                    if self.maze.move_macgyver("left"):
                        SCREEN.blit(pg.image.load(FL).convert(), (x, y))
                        x -= 1 * SIZE_SPRITE
                        SCREEN.blit(pg.image.load(PL).convert_alpha(), (x, y))
                elif event.type == Kd and event.key == pg.K_UP:
                    if self.maze.move_macgyver("up"):
                        SCREEN.blit(pg.image.load(FL).convert(), (x, y))
                        y -= 1 * SIZE_SPRITE
                        SCREEN.blit(pg.image.load(PL).convert_alpha(), (x, y))
                elif event.type == Kd and event.key == pg.K_DOWN:
                    if self.maze.move_macgyver("down"):
                        SCREEN.blit(pg.image.load(FL).convert(), (x, y))
                        y += 1 * SIZE_SPRITE
                        SCREEN.blit(pg.image.load(PL).convert_alpha(), (x, y))
                if event.type == pg.QUIT:
                    is_running = False
                self.count_display()
            pg.display.update() 
        pg.quit()


if __name__ == "__main__":
    game = Game()
    while True:
        game.run()
        game.main_loop()
<file_sep>/game/level.py
import pygame as pg
import pygame.event as event

from game.settings import E, T, S, FL, WA, BG, PL, WIDTH, HEIGHT, SIZE_SPRITE

SCREEN = pg.display.set_mode((WIDTH, HEIGHT))


class Level:
    def __init__(self):
        self.direction = 0

    def display(self, tiles):
        for key, value in enumerate(tiles):
            x = int(value[1]) * SIZE_SPRITE
            y = int(value[0]) * SIZE_SPRITE
            sprite = tiles[value]
            if sprite == " ":
                SCREEN.blit(pg.image.load(FL).convert(), (x, y))
            elif sprite == "X":
                SCREEN.blit(pg.image.load(WA).convert(), (x, y))
            elif sprite == "P":
                SCREEN.blit(pg.image.load(PL).convert(), (x, y))
            elif sprite == "B":
                SCREEN.blit(pg.image.load(BG).convert(), (x, y))
            elif sprite == "E":
                SCREEN.blit(pg.image.load(E).convert(), (x, y))
            elif sprite == "T":
                SCREEN.blit(pg.image.load(T).convert(), (x, y))
            elif sprite == "S":
                SCREEN.blit(pg.image.load(S).convert(), (x, y))

    def movement(self, x, y):
        for key, value in enumerate(self.tiles):
            x = int(value[1]) * SIZE_SPRITE
            y = int(value[0]) * SIZE_SPRITE
        for event.type in pg.event.get():
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_RIGHT:
                    y += (1 * SIZE_SPRITE)
                    SCREEN.blit(pg.image.load(PL).convert_alpha(), (x, y + 20))
                elif event.key == pg.K_LEFT:
                    y -= (1 * SIZE_SPRITE)
                    SCREEN.blit(pg.image.load(PL).convert_alpha(), (x, y - 20))
                elif event.key == pg.K_DOWN:
                    x -= (1 * SIZE_SPRITE)
                    SCREEN.blit(pg.image.load(PL).convert_alpha(), (x - 20, y))
                elif event.key == pg.K_UP:
                    x += (1 * SIZE_SPRITE)
                    SCREEN.blit(pg.image.load(PL).convert_alpha(), (x + 20, y))
