Main

December 16, 2006

Courage vs. Cowardice

My final project, the game Courage vs. Cowardice, derives its name from the random variable that influences spaceships to either move up or down as they fly from right to left. As I described in my proposal for this project I originally used this variable with the same name in my Mid-Term project: “Jousting Spaceships.” In that program, the spaceships simply reacted to each other, now they react when they get close to the Stealth Fighter, controlled by the player.

The effect of the “CC” variable makes the spaceships hard to predict and sometimes hard to hit with a missile. Basically in the game the player flies the “Stealth Fighter”, positioned on the left side of the screen over houses while trying to shoot down and dodge alien spaceships that appear from the right. For every spaceship destroyed with a missile the player fires, the player gets one point, which is tracked on the bottom part of the screen. However, the spaceships can destroy the fighter (and end the game) if they crash into the fighter. This will happen if the “CC” variable causes the spaceships to move up or down towards the fighter and the player is unable to get out of the way.

One of the final elements I added to the game was a timer. Once again, I brought back an element from the earlier program “Sunrise Surprise” by adding the sun moving in the background. In “Sunrise Surprise,” the sun, of course, rises. However in Courage vs. Cowardice, the sun sets, moving on to the screen after the game starts from the top. The Sun then heads towards the ground and as it gets closer, the background screen starts to turn from blue to black. Finally, after the sun sets, the background goes completely black. This has the interesting effect of making the Stealth Fighter, which is almost black in color, very difficult to see, a new challenge for the player to deal with. However, after several seconds, if the player is still alive, the level ends with the text: “Level Complete.” One last addition was to add code that actually slows the sun's progress if the player reaches a score of 7.

The game is both easy and hard in terms of playability. The controls are simple: one key, “I” moves the fighter up, one key “M” moves the fighter down. “F” fires the missile. What’s hard is that it is impossible to predict what the spaceships are going to do (since the “CC” variable is random) and if there are several on the screen, which sometimes occurs, dodging them all can be very tricky, particularly since the player can only go up and down. In addition, if the player fires a missile and destroys a spaceship at very close range, the explosion of the missile’s impact can still destroy the fighter. Crashing into the houses on the ground is also another way to die. As of right now, my best score in the game, which lasts about two minutes, is fifteen.

Looking at the game from a programmer’s point of view, I would have to say that I learned quite a lot. It is definitely true that programming a game is a great way to learn how to write code. I feel very confident with my skills in Processing now as I was able to use arrays and objects very effectively while writing the code for the game. In addition, I learned several clever ways to use variables such as “gameState” and “Distance.” Looking back at my progress through the semester, I can definitely see the thread of improvement as I went along creating one program after another.

The great thing is that there is still plenty of room for improvement with the Courage vs. Cowardice game. If I were to continue to work on it, and I might over the break, I can think of several things I would like to add. The first would be a button that would restart the game. The next would be harder levels, which is the reason why I added the text, “Level Complete” at the end. It would be great to have levels in which the spaceships moved faster and/or fired objects at the fighter. I would also like to add different types of spaceships with different abilities such as greater maneuverability and a cloaking device. I also think it would be interesting to add a spaceship that, when it appears on the screen, is beaming something up from the ground, but when the Stealth Fighter gets near, it moves straight up to escape. If the player can destroy this ship, the player would receive bonus points.

So there’s a lot that I can still do, not just with this game, but with others I can invent based on what I’ve learned from this game. The fact that I know how to control objects and the collisions between them opens a lot of doors for me. All of a sudden, those Atari games that I used to play twenty-five years ago now seem to be within my programming grasp and that idea is very exciting. Once upon a time, I thought the concept of creating games was immensely exciting. I’m now beginning to feel that way again and I’ve only just started.

Click Here to Play Courage vs. Cowardice
Game Controls:
I = Up
M = Down
F = Fire missile

Note: As soon as game loads, click on the game screen to active keys. For Macs, click once, for PCs, double-click.

November 15, 2006

ICM Final Project - Stealth vs. Spaceships

In thinking about my final project for ICM, my first idea was to take my “Jousting Spaceships” program from my Mid-Term project and use them in a new simulation. This time, I wanted to put them in a gladiator-type situation where the spaceships bounced around the edges of the screen, interacting with one another. I would use the same code to determine the distance between the two ships and they would again react to one another using the “Courage/Cowardice” variable. I hoped to then add code that would have the spaceships firing on each other.

