Tuesday, July 16, 2013

Source Code of the Universe: My First Game Pt. 1

So, I've been reading a blog known as Twenty Sided. It started out as a D&D blog, hence the name, but has become a lot more since. On it, Shamus Young talked about writing a program that procedurally created a night-time cityscape. Reading about his adventures in programming made me start itching really bad to do some programming myself.

I've always loved programming. I'm not great at it, not even good at it, but I do love it. I started with QBasic on Windows 95/98. I tried to make an Interactive Fiction game with it, but never finished it. I didn't know enough to even have a working inventory. After that, I dabbled a little in C++, but my real know-how, if it can actually be called that, is in Java. That's the language I studied in College, and the one I know the most about. I like it because it seemed a lot easier than C++ (though it as been a while since I tried C++), yet was still capable of a lot, and was easily portable to other platforms. As in, no additional programming required because it runs in its own environment.

My idea for my current project isn't as grand as a 3D cityscape, but it's probably the biggest project I've attempted. I'm revisiting my IF roots, and I'm going to try and make a really short, and really crappy Interactive Fiction game, complete with multiple endings and a fully functional inventory system. We'll see what the final project ends up looking like.

The first thing I need to do before I ever even touch a code editor is figure out what I'm doing. I have a story in mind: You are the Hero of the Universe, though no one knows that because you haven't saved shit, yet. You are looking for the mythical Biforce, which has the power to destroy the Great Evil that has descended upon the galaxy. You have tracked it's location to a dinner on the edge of the Galaxy, and, after a thorough search, have come to the conclusion that there is only one place it could possibly be - the women's bathroom. As you are the Hero of the Universe, and not the Heroine Of the Universe, this poses a problem. Especially with the big, burly guard that won't let males into the bathroom. Apparently, they've had issues with that sort of thing.

So, that's my premise. The game takes place entirely within the dinner, and there are only three rooms: the main dining area, the men's room, and the ladies' room. Maybe one or two other areas as the story requires, but that's really it. I have at least one solution in mind, and an idea of how I'm going to set everything up, so I guess I can dive into the fun part for awhile.

The first thing to do, programming wise, is set up the additional Classes I'm going to need for this program. I'm going to use two, one for the rooms/areas, and one for the objects in those rooms/areas. Let's see what we can do.


public class Object { String name = ""; String shortDiscrp = ""; String longDiscrp = ""; boolean inInv = false; Object(String n, String s, String l) { name = n; shortDiscrp = s; longDiscrp = l; } } public class Room { String name = ""; String shortDiscrp = ""; String longDiscrp = ""; String[] objects; Room(String n, String s, String l, String[] o) { name = n; shortDiscrp = s; longDiscrp = l; objects = o; } }

This is a very simple start. I can't, yet, think of everything these classes need (or maybe I have), but I can go back and add more stuff in later if the program requires it. Yes, I realize that this is not the proper way to program. You're supposed to know what it is you code is going to do before you start typing. However, when working for fun, I like to take the approach of coding is like painting; you start off with the basic colors, and add more detail as you go. That, and I'm just too lazy to write out all of that before I get to the fun part.

Now, to set up the Main method and get our opening screen to display.


public static void main(String[] args) { JFrame biforce = new Biforce(); biforce.setVisible(true); } public Biforce() { setTitle("Biforce"); setSize(1000,700); setLocation(300, 100); setLayout(new GridBagLayout()); GridBagConstraints gBC = new GridBagConstraints(); setDefaultCloseOperation(EXIT_ON_CLOSE); getContentPane().setBackground(new Color(100, 100, 100)); gBC.fill = GridBagConstraints.HORIZONTAL; gBC.gridx = 0; gBC.gridy = 0; gBC.weightx = 0.2; gBC.weighty = 0.2; gBC.anchor = GridBagConstraints.FIRST_LINE_START; Label location = new Label("Title Screen"); location.setSize(1000, 10); location.setForeground(new Color(220, 220, 50)); add(location, gBC); gBC.gridx = 0; gBC.gridy = 1; gBC.gridwidth = 3; gBC.weightx = 1; gBC.weighty = 10; gBC.fill = GridBagConstraints.BOTH; JTextArea view = new JTextArea("Welcome to Biforce!!!"); view.setSize(1000, 680); add(view, gBC); view.setEditable(false); gBC.gridx = 0; gBC.gridy = 2; gBC.weightx = 0.00001; gBC.weighty = 0.2; gBC.gridwidth = 1; gBC.fill = GridBagConstraints.NONE; gBC.anchor = GridBagConstraints.LINE_START; Label c = new Label("Type Command, then press ENTER:"); c.setForeground(new Color(220, 220, 50)); c.setSize(300, 10); add(c, gBC); gBC.gridx = 1; gBC.gridy = 2; gBC.weightx = 10; gBC.weighty = 0.2; gBC.gridwidth = 2; gBC.fill = GridBagConstraints.HORIZONTAL; gBC.anchor = GridBagConstraints.LINE_START; JTextArea comm = new JTextArea(); comm.setSize(690, 10); add(comm, gBC); }

This sets everything up and gives us a screen that looks like this:






At the moment, this is exactly what we want. It took a little tweaking to get it this way. I've never used GridBagConstraints, before, but I needed the control it provided over where elements went, and what they looked like. I think it turned out rather nicely, if I do say so myself.

Next time, I'll be creating some Rooms, some Objects to go in those Rooms, and adding the ENTER key functionality that will allow us to travel between those Rooms and manipulate those Objects.

No comments:

Post a Comment