In this step i will so you how to design the basic classes of a game and how to create figures with simple shapes using the java class Graphics
First of all you will need to specify our basic classes. For sure we will need a class for the player. Our player will have a number of lives, score, position on the screen, movement speed, direction and a state (alive, about to die, dead). In every different state there different things that must be drawn on the screen and different reactions to user input. If you dismiss the lives all there attributes belongs to the asteroids too. So we can encapsulate all these attributes into an abstract class, let's call it GameAvatar. The GameAvatar instances have specific functions
public abstract void getHit();The getHit methods is used when a GameAvater gets a hit (a bullet, or a crash)
The update method is used when we need to update the state, position etc of a GameAvatar.
public abstract void update();
public abstract void paint( Graphics g );The paint method whenwe have to draw the GameAvatar on the screen
So the complete source code of GameAvatar is
package games.logic.Asteroid;The Player must implement the abstract methods of GameAvatar and add new functionality. The extra method that we need is
import java.awt.Graphics;
/**
*
* @author alexm
*/
public abstract class GameAvatar {
public static int ALIVE_STATE = 0;
public static int DEAD_STATE = 1;
public static int DYING_STATE = 2;
//screen coordinates
protected int xPos;
protected int yPos;
//the spachip direction
protected int direction;
//the spaship velocity
protected int velocity;
//the player state
protected int state;
public int getxPos() {
return xPos;
}
public void setxPos( int xPos ) {
this.xPos = xPos;
}
public int getyPos() {
return yPos;
}
public void setyPos( int yPos ) {
this.yPos = yPos;
}
public int getDirection() {
return direction;
}
public void setDirection( int direction ) {
this.direction = direction;
}
public int getVelocity() {
return velocity;
}
public void setVelocity( int velocity ) {
this.velocity = velocity;
}
public int getState() {
return state;
}
public void setState( int state ){
this.state = state;
}
public abstract void getHit();
public abstract void update();
public abstract void paint( Graphics g );
}
public void fire() ;This method is used to fire a bullet from the spaceship
So we have
package games.logic.Asteroid;And last but not least the initial source code for the Asteroid class
import java.awt.Graphics;
/**
*
* @author alexm
*/
public class Player extends GameAvatar{
//number of lifes
protected int lifes;
public Player(){
lifes = 3;
xPos = yPos = direction = velocity = 0;
}
public int getLifes() {
return lifes;
}
public void setLifes( int lifes ) {
this.lifes = lifes;
}
public void fire() {
throw new UnsupportedOperationException( "Not supported yet." );
}
@Override
public void update() {
throw new UnsupportedOperationException( "Not supported yet." );
}
@Override
public void paint( Graphics g ) {
throw new UnsupportedOperationException( "Not supported yet." );
}
@Override
public void getHit() {
throw new UnsupportedOperationException( "Not supported yet." );
}
}
package games.logic.Asteroid;
import java.awt.Graphics;
/**
*
* @author alexm
*/
public class Asteroid extends GameAvatar{
//the size of the asteroid
int size;
public Asteroid(){
size = 3;
}
public Asteroid( int s ){
size = s;
}
public void fire() {
throw new UnsupportedOperationException( "Not supported yet." );
}
@Override
public void update() {
throw new UnsupportedOperationException( "Not supported yet." );
}
@Override
public void paint( Graphics g ) {
throw new UnsupportedOperationException( "Not supported yet." );
}
@Override
public void getHit() {
throw new UnsupportedOperationException( "Not supported yet." );
}
}
The Asteroid class must have an attribute size to decide what will be happend whan an Asteroid will be hit.
So that is for today. We will see how we can use these classes to make a game and how to render some graphics in the following steps
When the tutorial will be finished i will upload all the source code and the game frodownload so keep in touch
No comments:
Post a Comment