I tested this plan out with a few simple simulations, but unfortunately the code didn’t work out as I had planned. The spaceships bounced fine and they interacted. However, they had problems doing both at the same time. Often one of the ships would fly off the screen after it had detected the other ship or both ships would slow down greatly as they passed one another – then one would fly off the screen. I did my best to debug the code but after a while I realized I wasn’t making a lot of progress. In addition, I began to think that my original Gladiator Spaceships idea had a few flaws in it, the principle one being that the screen, or combat area, just wasn’t big enough for an interesting interaction and if I made it bigger or added more ships, the code would get way too complicated.

Thus I decided to scrap the Gladiator Spaceships idea.

I now began to look for something else to do for my final project. My thoughts returned to one of my first successful programs, the “Sunrise Surprise” simulation that featured a rising sun over several houses followed by a flyover by several Stealth Fighters. I always liked this code but I constantly found myself thinking, “Why did those Stealth Fighters suddenly fly over?” It just seemed like a random event without any explanation. In jest, I mentioned to someone that maybe I should add a UFO that starts out over one of the houses and then flies off as the sky gets lighter. The Stealth Fighters would then appear to be chasing the UFO.

Interestingly enough, it was this idea that led me to the “Jousting Spaceships” program, as I liked the idea of making spaceships that moved. Now, though, a new idea came to mind. What if I took the idea of the Stealth Fighters chasing the UFO and turned it into a game? And hence, “Stealth vs. Spaceships” was born.

First, however, I wanted to play out the original idea. I went back to the original “Sunrise Surprise” code and altered it to include the spaceship. Now the program starts with the spaceship, the exact one from the “Jousting” program in fact, sitting over one of the houses with a mysterious/dangerous-looking beam coming down and hitting house’s roof. As the sun starts to rise, the beam shuts off and the spaceship quickly departs to the right of the screen. The moment it disappears, one Stealth Fighter flies across the screen from the left and then fires a missile off-screen towards the departed spaceship. This nice little simulation is not a game, of course, but I thought of it as a demo or promo for the game because almost all of the elements that I plan to use in the game are present.

In the game, the player will control the Stealth Fighter, which will be position on the left side of the screen. The fighter will only move vertically, not horizontally, however, motion will be depicted by houses which will move from right to left on the ground across the screen. Clouds moving across the sky could also demonstrate this effect. With the Stealth Fighter in apparent motion, the spaceships will appear from the right side of the screen. The player will then be able to fire missiles at them. At the same time, the spaceships may fire rays at the Stealth Fighter, which the player will try to avoid.

This is the general idea for the game, however, if I have enough time, I should be able to make the game more complex. For instance, the spaceships could move at different speeds and they may also try to dodge the missiles. In addition, there could be different kinds of spaceships, some bigger, some smaller, and some with different shapes. There is a lot of room for flexibility.

As a final touch, the sun could be setting in the background and once it sets, that could signal the end of the game. Hopefully, I will be able to write a scoring algorithm based on the number of spaceships the player destroys before the game ends.

Interestingly enough, I realize now that this game idea almost mimics the old Atari 2600 game, Defender. That game was more advanced but the graphics where about the same or even a little less sophisticated than what I can do in Processing.

October 25, 2006

Midterm Project - Spaceship Battle

The idea for my project came to me one day when Dan mentioned that it was possible to construct a variable that could keep track of the distance between two objects. Thus, it was possible to write a code that could make something happen if two objects came into contact with one another. Almost immediately I thought of flying objects that could be struck by missiles. Soon, though, I came up with a better idea that included using spaceships and that’s when my Mid-Term Project was born.

In my mind, I imaged ships maneuvering through space and firing on another with projectiles. At first, this seemed like a daunting programming task but then I realized it would be manageable if I just broke down the code into different modules. The first module I actually worked on was the background. This was not my intention but when we learned in class how to create arrays, I saw the perfect opportunity to create a background of stars. This actually turned out to be simpler than I had first thought as I started with an example of an array from Dan’s book and just modified it. After I created a background of randomly placed stars (ellipses of 1 by 1), I then created planet objects, with random colors.

