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 4 of 4

Thread: Help With a Simple Game

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With a Simple Game

    So to start off here's my code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
     
     
    public class Ski implements KeyListener{
     
        Image imgSkier, imgTree, imgGoomba;
        boolean crashed, leftArrow, rightArrow, upArrow, downArrow;
     
        int skierX, skierY, treeX, treeY, goombaX, goombaY;
        int skierWidth, skierHeight, treeWidth, treeHeight, goombaWidth, goombaHeight;
        int score;
     
        JFrame frame;
        GamePanel gPanel;
     
        public Ski()    {
            score = 0;
            treeX = 10 + (int)(Math.random() * ((500 - 10) + 1));
            treeY = 500;        
            goombaX = 10 + (int)(Math.random() * ((500 - 10) + 1));
            goombaY = 600;
            skierX = 100;
            skierY = 10;
            crashed = false;
            leftArrow = false;
            rightArrow = false;
            upArrow = false;
            downArrow = false;
            frame = new JFrame();
            gPanel = new GamePanel();
            gPanel.setFocusable(true);
            gPanel.addKeyListener(this);
            getImages();
            frame.add(gPanel);
            frame.setSize(500,700);
            frame.setVisible(true);
            animate();
        }
     
        public void getImages(){
     
            imgSkier = new ImageIcon("skier.png").getImage();
            imgTree = new ImageIcon("tree.png").getImage();        
            imgGoomba = new ImageIcon("Goomba.png").getImage();    
            skierWidth = imgSkier.getWidth(null);
            skierHeight = imgSkier.getHeight(null);
            treeWidth = imgTree.getWidth(null);
            treeHeight = imgTree.getHeight(null);        
            goombaWidth = imgGoomba.getWidth(null);
            goombaHeight = imgGoomba.getHeight(null);
        }
     
     
        class GamePanel extends JPanel{//inner class to customize a JPanel for drawing
     
            public void paintComponent(Graphics g){
     
                g.setColor(Color.white);//Avoid trails
                g.fillRect(0,0,this.getWidth(), this.getHeight());//Avoid trails
     
                g.drawImage(imgSkier, skierX, skierY, 60, 100, null);
                g.drawImage(imgTree, treeX, treeY, 60, 100, null);            
                g.drawImage(imgGoomba, goombaX, goombaY, 60, 100, null);
     
                g.setColor(Color.black);
                g.fillRect(400,0,84,30);           
     
                g.setColor(Color.yellow);
                g.fillRect(404,4,76,22);
     
                g.setColor(Color.blue);
                g.setFont(new Font("Arial", Font.BOLD, 12));
                g.drawString("Score: " + score, 415, 20);
     
                if(crashed){
     
                    g.setColor(Color.black);
                    g.fillRoundRect(60,100,350,200,20,20);
                    g.setColor(Color.green);
                    g.fillRoundRect(65,105,340,190,20,20);
                    g.setColor(Color.blue);
                    g.fillRoundRect(70,110,330,180,20,20);
     
                    g.setColor(Color.black);
                    g.setFont(new Font("Arial", Font.BOLD, 50));
                    g.drawString("GAME OVER", 80, 180);
                    g.setFont(new Font("Arial", Font.BOLD, 20));
                    g.drawString("Your Final Score: " + score, 140, 230);                                
                }
     
     
            }        
        }  
     
     
     
        public void animate(){
          while(!crashed){
     
            treeY = treeY - 2;        
            goombaY = goombaY - 3;    
            if(leftArrow) skierX -= 5;
            if(rightArrow) skierX += 5;
            if(upArrow) skierY -= 5;
            if(downArrow) skierY += 5;
     
            try{
     
                Thread.sleep(10);
     
            } catch(Exception e){}
     
     
            Rectangle skierBounds = new Rectangle(skierX, skierY, 60, 100);
            Rectangle treeBounds = new Rectangle(treeX, treeY, 60, 100);
            Rectangle goombaBounds = new Rectangle(goombaX, goombaY, 60, 100);
     
            if(skierBounds.intersects(treeBounds)        ){
                crashed = true;
            }       
            if(skierBounds.intersects(goombaBounds)        ){
                crashed = true;
            }
     
            if(treeY < 1){
                score = score + 1;
                treeX = 10 + (int)(Math.random() * ((500 - 10) + 1));
                treeY = 600;
            }
     
            if(goombaY < 1){
                score = score + 1;
                goombaX = 10 + (int)(Math.random() * ((500 - 10) + 1));
                goombaY = 600;
            }
     
     
                            gPanel.repaint();
          }//close loop
        } //close animate  
        public void keyTyped(KeyEvent e){
     
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
            leftArrow = true;            
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            rightArrow = true;            
            }      
            if(e.getKeyCode() == KeyEvent.VK_UP){
            upArrow = true;            
            } 
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
            downArrow = true;            
            } 
        }
        public void keyPressed(KeyEvent e){
     
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
            leftArrow = true;            
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            rightArrow = true;            
            }          
            if(e.getKeyCode() == KeyEvent.VK_UP){
            upArrow = true;            
            }   
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
            downArrow = true;            
            }   
     
        }
        public void keyReleased(KeyEvent e){       
     
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
            leftArrow = false;            
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            rightArrow = false;            
            }   
            if(e.getKeyCode() == KeyEvent.VK_UP){
            upArrow = false;            
            }  
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
            downArrow = false;            
            }  
     
     
        }     
     
        public static void main(String[] y)
        {
            Ski s = new Ski();
     
        }
    }


    What I'm looking to accomplish is some kind of "leveling" system. I feel like this can be done by using if statements (ex. if(score > 4){....}). However I've tried placing a few in different spots in the code and I haven't gotten what I'm looking for. I'd like to start off with just one image you need to avoid and at some point have the other image appear and so on. Any help is appreciated, thank you. Also I'm new to these forums so if something is wrong with this post, don't hesitate to tell me.


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

    Default Re: Help With a Simple Game

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    I haven't gotten what I'm looking for.
    Can you describe in some detail what you are looking for?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With a Simple Game

    Thanks for the quick reply. Here's what I'm looking for. Right now I have the tree and the goomba moving vertically up the screen from the very beginning of the game. I would like the tree to be the only image until the score > 4, then the goomba image would appear and start moving. The closest I've gotten is having the goomba image sitting at the bottom of the screen until the score reaches a certain point and then it will start moving. If push comes to shove I can set the goombaY = 700 so it is completely off the screen but there must be a way to not have the image appear until the score reaches a certain point.

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

    Default Re: Help With a Simple Game

    To start with: You should look at using a Timer to control position updates instead of using an infinite loop with sleep()
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Programming AI for simple game?
    By YouGoLast in forum Java Theory & Questions
    Replies: 3
    Last Post: September 27th, 2024, 02:33 AM
  2. Need help on simple game for Practice
    By Harrald in forum What's Wrong With My Code?
    Replies: 21
    Last Post: October 8th, 2012, 06:57 PM
  3. Help with simple text game...
    By iansmiler in forum Java Theory & Questions
    Replies: 5
    Last Post: May 24th, 2012, 01:37 PM
  4. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  5. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM