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!