Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default I am trying to make a very simple game

    So, I am making an extremely simple game (I am trying to get into game programming)
    So far, I have a JFrame with a square that you can move around with WASD. (Believe it or not, I was ecstatic when I was able to make this!)
    Anyway, after playing the "move it around the screen for days" game, I got a bit bored. So, I figured that I could try and add another part to it, like possibly another square you have to get to, with a timer counting down the time you have.
    Do you guys have any idea on how to approach this? I would like to have my protagonist square with WASD movement, and then a little box that the square has to get to to go to the next level, which will have less time :D. Anyway, I have no idea on how I would make the program recognize that my square is within the box. Oh-- wait, maybe I could make an if statement, like if x = bla and y = bla, then start new level? What do you guys think? How would I go about the timer? Thanks for the help

    Heres my code right now
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class myGame extends JComponent {
        public int x = 20;
        public int y = 20;
        KeyListener kw = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
            int k = e.getKeyCode();
            if (k == KeyEvent.VK_W){
            y=y-10;
            repaint();
    }
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        
        };
        KeyListener ks = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
    int k = e.getKeyCode();
    if (k == KeyEvent.VK_S){
    	y = y+10;
    	repaint();
    }
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        };
        KeyListener ka = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
            int k = e.getKeyCode();
            if(k == KeyEvent.VK_A){
    	    x=x-10;
    	    repaint();
    }
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        };
        KeyListener kl = new KeyListener() {
     
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
                 
            }
     
            @Override
            public void keyPressed(KeyEvent e) {
                int k = e.getKeyCode();
                if(k == KeyEvent.VK_D) {
            x=x+10;
            repaint();
                }
            }
     
            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
                 
            }
            };
        myGame() {
            JFrame f = new JFrame("The most advanced game in the world");
            JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.add(this);
            f.add(p);
            f.setSize(500, 500);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.addKeyListener(kl);
            f.addKeyListener(ka);
            f.addKeyListener(kw);
            f.addKeyListener(ks);
            p.setBackground(Color.blue);
            
        }
     
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.white);
            g.drawString("My First Game", 20, 20);
            g.fillRect(x, y, 50, 50);
        }
     
    
        public static void main(String[] args) {
            new myGame();
        }
     
    }

  2. #2
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: I am trying to make a very simple game

    Why multiple key listneres. And you kinda answered your own question with if x=bla and y = bla. thats all you need just after it moves check to see if its equal to the other square.
    With the right know how, anything is possible

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    yeah, I figured that part. Now, I am trying to do that. I set up a square that randomly moves around, and then the one you control. However, when I do the if statement, it says cannot convert from int to boolean. What I am putting in the if statement is if the x value of the WASD square is equal to the x value of the random square, reset the WASD square. The x for the wasd is name "x", and the x for the random is named "ex". The code is below. Error on line 23

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Timer;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.util.*;
     
        public class myGame extends JComponent {
        public int x = 20;
        public int y = 20;
        public int w = 400;
        public int z = 400;
        Random r = new Random();
        Random rand = new Random();
        int ex = r.nextInt(400);
        int why = rand.nextInt(400);
        public void Reset(){
        	if(x = ex){
        		x = 20;
        	}
        }
        KeyListener kw = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
    int k = e.getKeyCode();
    if (k == KeyEvent.VK_W){
    	y=y-10;
    	repaint();
    }
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        
        };
        KeyListener ks = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
    int k = e.getKeyCode();
    if (k == KeyEvent.VK_S){
    	y = y+10;
    	repaint();
    }
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        };
        KeyListener ka = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
            int k = e.getKeyCode();
            if(k == KeyEvent.VK_A){
    	    x=x-10;
    	    repaint();
    }
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        };
        KeyListener kl = new KeyListener() {
     
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
                 
            }
     
            @Override
            public void keyPressed(KeyEvent e) {
                int k = e.getKeyCode();
                if(k == KeyEvent.VK_D) {
            x=x+10;
            repaint();
                }
            }
     
            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
                 
            }
            };
        myGame() {
            JFrame f = new JFrame("SquareMove");
            JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.add(this);
            f.add(p);
            f.setSize(500, 500);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.addKeyListener(kl);
            f.addKeyListener(ka);
            f.addKeyListener(kw);
            f.addKeyListener(ks);
            p.setBackground(Color.blue);
            
        }
     
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillRect(x, y, 50, 50);
            g.drawRect(ex, why,50,50);
            g.setColor(Color.white);
            g.drawString("Use WASD to move around", 20, 20);
        }
     
    
        public static void main(String[] args) {
            new myGame();
        }
     
    }

  4. #4
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: I am trying to make a very simple game

    You used only 1 equals sign with is a declarative statement. You need a statement that compares which is done with 2 equals signs.
    With the right know how, anything is possible

  5. #5
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    I got that now, here is the code. It is kinda working. Two problems: one is that if I move it by intervals of 5 or 10 pixels, it will not hit the spot it needs to to reset to the spawn point, and if I move it by one pixel it is too slow. The second problem is that even when I am doing it by one pixel per movement, it did not reset when it hit. Is there any way to say "when it hits that general area then reset"?
    Thanks, and here is the code (it is prettier now )

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Timer;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.util.*;
     
        public class myGame extends JComponent {
        public int x = 20;
        public int y = 20;
        public int w = 400;
        public int z = 400;
        Random r = new Random();
        Random rand = new Random();
        int ex = r.nextInt(400);
        int why = rand.nextInt(400);
        
        public void ResetIt(){
        	if(x == ex && y == why){
        		x = 20;
        		y = 20;
        	}
        }
        KeyListener kw = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
            int k = e.getKeyCode();
            if (k == KeyEvent.VK_W){
    	    y=y-1;
    	    repaint();
            }
            if (k == KeyEvent.VK_S){
            	y = y+1;
            	repaint();
            }
            if(k == KeyEvent.VK_A){
        	    x=x-1;
        	    repaint();
            }
        	if(k == KeyEvent.VK_D) {
        	    x=x+1;
        	    repaint();
        	            }
        };
            
    		
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        
        };
        myGame() {
            JFrame f = new JFrame("SquareMove");
            JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.add(this);
            f.add(p);
            f.setSize(500, 500);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.addKeyListener(kw);
            p.setBackground(Color.blue);
            
        }
     
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillRect(x, y, 50, 50);
            g.drawRect(ex, why,50,50);
            g.setColor(Color.white);
            g.drawString("Use WASD to move around", 20, 20);
        }
     
    
        public static void main(String[] args) {
            
        	new myGame();
        }
     
    }

  6. #6
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: I am trying to make a very simple game

    So even before the last post I was playing around with your code to get it working. It is reseting but you don't see it becuase you dont call repaint and to see use bigger pixels you just check to see if its in the square using if x<=exrightside&&x>=leftSide
    With the right know how, anything is possible

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    Oh, I didn't know it was resetting. Well, I tried the one pixel one by one again and I did call the repaint method. But it is still not resetting for me.
    Sorry for being so bad at Java :(
    Here is the code

    (I just changed movement to 10 pixels but it still doesn't reset at one either)
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Timer;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.util.*;
     
        public class myGame extends JComponent {
        public int x = 20;
        public int y = 20;
        public int w = 400;
        public int z = 400;
        Random r = new Random();
        Random rand = new Random();
        int ex = r.nextInt(400);
        int why = rand.nextInt(400);
        
        public void ResetIt(){
        	if(x == ex && y == why){
        		x = 20;
        		y = 20;
        		repaint();
        	}
        }
        KeyListener kw = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
            int k = e.getKeyCode();
            if (k == KeyEvent.VK_W){
    	    y=y-10;
    	    repaint();
            }
            if (k == KeyEvent.VK_S){
            	y = y+10;
            	repaint();
            }
            if(k == KeyEvent.VK_A){
        	    x=x-10;
        	    repaint();
            }
        	if(k == KeyEvent.VK_D) {
        	    x=x+10;
        	    repaint();
        	            }
        };
            
    		
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        
        };
        myGame() {
            JFrame f = new JFrame("SquareMove");
            JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.add(this);
            f.add(p);
            f.setSize(500, 500);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.addKeyListener(kw);
            p.setBackground(Color.blue);
            
        }
     
        public void paintComponent(Graphics g) {
            g.fillRect(x, y, 50, 50);
            g.drawRect(ex, why,50,50);
            g.setColor(Color.white);
            g.drawString("Use WASD to move around", 20, 20);
        }
     
    
        public static void main(String[] args) {
            myGame g = new myGame();
            g.ResetIt();
        	new myGame();
        }
     
    }

  8. #8
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: I am trying to make a very simple game

    you are only calling resetIt one time. you need it to be called everytime it moves
    With the right know how, anything is possible

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    Awesome! It worked! Now the only thing left that I have to figure out is how to make it reset when it is in the general area, and not exactly in the specific square.

  10. #10
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    Do you know how to make it so that when it is within the square it will reset, instead of necessary being in the same exact coordinate as the square?

  11. #11
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: I am trying to make a very simple game

    using the 4 corneres of the square and instead of == use a combination of <= and >=
    With the right know how, anything is possible

  12. #12
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    How would I find the values of the four corners of the square?

  13. #13
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    Got it working! Thanks!
    One LAST question (I Promise!)
    How would I go about making the terminal square box reset to a new random position each time the WASD rectangle hits it?
    Is it possible to set a timer that runs on the screen? It sounds very difficult. I mean, I would know how to reduce it, but I wouldn't know how to set the timer and let it run on the screen.
    Last edited by MW130; 01-16-2014 at 05:30 AM.

  14. #14
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: I am trying to make a very simple game

    No problem glad to help
    With the right know how, anything is possible

  15. #15
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    Ugh I am so sorry to bother you again. I think the problem now is simple; I just need to make the program dynamic. What I mean is I now set up the program to start the WASD Square at a random point, and to reset to a random point when it goes into the new square. Now the problem is it always resets to the same random point for that opening of the program. Here is the new code, and if you are tired then it is totally fine and I really appreciate your help.

    Java Code:
     import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Timer;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import java.util.*;
     
        public class myGame extends JComponent {
        public int w = 400;
        public int z = 400;
        Random ar = new Random();
        public int x = ar.nextInt(400);
        Random har = new Random();
        public int y = har.nextInt(400);
        Random r = new Random();
        Random rand = new Random();
        Random newx = new Random();
        Random newy = new Random();
        int newex = newx.nextInt(450);
        int newwhy = newy.nextInt(450);
        int ex = r.nextInt(400);
        int resetTimes = 0;
        int why = rand.nextInt(400);
        
        public void ResetIt(){
        	if(x <= ex+17.5 && y <= why+17.5 && x >= ex-17.5 && y >= why-17.5){
        		x = newex;
        		y = newwhy;
        		resetTimes++;
        		repaint();
        		
        	}
        }
        KeyListener kw = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
            int k = e.getKeyCode();
            if (k == KeyEvent.VK_W){
    	    y=y-10;
    	    repaint();
    	    ResetIt();
    
            }
            if (k == KeyEvent.VK_S){
            	y = y+10;
            	repaint();
        	    ResetIt();
    
            }
            if(k == KeyEvent.VK_A){
        	    x=x-10;
        	    repaint();
        	    ResetIt();
    
            }
        	if(k == KeyEvent.VK_D) {
        	    x=x+10;
        	    repaint();
        	    ResetIt();
        	            }
        };
            
    		
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        
        };
        myGame() {
            JFrame f = new JFrame("SquareMove");
            JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.add(this);
            f.add(p);
            f.setSize(500, 500);
            f.setVisible(true);
            setLayout(new BorderLayout());
            f.add(l);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.addKeyListener(kw);
            p.setBackground(Color.blue);
            
        }
     
        public void paintComponent(Graphics g) {
            g.fillRect(x, y, 50, 50);
            g.drawRect(ex, why,70,70);
            g.drawString("enter",ex,why);
            g.setColor(Color.white);
            g.drawString("Use WASD to move around", 20, 20);
    		if(resetTimes > 5 && resetTimes <8){
    			g.drawString("Welcome to level two", 20, 50);
    		}
        }
     
    
        public static void main(String[] args) {
            myGame g = new myGame();
            g.ResetIt();
        	new myGame();
        }
     
    }

  16. #16
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: I am trying to make a very simple game

    Do you remeber why It wasnt reseting before. Its the same problem as back then
    With the right know how, anything is possible

  17. #17
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    hmmm. But the numbers are within the ResetIt method, and I am calling it repeatedly. So what could I do to make the random number change each time the WASD square enters the box? I don't know which method to call again.

  18. #18
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: I am trying to make a very simple game

    Right now lines 28 and 29 create the random number and then it is stored in your variables that are used in resetIt. These variables always contain the same number because you never called anything to change it. You need to set them equal to a new random inside the reset function. by calling a new random object
    With the right know how, anything is possible

  19. #19
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    You are so good at Java. Can I hire you as a teacher?

  20. #20
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I am trying to make a very simple game

    Daryn, do you know how to display an image? I'd like to bump it up a level and display peter griffin instead of the square and a burger instead of the terminal square.

Page 1 of 2 12 LastLast

Similar Threads

  1. What do i need to know to make this game?
    By javaP1 in forum New To Java
    Replies: 1
    Last Post: 06-02-2013, 12:50 AM
  2. How do I make a simple java text game?
    By bluecloak in forum New To Java
    Replies: 7
    Last Post: 11-12-2012, 07:14 AM
  3. want to make a simple game in java
    By Tarasj in forum New To Java
    Replies: 1
    Last Post: 03-26-2012, 10:54 PM
  4. Replies: 1
    Last Post: 08-30-2011, 03:28 PM
  5. Help to make a game ???
    By Peter20 in forum Java Applets
    Replies: 3
    Last Post: 05-10-2010, 06:36 AM

Posting Permissions

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