diff --git a/.gitignore b/.gitignore index 7bbc71c..b250500 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ + +.idea/ +.vscode/ \ No newline at end of file diff --git a/README.md b/README.md index 7050016..ec41c40 100644 --- a/README.md +++ b/README.md @@ -1,3 +1 @@ -# python-textovka - -introduction to python by creating the text based adventure game (interactive fiction) +# Textovka diff --git a/assets/world.json b/assets/world.json new file mode 100644 index 0000000..cc7d8c3 --- /dev/null +++ b/assets/world.json @@ -0,0 +1,46 @@ +{ + "kobka": { + "description": "Nachádzaš sa v miestnosti plnej ružových slonov. Aby si si neublížil, tak stena je pokrytá vankúšikmi. Tiež ružovými. Žiadne okno ti neposkytne rozkošný pohľad na vonkajšiu faunu a flóru.", + "items": ["vedro", "kanister", "dvere"], + "exits": { + "north": null, + "south": null, + "east": null, + "west": null + }, + "name": "kobka" + }, + "záhradka": { + "name": "záhradka", + "description": "Značne neudržiavané miesto, ktoré zrejme kedysi bolo záhradkou.", + "items": [], + "exits": { + "north": "hangár", + "south": "priekopa", + "east": "kobka", + "west": null + } + }, + "priekopa": { + "name": "priekopa", + "description": "Nedával si si pozor a rovno si čľupol medzi aligátory. Čo tu robia na Slovensku také ušľachtilé tvory? Pomyslel si si. Čo tu robí taký výdatný štyridsiatnik? Pomysleli si oni. A nerozmýšľali o tom dlho.", + "items": [], + "exits": { + "north": null, + "south": null, + "east": null, + "west": null + } + }, + "hangár": { + "name": "hangár", + "description": "Nachádzaš sa v priestor známeho leteckého dopravcu značky Aeroflot, ktorý ťa určite dopraví bezpečne domov.", + "items": [], + "exits": { + "north": null, + "south": "záhradka", + "east": null, + "west": null + } + } +} \ No newline at end of file diff --git a/commands/__init__.py b/commands/__init__.py new file mode 100644 index 0000000..9f509f9 --- /dev/null +++ b/commands/__init__.py @@ -0,0 +1,14 @@ +from .about import About +from .commands import Commands +from .drop import Drop +from .east import East +from .examine import Examine +from .inventory import Inventory +from .look_around import LookAround +from .north import North +from .quit import Quit +from .save import Save +from .south import South +from .take import Take +from .use import Use +from .west import West diff --git a/commands/about.py b/commands/about.py new file mode 100644 index 0000000..2ff5f32 --- /dev/null +++ b/commands/about.py @@ -0,0 +1,16 @@ +from dataclasses import dataclass, field +from typing import List + +from context import Context +from .command import Command + + +@dataclass +class About(Command): + name: str = 'o hre' + # aliases: List[str] = field(default_factory=['about']) + description: str = 'zobrazí informácie o hre' + + def exec(self, context: Context, param: str): + print('Ďalšie napínavé dobrodružstvo Indiana Jonesa. Tentokrát sa Indy ...') + print('Túto nadupanú hru spáchal (c) mirek') diff --git a/commands/command.py b/commands/command.py new file mode 100644 index 0000000..a255e64 --- /dev/null +++ b/commands/command.py @@ -0,0 +1,17 @@ +from dataclasses import dataclass +from typing import List + +from context import Context + + +@dataclass +class Command: + name: str + # aliases: List[str] + description: str + + def exec(self, context: Context, param: str): + raise NotImplementedError('Command was not yet implemented.') + + def __str__(self): + return f'{self.name} - {self.description}' diff --git a/commands/commands.py b/commands/commands.py new file mode 100644 index 0000000..bbb44eb --- /dev/null +++ b/commands/commands.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass +from typing import List + +from .command import Command +from context import Context + + +@dataclass +class Commands(Command): + name: str = 'prikazy' + # aliases: List[str] + description: str = 'vypíše zoznam príkazov hry' + + def exec(self, context: Context, param: str): + print('Zoznam príkazov hry:') + + for command in context.commands: + # print(f' * {command.name} - {command.description}') + print(f' * {command}') diff --git a/commands/drop.py b/commands/drop.py new file mode 100644 index 0000000..14cc105 --- /dev/null +++ b/commands/drop.py @@ -0,0 +1,38 @@ +from dataclasses import dataclass +from typing import List + +from helpers import get_item_by_name +from .command import Command +from context import Context + + +@dataclass +class Drop(Command): + name: str = 'poloz' + # aliases: List[str] + description: str = 'polozi predmet z batohu do miestnosti' + + def exec(self, context: Context, param: str): + # bol zadany nazov predmetu? + if param == '': + print('Neviem, čo chceš položiť.') + return + + # je v batohu? + item = get_item_by_name(param, context.backpack) + + if item is None: + print('Taký predmet pri sebe nemáš.') + return + + # vymazem z batohu + context.backpack.remove(item) + + # vlozim do miestnosti + context.room['items'].append(item) + + # render + print(f'Do miestnosti si položil predmet {param}.') + + # append to history + context.history.append(f'{self.name} {param}') diff --git a/commands/east.py b/commands/east.py new file mode 100644 index 0000000..1d85865 --- /dev/null +++ b/commands/east.py @@ -0,0 +1,28 @@ +from dataclasses import dataclass +from typing import List + +from helpers import show_room +from .command import Command +from context import Context + + +@dataclass +class East(Command): + name: str = 'vychod' + # aliases: List[str] + description: str = 'presunie sa do miestnosti na vychod od aktuálnej' + + def exec(self, context: Context, param: str): + room_name = context.room['exits']['east'] + + # je na vychod prechod? + if room_name is None: + print('Tam sa nedá ísť.') + return + + # prejdem na vychod + context.room = context.world[room_name] + show_room(context.room) + + # append to history + context.history.append(self.name) diff --git a/commands/examine.py b/commands/examine.py new file mode 100644 index 0000000..a1e8d0b --- /dev/null +++ b/commands/examine.py @@ -0,0 +1,27 @@ +from dataclasses import dataclass +from typing import List + +from helpers import get_item_by_name +from .command import Command +from context import Context + + +@dataclass +class Examine(Command): + name: str = 'preskumaj' + # aliases: List[str] + description: str = 'preskúma zvolený predmet' + + def exec(self, context: Context, param: str): + # bol zadany nazov predmetu? + if len(param) == 0: + print('Neviem čo chceš preskúmať.') + return + + item = get_item_by_name(param, context.room['items'] + context.backpack) + + if item is None: + print('Taký predmet tu nikde nevidím.') + return + + print(item.description) diff --git a/commands/inventory.py b/commands/inventory.py new file mode 100644 index 0000000..066cd30 --- /dev/null +++ b/commands/inventory.py @@ -0,0 +1,21 @@ +from dataclasses import dataclass, field +from typing import List + +from context import Context +from .command import Command + + +@dataclass +class Inventory(Command): + name: str = 'inventar' + # aliases: List[str] = field(default_factory=['inventory', 'i']) + description: str = 'zobrazí obsah hráčovho batoha' + + def exec(self, context: Context, param: str): + if len(context.backpack) == 0: + print('Batoh je prázdny.') + return + + print('V batohu máš:') + for item in context.backpack: + print(f' * {item.name}') diff --git a/commands/look_around.py b/commands/look_around.py new file mode 100644 index 0000000..ab13509 --- /dev/null +++ b/commands/look_around.py @@ -0,0 +1,21 @@ +# systemove importy/moduly +from dataclasses import dataclass, field + +# tretostranove importy/moduly + +# moje vlastne importy/moduly +from typing import List + +from context import Context +from helpers import show_room +from .command import Command + + +@dataclass +class LookAround(Command): + name: str = 'rozhliadni sa' + # aliases: List[str] = field(default_factory=['look around']) + description: str = 'zobrazí opis miestnosti' + + def exec(self, context: Context, param: str): + show_room(context.room) diff --git a/commands/north.py b/commands/north.py new file mode 100644 index 0000000..0101c78 --- /dev/null +++ b/commands/north.py @@ -0,0 +1,28 @@ +from dataclasses import dataclass +from typing import List + +from helpers import show_room +from .command import Command +from context import Context + + +@dataclass +class North(Command): + name: str = 'sever' + # aliases: List[str] + description: str = 'presunie sa do miestnosti na sever od aktuálnej' + + def exec(self, context: Context, param: str): + room_name = context.room['exits']['north'] + + # je na sever prechod? + if room_name is None: + print('Tam sa nedá ísť.') + return + + # prejdem na sever + context.room = context.world[room_name] + show_room(context.room) + + # append to history + context.history.append(self.name) \ No newline at end of file diff --git a/commands/quit.py b/commands/quit.py new file mode 100644 index 0000000..8bea7bb --- /dev/null +++ b/commands/quit.py @@ -0,0 +1,20 @@ +from dataclasses import dataclass +from typing import List + +import states +from context import Context +from .command import Command + + +@dataclass +class Quit(Command): + name: str = 'koniec' + # aliases: List[str] + description: str = 'ukončí rozohratú hru' + + def exec(self, context: Context, param: str): + line = input('Naozaj chceš skončiť? (a/n) ').strip().lower() + if line in ('a', 'ano', 'y', 'yes'): + context.game_state = states.QUIT + else: + print('Tak hráme ďalej...') diff --git a/commands/save.py b/commands/save.py new file mode 100644 index 0000000..eda41b2 --- /dev/null +++ b/commands/save.py @@ -0,0 +1,38 @@ +import json +from dataclasses import dataclass +from typing import List + +from .command import Command +from context import Context + + +@dataclass +class Save(Command): + name: str = 'uloz' + # aliases: List[str] + description: str = 'uloží aktuálny stav hry do súboru' + + def exec(self, context: Context, path: str): + # je historia prazdna? + if len(context.history) == 0: + print('Zatiaľ nie je čo ukladať.') + return + + # bol zadany subor na ulozenie? + if path == '': + print('Neviem, do akého súboru chceš stav hry uložiť.') + return + + try: + with open(path, 'w') as file: + json.dump(context.history, file) + print(f'Stav hry bol úspešne uložený do súboru {path}.') + except PermissionError: + print(f'Chyba: nemáš dostatočné práva na uloženie stavu hry do súboru {path}.') + except FileNotFoundError: + print(f'Chyba: cesta k súboru {path} neexistuje.') + except IsADirectoryError: + print(f'Chyba: cesta {path} je priečinok.') + except Exception as ex: + print('Chyba: Pri ukladaní stavu do súboru došlo k chybe.') + print(type(ex)) diff --git a/commands/south.py b/commands/south.py new file mode 100644 index 0000000..d29ca38 --- /dev/null +++ b/commands/south.py @@ -0,0 +1,28 @@ +from dataclasses import dataclass +from typing import List + +from helpers import show_room +from .command import Command +from context import Context + + +@dataclass +class South(Command): + name: str = 'juh' + # aliases: List[str] + description: str = 'presunie sa do miestnosti na juh od aktuálnej' + + def exec(self, context: Context, param: str): + room_name = context.room['exits']['south'] + + # je na juh prechod? + if room_name is None: + print('Tam sa nedá ísť.') + return + + # prejdem na juh + context.room = context.world[room_name] + show_room(context.room) + + # append to history + context.history.append(self.name) diff --git a/commands/take.py b/commands/take.py new file mode 100644 index 0000000..3cacdb2 --- /dev/null +++ b/commands/take.py @@ -0,0 +1,44 @@ +from dataclasses import dataclass +from typing import List + +from helpers import get_item_by_name +from items.features import MOVABLE +from .command import Command +from context import Context + + +@dataclass +class Take(Command): + name: str = 'vezmi' + # aliases: List[str] + description: str = 'zoberie predmet z miestnosti a vloží ho do batohu' + + def exec(self, context: Context, param: str): + # bol zadany nazov predmetu? + if param == '': + print('Neviem, čo chceš vziať.') + return + + # je v miestnosti? + item = get_item_by_name(param, context.room['items']) + if item is None: + print('Taký predmet tu nikde nevidím.') + return + + # je prenositelny? + if MOVABLE not in item.features: + print('Tento predmet sa nedá zobrať.') + return + + # vezmi! + # vymazem z miestnosti + context.room['items'].remove(item) + + # vlozim do batohu + context.backpack.append(item) + + # render + print(f'Do batohu si si vložil predmet {param}.') + + # append to history + context.history.append(f'{self.name} {param}') diff --git a/commands/use.py b/commands/use.py new file mode 100644 index 0000000..df893ff --- /dev/null +++ b/commands/use.py @@ -0,0 +1,37 @@ +from dataclasses import dataclass +from typing import List + +from helpers import get_item_by_name +from items.features import USABLE +from .command import Command +from context import Context + + +@dataclass +class Use(Command): + name: str = 'pouzi' + # aliases: List[str] + description: str = 'použije zvolený predmet' + + def exec(self, context: Context, param: str): + # bol zadany nazov predmetu? + if param == '': + print('Neviem, čo chceš použiť.') + return + + # je v miestnosti alebo batohu? + item = get_item_by_name(param, context.room['items'] + context.backpack) + if item is None: + print('Taký predmet tu nikde nevidím.') + return + + # je pouzitelny? + if USABLE not in item.features: + print('Tento predmet sa nedá použiť.') + return + + # append to history + context.history.append(f'{self.name} {param}') + + # use item + item.use(context) diff --git a/commands/west.py b/commands/west.py new file mode 100644 index 0000000..f011192 --- /dev/null +++ b/commands/west.py @@ -0,0 +1,28 @@ +from dataclasses import dataclass +from typing import List + +from helpers import show_room +from .command import Command +from context import Context + + +@dataclass +class West(Command): + name: str = 'zapad' + # aliases: List[str] + description: str = 'presunie sa do miestnosti na západ od aktuálnej' + + def exec(self, context: Context, param: str): + room_name = context.room['exits']['west'] + + # je na zapad prechod? + if room_name is None: + print('Tam sa nedá ísť.') + return + + # prejdem na zapad + context.room = context.world[room_name] + show_room(context.room) + + # append to history + context.history.append(self.name) diff --git a/context.py b/context.py new file mode 100644 index 0000000..a298f81 --- /dev/null +++ b/context.py @@ -0,0 +1,13 @@ +from dataclasses import dataclass + +import states + + +@dataclass +class Context: + world: dict + room: dict + backpack: list + commands: list + history: list + game_state: str = states.PLAYING diff --git a/game.py b/game.py new file mode 100755 index 0000000..1af97ea --- /dev/null +++ b/game.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 +import json + +import states +from commands import About, Inventory, LookAround, Quit, Drop, Examine, Commands, Take, Use, West, South, North, East, \ + Save +from context import Context +from helpers import show_room +from items import matches, door, bucket, newspaper, canister + +# from world import world +from world import world + + +def intro(): + """ + Shows intro. + + This function shows intro banner for the game. + """ + print(" ___ _ _ _") + print("|_ _|_ __ __| (_) __ _ _ __ __ _ | | ___ _ __ ___ ___") + print(" | || '_ \\ / _` | |/ _` | '_ \\ / _` | _ | |/ _ \\| '_ \\ / _ \\/ __|") + print(" | || | | | (_| | | (_| | | | | (_| | | |_| | (_) | | | | __/\\__ \\") + print("|___|_| |_|\\__,_|_|\\__,_|_| |_|\\__,_| \\___/ \\___/|_| |_|\\___||___/") + + print("Indiana Jones and his Great Escape".center(70)) + + +def outro(): + """ + Shows outro (credits). + + This functions shows credits at the end of the game. This is the last test, the player will see. + """ + print('*-' * 35) + print('Created by (c) mirek - A very talented young python programmer.') + print('Please support his next magic project by sending some funds') + print('(at least 100 Euros). He will create something.') + + +def parse(line: str, commands): + """ + Parse the command from input + + @param line: input from the user + @param commands: list of commands + @return: tuple with cmd and it's param, or None otherwise + """ + for command in commands: + if line.startswith(command.name): + param = line.split(command.name)[1].strip() + return command, param + + # return None + + +def main(): + # game init + # with open('assets/world.json', 'r') as file: + # world = json.load(file) + # world['kobka']['items'] += [ + # door, + # bucket, + # newspaper, + # canister + # ] + + context = Context( + history=[], + world=world, + backpack=[ + matches, + ], + room=world['kobka'], + commands=[ + About(), + Commands(), + Drop(), + East(), + Examine(), + Inventory(), + LookAround(), + North(), + Quit(), + Save(), + South(), + Take(), + Use(), + West(), + ] + ) + + # intro + intro() + show_room(context.room) + + # game loop + while context.game_state == states.PLAYING: + line = input('> ').lstrip().rstrip().lower() + + if line == '': + continue # pass + + # parse line + try: + cmd, param = parse(line, context.commands) + cmd.exec(context, param) + + # set state according to the current room + if context.room['name'] == 'priekopa': + context.game_state = states.DEATH + elif context.room['name'] == 'hangár': + context.game_state = states.WIN + except TypeError: + print('Taký príkaz nepoznám.') + + # evaluate the final states + if context.game_state == states.DEATH: + print('Ani taký slávny dobrodruh ako Indiana Jones nezvládne chúťky slovenských aligátorov. Tvoja púť sa ' + 'skončila v žalúdku niekoľkých z nich.') + + elif context.game_state == states.WIN: + print('Pohodlne si sa usadil na sedadle značne nepohodlného lietadla a užil si si let smerom do Moskvy.') + print('Ďalšie dobrodružstvo neohrozeného dobrodruha Indiana Jonesa sa tým skončilo.') + + # credits + outro() + + +if __name__ == '__main__': + main() diff --git a/guess.py b/guess.py deleted file mode 100755 index 9138c04..0000000 --- a/guess.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python3 -from random import randint - -# 8. sprav funkciu, play_game(). a v nej sa odohrá jedna partia hry. -def play_game(secret=11): - """ - Plays a signle round of game. - - This function runs a one round of a game. Goal is to find the secret number. - - :param secret: the secret number to find - """ - - # 1. pozdravis - myslim si cislo od 1 do 20 - print('Myslím si číslo od 1 do 20.') - tip = None - attempts = 1 - - # 6. hraj, kym neuhadnes - # 7. mas na to 5 pokusov a ked neuhadnes, tak vypis na obrazovku: Ta ty si jaká lama. - while attempts <= 5: - attempts += 1 - - # 2. opytaj sa hraca na jeho tip - s_tip = input('Tvoj tip: ') - tip = int(s_tip) - - # 3. ak je tajne cislo mensie ako hracove, tak vypise na obrazovku: Hmm... Moje číslo je menšie ako tvoje. - if secret < tip: - print('Hmmm... Moje číslo je menšie ako tvoje.') - - # 4. ak je tajne cislo vacsie ako hracove, tak vypise na obrazovku: Hmm... Moje číslo je väčšie ako tvoje. - elif secret > tip: - print('Hmmm... Moje číslo je väčšie ako tvoje.') - - # 5. ak je tajne cislo rovne s hracovym, tak vypis: Ta ty genius. - else: - print('Ta ty genius!') - break - - else: - print(f'Ta ty si jaká lama. Moje tajné číslo bolo {secret}.') - - -if __name__ == '__main__': - secret = randint(1, 20) - play_game(secret) - print('the end') diff --git a/helpers.py b/helpers.py new file mode 100644 index 0000000..36d372f --- /dev/null +++ b/helpers.py @@ -0,0 +1,56 @@ +def get_item_by_name(name: str, items: list): + for item in items: + if name == item.name: + return item + + # return None + + +def show_room(room: dict) -> None: + """ + Shows the content of the room + + This function shows the content of the room: it's name, description, exits and items, which are located in the room. + @param room: the room to show + """ + if not isinstance(room, dict): + # if type(room) != dict: + raise TypeError('Room is not of type dictionary.') + + print(f'Nachádzaš sa v miestnosti {room["name"]}') + print(room["description"]) + + # show exits + translation = { + 'north': 'sever', + 'south': 'juh', + 'east': 'východ', + 'west': 'západ' + } + + # check if there is any exit + some_exit = False + for ex in room['exits']: + if room['exits'][ex] is not None: + some_exit = True + break + + if some_exit is False: + print('Z miestnosti nevedú žiadne východy.') + else: + print('Možné východy z miestnosti:') + for exit in room['exits']: + if room['exits'][exit] is not None: + print(f' * {translation[exit]}') + + # print items + if len(room['items']) == 0: + print('Nevidíš tu nič zvláštne.') + else: + print('Vidíš:') + for item in room['items']: + print(f' * {item.name}') + + # print('Vidíš:', ', '.join(room['items'])) + + # return None diff --git a/items/__init__.py b/items/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/items/bucket.py b/items/bucket.py new file mode 100644 index 0000000..3e2b4ac --- /dev/null +++ b/items/bucket.py @@ -0,0 +1,40 @@ +from context import Context +from helpers import get_item_by_name +from .features import USABLE, MOVABLE + +name = 'vedro' + +description = 'Vedro plné vody.' + +features = [ + USABLE, + MOVABLE +] + + +def use(context: Context): + # 1. preconditions + # som v miestnosti s dverami? + # horia dvere? + door = get_item_by_name('horiace dvere', context.room['items']) + if door is None: + print('Nabral si si vodu do dlaní, chlipol si si, poprevaľoval si si vodu v papuľke a vypľul si ju naspäť. Na ' + 'neskôr.') + return + + # 2. action + # aktualizujem vedro + # - nebude USABLE + features.remove(USABLE) + # - aktualizujem opis (prazdne vedro) + global description + description = 'Prázdne vedro' + # aktualizujem dvere + # - odstranim dvere z hry (zmazem ich z room['items'] + context.room['items'].remove(door) + # otvorim prechod do novej miestnosti + # - na zapad -> garden + context.room['exits']['west'] = 'záhradka' + + # 3. render + print('Masaker. ta si zahasil poziar a sa dvere rozpadli.') diff --git a/items/canister.py b/items/canister.py new file mode 100644 index 0000000..c17e5fe --- /dev/null +++ b/items/canister.py @@ -0,0 +1,39 @@ +from context import Context +from helpers import get_item_by_name +from .features import USABLE, MOVABLE + +name = 'kanister' + +description = '10 litrový kanister plný kvalitného slovnafťáckeho vysokooktánového 95% benzínu.' + +features = [ + USABLE, + MOVABLE +] + + +def use(context: Context): + # 1. preconditions + # * dvere su v miestnosti + door = get_item_by_name('dvere', context.room['items']) + if door is None: + print('Zohol si sa, vzal si plny kanister do ruky a zacal si posilovat. Ved predsa plati, ze v zdravom tele ' + 'zdravy duch.') + return + + # 2. action + # * polejeme dvere + # - aktualizujeme dvere - description, name + door.description = 'Veľké masívne drevené dvere nasiaknuté kvalitným vysokooktánovým 95% benzínom.' + door.state = door.SOAKED_STATE + + # * aktualizujeme kanister + # - kanister uz nebude USABLE + features.remove(USABLE) + # - aktualizujeme description + global description + description = '10 litrový prázdny kanister na benzín.' + + # 3. render + print('Odsroboval si vrchnak kanistru a cely jeho obsah si vylial na velke masivne dvere. Doteraz tu bol taky ' + 'pokoj a klud, ale odteraz sa vzduchom rozplyva vona 95% kvalitneho vysokooktanoveho benzinu z ruska.') diff --git a/items/door.py b/items/door.py new file mode 100644 index 0000000..a6ea39d --- /dev/null +++ b/items/door.py @@ -0,0 +1,12 @@ + +name = 'dvere' + +description = 'Veľké masívne drevené dvere.' + +features = [] + +NORMAL_STATE = 1 +SOAKED_STATE = 2 +BURNING_STATE = 3 + +state = NORMAL_STATE diff --git a/items/features.py b/items/features.py new file mode 100644 index 0000000..289d7ac --- /dev/null +++ b/items/features.py @@ -0,0 +1,5 @@ +# if the item can be taken or dropped from/to backpack +MOVABLE = 1 + +# if it is possible to use item (somehow) +USABLE = 2 diff --git a/items/matches.py b/items/matches.py new file mode 100644 index 0000000..6b65cc9 --- /dev/null +++ b/items/matches.py @@ -0,0 +1,46 @@ +from context import Context +from helpers import get_item_by_name +from .features import USABLE, MOVABLE + +name = 'zapalky' + +description = 'Krabička bezpečnostných zápaliek značky Billa. Zahrkal si si s nimi pri ušku, aby si sa presvedčil, ' \ + 'že krabička nie je prázdna.' + +features = [ + USABLE, + MOVABLE +] + + +def use(context: Context): + # 1. preconditions + # som v miestnosti s dverami? + door = get_item_by_name('dvere', context.room['items']) + if door is None: + print('zahrkal si zapalkami pri usku a presvedcil si sa, ze v nej naozaj nieco je.') + return + + # su dvere su poliate benzinom? + if door.state != door.SOAKED_STATE: + print('Hmm... To mi nejako nevychádza. Si istý, že tie drevené dvere len tak podpáliš jednou zápalkou?') + return + + # 2. action + # dvere zacnu horiet + + # - aktualizujeme popis aj nazov dveri + door.name = 'horiace dvere' + door.description = 'Veľké dubové masívne horiace dvere.' + door.state = door.BURNING_STATE + + # zapalky uz nebudu pouzitelne + # - odstranime USABLE zo zapaliek + features.remove(USABLE) + # - aktualizujeme opis (prazdna krabicka) + global description + description = 'Prázdna krabička bezpečnostných zápaliek značky Billa.' + + # 3. render + print('Skrtol si zapalkou a dvere okamzite zblkli. Ak si si doteraz myslel, ze ti tu bola zima a este aj sero, ' + 'tak uz to tak nie je. Je tu prijemne teplucko.') diff --git a/items/newspaper.py b/items/newspaper.py new file mode 100644 index 0000000..ba9529d --- /dev/null +++ b/items/newspaper.py @@ -0,0 +1,27 @@ +import random + +from context import Context +from .features import USABLE, MOVABLE + +name = 'noviny' + +description = 'Čerstvé vydanie liberálneho denníka plného americkej nenávisti Denník N.' + +features = [ + USABLE, + MOVABLE +] + + +def use(context: Context): + print('Zalistoval si si v novinách a začítal si sa') + + headlines = [ + 'Tri steniatky najdene po troch dnoch nezvestnosti', + 'Matka predala vlastny byt. Zostali jej oci pre plac', + 'Radost v Kosiciach. Novy stadion otvoreny', + 'Macky boli pravymi vladcami Egypta, tvrdi historik', + 'Klobuky ktore sa hodia na vsetky mudre hlavy' + ] + + print(random.choice(headlines)) diff --git a/states.py b/states.py new file mode 100644 index 0000000..d8bf3a4 --- /dev/null +++ b/states.py @@ -0,0 +1,4 @@ +PLAYING = 'playing' +QUIT = 'quit' +DEATH = 'death' +WIN = 'win game' diff --git a/world.py b/world.py new file mode 100644 index 0000000..1d92452 --- /dev/null +++ b/world.py @@ -0,0 +1,61 @@ +from items import door, bucket, newspaper, canister + +world = { + 'kobka': { + "description": 'Nachádzaš sa v miestnosti plnej ružových slonov. Aby si si neublížil, tak stena je pokrytá ' + 'vankúšikmi. Tiež ružovými. Žiadne okno ti neposkytne rozkošný pohľad na vonkajšiu faunu a ' + 'flóru.', + "items": [ + door, + bucket, + newspaper, + canister + ], + "exits": { + 'north': None, + 'south': None, + 'east': None, + 'west': None + }, + "name": 'kobka', + }, + + 'záhradka': { + 'name': 'záhradka', + 'description': 'Značne neudržiavané miesto, ktoré zrejme kedysi bolo záhradkou.', + 'items': [], + "exits": { + 'north': 'hangár', + 'south': 'priekopa', + 'east': 'kobka', + 'west': None + }, + }, + + 'priekopa': { + 'name': 'priekopa', + 'description': 'Nedával si si pozor a rovno si čľupol medzi aligátory. Čo tu robia na Slovensku také ' + 'ušľachtilé tvory? Pomyslel si si. Čo tu robí taký výdatný štyridsiatnik? Pomysleli si oni. ' + 'A nerozmýšľali o tom dlho.', + 'items': [], + "exits": { + 'north': None, + 'south': None, + 'east': None, + 'west': None + }, + }, + + 'hangár': { + 'name': 'hangár', + 'description': 'Nachádzaš sa v priestor známeho leteckého dopravcu značky Aeroflot, ktorý ťa určite dopraví ' + 'bezpečne domov.', + 'items': [], + 'exits': { + 'north': None, + 'south': 'záhradka', + 'east': None, + 'west': None + } + } +}