CyberiaPC.com Community Forums Main Page
RSS Feeds: Late(st) News | Zenith Picture Gallery | Most Recent Community Topics | Tips, Tricks and Other Useful Stuff

Welcome Guest ( Log In | Register )

Reply to this topicStart new topic
> Writing Centipede in Java
usr.c
post Feb 8 2003, 11:31 AM
Post #1


Boss, my code's compiling (xkcd)
Group Icon

Group: Admins
Posts: 10,437
Joined: 19-September 01
Member No.: 1

Nothing Selected




Well, I've got an assignment due in the beginning of April when I have to hand in a clone of the original Centipede game (Atari, 1980) written in Java. So I thought it would be a good idea to have some form of a log where I would post how the project is coming along and how certain obstacles are dealt with. This should be helpful and interesting, since I'm new to Java too.

Hopefully, the final code will be released when the project is complete. original.gif

How the game works:

The Centipede universe is made up of three main types of objects: Bugs, mushrooms and the player. Here is a breakdown of the game objects:

*Mushrooms
|_Poisonous: Cause the centipede to drop to the bottom of the screen.
|_Normal: Cause the centipede to change direction.

*Bugs (kill the player upon impact)
|_Centipede: Moves across the screen.
|_Fleas: Drop randomly from the top and create mushrooms.
|_Spiders: Lurk in the bottom 1/3 of the screen to keep the player moving.
|_Scorpions: Turn mushrooms into poisonous ones.

The player moves in the bottom 1/3 of the screen and tries to kill the centipede. When a part of the centipede is shot, it turns into a mushroom and the centipede itself breaks into two seperate ones.


--------------------


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com
Go to the top of the page
 
+Quote Post
usr.c
post Feb 8 2003, 08:03 PM
Post #2


Boss, my code's compiling (xkcd)
Group Icon

Group: Admins
Posts: 10,437
Joined: 19-September 01
Member No.: 1

Nothing Selected




The reason for creating this log is because I've noticed that many first-year programming students all around the world are given the task of making such a game for their course. Hopefully, my humble experiences and tips might prove useful.

Here's how the final game should look like:



I really did not know where to start when the project was first assigned, but after sketching some quick diagrams showing the different classes needed for the project I decided to start with the simplest form possible: Having a circle move across the screen. This was achieved by making use of the Timer class, which basically creates a delay of a given number of milliseconds between each execution. After the delay a certain action is performed in its actionPerformed() method. The main reason for having this delay is to move the circle by 1 pixel every 10 milliseconds. When it reaches an edge it either drops down a row or climbs up one.

With a little bit of tweaking it worked great. Here's a sample:

http://www.cyberiapc.com/java/centipede/1/.../Centipede.html


--------------------


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com
Go to the top of the page
 
+Quote Post
usr.c
post Feb 11 2003, 08:08 PM
Post #3


Boss, my code's compiling (xkcd)
Group Icon

Group: Admins
Posts: 10,437
Joined: 19-September 01
Member No.: 1

Nothing Selected




The next step would be to organize the layout so that the animation loads in a specific container whose attributes, such as size and background color, could be specified. For the moment, I've gone with the following solution: To draw a rectangle using the class's paint() method and set its color and dimensions. This will give the illusion that the animation is occuring within the rectangle's borders.

In this next version of the code, I've actually added the 'start' button to a panel and set its background color to black.

I'll probably be changing this in the coming week since I'd like to refresh the screen using the main class' paint() method instead of redrawing each object, such as the player, the centipede and all the other bugs and elements, independantly.

If what I've just explained sounds confusing, that's because I'm also confused at the moment with the properties of applets in Java. Hopefully, all will become clear in the coming few days. original.gif

The next version of the code is up at: http://www.cyberiapc.com/java/centipede/3/.../Centipede.html

P.S. Thanks to everyone who has contacted me regarding this log. I just hope that my posts will help you with your own projects.


--------------------


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com
Go to the top of the page
 
+Quote Post
usr.c
post Feb 17 2003, 03:35 PM
Post #4


