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

2016/2/29

I need help with four things. Newbie here. URGENT :(

starworld31 starworld31

2016/2/29

#
I'm making a tower defense game and I am still on the first level here are a few things that I would like to know as well as my inquiries regarding the games and my code. - Since our game works in levels how do I add a stage clear option wherein the random number of enemies STOPS and the game notices that the stage has been cleared and moves on to the next map along with spawning the 2 more characters and more enemies. - I want to add a random spawn for monsters moving from the right side of the screen to the castle gate: how do I make an AI system that makes the enemy spawn and attack the castle's health? (The castle is part of the background though) -How do I also make a bullet delay so that the bullet doesn't shoot like railgun but almost like a regular pistol with a delay of 3 secs and how the heck do I make the bullet come out of the gun and not the guy's mouth -How do I also put a "Game Start" option as well as add a character instantaneously after the word "LEVEL 1" shows up much like the supermario games or pacman. Attached are my codes and picture of the program so far here is the Infantry code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Infantry here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Infantry extends Actor
{
    public Infantry()
    {    
        GreenfootImage image = getImage();  
        image.scale(150, 150);
        setImage(image);

    }
    
    private double lastShot;
    public void act()
    {
       MouseInfo m = Greenfoot.getMouseInfo();
        
        if(m != null) {
            int mouseX = m.getX();
            int mouseY = m.getY();
            turnTowards(mouseX, mouseY);
        }
        bulletfeed();
        checkFire();
            //leave the rest of your player's act method alone
    }

    public void checkFire()
   {
    if(Greenfoot.isKeyDown("space")) 
    {
       Bullet b = new Bullet(getRotation());
      getWorld().addObject(b, getX(), getY());
      b.setRotation(getRotation());
      b.move(5);

    }
   }
   
    public void bulletfeed()
    { 
         if (Greenfoot.isKeyDown("space") && System.currentTimeMillis()>lastShot+3000)  
        {  
            getWorld().addObject(new Bullet(getRotation()), getX(), getY());  
            lastShot = System.currentTimeMillis();
        }   
    
    }
}
Here is for the bullet
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    public Bullet()
    {    
        GreenfootImage image = getImage();  
        image.scale(image.getWidth() - 50, image.getHeight() - 50);
        setImage(image);
    }
    
    private int direction,speed; 
    public Bullet(int dir)
    {
        direction = dir;
        speed = 1;
    }
 
 
    //int time=10;
    
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    private double lastShot;
    public void act()
    {
        setRotation(direction);
        move(speed);
        move(3);
        
         if (Greenfoot.isKeyDown("space") && System.currentTimeMillis()>lastShot+3000)  
        {  
            getWorld().addObject(new Bullet(getRotation()), getX(), getY());  
            lastShot = System.currentTimeMillis();
        }
     
        /*destroyEnemies ();
        move(9);
        kill();
       */
        if (this.atWorldEdge()) 
        {
            getWorld().removeObject(this);
        }  
        }
   public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy. 
       Actor enemy = getOneIntersectingObject(Enemy.class);
       if(enemy != null) {
            getWorld().removeObject(enemy);
            getWorld().removeObject(this);
       }
   }
 
}
I have no code for the enemy yet. here is what it looks like when I run the game
danpost danpost

2016/2/29

#
One thing that bothers me is that you are creating bullets from within the bullet class. The behavior of a bullet is just to move until either an enemy or the edge of the world is arrived at and to be removed from the world. The constructor can do exactly what you already have it doing -- preparing its image. The actors that shoot the bullets should have the code to create and set the rotations, speeds and initial positions of the new bullets. A int field can be used in the classes of the actors that shoot to control the rate of spawning of the bullets.
davmac davmac

2016/2/29

#
starworld31 wrote...
here is what it looks like when I run the game
You posted a link to a web page, not to the image it contained. The correct link is:
starworld31 starworld31

2016/3/6

#
That's the wrong code sorry here is what my bullet class contains
 */
public class Bullet extends Actor
{
    public Bullet()
    {    
        GreenfootImage image = getImage();  
        image.scale(image.getWidth() - 50, image.getHeight() - 50);
        setImage(image);
    }
    
    private int direction,speed; 
    public Bullet(int dir)
    {
        direction = dir;
        speed = 1;
    }
 
 
    int time=10;
    
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
  
    public void act()
    {
        setRotation(direction);
        move(speed);
        move(5);
     
        /*destroyEnemies ();
        move(9);
        kill();
        */
        if (this.atWorldEdge()) 
        {
            getWorld().removeObject(this);
        }  
        }
   public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy. 
       Actor enemy = getOneIntersectingObject(Enemy.class);
       if(enemy != null) {
            getWorld().removeObject(enemy);
            getWorld().removeObject(this);
       }
   }
 
}
@davmac Thank you for assissting me by showing the picture! @danpost I'm just looking for codes by typing it on google so I don't really know how these codes are meant to work. My programming language is highly inadequate. Still the 4 questions stand so can you help me :( and can you put a code that would help me in any way?
You need to login to post a reply.