How to write a text based adventure game java




















The one above is the first. After that, the compiler will treat it as a room. The description immediately follows, and that should be pretty obvious. The next thing I introduce is the admin, who is an NPC. That's currently the only room in the program, so I don't need to further specific where he is. Then I specified what his description is. Then I placed an article in the room, and specified that it was edible.

Then gave it a description. This is all really simple stuff, and pretty much all examples of the same principle: displaying 'things', be they rooms, articles, or people. This introduces the first really, really important bit of coding. If the player inputs a command, you can subvert that command by saying that instead of doing it, to do something else.

In this case, rather than actually eating the article, you're cleverly rebuffed. In the following example, I'll introduce another room, and the 'if' mechanic. Between those two, you should make it pretty far! The first thing you may notice is that I added a second room, called WonderHowTo. I didn't even have to call it a room in the code, because the compiler knows that if an object is east or west, or north, or whatever of another object, it's probably a room.

Since there are two rooms now, I had to edit their descriptions to say where they were in relation to each other so that players can easily navigate between them.

The most important part, though, is the "Instead of going east" section. Much like the "instead of eating article" that I included before, this time I provided a circumstance in which the player could continue to the East. When the player types 'go east', the program will check to see whether they're carrying the article. If so, it will go ahead and move them to the room.

If not, it will rebuff them by saying that they need to take an article with them. This simple mechanic of 'if' will allow you to create incredibly complicated situations and relationships. Of course, as I said, things can get much, much more complicated, but this quick tutorial should have you hit the ground running. As I mentioned earlier, there's a thriving interactive fiction community out there.

If you just want to share the game with your friends, you can host it somewhere and then use Parchment to let people play it online. That's what I did. However, there's a full Interactive Fiction Database where you can have your game listed. Additionally, there are forums and even competitions that you can enter. There doesn't seem to really be a market for games like this, but it's a fun and rewarding thing to make!

Now you could have 1 method to handle combat with a monster instead of 1 x the number of monsters in your game! A lot of your methods throw an InterruptedException.

This is implementation detail leaking out of your methods. There's no reason the calling code should have to handle these exceptions just because you want to use Thread. I would recommend something like this.

Normally you should never do a try catch with an empty block, but in this scenario, it doesn't really matter if something messes up when sleeping, which will probably never happen.

Now the rest of your code doesn't have to deal with the exceptions. You could use it like this. Don't use public variables. Some good examples of good use cases for public are Integer.

These are good examples because these are values that will never ever change. If you want to provide access to variables in other classes, create private fields and then create public getter methods. Your variable names and method names should use camelCase. You adhere to this correctly in a lot of places, but variables like Name and StartGame violate this convention.

See the docs on naming conventions here. The methods combatskel and combatzombie are broken. A solution to this problem would be to put everything in a loop instead, like I already suggested when reviewing the method StartRoom. To illustrate what I mean, here a simplified version of your code:. Also, the methods combatskel and combatzombie contain a lot of common code. It would be better to put this common code in one place, and only put that which is specific for a skeleton or a zombie i.

For example, you could write a method combatMonster String name, int health, int damage , and call this method from within combatskel and combatzombie :. Finally, a general remark about naming conventions: A lot of your code does not follow Java naming conventions. Sign up to join this community.

The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Learn more. Java text-based adventure game Ask Question. Asked 3 years, 9 months ago. Active 1 month ago. Viewed 11k times. Improve this question. Liam Liam 73 1 1 gold badge 1 1 silver badge 5 5 bronze badges. Now you can relive those days, and write your very own text-adventure game, and do so in an object-orientated way! Each tutorial will describe one aspect of writing a text-adventure game in an object orientated language like Java.

I'll show you what classes you need to write, how they need to interact, and give you the source code to complete it. We'll construct a text-adventure gaming system , from which you can design your own style of game creating different rooms and objects. Back to the Java Coffee Break.



0コメント

  • 1000 / 1000