Boss, my code's compiling (xkcd)
Group Icon

Group: Admins
Posts: 10,437
Joined: 19-September 01
Member No.: 1

Nothing Selected




I've decided to work on the Player class. This next version of the code implements a class with the following methods and attributes:

Player
|_x: x position of player
|_y: y position of player
|_fire(): shoots a bullet vertically across the screen
|_move(): moves the player across the screen

I've also started work on the Mushrooms class:

Mushroom
|_generate(): generates mushrooms at random positions
|_display(): displays the mushrooms on the screen

Basically, the 'Player' listens to keystrokes and moves the play accordingly. For example, when hte left key is pressed, the x coordinate of the player is changed by -jump, where 'jump' is a variable that determines how far the player moves every time.

Problems that still need to be addressed include:
- Interaction between the bullets and the mushrooms
- Allowing multiple shots to be fired


A demo can be found at: http://www.cyberiapc.com/java/centipede/4/...4/Centipede.htm


Controls
-----------
Movement: Arrow Keys
Fire: Space Bar
Generate random mushrooms: m



--------------------


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com
Go to the top of the page
 
+Quote Post
usr.c
post Mar 5 2003, 12:38 PM
Post #5


Boss, my code's compiling (xkcd)
Group Icon

Group: Admins
Posts: 10,437
Joined: 19-September 01
Member No.: 1

Nothing Selected




Here's is the next version of the game. Not much has changed, but I thought I'd upload it anyway. I'm currently busy with a few other assignments and hopefully updates to this thread will be more frequent in a week or two.

http://www.cyberiapc.com/java/centipede/5/...5/Centipede.htm


--------------------


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com
Go to the top of the page
 
+Quote Post
usr.c
post Mar 21 2003, 02:00 PM
Post #6


Boss, my code's compiling (xkcd)
Group Icon

Group: Admins
Posts: 10,437
Joined: 19-September 01
Member No.: 1

Nothing Selected




I managed to get back to work on the Centipede game yesterday. Here is a list of what has been implemented since the last revision:

1. Shots fired by the player now interact with the mushrooms. Two collisions are necessary to completely destroy a mushroom.
2. A maximum of 5 shots can be fired at the same time by the player.
3. Once the number of mushrooms on the screen goes below seven, a flea drops from a random x position from the top of the screen and generates mushrooms, which are then added to the collection.

I initially had a number of parallel arrays to store the coordinates, as well as other attributes, of the mushrooms. The mushrooms are now stored as a Vector collection of objects, each with its own unique attributes.


note that mushrooms that have been hit appear differently.

I'll try and get a working demo up as soon as possible.


--------------------


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com
Go to the top of the page
 
+Quote Post
usr.c
post Mar 22 2003, 05:28 PM
Post #7


Boss, my code's compiling (xkcd)
Group Icon

Group: Admins
Posts: 10,437
Joined: 19-September 01
Member No.: 1

Nothing Selected




The Spider class has now been created. Remember that the spider crawls in the bottom 1/3 of the screen, keeping the player constantly on the move.

This class is fairly easy to implement. The spider itself has a few attributes and one main function:

Attributes
|_ x position (int)
|_ y position (int)
|_ x position old (int)
|_ y position old (int)
|_ isHit (boolean)

Functions
|_ move()

Upon instantiating and then starting the class, which is a Thread, no other methods need to be called. What I have done is embedded a loop within the run() method of the spider class that breaks only when the spider has been hit. The loop itself needs to check a few things to determine how the spider is displayed, namely:

-what to do if the spider goes beyond the x boundaries
-what to do if the spider goes beyond the y boundaries

Note that all of the collision checking is done in the main class by a dedicated sub-class that checks collisions between all objects every time a shot moves a pixel upwards, or in the case of the spider, every time the spider moves a step.



Once again, I am unable to get a working demo of the code up on the site at the moment. I'll have it up as soon as possible.


--------------------


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com
Go to the top of the page
 
+Quote Post
usr.c
post Apr 1 2003, 05:49 PM
Post #8


Boss, my code's compiling (xkcd)
Group Icon

