注38:Pythonを難しい方法で学ぶ、演習43オブジェクト指向OOPゲームコードの変更(3)独自のストーリーを作成する



Note 38 Learn Python Hard Way



注38:Pythonを愚かな方法で学ぶ、演習43オブジェクト指向OOPゲームコード(3)ストーリーを構成する

この演習43は、統合演習の要件に従って自分でストーリーを構成するためのメモが必要ですが、コーディングレベルが制限されているため、元のコードに基づいてわずかな変更を加え、ゲームストーリーを作成します。すべてのリンクをループする可能性があります。 。少なくともこの話のようになってはいけません。パスワードはどうしても推測できません。後のプロットをゲームの背後に隠すだけで、太陽が見えなくなります。
私は暴力的になりたくありません、あまりにも多くの人々がこのタイプが好きです。実際、それらの暴力の終焉を想像するなら、あなたは暴力を憎むべきです。しかし、この世界の多くの人々の論理は、おそらく自分自身を排除することです。私がミートソースで肉のパテになれない限り、彼らの外の世界は氾濫し、血まみれです。
最初の仮定:
1.ゲームの背景の基本的な説明:
1大陸間宇宙船、観光客
2野蛮人の網を根絶し、野蛮人を根絶します。
3方向ボードフランクフルト広州



第二に、ゲームシーン
1ship(シーン)宇宙船
2中央回廊
3ship room spaceship room、部屋のパスワードがあります、入力するにはパスワードを推測する必要があります
4theブリッジ宇宙船のメインコントロールキャビンで、野蛮人は船をハイジャックしようとします、
5エスケープポッド。
6マップ、エンジン、プレイ

3.ストーリー編集
コードに存在するため、ここでは省略します。
4.コード設計
1.シーン(オブジェクト)の基本クラスには、その下にあるすべてのシーンの一般的な情報が含まれており、多くの場所が理解されていません。それは最も基本的なカテゴリでなければなりません、それを静止させてください
2.エンジン(シーン)クラス:これは、シーンの下の2番目のクラスである必要があります。どちらの関数もmapに関連しており、このクラスの下にwhileループがあり、当面は変更されません。
3.ship(シーン)クラス:これもシーンの下のセカンダリクラスである必要があり、スクリプトのプロットはここで説明されています。
quips quips:ここにいくつかの機能が追加され、出発点は船に設定されています。
4. CentralCorridor(scene)カテゴリ:これもシーンの下の第2レベルのカテゴリであり、defenterの後の別の行です。
5.エスケープポッド(シーン)クラス:これはシーンの下のセカンダリクラスでもあります
行:
6.終了しました。
a_mapの下のすべてのシーンを試し、中央の廊下ではなく船に入れました。
コードは部分的にしか変更されていないので、さりげなく動かさないようにしています。結局のところ、まだなじみのない多くのリンクがまだあります。しかし、すべてのテキストは書き直されており、意図は継続的で中断されない可能性があります。しかし、変更後の結果は満足のいくものではなく、繰り返しのプロセスを達成することは困難です。しかし、それでも人々にある程度の安心感を与えます。スムーズに実行することはできませんが、それでも流れることができます。この言語の学習プロセスはまだ非常に長いようです。これは便利な試みです。人々にこのプログラミング言語を理解させるいくつかのことは、学習への興味を消すのではなく、人々の興味をそそります。
以下は、コードを統合し、コードを変更し、テキストを再編集するための演習ex43.7.pyです。もともとは動かないと思っていました。数日間熟考した後、それは流れることができますが、それでもシーケンスすることはできません。サイクル、後で考えてください。記念としてここに転写します。
演習のex43.7.pyコードを統合する



