Sei sulla pagina 1di 5

Game

package veersion6;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GameSkeleton extends JFrame implements KeyListener


{

static final long serialVersionUID=0;


int ms=100;
boolean splash = true;
int KeystrokeCount=0;
Image im;
Hero ourHero = new Hero();
Board ourBoard = new Board();
Splash ourSplash = new Splash();
Food ourfood = new Food(ourBoard.getWidth(), ourBoard.getHeight());
int foodcount = 0;
int s;
int level=1;
GameSkeleton()
{
super("Game Sheleton");

JPanel p = new JPanel();

this.setSize(ourBoard.getWidth(), ourBoard.getHeight());
this.setVisible(true);
p.requestFocus();
addKeyListener(this);
newGame();
}

public void newGame()


{
ourHero.newGame();
}

public void makeProgramWait(int milliseconds)


{
try
{
Thread.sleep(milliseconds);
}
catch(Exception e)
{
System.out.println("An error has occurred during the sleep
process.");
}
}

public void playGame()


{
while(true)
{
if(level == 1)
{
ms=90;
}
if(level == 2)
{
ms=30;
}
if(level == 3)
{
ms=70;
}
if(level == 4)
{
ms=60;
}
if(level == 5)
{
ms=50;
}
if(s > 0)
{
level = 1;
}
if(s > 100)
{
level = 2;
}
if(s > 200)
{
level = 3;
}
if(s > 300)
{
level = 4;
}
if(s > 400)
{
level = 5;
}
{
ourHero.inAction();
if(ourHero.getXCoordinate() == ourfood.getxCoordinate() &&
ourHero.getYCoordinate() ==
ourfood.getyCoordinate())

{
ourfood= new Food(ourBoard.getWidth(),
ourBoard.getHeight());
ourHero.addSegment();
s =s+100;
}
makeProgramWait(ms);
repaint();
}
}
}
public void paint(Graphics g)
{
if(im == null)
{
im = createImage(this.getWidth(), this.getHeight());
}

Graphics tempG = im.getGraphics();


paintScreen(tempG);
g.drawImage(im, 0,0, this);
}

public void paintScreen(Graphics g)


{
ourBoard.draw(g);
ourHero.draw(g);
ourfood.draw(g);
if (splash == true)
{
ourSplash.draw(g);
}
g.setColor(Color.yellow);
g.drawString("level "+level+" SCORE: " +s, 50, 50);
}
public void keyPressed(KeyEvent e)
{
int inKey;
inKey = e.getKeyCode();

if(KeyEvent.VK_Q == inKey)
{
System.exit(0);
}

else if(KeyEvent.VK_S == inKey)


{
newGame();
}

ourHero.keyActions(e);

repaint();

public void keyReleased(KeyEvent e)


{
}

public void keyTyped(KeyEvent e)


{
}

public static void main(String args[])


{
GameSkeleton app = new GameSkeleton();

app.addWindowListener (new WindowAdapter()


{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
app.playGame();
}
}

Potrebbero piacerti anche