This site requires JavaScript, please enable it in your browser!
Greenfoot back
Blakehammond
Blakehammond wrote ...

2013/3/12

Need help with programming the AI

1
2
Game/maniac Game/maniac

2013/3/12

#
Do this for the drone:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Drone here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Drone extends Actor
{
    /**
     * Act - do whatever the Drone wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if(Greenfoot.getRandomNumber(5)==0)   
        {   
            setLocation(getX()+5, getY()); 
        }
    }   
}
Do this for the Dom:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Dom here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Dom extends Mover
{
  private int speed=5;
    
    public Dom()
    {
    }
    public void act()
    {
        checkKeys();
        Actor actor = getOneIntersectingObject(Dom.class); 
        if(actor != null
        
            getWorld().removeObject(this); 
        }
    }   
    private void checkKeys(){
 
        if(Greenfoot.isKeyDown("left")){
            moveLeft();
        }
 
        if(Greenfoot.isKeyDown("right")){
            moveRight();
        }
     
        if(Greenfoot.isKeyDown("up")){
            moveUp();
        }
 
        if(Greenfoot.isKeyDown("down")){
            moveDown();
        }   
    }
    public void moveRight(){
        setLocation(getX()+speed,getY());
    }
    public void moveLeft(){
        setLocation(getX()-speed,getY());
    }
    public void moveUp(){
        setLocation(getX(),getY() -speed);
    }
    public void moveDown(){
        setLocation(getX(),getY() +speed);
    }
}
Blakehammond Blakehammond

2013/3/12

#
Thank you so much for your help, putting it in now
FairerFish101 FairerFish101

2014/5/29

#
Hey, I'm doing a game where you need to race other AI on a track but I don't know the code for the AI to move around the track without hitting the walls on purpose. Can anyone help?
elias.groll elias.groll

2014/5/29

#
Easy configurable method for random movement:
1
2
3
4
5
6
7
8
9
public void moveRandomly(){
      move(7);
      if (Greenfoot.getRandomNumber(100) < 10){
          turn(Greenfoot.getRandomNumber(90) -45);
      }
      if (getY() < 20 || getY() > 378 || getX() > 590 || getX() < 20){
          turn(180);
      }
  }
For the killing method u could just use the removeTouching(urProtagonist.class); method or write a more complicated, but more interesting function. U could do a collisioncheck inside ur protagonist too. Here is an example:
1
2
3
4
5
6
7
8
public boolean collision(){
    if(whateverUrConditionsAre || this.isTouching(Enemy.class)){
        return true;
    }
    else{
        return false;
    }
}
and then let ur protagonist call a die-function in wich he dies dramatically :P
elias.groll elias.groll

2014/5/29

#
@FairerFish: Make a new thread..:) BTW: you have to program a pathfinding algo like the A*algo( can be found at Wikipedia) or let the AI follow a Array of positions u set up. Both ways are not too easy to implement i know.
You need to login to post a reply.
1
2