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

2014/11/22

School project- create a whack a 'pig' game

Korina Korina

2014/11/22

#
Hi! We used the sample whack a mole here in greenfoot as a guide for our coding. How do we change the cell size for the world? Cause we intend to only put the actors on half of the world so how are we gonna do it??
danpost danpost

2014/11/22

#
If you used the 'basic whack a mole' by ShondelxA, WhackAMole, then you can change (increase) one of the parameters (length or width) of the world 'super' constructor call. The game will still play in the initial 3 by 3 area to the top and left of the window. You can paint over the area of the background image that is not used for the game using 'setColor' and 'fillRect'.
Korina Korina

2014/11/23

#
Yes that's the game we are referring to! Another question, Is there like a code for nullyfying some area in the cells?? Cause instead of 3x3 we made 10x10 with a cell size 60x60 pixels. We want the pigs to only appear in cell 6-10. The codes written for random mole to appear is this: xRand = rand.nextInt(10); yRand = rand.nextInt(10); setLocation(xRand, yRand);
danpost danpost

2014/11/23

#
Did you try:
xRand = 5+rand.nextInt(5);
yRand = 5+rand.nextInt(5):
setLocation(xRand, yRand);
Korina Korina

2014/11/26

#
Lovely! thanks that really helped :)) But is there anyway instead of making it random we will set the location where the actors will appear?
danpost danpost

2014/11/26

#
What do you mean by 'we will set the location where the actors will appear'? What criteria will you use in placing them? How are these locations to be brought about for placing them? (will you have fields holding those locations or will the location be entered during gameplay, which I seriously doubt or will they be programmatically calculated). Are we still discussing a whack-a-pig game? I ask this because it would seem logical that the pigs should show up in random places.
Korina Korina

2014/11/26

#
Yes still whac-apig. What criteria? I am so sorry I am not very familiar into coding. For example this world background -> *Sorry I couldn't find the image background that we used. Using a 10x10, 60 pixels world size. How can I set the location that the pigs will only appear in the pond side?
danpost danpost

2014/11/26

#
You could make an array of starting points along each line along either the horizontal or the vertical (in the picture above, the horizontal might be easier as less lines of cells will be needed horizontally). You can still use random x and y as shown in previous posts while applying the starting points.
int[] starters = { 3, 2, 1 }; // for y-values of 7, 8, and 9, respectively
randY = 7+Greenfoot.getRandomNumber(3);
randX = starters[randY-7]+Greenfoot.getRandomNumber(9-starters[randY-7]);
This is just one way to limit where they will be placed.
Korina Korina

2014/11/29

#
We had a hard time setting the location for our actors so we did what you recommended us in the first place. But I do have another question, how do we put bombs in the game?
danpost danpost

2014/11/29

#
Korina wrote...
But I do have another question, how do we put bombs in the game?
That is such a wide open question. Please describe how you want to have then entered into the game in general (not in a code context).
Korina Korina

2014/11/29

#
Well instead of just pigs appearing randomly there'll be bombs too. But it will seldom appear in the world.
danpost danpost

2014/11/29

#
Korina wrote...
Well instead of just pigs appearing randomly there'll be bombs too. But it will seldom appear in the world.
It should be pretty much the same way as you add pigs into the world. If help is required, then post the code. Use the 'code' tag below the reply box to insert code into you posts.
Korina Korina

2014/12/2

#
Yes but the bombs and the pigs are over lapping :( Here is the code for RandomMole:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)


import java.util.Random;

public class RandomMole extends Actors
{

    private Random rand = new Random();
    private int xRand, yRand;
    private int timer;
    private GreenfootImage[] images;
    private int noOfImages = 2;
    private int currentImage = 0;
    private int moleTimer=103;
    private int moles;
    private int maxMoles = 8;
    private int bombs;
    
    /**
     * Constructor - creates the timer and makes the mole 
     * animated.
     */
    public RandomMole()
    {

        timer=0;
        images = new GreenfootImage[noOfImages];
        for(int i=0; i < noOfImages; i++) 
        {
            images[i] = new GreenfootImage("pinky" + i + ".png");
        }
        setImage(images[currentImage]);
           
    }
    
    /**
     * Act - do whatever the RandomMole wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        super.act();
        currentImage = (currentImage + 1) % images.length;
        setImage(images[currentImage]);
        
        addMole();
        bonkIfHit();
        addBomb
        smokeIfHit();
    }
    
    /**
     * Moves the mole randomly.
     */
    public void move()
    {
        
        if(timer<2)
        {
            timer++;
            return;
        }
        else
        {
            timer=0;
        }
        
        xRand = rand.nextInt(3);
        yRand = rand.nextInt(3);
        
        if(!canMove())
        {
            return;
        }
        
        setLocation(xRand, yRand);
        
    }
    
    /**
     * Adds moles to the world randomlly in random places.
     */
    public void addMole()
    {
        moles = getWorld().numberOfObjects();
        
        if(moles < maxMoles) 
        {
            if(Greenfoot.getRandomNumber(150) < 65) 
            {            
                getWorld().addObject(new RandomMole(), Greenfoot.getRandomNumber(2), Greenfoot.getRandomNumber(2));
            }
        }
        else
        {
            return;
        }
    }
    

    public void bonkIfHit()
    {
        if(this.getOneIntersectingObject(Hammer.class) != null)
        {
            setImage("ouch.png");
        }
    }
    
  
    
}
Some of the codes I don't understand the functions.
danpost danpost

2014/12/2

#
A RandomMole object is not the cause of another one appearing. In other words the 'addMole' method should not be in this class; it should probably be in your World subclass act method. The same with 'addBomb'. You can always check the location before putting an actor (a mole or a bomb) there using the 'getObjectsAt(x, y, null).isEmpty()' method call so there is no overlapping.
danpost danpost

2014/12/2

#
Korina wrote...
Some of the codes I don't understand the functions.
Do you know how to use the class documentation pages? Each class describes, in detail, what each member (field, constructor, or method) does. You can use it to trace from one class to another to find what you need or learn.
You need to login to post a reply.