Skip to content

Commit

Permalink
edit - random langin area
Browse files Browse the repository at this point in the history
In LadingArea.java & delete 주석
  • Loading branch information
SEOYEONGO committed Oct 13, 2021
1 parent 576e3b9 commit c2ebe83
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 299 deletions.
37 changes: 3 additions & 34 deletions src/moon_lander/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,31 @@
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
* Create a JPanel on which we will draw and listen for keyboard and mouse events.
*
* @author www.gametutorial.net
*/

public abstract class Canvas extends JPanel implements KeyListener, MouseListener {

// Keyboard states - Here are stored states for keyboard keys - is it down or not.
private static boolean[] keyboardState = new boolean[525];

// Mouse states - Here are stored states for mouse keys - is it down or not.
private static boolean[] keyboardState = new boolean[525];
private static boolean[] mouseState = new boolean[3];


public Canvas()
{
// We use double buffer to draw on the screen.
this.setDoubleBuffered(true);
this.setFocusable(true);
this.setBackground(Color.black);

// If you will draw your own mouse cursor or if you just want that mouse cursor disapear,
// insert "true" into if condition and mouse cursor will be removed.
if(false)
{
BufferedImage blankCursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(blankCursorImg, new Point(0, 0), null);
this.setCursor(blankCursor);
}

// Adds the keyboard listener to JPanel to receive key events from this component.
this.addKeyListener(this);
// Adds the mouse listener to JPanel to receive mouse events from this component.
this.addMouseListener(this);
}


// This method is overridden in Framework.java and is used for drawing to the screen.
public abstract void Draw(Graphics2D g2d);

@Override
Expand All @@ -63,19 +49,11 @@ public void paintComponent(Graphics g)
}


// Keyboard
/**
* Is keyboard key "key" down?
*
* @param key Number of key for which you want to check the state.
* @return true if the key is down, false if the key is not down.
*/
public static boolean keyboardKeyState(int key)
{
return keyboardState[key];
}

