Hey its my first year doing java so I'm still quite bad.
I'm making a lunar lander for my coursework and I'm struggling to make some kind of gravity to pull my rocket with increasing speed, and I cant find a way of my rocket to move with increasing speed in any direction. Here is my code
import javax.swing.JFrame;
import java.awt.Color;
import java.util.Random;
import javax.swing.Timer;
import java.awt.event.*;
import java.awt.;
import javax.swing.;
import java.awt.image.BufferedImage;
public class LunarLander extends JFrame {
private static JFrame window;
private static Graphics gr;
private static Image theBackground;
private static Image theLander;
private static Image theBang;
private static Image theBottomFlame;
private static Image theLeftFlame;
private static Image theRightFlame;
private static Image theFlag;
private static Image theLandingSite;
private static int landerX=350, landerY=20;
private static boolean explosion=false;
private static int fuel=740;
private static int landingY;
private static boolean stop_moving=false;
private static boolean fuel_stoped = false;
public static void main(String[] args) {
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(550,10);
window.setTitle("Houston, we have a Problem");
window.setSize(1024,768);
window.setResizable(false);
BufferedImage canvas=new BufferedImage(1024,768,BufferedImage.TYPE_INT_ARGB);
gr=canvas.getGraphics();
JLabel label=new JLabel(new ImageIcon(canvas));
window.add(label);
window.setVisible(true);
theBackground = GameImage.loadImage("Images//Background.jpg");
theLander = GameImage.loadImage("Images//Rocket.png");
theBang = GameImage.loadImage("Images//explosion.gif");
theBottomFlame = GameImage.loadImage("Images//flamebottom.png");
theLeftFlame = GameImage.loadImage("Images//flameleft.png");
theRightFlame = GameImage.loadImage("Images//flameright.png");
theFlag = GameImage.loadImage("Images//flag.png");
theLandingSite = GameImage.loadImage("Images//site.png");
GameKeyboard.initialise();
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
doTimerAction();
}
};
Timer t = new Timer(25, taskPerformer);
t.start();
}
static Random randomGenerator = new Random();
static int landingX = randomGenerator.nextInt(900);
private static boolean check_position(int x, int y){
String CHECK="";
if(y>485 && y<600){
if(x>landingX-150 && x<landingX+20) {
CHECK="TRUE";
stop_moving = true;
}
}else{
CHECK="FALSE";
}
if(CHECK=="TRUE"){
return true;
}else{
return false;
}
}
private static void doTimerAction() {
gr.drawImage(theBackground, 0, 0, null);
gr.drawImage(theLandingSite, landingX , landingY+670, null);
char key= GameKeyboard.getKey();
int specialKey=GameKeyboard.getSpecialKey();
gr.setColor( Color.orange );
gr.setFont(new Font("Arial", Font.PLAIN, 16));
gr.drawString("LEFT RIGHT UP and DOWN arrow keys to move", 10,80);
gr.drawString("Key="+key+" specialKey="+specialKey ,10,100);
if (stop_moving == false){
if(fuel_stoped==false){
if (explosion == false) {
switch (key) {
case 'W': landerY=landerY-3; --fuel; gr.drawImage(theBottomFlame, landerX+148, landerY+195, null); break;
case 'w': landerY=landerY-3; --fuel; gr.drawImage(theBottomFlame, landerX+148, landerY+195, null); break;
case 'S': landerY=landerY+1; break;
case 's': landerY=landerY+1; break;
case 'A': landerX=landerX-1; --fuel; gr.drawImage(theRightFlame, landerX+189, landerY+83, null); break;
case 'a': landerX=landerX-1; --fuel; gr.drawImage(theRightFlame, landerX+189, landerY+83, null); break;
case 'D': landerX=landerX+1; --fuel; gr.drawImage(theLeftFlame, landerX+115, landerY+83, null); break;
case 'd': landerX=landerX+1; --fuel; gr.drawImage(theLeftFlame, landerX+115, landerY+83, null); break;
case 'Q': landerY=landerY-3; landerX=landerX-2; gr.drawImage(theRightFlame, landerX+189, landerY+83, null);gr.drawImage(theBottomFlame, landerX+148, landerY+195, null); --fuel;break;
case 'q': landerY=landerY-3; landerX=landerX-2; gr.drawImage(theRightFlame, landerX+189, landerY+83, null);gr.drawImage(theBottomFlame, landerX+148, landerY+195, null); --fuel; break;
case 'E': landerY=landerY-3; landerX=landerX+2; gr.drawImage(theLeftFlame, landerX+115, landerY+83, null); gr.drawImage(theBottomFlame, landerX+148, landerY+195, null); --fuel; break;
case 'e': landerY=landerY-3; landerX=landerX+2; gr.drawImage(theLeftFlame, landerX+115, landerY+83, null); gr.drawImage(theBottomFlame, landerX+148, landerY+195, null); --fuel; break;
}
}
}
}
// If the lander hasn't hit the ground yet, draw it and the rocket flame
if(landerY < 550) {
gr.drawImage(theLander, landerX, landerY, null);
}
// If it is at the bottom draw and explosion again for a while, i.e. <400
else if (landerY < 600) {
gr.drawImage(theBang, landerX, landerY, null);
gr.setFont(new Font("Arial", Font.BOLD, 40));
gr.setColor( new Color(255, 100,100) );
gr.drawString("GAME OVER", 400,400);
explosion = true;
//landerY = 560;
}
if(check_position(landerX, landerY)) {
gr.setFont(new Font("Arial", Font.BOLD, 40));
gr.setColor( new Color(255, 100,100) );
gr.drawString("Well Done", 400,400);
gr.drawImage(theFlag, landingX+180 , landingY+540, null);
stop_moving = true;
}
if(fuel<5){
fuel_stoped= true;
}
// Example how to do non-image graphics, i.e. graphics that is created at runtime.
// Here a fuel gauge ('fuel' is decreased by one every time this method is run.
if(landerY < 550) {
gr.setColor( new Color(200, 200, 200) ); //grey background
gr.fillRect(1000, 0, 23, 800);
if(fuel>200) gr.setColor( new Color(0, 255, 0) ); // green
else if(fuel>100) gr.setColor( new Color(255, 255, 0) ); // yellow
else gr.setColor( new Color(255, 0, 0) ); // red
gr.fillRect(1003, 755-fuel, 16, fuel);
}
window.repaint();
}