Group: Admins
Posts: 10,437
Joined: 19-September 01
Member No.: 1

Nothing Selected




This is the alpha version of the completed game. There still remain some bugs that will need to be ironed out. Note that double buffering is not currently implemented for all objects on screen (at present, only the player and the mushrooms collection implement it). As a result, there is a noticable flickering of the spider, scorpion and centipede objects

I think the only thing worth going into detail about is the structure of the centipede class:

Attributes
|_ x position (int)
|_ y position (int)
|_ diameter (int)
|_ isHit (boolean) //goes high when a segment is hit
|_ allBlobsDead (boolean) //goes high when all segments are dead (at which point, the player progresses to the next level)

|_ centipedeBug[] (Centipede) //array that stores centipede's "blobs"
|_ n (int) //length of centipede

Functions
|_ Centipede[] generate(int n) //creates a centipede array of length 'n'
|_ void checkCollision(Centipede currentBlob) //constantly checks for any collisions between the centipede and the mushrooms on the screen
|_ void run() //loops as long as the centipede thread is alive performing all the movement and collision detection operations

It's important to note that with my design, and there might be better ways of doing this, each segment in the centipede is independant and only linked to the other segments via the array collection. This means that each segment's isFlag can go to 'true' when it is hit, and then it can either be removed or transformed into a mushroom or whatever.

Here is an outline of how checkCollision() works:

CODE
Loop i=0 to length of centipede[]
  currentBlob=centipede[i]
  Loop i=0 to length of mushrooms[]
     Get Mushroom object at mushrooms[i]
     if currentBlob.xPosition or currentblob.yPos overlap mushroom[i].xPosition or mushroom[i].yPosition
       
        if mushroom is poisoned
           drop to bottom of screen
        end if
   
        if mushroom is not poisoned
           switch direction
        end if

     end if
  Loop
Loop


A working demo can be found at: http://www.cyberiapc.com/java/centipede/8/...8/Centipede.htm

Note: For the same reason I couldn't get the previous versions up, this one seems to be loading too slowly in the context of a browser, making the game virtually unplayable. ohmy.gif

This post has been edited by Ali Almossawi: Apr 1 2003, 05:58 PM


--------------------


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com
Go to the top of the page
 
+Quote Post
usr.c
post May 5 2003, 06:41 AM
Post #9


Boss, my code's compiling (xkcd)
Group Icon

Group: Admins
Posts: 10,437
Joined: 19-September 01
Member No.: 1

Nothing Selected




As promised, here is the source code for the Centipede game.

License:

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Attached File(s)
Attached File  source.zip ( 41.32K ) Number of downloads: 326
 


--------------------


Things that I don't suck at: Photography (flickr, JPG Mag), Skydiving, Splitting atoms, Flying a space shuttle
"Don't bail; the best gold is at the bottom of barrels of crap!" -Randy Pausch
I have people-skills goddamnit! What is wrong with you people!!! | www.skyrill.com
Go to the top of the page
 
+Quote Post
snapplehooligan
post Dec 9 2003, 12:49 AM
Post #10


High Commander of the Eastern Forces
Group Icon

Group: Support Team
Posts: 232
Joined: 2-November 01
From: Terre Haute, IN
Member No.: 4

United States




I must say Ali, I really enjoyed reading this! It's always interesting to see how other programs work around obstacles. Even though I don't program in Java, I very much enjoyed reading about the process you followed working on this project.

Well done! thumbsup.gif
Go to the top of the page
 
+Quote Post
FiShMaStEr
post Jun 16 2004, 11:32 AM
Post #11


New Member
Group Icon

Group: +Zenith User
Posts: 33
Joined: 25-April 04
Member No.: 1,073

Australia




very nice, good work on the log system. im almost doin the same for my site, just im runnin it through a downloads section the source code, with updates as constant as i do something. its hard though coding AI for battleships from scratch in java, and doing the entire program truthfully tounge.gif


--------------------
user posted image
image on right courtesy of Gerardo Sandoval
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 20th November 2009 - 11:33 PM