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

Thread: Java game problem! Square wont move(FIXED)

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

    Default Java game problem! Square wont move(FIXED)

    Hello!
    I am a beginner java programmer and a couple of days ago i started programming again(i had kinda like a break for a while). So yesterday when i got bored i just started making a random game. It is supposed to be a square that moves to random locations but i *ed up somewhere. The square wont move and im not sure if it is because the updating of the screen if weird or just my other code. Im really new to making games and i have just started learning so the code will be bad. If there is any like really weird code in there it might be because i changed and tried alot of things when trying to fix it. So have i just made a small mistake or is it a big failure that i should delete and work on when i get better? This is just a thing i started making when i was bored so I dont really care about it and its mostly a test. Here is the code:
    First class:
    import javax.swing.*;
    import java.awt.*;
     
     
    public class Main extends JFrame{
     
    	Image dbImage;
    	Graphics dbg;
     
    	AI bot;
    	public Main(){
    		bot = new AI();
    		setVisible(true);
    		setSize(500,500);
    		setResizable(false);
    		setDefaultCloseOperation(3);
    		setLocation(500,250);
     
    	}
     
    	public void draw(Graphics g){
    		g.setColor(Color.GRAY);
    		g.fillRect(0,0,500,500);
    		bot.draw(g);
    	}
     
    	public void paint(Graphics g){
    		dbImage = createImage(getWidth(),getHeight());
    		dbg = dbImage.getGraphics();
    		draw(dbg);
    		g.drawImage(dbImage,0,0,this);
     
    	}
     
    	public static void main(String[] args) {
    		new Main();
     
    	}
     
    }
    And second class:
    import java.awt.*;
    import java.util.Random;
     
    public class AI implements Runnable{
     
    	Rectangle enemy;
    	int x,y;
    	int randXDir;
    	int randYDir;
     
    	Random r;
     
    	Thread t1;
     
    	public AI(){
    		r = new Random();
    		Thread t1 = new Thread();
    		enemy = new Rectangle(250,250,15,15);
    		x = enemy.x;
    		y = enemy.y;
    		setRandomXDir();
    		setRandomYDir();
    		t1.start();
    	}
     
    	public void setRandomXDir(){
     
    		randXDir = r.nextInt(490);
     
    	}
    	public void setRandomYDir(){
     
    		randYDir = r.nextInt(485);
    	}
    	public void draw(Graphics g){
    		g.setColor(Color.GREEN);
    		g.fillRect(x, y, enemy.width, enemy.height);
    	}
    	public void setDirection(){
     
    		if(x > randXDir){
    			x -= 1;
    			if(x == randXDir){
    				setRandomXDir();
    				setDirection();
    			}
    		}
    		if(x < randXDir){
    			x += 1;
    			if(x == randXDir){
    				setRandomXDir();
    				setDirection();
    			}
    		}
    		if(y > randYDir){
    			y = y - 1;
    			if(y == randYDir){
    				setRandomXDir();
    				setDirection();
    			}
    		}
    		if(y < randYDir){
    			y = y + 1;
    			if(y == randYDir){
    				setRandomXDir();
    				setDirection();
    			}
    		}
     
    	}
    	public void move(){
    		setDirection();
     
    	}
    	public void run(){
    		try{
    			move();
    			Thread.sleep(5);
    		}catch(Exception e){System.out.println("error");}
     
    	}
    }
    Also tell me if i should do something wrong!(Remember im really new to making games)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java game problem! Square wont move

    Have you tried debugging the code by adding some printlns to show when and where the location of the square is changed?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java game problem! Square wont move

    Quote Originally Posted by Norm View Post
    Have you tried debugging the code by adding some printlns to show when and where the location of the square is changed?
    I think I know what you mean and no i have not tried that. I will try it now but if someone got a quicker answer please tell me!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java game problem! Square wont move

    There are several things you need to find out about what the code is doing. One way of debugging is by adding println statements. Another is to use an interactive debugger. I have to use the println method with my simple IDE
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java game problem! Square wont move

    Well I am getting closer to solving my problem! First of all i was stupid enough to not give the Thread object the location of the method run. So i wrote:
    t1 = new Thread();
    Instead of:
    t1 = new Thread(this);

    --- Update ---

    Now when i start the program the square is just in a random position. Wow this code is bad!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java game problem! Square wont move

    You need to tell the jvm to call the paint() method when the shape's position has changed so it will be drawn at the new location. The repaint() method does that.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java game problem! Square wont move

    Was trying stuff without it... -.- Thank you for the respons!

    --- Update ---

    And btw i know that it will only move on the x axis and will fix that! Thank you guys for replying!

Similar Threads

  1. [SOLVED] is there any good formula's for 2d java game - to make ur player move right or left?
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 23rd, 2012, 01:42 AM
  2. Player wont move
    By jokr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 25th, 2012, 09:46 AM
  3. java game, both players move for player 2 but not player 1
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 21st, 2011, 12:52 PM
  4. Square Game (a half solved, please help with the rest!!!)
    By LadyBelka in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 23rd, 2011, 04:42 PM
  5. Magic square class (modular math problem)
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 30th, 2010, 10:56 AM

Tags for this Thread