// Methods of the keyboard listener.
@Override
public void keyPressed(KeyEvent e)
{
Expand All @@ -94,22 +72,13 @@ public void keyTyped(KeyEvent e) { }

public abstract void keyReleasedFramework(KeyEvent e);


// Mouse
/**
* Is mouse button "button" down?
* Parameter "button" can be "MouseEvent.BUTTON1" - Indicates mouse button #1
* or "MouseEvent.BUTTON2" - Indicates mouse button #2 ...
*
* @param button Number of mouse button for which you want to check the state.
* @return true if the button is down, false if the button is not down.
*/

public static boolean mouseButtonState(int button)
{
return mouseState[button - 1];
}

// Sets mouse key status.

private void mouseKeyStatus(MouseEvent e, boolean status)
{
if(e.getButton() == MouseEvent.BUTTON1)
Expand Down
153 changes: 43 additions & 110 deletions src/moon_lander/Framework.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,80 +14,37 @@
import javax.swing.JButton;
import javax.swing.JFrame;

/**
* Framework that controls the game (Game.java) that created it, update it and draw it on the screen.
*
* @author www.gametutorial.net
*/

public class Framework extends Canvas {

/**
* Width of the frame.
*/
public static int frameWidth;
/**
* Height of the frame.
*/
public static int frameHeight;

/**
* Time of one second in nanoseconds.
* 1 second = 1 000 000 000 nanoseconds
*/
public static final long secInNanosec = 1000000000L;

/**
* Time of one millisecond in nanoseconds.
* 1 millisecond = 1 000 000 nanoseconds
*/
public static final long milisecInNanosec = 1000000L;

/**
* FPS - Frames per second
* How many times per second the game should update?
*/
private final int GAME_FPS = 16;
/**
* Pause between updates. It is in nanoseconds.
*/
private final long GAME_UPDATE_PERIOD = secInNanosec / GAME_FPS;

/**
* Possible states of the game
*/
public static enum GameState{STARTING, VISUALIZING, GAME_CONTENT_LOADING, MAIN_MENU, OPTIONS, PLAYING, GAMEOVER, DESTROYED}
/**
* Current state of the game
*/

public static GameState gameState;

/**
* Elapsed game time in nanoseconds.
*/
private long gameTime;
// It is used for calculating elapsed time.
private long lastTime;

// The actual game
private Game game;


/**
* Image for menu.
*/
private BufferedImage moonLanderMenuImg;

// used for mode selection.
// JButton name : Level + playerCnt.
private JButton Easy1, Normal1, Hard1, Easy2, Normal2, Hard2;

// used for selecting number of rocket.
// playerCnt 0 : initializing, playerCnt 1 : 1 person, playerCnt 2 : 2 person.
// playerCnt 0: initializing, playerCnt 1: 1 person, playerCnt 2: 2 person.
public static int playerCnt = 0;

// used for setting level of game.
// level 1 : Easy, level 2 : Normal, level 3 : Hard.
// level 1: Easy, level 2: Normal, level 3: Hard.
public static int level = 1;

public Framework ()
Expand All @@ -96,7 +53,6 @@ public Framework ()

gameState = GameState.VISUALIZING;

//We start game in new thread.
Thread gameThread = new Thread() {
@Override
public void run(){
Expand All @@ -105,21 +61,13 @@ public void run(){
};
gameThread.start();
}


/**
* Set variables and objects.
* This method is intended to set the variables and objects for this class, variables and objects for the actual game can be set in Game.java.
*/


private void Initialize()
{

}

/**
* Load files - images, sounds, ...
* This method is intended to load files for this class, files for the actual game can be loaded in Game.java.
*/
private void LoadContent()
{
try
Expand All @@ -132,16 +80,12 @@ private void LoadContent()
}
}

/**
* In specific intervals of time (GAME_UPDATE_PERIOD) the game/logic is updated and then the game is drawn on the screen.
*/

private void GameLoop()
{
// This two variables are used in VISUALIZING state of the game. We used them to wait some time so that we get correct frame/window resolution.
long visualizingTime = 0, lastVisualizingTime = System.nanoTime();

// This variables are used for calculating the time that defines for how long we should put threat to sleep to meet the GAME_FPS.
long beginTime, timeTaken, timeLeft;
long beginTime, timeTaken, timeLeft;

while(true)
{
Expand All @@ -150,11 +94,13 @@ private void GameLoop()
switch (gameState)
{
case PLAYING:
gameTime += System.nanoTime() - lastTime;
gameTime += System.nanoTime() - lastTime;
game.UpdateGame(gameTime, mousePosition());
lastTime = System.nanoTime();

game.UpdateGame(gameTime, mousePosition());
// // 고민..
game.InitializeE(level);

lastTime = System.nanoTime();
break;
case GAMEOVER:
//...
Expand All @@ -169,25 +115,17 @@ private void GameLoop()
//...
break;
case STARTING:
// Sets variables and objects.
Initialize();
// Load files - images, sounds, ...
LoadContent();

// When all things that are called above finished, we change game status to main menu.
gameState = GameState.MAIN_MENU;
break;
case VISUALIZING:
// On Ubuntu OS (when I tested on my old computer) this.getWidth() method doesn't return the correct value immediately (eg. for frame that should be 800px width, returns 0 than 790 and at last 798px).
// So we wait one second for the window/frame to be set to its correct size. Just in case we
// also insert 'this.getWidth() > 1' condition in case when the window/frame size wasn't set in time,
// so that we although get approximately size.
if(this.getWidth() > 1 && visualizingTime > secInNanosec)
if(this.getWidth() > 1 && visualizingTime > secInNanosec)
{
frameWidth = this.getWidth();
frameHeight = this.getHeight();

// When we get size of frame we change status.
gameState = GameState.STARTING;
}
else
Expand All @@ -201,10 +139,8 @@ private void GameLoop()
// Repaint the screen.
repaint();

// Here we calculate the time that defines for how long we should put threat to sleep to meet the GAME_FPS.
timeTaken = System.nanoTime() - beginTime;
timeTaken = System.nanoTime() - beginTime;
timeLeft = (GAME_UPDATE_PERIOD - timeTaken) / milisecInNanosec; // In milliseconds
// If the time is less than 10 milliseconds, then we will put thread to sleep for 10 millisecond so that some other thread can do some work.
if (timeLeft < 10)
timeLeft = 10; //set a minimum
try {
Expand All @@ -214,32 +150,33 @@ private void GameLoop()
}
}

/**
* Draw the game to the screen. It is called through repaint() method in GameLoop() method.
*/

public void DrawButtonForLevel() {
// locate level/mode button.
Easy1 = new JButton("1인 easy");
Easy1.setBounds(frameWidth / 2 - 110, frameHeight / 2 + 30, 95, 20); Easy1.addMouseListener(this);

Normal1 = new JButton("1인 normal");
Normal1.setBounds(frameWidth / 2 - 110, frameHeight / 2 + 55, 95, 20); Normal1.addMouseListener(this);

Hard1 = new JButton("1인 hard");
Hard1.setBounds(frameWidth / 2 - 110, frameHeight / 2 + 80, 95, 20); Hard1.addMouseListener(this);

Easy2 = new JButton("2인 easy");
Easy2.setBounds(frameWidth / 2 - 5, frameHeight / 2 + 30, 95, 20); Easy2.addMouseListener(this);

Normal2 = new JButton("2인 normal");
Normal2.setBounds(frameWidth / 2 - 5, frameHeight / 2 + 55, 95, 20); Normal2.addMouseListener(this);

Hard2 = new JButton("2인 hard");
Hard2.setBounds(frameWidth / 2 - 5, frameHeight / 2 + 80, 95, 20); Hard2.addMouseListener(this);
}

@Override
public void Draw(Graphics2D g2d)
{
// locate level/mode button.
Easy1 = new JButton("1인 easy");
Easy1.setBounds(frameWidth / 2 - 110, frameHeight / 2 + 30, 95, 20);
Easy1.addMouseListener(this);
Normal1 = new JButton("1인 normal");
Normal1.setBounds(frameWidth / 2 - 110, frameHeight / 2 + 55, 95, 20);
Normal1.addMouseListener(this);
Hard1 = new JButton("1인 hard");
Hard1.setBounds(frameWidth / 2 - 110, frameHeight / 2 + 80, 95, 20);
Hard1.addMouseListener(this);
Easy2 = new JButton("2인 easy");
Easy2.setBounds(frameWidth / 2 - 5, frameHeight / 2 + 30, 95, 20);
Easy2.addMouseListener(this);
Normal2 = new JButton("2인 normal");
Normal2.setBounds(frameWidth / 2 - 5, frameHeight / 2 + 55, 95, 20);
Normal2.addMouseListener(this);
Hard2 = new JButton("2인 hard");
Hard2.setBounds(frameWidth / 2 - 5, frameHeight / 2 + 80, 95, 20);
Hard2.addMouseListener(this);

DrawButtonForLevel();

switch (gameState)
{
case PLAYING:
Expand Down Expand Up @@ -273,33 +210,27 @@ public void Draw(Graphics2D g2d)
g2d.setColor(Color.white);
g2d.drawString("GAME is LOADING", frameWidth / 2 - 50, frameHeight / 2);
break;
default:
break;
}
}

/**
* Starts new game.
*/
private void newGame()
{
// We set gameTime to zero and lastTime to current time for later calculations.
gameTime = 0;
lastTime = System.nanoTime();

game = new Game();
}

/**
* Restart game - reset game time and call RestartGame() method of game object so that reset some variables.
*/

private void restartGame()
{
// We set gameTime to zero and lastTime to current time for later calculations.
gameTime = 0;
lastTime = System.nanoTime();

game.RestartGame();

// We change game status so that the game can start.
gameState = GameState.PLAYING;
}

Expand Down Expand Up @@ -373,6 +304,8 @@ public void mouseClicked(MouseEvent e)
case GAMEOVER:
restartGame();
break;
default:
break;
}
this.removeAll();
}
Expand Down
Loading

0 comments on commit c2ebe83

Please sign in to comment.