/**
* Write a description of class LunarLander here.
*
* @author (your name)
* @version (a version number or a date)
*/
//Functionality
import javax.swing.JFrame; // window functionality
import java.awt.Color; // RGB color stuff
import javax.swing.Timer; // import timer functionality
import java.awt.event.*; // functionality for the event fired by the timer
import java.awt.*; // Graphics stuff from the AWT library, e.g. Graphics
import javax.swing.*; // Graphics stuff from the Swing library, e.g. JLabel
import java.awt.image.BufferedImage; // Graphics drawing canvas
import java.awt.event.KeyEvent;
import java.util.Random;
public class LunarLander extends JFrame{
// The Java window
private static JFrame window;
// Graphics "Handle"
private static Graphics gr;
// Image Storage
private static Image theBackground; // Background Image
private static Image theLander; // PNG of the Lunar Lander
private static Image theBang; // Animated Explosion
public static Random randomGen= new Random();
private static int landerX = randomGen.nextInt(800 - 50), landerY = 50; // Starting position of the lander
private static boolean explosion=false; // Sets explosion to false
public static int speedY = 0;
public static int speedX = 0;
public static double speedStopping = 1;
public static int speedAccelerating = 2;
public static int topLandingSpeed = 5;
// Main Method, Create window
public static void main(String[] args) {
//Window Setup
window = new JFrame(); // Renames the new window
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Sets the X to close
window.setTitle("Moon Lander, Can you be the first man on the moon!"); // Title Name
window.setSize(800, 600); // Window Size
window.setResizable(true); // Disables resizable window
window.setLocationRelativeTo(null); // Sets location to top of the screen
BufferedImage canvas=new BufferedImage(800,600,BufferedImage.TYPE_INT_ARGB); // Creates A Canvas
gr=canvas.getGraphics(); // Creates handle for drawing on the canvas
JLabel label=new JLabel(new ImageIcon(canvas)); // Creates label to draw on
window.add(label);
window.setVisible(true); // Displays above
//Loading Images
theBackground = GameImage.loadImage("Images//Background.png"); // Loads background
theLander = GameImage.loadImage("Images//LunarLander.png"); // Loads lander
theBang = GameImage.loadImage("Images//explosion.gif"); // Loads Explosion
GameKeyboard.initialise(); // Starts keyboard input
//Timer Setup
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent evt) // Calls the actionPerformed() method
{
doTimerAction();
}
};
Timer ScreenTimer = new Timer(75, taskPerformer); // Creates Timer
ScreenTimer.start(); // Starts Timer
}
// Method to draw all needed images, then timer resets to display on the screen
private static void doTimerAction() {
gr.drawImage(theBackground, 0, 0, null); // Overwrites everything with the background image
// User Instructions
gr.setColor( Color.red ); // Sets colour red
gr.setFont(new Font("Arial", Font.BOLD, 16)); // Sets font
gr.drawString("Arrow keys to move, land on the platform to win", 200,200); // Output and location
// Get keys pressed from the keyboard
char key= GameKeyboard.getKey();
int specialKey=GameKeyboard.getSpecialKey();
//Keyboard Binding
if (landerY >= 500){
gr.drawImage(theBang, landerX, landerY, null);
gr.setColor( Color.red ); // Sets colour red
gr.setFont(new Font("Arial", Font.BOLD, 16)); // Sets font
gr.drawString("You have crashed! Restart game!", 200,400);
}else{
if (specialKey == 38){
speedY -= speedAccelerating;
}else{
speedY += speedStopping;
}
if (specialKey == 37){
speedX -= speedAccelerating;
}else{
speedX += speedStopping;
}
if (specialKey == 39){
speedX += speedAccelerating;
}else{
speedX -= speedStopping;
}
landerX += speedX;
landerY += speedY;
gr.setColor( Color.red ); // Sets colour red
gr.setFont(new Font("Arial", Font.BOLD, 16)); // Sets font
gr.drawString("speedX" + speedX + "speedY" + speedY + "speedAccelerating" +speedAccelerating + "speedStopping" + speedStopping + "landerY" + landerY + "landerX" + landerX, 200,400);
gr.drawImage(theLander, landerX, landerY, null); // Draws lander and its position
}
window.repaint(); // Re paints everything onto the screen
}
}