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

2014/7/15

Ai shooting in my game

Y0ger Y0ger

2014/7/15

#
Hey guys. I am pretty new to coding so I may not have that great of code. But I was planing to make a game where you have to defend yourself from enemy ships attacking back at you. It is kind of like space invaders. My problem is that I cannot make my enemy spaceship shoot back at the user. (the "badguy" is the enemy spaceship I trying to program to shoot towards the bottom of the screen.) Here is my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class laserv3bad here. * * @author (your name) * @version (a version number or a date) */ public class laserv3bad extends Actor { /** * Act - do whatever the laserv3bad wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setRotation(270); setLocation(getX(), getY()-10); disappear(); } public void disappear() { Actor SpaceShip; SpaceShip = getOneObjectAtOffset(0, 0, SpaceShip.class); if (badguy != null) { World world; world = getWorld(); world.removeObject(SpaceShip); world.removeObject(this); } } }
danpost danpost

2014/7/15

#
I am kind of confused. This looks like the class of the projectile that the user shoots at the bad guys (enemy spaceships). For that, it looks ok, except for the 'if' statement in the 'disappear' method (which should probably be: 'if (SpaceShip != null)'. You may want to change the name to 'Actor spaceShip' instead of 'Actor SpaceShip' so as not to confuse the compiler (as 'SpaceShip' is one of your class names). You need to post the code for the class of the projectile that the enemy ships will shoot at the user's ship. Use the 'code' tag below the 'Post a reply' input box to insert your code into the post.
Y0ger Y0ger

2014/7/15

#
I am pretty sure in my other post that I posted my correct code for the projectile that the enemy ships will shoot. Before I made it so then the users is able to shoot by pressing space, but i used a different object for that. I kind of copied the code that I wrote, which enabled the user's ship to shoot, into the code for the ai controlled laser. So there are some mistakes. Anyway the first code that I posted was the code you asked for, but just filled with mistakes... Can you help me walk through it?
davmac davmac

2014/7/15

#
If the problem is that the projectile is moving up and you want it to move down, you need to change this line:
1
setLocation(getX(), getY()-10);
You are subtracting 10 from the Y coordinate, which moves up. You need to add 10 instead. If that's not the problem, please be more specific about what you need. Please also use 'code' tags when you post your code (as I have). Otherwise the code is harder to read, and more difficult to copy and paste; it is also harder to identify parts of the code by line number.
Y0ger Y0ger

2014/7/15

#
Sorry for all this madness. I will start using the code option. What I am aiming to fix is for my enemy AI space ship to fire back at the user. Here is the code for the laser that the ai will firing.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class laserv3bad here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class laserv3bad extends Actor
{
    /**
     * Act - do whatever the laserv3bad wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setRotation(270);
        setLocation(getX(), getY()10);
        disappear();
    }   
     
    public void  disappear()
    {
        Actor spaceShip;
        Actor badguy;
        spaceShip = getOneObjectAtOffset(0, 0, SpaceShip.class);
         
        if (badguy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(spaceShip);
            world.removeObject(this);
}
}
}
Y0ger Y0ger

2014/7/15

#
To be more clear, what I am aiming to do is to make the enemy ai able to shoot lasers back at the user and killing the user on hit. This is the code for the AI spaceship that will be shooting the lasers. Once again, sorry for all the confusion. I am fairly new to Java and I am not the best at it.
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
    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class badguy here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class badguy extends Actor
{
    /**
     * Act - do whatever the badguy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Actor SpaceShip;
        SpaceShip = getOneObjectAtOffset(0, 0, SpaceShip.class);
        move (5);
       if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
        {
            turn(180);
        }
        if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
        {
            turn(180);
        }
            if (SpaceShip != null)
            // this has nothing to do with shooting. This code was in case the user would collide into the enemy. If this happens, the user's spaceship is removed.
    {
        World world;
        world = getWorld();
        world.removeObject(SpaceShip);
    }
     
    }  
     
     
 
}
danpost danpost

2014/7/15

#
The laserv3bad class needs to be adjusted to accommodate movement in different directions. This can be easily accomplished by using the 'move(int)' method instead of the 'setLocation(int, int)' method for movement since the rotation will already be appropriately set. There are several ways to inform a new laserv3bad object what amount of rotation to take on. The angle of rotation can be passed to the object as the value of a parameter in the constructor call; or, the rotation can be set by the creator after creating it by first getting a local reference to the laserv3bad object; or, well, those are the two usually methods that are used. Then -- the badguy object needs to be able to create laserv3bad objects. But, it must not be allowed to create one every act cycle. The production of laserv3bad objects must be regulated by placing conditions on when one is spawned by using an 'if' statement. The spawning can be done on a regular basis at random times. The choice is yours.
Y0ger Y0ger

2014/7/15

#
Thanks a lot! It works.
You need to login to post a reply.