With my background complete, it was time to move on to the heart of my idea, the spaceships. I created two spaceships by using three ellipses of varying sizes and I colored them differently, one green, the other red. I then made them move across the screen in opposite directions towards one another from random start points. Using the code from my “Planes” sketch, I had the spaceships start over every time they reached the end of the screen.

Now, though, I had to decide how I wanted the starships to interact. In the grand scheme of my idea, I wanted to have many spaceships moving back and forth across the screen firing on one another. However, I decided to keep it simple at first. Thus, my idea of “Jousting Spaceships” came about. In the end, I planned to have spaceships moving randomly around the screen like gladiators trapped in a ring, but for starters I would just have two flying at one another from the left and right.

This is when the distance variable finally came into play. Actually, I later discovered that there is an actually “Distance” function built into Processing that can track the distance between two points. I could have used that, but instead I created my own variables, d1 and d2. D1 tracks the distance between the x variables of the two spaceships, x1 and x2, and d2 tracks the distance between the y variables, y1 and y2. The formula for d1 is “x2 – x1” and the formula for d2 is “(abs)y1 – y2.” The (abs), or absolute value, is necessary since the answer to that equation might be a negative number.

With the distance variables created, it was now time for logic statements. In this case, I wanted something to happen if d1 and d2 were less than 170. This had the effect of creating a sphere around each of the spaceships with a diameter of 170 pixels. If one spaceship entered the “sensor” sphere of the other, something would happen. Thinking ahead, my plan was to have the spaceships fire on one another but since I wasn’t up to that yet, I decided the first step would be to just have them change course.

Under normal circumstances, each spaceship’s x variable is constantly changing as they move. Their y variables, however, remain constant, which gives them the appearance of moving horizontally across the screen. The spaceships start at random y variables at the start of each loop. Sometimes they pass close to one another, sometimes they don’t. Now, though, I had a logic statement that would tell the spaceships to do something if they passed within 170 pixels of one another. The question was: what should they do?

Having the spaceships immediately change course towards one another when they entered sensor range seemed a little too obvious. It also wouldn’t make for a very interesting simulation once the ships firing started. Thus, I came up with another idea. What if the spaceships didn’t always want to move towards each other when one ship detected the other? What if they got scared and tried to move away? And so the Courage/Cowardice variable (cc) was born.

The cc variables (one for each ship) were actually very simple. For every loop, cc would equal a random number between 1.5 and –1.5. This number could be added to the y variables (when they passed within 170 pixels) of each ship causing them to begin to move diagonally up (if cc was negative) or diagonally down (if cc was positive). The farther away from zero, the steeper the turn. However, once the ships moved out of range, the y variable would automatically return to zero and the ships would again move horizontally.

This code worked surprisingly well. However, there were still a few more issues to take care off. There were times when both spaceships did move right at each other and collided. Of course, with no code to handle this, nothing happened and ships just crossed through each other and continued on their merry way. Since I wasn’t yet ready to figure out how to write code for collisions, I decided the best I could do for now was to try to limit the number of times the spaceships collided. For this, I added another logic statement, which was basically a duplicate of the first but with a smaller distance quotient. If the spaceships got within 50 pixels of one another, their cc variables would be multiplied by –2. This would have the effect of changing their course by around ninety degrees and making their turns steeper. I arrived at this formula after some trial and error. Now the ships were able to avoid each other most of the time, although they still “collided” every now and then.

My last step for this phase of my project was to add one more logic statement that prevented the spaceships from flying off the top and bottom the screen. For this I simply added the statement that of the spaceship’s y variables reached either zero or the height amount (500), y would automatically go to zero. This worked fine except when both ships reached the top or bottom before crossing and would actually “collide” again. To deal with that, I added one last logic statement for only one of the spaceships, which told it to get out of the way if the other spaceship hadn’t passed it yet. This logical statement was actually my longest, featuring one “or (||)” and four “ands (&&)”.

Overall, I was pretty happy with my end result. I found myself spending several minutes just watching my spaceships interact. I really enjoyed how almost every time the interaction was different. However, I realize that if I want to continue on with this project, there is still a lot of work to be done. My next step will be to add code for collisions. At first, I will just do this for the spaceships themselves, but once I have that code down, I will move on to examining how to create projectiles along with fire control for each ship.