Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with java code (Lunar Lander game).

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with java code (Lunar Lander game).

    Hi there guys, i've got an assignment in university due tomorrow. And im struggling on finishing my code. Its 2D btw.

    Basically we have to make a Lunar Lander type game (Accelerating, gravity, crash zone, land pad etc).

    Ive done all the gravity, acceleration.

    But i'm having trouble making the certain areas of my screen a crash zone and land pad.

    The crash zone is basically all the floor (no including the land pad). so that when the image hits the crash zone it changes to an explosiong gif,

    And the land pad i need to have the lander speed low enough and it to land on the land pad to be a successfull landing if that makes sense?

    Basically I can't get my head around how i tell it that the floor area is solid and is a crash zone and that the land pad is solid and able to land on?

    Lunar Lander

    sort of like that game but not as many complex cliffs etc.

    Any helps appreciated cheers - Jay.

    /**
     * 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
        }    
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with java code (Lunar Lander game).

    Also posted at Need help with uni coursework - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  2. Lunar Lander
    By sqwundle in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2012, 08:07 AM
  3. java game code
    By bhavana_1993 in forum AWT / Java Swing
    Replies: 4
    Last Post: August 30th, 2012, 11:30 AM
  4. Can anyone please help me with this java code for minesweeper game!
    By Mahela in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2011, 08:10 AM
  5. Help With Java Game Code
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2010, 04:07 PM