Results 1 to 4 of 4

Thread: Java Mini-Game!

  1. #1
    Ehkx's Avatar
    Ehkx is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Talking Java Mini-Game!

    Hey everyone! I've recently taken up Java coding a hobby and decided to try and make my first mini game!

    So no, this isn't a little "plz do my hom3work plz" post, just looking for a little direction!

    Alright so what my objective is is to create a game where you move a paddle with WASD in a JFrame. Secondly the score at the top of the screen will increase by one every time another "object" hits the paddle that you control with WASD.

    So far, i can control the paddle, and have the JLabel which displays the current score using an integer:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*; 
    import java.awt.event.*;
    import javax.swing.*;                
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    
    
    
    class MovingTextPanel extends JPanel implements KeyListener {
    
        String display = ""; 
        private int x = 50;  
        private int y = 50;
        int score = 0;
       
     
        
        /****************/
        
        JLabel playerscore;
    
        private int speed = 2; // number of pixels to move
        
    
        public MovingTextPanel() {
            this.setBackground(Color.white);
            this.setPreferredSize(new Dimension(300,190));
            this.addKeyListener(this); 
            this.setFocusable(true);    
     
            
        playerscore= new JLabel("Your Score is: "+ score); 
        this.add(playerscore);
        playerscore.setForeground(Color.orange);
    
        
    
        }
    
    
        public void paintComponent(Graphics g) {
            
          Image img = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Davis\\Desktop\\img.jpg");
          
          super.paintComponent(g);
          
          g.drawImage(img, 0,0, null);
          g.drawString(display, x, y);
          
            
          
          g.drawString(display, x, y);
          g.setColor(Color.GREEN);
          g.drawRect(x,y,38,7);
          g.fillRect(x,y,38,7);
    
            
        }
       
        public void keyTyped(KeyEvent kevt) {
          
            this.repaint();
        }
        
    
    
        public void keyPressed(KeyEvent e) {
            // Check the shift key, and do 10x the movement if the
            // shift key is down when the arrow keys are pressed.
            // Altho keyPressed is called for normal characters, they
            // are ignored here and handled in keyTyped.
            if (e.isShiftDown()) {
                speed = 10;
            } else {
                speed = 5;
            }        
            
            
            switch (e.getKeyCode()) {
                case KeyEvent.VK_A : x -= speed; x = Math.max(x, 0);   break;
                case KeyEvent.VK_D: x += speed; x = Math.min(x, 260); break;
                case KeyEvent.VK_W   : y -= speed; y = Math.max(y, 0);   break;
                case KeyEvent.VK_S : y += speed; y = Math.min(y, 181); break;
            }
            
            speed = 5;      
                
            this.repaint();  
        }
    
        public void keyReleased(KeyEvent ke) {}  
    }
    And The executeable .java is here :

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.*;
    
    public class KeyDemoGUI extends JFrame {
        MovingTextPanel drawing;
    
        //==========================================================constructor
        public KeyDemoGUI() {
            drawing = new MovingTextPanel();
            this.getContentPane().setLayout(new BorderLayout());
            this.getContentPane().add(drawing, BorderLayout.CENTER);
            this.setTitle("GUIGame");
            this.pack();
            
            drawing.requestFocus();      // Give the panel focus.
        }//end constructor
     
     
        public static void main(String[] args) {
            JFrame window = new KeyDemoGUI();
            window.setDefaultCloseOperation(EXIT_ON_CLOSE);
            window.setVisible(true);
           
            
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Ehkx View Post
    Hey everyone! I've recently taken up Java coding a hobby and decided to try and make my first mini game!

    So no, this isn't a little "plz do my hom3work plz" post, just looking for a little direction!

    Alright so what my objective is is to create a game where you move a paddle with WASD in a JFrame. Secondly the score at the top of the screen will increase by one every time another "object" hits the paddle that you control with WASD.

    So far, i can control the paddle, and have the JLabel which displays the current score using an integer:
    What is your specific question at the moment? What are you currently stuck on?

  3. #3
    Ehkx's Avatar
    Ehkx is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Ahh, knew i was forgetting something!

    I need help adding objects that will fall from the top of the screen, and what to make the if statements for when they make contact with the paddle you control.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Some suggestions:

    1) Here:
    Java Code:
        public void paintComponent(Graphics g) {
          Image img = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Davis\\Desktop\\img.jpg");
          super.paintComponent(g);

    You appear to be reading in an image each time the paintComponent method gets called, and this can slow your GUI's drawing to a crawl. Never read in am image or any file from within this method, and in fact since the image shouldn't change with time, you should only read the image in once in the program's life time and store the image in an instance field of the class.

    2) You're using a KeyListener where key binding would probably be better. Check out the Swing tutorials on how to use these.

    3) You have a god-class that tries to do everything and will be a bear to debug or upgrade. Better to separate out different functionality into different classes, and in fact your little project would be a good one to learn about using an MVC (model-view-controller) type structure. Check out the Wikipedia article on this.

    4) If you need to create multiple of the same type of objects during the program's life, consider placing them in a collection such as an ArrayList or LinkedList. Then the paintComponent can iterate through the list to do drawing, and you can remove from the list once a sprite is no longer needed.

    Luck!

Similar Threads

  1. java 2d game
    By zombie000 in forum Java Software
    Replies: 2
    Last Post: 02-07-2011, 02:13 AM
  2. Mini network application test and help required
    By louwhopley in forum Networking
    Replies: 0
    Last Post: 08-04-2009, 10:52 PM
  3. JSP sending a String to a "mini App" and calls that program too.
    By pikapika2501 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-25-2009, 12:19 PM
  4. How to do parsing and mini-Compler for ISA?
    By Nael in forum New To Java
    Replies: 1
    Last Post: 02-11-2009, 01:01 AM
  5. The 3 Word Story [Mini Game]
    By Eku in forum Forum Lobby
    Replies: 90
    Last Post: 11-16-2008, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •