GPWiki.org
GPWiki.org
It is currently Tue May 21, 2013 10:59 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Java 2D game: Monster AI
PostPosted: Sun Feb 05, 2012 4:06 pm 
Hello everyone. I'm new to this forum, but I was hoping to get some help with developing my monster's AI for a game I'm programming in Java.

Here is what I have:

My Monster class:
Code:
package Game;

import java.awt.Image;
import java.util.Random;
import javax.swing.ImageIcon;

public class Monster implements Runnable{

   int xmove,ymove, x ,y;
   Image AI;   
   ImageIcon i = new ImageIcon("H:/Suncoast/Senior Project/Images/Monster.gif");
   boolean rest = false;
   boolean set = true;
   
   public Monster(Monster b){
      AI = i.getImage();
      x= 500; //x coordinate
      y = 400; //y coordinate
   }

public int ChooseDir(){
   Random z = new Random();
   int[] randmove = new int [3];
   randmove[0] = 0;
   randmove[1] = 1;
   randmove[2] = -1;
   int randpick = z.nextInt(3);
   return randmove[randpick];
}

public void setx(int a){
   xmove= a;
}

public void sety(int a){
   ymove = a;
}

public void move() {
   x += xmove;
   y += ymove;
}
public void run(){
   try{
      while(true){
         if(!rest){
            if(set){
            setx(ChooseDir());
            sety(ChooseDir());
            set = false;
            }
            long start = System.currentTimeMillis();
            long end = start + 1*1000;
            while(System.currentTimeMillis() < end){
               move();
               Thread.sleep(10);
            }
            rest = true;
         }
         else{
            Thread.sleep(3000);
            set = true;
            rest = false;
         }
      }
      }catch(Exception ex){
         System.err.println(ex.getMessage());
   }
}

public Image getImage(){
   return AI;
}
}


My board class:

Code:
package Game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener, Runnable{
Link a; //Link character
static Monster b;
Image back; //background
Timer time;
static Monster AI = new Monster(b);
Thread t1 = new Thread(AI);
   public Board(){
      a = new Link();
      setFocusable(true);// Allows for the movement of the character
      ImageIcon i = new ImageIcon("H:/Suncoast/Senior Project/Images/Original.jpg");
      back = i.getImage();
      time = new Timer(5,this);
      time.start(); //Related to the action performance
      this.addKeyListener(a);
      t1.start();
   }

   public void actionPerformed(ActionEvent e){
      a.move();
      b.move();
      repaint();
   }
      
   public void paint(Graphics g){
      super.paint(g);
         Graphics2D g2D = (Graphics2D) g;
         
      g2D.drawImage(back,0,0,null);
      g2D.drawImage(a.getImage(),a.getx(),a.gety(),null);
      g2D.drawImage(b.getImage(),b.setx(a),b.sety(a),null);
   }
   
}



The issue I'm having is that my constructor for static Monster AI = new Monster(b); is apparently undefined although I have it defined in my monster class. Another error I'm having is with g2D.drawImage(b.getImage(),b.setx(a),b.sety(a),null); where there is an error with b.setx(a) and b.sety(a).

Any help would be greatly appreciated! Thank you!


Top
  
 
PostPosted: Sun Feb 05, 2012 6:46 pm 
Just in case you might want to see Frame class:

Code:
package Game;

import javax.swing.*; //import swing class for creation of JFrame

public class Frame {

   public static void main(String[] args){
      JFrame frame = new JFrame("Test");//Create frame for applet
      frame.add(new Board()); //Add the board where the character and background are
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Applet will close when exited
      frame.setSize(1000,600); //Set size of background based on image
      frame.setVisible(true);
      
      

   }
}


Top
  
 
PostPosted: Sun Feb 05, 2012 6:53 pm 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
'setx' and 'sety' return 'void' in your Monster, which you cannot use as parameter values.

Some other stuff I spotted:

In ChooseDir, wouldn't you be able to do "return z.nextInt(3) - 1';" ? The order of the return values don't matter I think.
Also if you create a Random() in the constructor, you don't need to make a new one every call.

In move, you may want to check some borders to make the monster not run off too far.

In run, If you look at how you use 'rest', you will notice that in the first iteration you do the first part and in the second iteration you do the second part, then the first part again, etc. In other words, just put both pieces underneath each other :)
Something similar is also happening with 'set' (which is a siub-optimal name btw as it does not express well what it sets.

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Sun Feb 05, 2012 7:09 pm 
Thanks Alberth! I figured out how to solve my issue with getting the x values and y values of my initial location for my monster. Haha, I guess I overlooked that it was void =)

In ChooseDir that was what I initially tried but if I were to use that, it wouldnt incorporate moving left. Seeing as it would only give the x and y movements a change from 0 to 2.

Yea, I'm going to look into implementing the borders so that the monster does not go off the screen but, right now I'm just focused on getting him on the screen and moving on his own.

But the problem still stands that I'm having errors with my constructor:

Code:
public class Board* extends JPanel implements ActionListener, Runnable{
Link a; //Link character
static Monster b;
Image back; //background
Timer time;
static Monster AI = Monster*(b);
Thread t1 = new Thread(AI);
   public Board(){
      a = new Link();
      setFocusable(true);// Allows for the movement of the character
      ImageIcon i = new ImageIcon("H:/Suncoast/Senior Project/Images/Original.jpg");
      back = i.getImage();
      time = new Timer(5,this);
      time.start(); //Related to the action performance
      this.addKeyListener(a);
      t1.start();
   }


The exact area of my errors have an asterisk next to them.

But once again, thank you for your help so far Alberth!


Top
  
 
PostPosted: Sun Feb 05, 2012 9:01 pm 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
Mangomunch wrote:
In ChooseDir that was what I initially tried but if I were to use that, it wouldnt incorporate moving left. Seeing as it would only give the x and y movements a change from 0 to 2.
Hence my additional "- 1" :p

Mangomunch wrote:
But the problem still stands that I'm having errors with my constructor:

Code:
static Monster AI = Monster*(b);
Ah, I think I see it, are you missing a "new".
I also make that error very often, I programmed too much Python :)

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Sun Feb 12, 2012 3:05 am 
a couple points:
> void methods can have parameters
> the constructor of the Monster class needs a "monster b" parameter which means in order to create a monster, you already need to have a defined monster to create another monster
> you used a "static monster b" as your parameter which you haven't initialized, so it isn't the constructor that's undefined, but the parameter you put into your monster AI.
> anyways it doesn't look like you used the "monster b" in the constructor method, so i don't see the point of having it in there yet


Top
  
 
PostPosted: Sun Feb 12, 2012 3:11 am 
after rereading the above posts i realized that i misread Alberth's post, but he's right in the fact that void doesn't return values
:doh sorry


Top
  
 
PostPosted: Sun Feb 12, 2012 9:38 am 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
Yeah, the constructors are weird.

What worries me much more however is that the OP is using threads without any protection against concurrent read/write access.

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group