2d game in java-Monster-Java 2D Game Graphics and Animation
Mon Feb 11, 2008 7:44 pm
* Project Name:
Java 2d Game - Monster 2d * Programmer:msi_333 * Type:Game 2D/Desktop * Technology:Java * IDE:NetBeans * Description:Base on 2d platform engine to create a 2d game. This is the Monster2d game that i made . It is a net beans Project so if u don't have it you will find all the codes in folder "src". There is also an executable called monster.jar in the folder "dist" which u can run the game without compiling. To run the game you must have JVM(Java Virtual Machine).
1. download Monster_1.rar 2. Unrar it. 3. run the game have fun, you can make your changes in the code to fix bugs and add new features. You Can learn many things form this code including :
2D Graphics and Animation with Java Full-Screen Graphics with Java Creating a 2D Platform Game Images with Java Keyboard Input Sound Basics using Java Sound API
} public void newGame() { mapl=new myMap_Loader("map.txt");//Load the Images myPlayer=mapl.getPlayer();// get The Player myEnemys=mapl.getEnemy();// get The Enemy myMeat=mapl.geMeat();// get The Meat Height=mapl.getHeight();// get Height of Map Width=mapl.getWidth();//get Width of Map myPlayer.SetEnemy(myEnemys);//Point the Enemies to Player if(inputManager!=null)//Not Yet Created inputManager.setPlayer(myPlayer);//The Input Manager point to the new Player
} public void start_Animation() { BufferStrategy myDrawBuffer;
myDrawBuffer=GameScreen.getBufferStrategy();
long StartTime;//Time start in Loop long TimeAfter;//Diff in Time
Graphics g=myDrawBuffer.getDrawGraphics();// Get the graphices object of Buffer Startagy
if(myinput.status==0)// In Start Menu Status { myMenu.draw(g);// Draw start menu in the Double Buffers g.dispose();// Free Memory
if(!myDrawBuffer.contentsLost()) myDrawBuffer.show();// Show the Buffers on the Screen } else if(myinput.status==1)// In Game Status { draw(g);//Draw all objects , Map, Player, Enemies,Meat in Double Buffers
g.dispose();//Free Memory
if(!myDrawBuffer.contentsLost()) myDrawBuffer.show();// Show the Buffers on the Screen
if(myPlayer.IsAlive())// check the player live myPlayer.Up_Date_Animation(TimeAfter);// Update the Animation of Player for(int i=0;i<myEnemys.size();i++) { myEnemy=((Enemy)(myEnemys.get(i))); if(myEnemy.IsAlive())// check the Enemy live myEnemy.Up_Date_Animation(TimeAfter);// Update the Animation of Enemy } } try { Thread.sleep(10);// Sleep for 10 MilliSeconds } catch (InterruptedException ex) { }
TimeAfter=System.currentTimeMillis()-StartTime;// Get The Difference in the Time
} }
}
public void draw(Graphics g) {
Graphics2D g2d=(Graphics2D)g;
myPlayer.jumpUpdate();//Update Player Jump mode
for(int i=0;i<myEnemys.size();i++) { myEnemy=((Enemy)(myEnemys.get(i))); if(myEnemy.IsAlive()) { myEnemy.Move_turn();// Update Postions of enemies myEnemy.check_for_death(myPlayer);// Check if it killed by player } }
int offsetX=(screen.width/2)-(myPlayer.getX());// Start X to draw Maps
offsetX=Math.min(offsetX,0); offsetX=Math.max(offsetX,screen.width-(mapl.getWidth())*100); int offsetY=screen.height-(mapl.getHeight()*22);//Start Y to draw Maps
int firstCubX=(-offsetX)/60;//First Cub to draw from X int lastCubX=firstCubX+(screen.height/60)+1;//Last Cub to draw
int startY=0; int additionY=0; if(myPlayer.getY()>=screen.height)// This is Changes in Y-Postions { int X=myPlayer.getY()/screen.height;
Image myCub; for(int x=firstCubX;x<=lastCubX+4&&x<Width;x++) for(int y=(startY);y<14+startY+additionY&&y<Height;y++) { myCub=mapl.getImage(x,y);// get Each Cube Image on the Map
// Draw the Meat for(int i=0;i<myMeat.size();i++) {
myfood=((Meat)myMeat.get(i)); if(myfood.IsAlive()) myfood.EatMe(myPlayer); if(myfood.IsAlive())// is not eaten g2d.drawImage(myfood.getImage(),myfood.getX()+offsetX,myfood.getY()+offsetY,null);
} g2d.drawImage(Exit_img,1026/2,680,null); if(myPlayer.IsAlive()) if(myPlayer.face_to_right())//drawing the player {