CodeGym/コース/Java のマルチスレッドプログラミング/大きな仕事: Java でゲームを書く

大きな仕事: Java でゲームを書く

使用可能

「こんにちは、アミーゴ!」

「こんにちは、リス船長さん!」

「これから、大きなプロジェクトの書き方を学んでいきます。したがって、新しいタイプのタスク、«大きなタスク» を導入します。これは 1 つの大きなタスクであり、多くの小さなタスクに分割されます。それぞれの «小さなタスク» を解決するとき» 最初から何かを記述するのではなく、既存のコードに新しいコードを追加します。フェデレーションの未来はあなたの手の中にあります。」

"かしこまりました!"

「最初の 5 つの「大きなタスク」の目標は、大規模で複雑なプロジェクトの書き方を学ぶことです。最初は、「小さなタスク」の説明はかなり詳細で、場合によっては過度に詳細になることもあります。その後、説明はより一般的になります。 「そして、タスクは大きくなります。最初は、タスクは「あなたの」コードの小さな断片に過ぎません。その後、タスクは大きくなり、最終的にはフレームワーク (ライブラリ) 全体になります。」

「準備はできています、先生!」

「タスクの説明をできるだけ明確にするよう努めました。しかし、何かがうまくいかない場合は、次のようにします。」

a) 状態を解釈する他の方法を検討します。おそらくそれは私が望んでいたほど明確ではありません。

b) いくつかの異なる方法で解決してみます。

c) 助けを求めるか、手紙を書いてください。これらは新しいタスクですが、必要に応じて喜んで「磨き」ます。

「これがあなたの最初のタスクです。」

「今日は«Hippodrome»という小さなゲームを書きます。

「そして、私たちが言うとき、私はあなたのことを指します。私はあなたの指導者になります。」

「条件はどこにあるの?」

「どういう状況ですか、プライベートですか? まだ基礎訓練を受けていますか? これは秘密の軍事プログラムです。IntelliJ IDEA を開始してください。このタスクの最初の部分がそこにあります。後続の各タスクは、タスクを正常に完了した後にのみ利用可能になります。前のやつ。出て行け!」

「はい、先生!引っ越します!」

「そして、ソリューションが絶望的に​​混乱した場合は、大きなタスクをリセットしてやり直すことができることを覚えておいてください。タスク リスト プラグインで大きなタスクを右クリックすると、さまざまなオプションを含むコンテキスト メニューが表示されます。」

6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 1)
Today we'll write a small game called 'Hippodrome'. And when I say we, I mean you. I will be your mentor. To start, we'll need Hippodrome and Horse classes. Create the Hippodrome and Horse classes. And don't forget that every program starts with a main method. Add one to the Hippodrome class.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 2)
Hey, we've got a hippodrome, and it should have some horses. So our hippodrome should store a list of its horses. Add a List horses field to the Hippodrome class. And to ensure that the horses won't be stolen, make this field private. Add a getter for this field.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 3)
Even though we've declared the horses variable, we haven't yet created the list itself (unless of course you've managed to get ahead of us). Create a Hippodrome class constructor with one List parameter. Save the passed list in the horses field (initialize the horses field).
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 4)
Now back to the horses. Every horse in a horse race must have a name and speed. Our horses will simply run a certain amount of time (100 seconds or "steps"). The horse that runs the farthest is the winner. Accordingly, we'll also need to store the distance the horse has already run.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 5)
Finish writing the Horse class. Add a constructor with name, speed, and distance parameters. Add getters and setters for all the fields in the Horse class. Make all of the methods public unless indicated otherwise.
12
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 6)
Now let's move on to the Hippodrome class and the main method. We need to create a Hippodrome object and add some horses to it. To start, in the Hippodrome class: Create a static Hippodrome game field.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 7)
But that's not all. We need to make the horses run. Add run, move, and print methods to the Hippodrome class. Without parameters. The move method will control the movement of all horses. The print method will draw them on the screen. And the run method manages it all.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 8)
Make a loop from 1 to 100 in the run method. This will be our race. In the body of the loop, we'll call move first, then print. To prevent the whole loop from finishing in a split second, add Thread.sleep(200); as well.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 9)
Now let's return to the move and print methods. Let's start with move. In the Hippodrome class's move method, we call the move method on each horse in the loop. Yes, you're right: the Horse class doesn't have it yet. This means you need to add your own move method to the Horse class. :)
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 10)
We still need to write the Hippodrome class's print method. Everything is simple here, too: In a loop, we call the print method on each horse. Oh, and display 10 empty lines after the loop (using System.out.println()) to make it more beautiful.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 11)
We're almost done with the Hippodrome class. Add a call to run() at the end of the main method. Hint: The run() method is not static, so you can only call it on an object. But where do you get the object? Hint 2: game.run();
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 12)
There's just a little left to do: finish writing the Horse class. The move method is called each time a horse takes a step. When the move method is called on a horse, the horse must run a certain distance. The distance depends on the horse's speed.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 13)
Now we'll return to the Horse class's print method. Because we're working with the console, then all the racing horses will look something like this: ........Slevin ....Lucky ..........Homer In other words, the print method should display a string consisting of dots and the horse's name.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 14)
Time to enjoy our creation. A new snapshot of the racetrack is displayed every half second. Use the mouse to decrease the size of the console so that only one "snapshot" is visible at a time and remains in the same place. Then you can watch the race live and even commentate.
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 15)
Add code to announce the winner. We'll make two methods in the Hippodrome class: public Horse getWinner() and public void printWinner() The getWinner method must return the horse that has run the farthest. The printWinner method displays the winner's name as follows: The winner is <name>!
6
タスク
Java のマルチスレッドプログラミング,  レベル 1レッスン 16
ロック未解除
Hippodrome (part 16)
Now we're done for sure. Add a call to the printWinner method to the end of the main method. Launch and enjoy your first computer game. :)
コメント
  • 人気
  • 新規
  • 古い
コメントを残すには、サインインしている必要があります
このページにはまだコメントがありません