Insert code snippet herefrom sys import exit from random import randint from textwrap import dedent class Scene(object): def enter(self): print('This scene is not yet configured.') print('Subclass it and implement enter().') exit(1) class Engine(object): def __init__(self, scene_map): self.scene_map = scene_map def play(self): current_scene = self.scene_map.opening_scene() last_scene = self.scene_map.next_scene('finished') while current_scene != last_scene: next_scene_name = current_scene.enter() current_scene = self.scene_map.next_scene(next_scene_name) current_scene.enter() class Ship(Scene): quips = [ ' Problem Intercontinental Spaceship Passengers and Barbarians ' 'Welcome to this intercontinental spacecraft traveling through Eurasian space ' 'You will fly from Guangzhou to Frankfurt, or fly from Frankfurt to Guangzhou ' 'A wonderful intercontinental trip, enjoying the luxury and transcendence of the spaceship ' 'There are puppies and kittens on the boat, as well as pets that are cuter than them ' 'You will pass the time when the spaceship flies like the speed of light dreamily.' ] def enter(self): print(Ship.quips[randint(0, len(self.quips)-1)]) timu = input('> ') if timu == 'Intercontinental Spaceship Travel Notes': print(dedent(''' Topic: Passengers and Barbarians in Intercontinental Spacecraft Welcome to this intercontinental spacecraft traveling through Eurasian space You will fly from Guangzhou to Frankfurt, or fly from Frankfurt to Guangzhou A wonderful intercontinental trip, enjoy the luxury and transcendence of the spacecraft There are puppies and kittens on the boat, as well as pets that are cuter than them You will dream through the time when the spaceship flies like the speed of light. ''')) writor = input('> ') if writor == 'namezhou': print(dedent(''' The author of the game, namezhou, imitated the stupid way to learn exercise 43 as a consolidation exercise The basic knowledge of the question. The game idea is: an intercontinental spacecraft interacts with Europe and Asia, During the flight, the visitors were hijacked by barbarians, and the tourists first used the anti-barbarian nets and boards to resist the hijacking. Later use The steering board allows the spacecraft to fly normally. The barbarian finally lost because of his stupidity. Spaceship experience The intercontinental experience from Guangzhou to Frankfurt and back to Guangzhou from Frankfurt. ''')) return 'central_corridor' class CentralCorridor(Scene): def enter(self): print(dedent(''' Traveling is always risky, and unfortunately this spaceship was spotted by barbarians. They are hidden in the ship and want to hijack this ship. To ensure the safety of tourists on board, These barbarians must be driven away and driven into the sky outside the ship Go to the main control cabin, where there are weapons to resist the barbarians. Ugly, stupid, but powerful barbarians need smart tools to subdue them. It just so happens that the main control cabin of the spacecraft has such a smart tool, a group of mieman nets, Miemanwang traps the barbarian’s hands and automatically throws them to the sky, Go ahead and find those barbarians who have been hijacked by Miibarnet. ''')) action = input('> ') if action == 'Mieman.com': print(dedent('''' Quickly find the Destroyer Net, and then aim at the barbarians. The barbarian didn’t know it himself, Why did they hijack this traveling spacecraft. You should have a lot of The role will definitely be able to subdue these barbarians. This world is really strange , This spaceship has almost nothing to do with the barbarians, I don’t know who provoked whom Up. Let them go stupid, a lot of stupid in this world is unreasonable, if it makes sense , How can you call it stupid? ''')) return 'central_corridor' elif action == 'Leave temporarily': print(dedent(''' I found Mieman.com and threw it to the barbarians. They are stupid. Will catch it, as long as they touch it, they will be caught by the net. In an instant, this net has a huge thrust, pushing them into the sky If you can’t find this Miemannet, then you have to escape temporarily ''')) return 'central_corridor' elif action == 'reading': print(dedent('''' Open the book quickly, just remember what to take to find a way Attention, the book tells you how to guess the password. ''')) return 'central_corridor' else: print('DOES NOT COMPUTE!') return 'central_corridor' class EscapePod(Scene): def enter(self): print(dedent(''' Like a prophet, the spacecraft laboratory had expected the hijacking, The door opens automatically, and the book you are looking for is at the door. Go find Miemanban, find Miemanban, and follow the instructions in the book Open the board password by the password, and the barbarian drove out of the spaceship. You are safe. If luck is too bad, it will be a bit difficult. ''')) codecombine = f'{randint(1, 9)} {randint(1, 9)} {randint(1, 9)}' code = O012 guess = input('[keypad]> ') guesses = 0 while guess != code and guesses < 10: print('BZZZZEDDD') guesses += 1 guess = input('[keypad]> ') if guess == code: print(dedent(''' The container was opened, the seal was cracked, and the fragrance overflowed. The slaying board is in your hands, you run to the main control cabin with all your strength, You have to point the barbarians at the barbarians. But that is not easy. There is still a way to go for them to fly out of the spacecraft. ''')) return 'the_bridge' else: print(dedent(''' The sound generator of the Mianmanban will make a sound and you hear a sinking sound The sound of fusing. You may be able to see that the barbarians are still there, Because there is no password, Mianmanban cannot function, you are confused. ''')) return 'the_bridge' class TheBridge(Scene): def enter(self): print(dedent(''' You rushed into the main control compartment with the destroyer board, the barbarian was shocked. They are trying their best to control the spacecraft. Every one of the barbarians is covered Black hair, red hair and round eyes, hideous complexion. When these savages see you under threat They didn’t have time to carry the Destroyer Board Draw out their weapons, they don't know what this board is. ''')) action = input('> ') if action == 'Maimanban': print(dedent(''' The spaceship passenger confronts the barbarian, and you turn the barbarian towards the barbarian, The hatch jumped a little. Just when you are ready to activate the killer board function, There is a barbarian rushing from the right behind you. You were knocked out, But you also see that another barbarian is trying to take your hand The killer board. ''')) return 'escape_pod' elif action == 'Find Direction Board': print(dedent(''' The barbarian sees you walking over with the killer board, you turn the killer board towards Their hands began a desperate struggle to resist the hijacking. Soon, kill the man board Grabbed the barbarian's hand. You slam the remote control of the Demon Board on your body, the Demon Board It was activated, and the barbarian flew to a uniform of the spaceship under the power of the destroyer board. The hijacked hatch, the hatch opened, and the barbarians all drove out of the spaceship and entered the sky, The barbarian's abduction failed. ''')) return 'escape_pod' else: print('DOES NOT COMPUTE!') return 'the_bridge' class EscapePod(Scene): def enter(self): print(dedent(''' The barbarians retreated, but new problems arose. The spaceship is fighting, the one that guides the voyage The steering board is broken, you must have a new steering board to fly normally. The place where the steering board was stored, the tourists were all confused. The 5 direction boards are there, But not all of these direction boards are intact, only one direction board is available. There are 5 direction boards in total, which one do you take? Another guess, how to guess? ''')) good_pod = randint(1, 5) guess = input('[pod #]> ') action = input('> ') if int(guess) != good_pod: print(dedent(''' You took the wrong direction board, it is bad not good. Press the steering pad start button. The steering board bounces to a direction that no one knows, then the hull is in the air Stopped taxiing, not knowing where to fly, the spacecraft tourists were once again confused. ''')) return 'escape_pod' elif action == 'take them all': print(dedent(''' You get all 5 direction boards, press the pop-up button all the way. There is a direction board Pointing very lightly in the direction of Frankfurt, the intercontinental spacecraft started from the west of Asia Flying west, you look west through the porthole of the spacecraft, clouds and snowy mountains, sun and sunset, A beautiful picture. When night is approaching, there are twinkling stars. Frankfurt was just underfoot, and the spacecraft successfully landed in Europe. ''')) return 'finished' else: print(dedent(''' You got the only good steering board, which is perfect. Press the direction board Button, it points lightly in the direction of Guangzhou, and the spacecraft continues from the east of Europe Flying east, you look east through the porthole of the spacecraft, the Yangtze River, the Yellow River, and Mount Tai Kunlun, The magnificent rivers and mountains have a panoramic view. As night approaches, the glamorous tower matches the twinkling stars. Baiyun Airport was just underfoot, and the spacecraft successfully landed in Guangzhou. ''')) return 'finished' class Finished(Scene): def enter(self): print('You win! Good job.') GreatEnd = input('> ') if GreatEnd == 'Fantastic Intercontinental Travel': print(dedent(''' Good travel! ! We're back!! We have to travel! ! ''')) return 'finished' class Map(object): scene = { 'ship': Ship(), 'central_corridor': CentralCorridor(), 'the_bridge': TheBridge(), 'escape_pod': EscapePod(), 'finished': Finished(), } def __init__(self, start_scene): self.start_scene = start_scene def next_scene(self, scene_name): val = Map.scene.get(scene_name) return val def opening_scene(self): return self.next_scene(self.start_scene) a_map = Map('ship') a_game = Engine(a_map) a_game.play()

