Jump to content


Photo
- - - - -

Writing Centipede in Java


  • Please log in to reply
10 replies to this topic

#1 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 08 February 2003 - 11:31 AM

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. :)

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

#2 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 08 February 2003 - 08:03 PM

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:

Posted Image

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.../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

#3 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 11 February 2003 - 08:08 PM

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. :)

The next version of the code is up at: http://www.cyberiapc.../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

#4 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 17 February 2003 - 03:35 PM

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...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

#5 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 05 March 2003 - 12:38 PM

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...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

#6 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 21 March 2003 - 02:00 PM

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.

Posted Image
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

#7 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 22 March 2003 - 05:28 PM

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.

Posted Image Posted Image

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

#8 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 01 April 2003 - 05:49 PM

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:

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...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. :0

Edited by Ali Almossawi, 01 April 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

#9 usr.c

usr.c

    Boss, my code's compiling (xkcd)

  • Admins
  • 10,440 posts
  • Gender:Male
  • Interests:Software
    Soccer
    Photography
    RC cars
    Electronics

  • Nothing Selected

Posted 05 May 2003 - 06:41 AM

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 Files




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

#10 snapplehooligan

snapplehooligan

    High Commander of the Eastern Forces

  • Support Team
  • 232 posts
  • Location:Terre Haute, IN

  • United States

Posted 09 December 2003 - 12:49 AM

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:

#11 FiShMaStEr

FiShMaStEr

    New Member

  • +Zenith User
  • 33 posts

  • Australia

Posted 16 June 2004 - 11:32 AM

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 :p




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users