Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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();       

} 

share|improve this question
    
I'm at work now but when I get home ill show you a simple way which involves a physics class with an array of entities and an update method and you can just add your entities you want affected by gravity to the array and then cycle through them adding to the y position if there isn't a solid object underneath... It's basic but can still achieve the desired result –  Savlon Feb 3 '13 at 23:45
add comment

1 Answer 1

up vote 5 down vote accepted

Well i haven't gone through the whole code but did notice one thing that you don't have any track of velocity.

well a simple tip to mimic gravity would be use a vector for position of object, velocity and gravity.

position.x=350; position.y=20;
velocity.x=0; velocity.y=0;
gravity.x=0; gravity.y=1; //depending on the strength of the gravity

//Update the position of object by adding velocity to it
position+=velocity;

//in case of keystrokes manipulate velocity rather that the position itself
case "w":velocity.y-=1;   //similarly for other cases

//constantly keep adding gravity to velocity
velocity+=gravity;

Hope you got the idea

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.