コードの実行後にいくつかの結果がありますが、これは少し安心です。

Insert code snippet here Traveling is always risky, and unfortunately this spaceship was spotted by barbarians. They are hidden in the ship and want to hijack this ship. To ensure the safety of tourists on board, These barbarians must be driven away and driven into the sky outside the ship Go to the main control cabin, where there are weapons to resist the barbarians. Ugly, stupid, but powerful barbarians need smart tools to subdue them. It just so happens that the main control cabin of the spacecraft has such a smart tool, a group of Miman, Mieman.com sets the hand of the barbarians and automatically throws them to the sky, Go ahead and find those barbarians who have been hijacked by Miibarnet. > Reading ' Open the book quickly, just remember what to take to find a way Attention, the book tells you how to guess the password. You rushed into the main control compartment with the destroyer board, the barbarian was shocked. They are trying their best to control the spacecraft. Every one of the barbarians is covered Black hair, red hair and round eyes, hideous complexion. When these savages see you under threat They didn’t have time to carry the Destroyer Board Draw out their weapons, they don't know what this board is. > Destroyer board The spaceship passenger confronts the barbarian, and you turn the barbarian towards the barbarian, The hatch jumped a little. Just when you are ready to activate the killer board function, There is a barbarian rushing from the right behind you. You were knocked out, But you also see that another barbarian is trying to take your hand The killer board. The barbarians retreated, but new problems arose. The spaceship is fighting, the one that guides the voyage The steering board is broken, you must have a new steering board to fly normally. The place where the steering board was stored, the tourists were all confused.5A steering board is placed there, But not all of these direction boards are intact, only one direction board is available. total5A steering board, which one do you take? Another guess, how to guess? > Topic: Passengers and Barbarians in Intercontinental Spacecraft Welcome to this intercontinental spacecraft traveling through Eurasian space You will fly from Guangzhou to Frankfurt, or fly from Frankfurt to Guangzhou A wonderful intercontinental trip, enjoy the luxury and transcendence of the spacecraft There are puppies and kittens on the boat, as well as pets that are cuter than them You will dream through the time when the spaceship flies like the speed of light. > namezhou > Deman ' Quickly find the Destroyer Net, and then aim at the barbarians. The barbarian didn’t know it himself, Why did they hijack this traveling spacecraft. You should have a lot of The role will definitely be able to subdue these barbarians. This world is really strange , This spaceship has almost nothing to do with the barbarians, I don’t know who provoked whom Up. Let them go stupid, a lot of stupid in this world is unreasonable, if it makes sense , How can you call it stupid? Traveling is always risky, and unfortunately this spaceship was spotted by barbarians. They are hidden in the ship and want to hijack this ship. To ensure the safety of tourists on board, These barbarians must be driven away and driven into the sky outside the ship Go to the main control cabin, where there are weapons to resist the barbarians. Ugly, stupid, but powerful barbarians need smart tools to subdue them. It just so happens that the main control cabin of the spacecraft has such a smart tool, a group of Miman, Mieman.com sets the hand of the barbarians and automatically throws them to the sky, Go ahead and find those barbarians who have been hijacked by Miibarnet. > Leave temporarily I found Mieman.com and threw it at the barbarians. They are stupid., Will catch it, as long as they touch it, they will be caught by the net. In an instant, this net has a huge thrust, pushing them into the sky If you can’t find this Miemannet, then you have to escape temporarily > Reading ' Open the book quickly, just remember what to take to find a way Attention, the book tells you how to guess the password. `` Wonderful intercontinental travel Good travel! ! We're back!! We have to travel! ! `