Hello,
I'm re-creating the Atari Centipede game. I'm adding my own powerups, like a laser that gives 2x points and also a power laser, which can destroy mushrooms in just one hit. Both powerups are intended to work on mushrooms, for now. But when I added them, they didn't seem to work accordingly. I'm not sure why, can someone please help me? Thank you.
Here's the code for the Mushroom class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
public class Mushroom extends MultiHittable
{
//public static ArrayList <Particle> explos = new ArrayList(10);
//public static List <Particle> explos;
public Mushroom(){
getImage().scale(MyWorld.GRIDSIZE, MyWorld.GRIDSIZE);
}
public void goToGrid(){
int gs = MyWorld.GRIDSIZE;
int gx = getX() / gs * gs + gs/2;
int gy = getY() / gs * gs + gs/2;
setLocation(gx, gy);
}
public void act(){
if (isTouching(Laser.class)){
removeTouching(Laser.class);
onLaserHit();
return;
}
if (isTouching(PointsLaser.class)){
removeTouching(PointsLaser.class);
onPointsLaserHit();
}
if (isTouching(PowerLaser.class)){
removeTouching(PowerLaser.class);
onPowerLaserHit();
}
}
public void onLaserHit(){
super.onHit();
MyWorld d = (MyWorld) getWorld();
Score scTxt = getWorld().getObjects(Score.class).get(0);
if (getHits() < 4){
getImage().setTransparency(getImage().getTransparency() - getImage().getTransparency()/4);
d.setScore(d.getScore() + d.getPPM());
scTxt.setText("Score: " + d.getScore());
} else {
d.setScore(d.getScore() + d.getPPM() * 4);
scTxt.setText("Score: " + d.getScore());
Particle p = new Particle();
getWorld().addObject(p, getX(), getY());
destroy();
}
}
public void onPointsLaserHit(){
super.onHit();
MyWorld d = (MyWorld) getWorld();
Score scTxt = getWorld().getObjects(Score.class).get(0);
if (getHits() < 4){
getImage().setTransparency(getImage().getTransparency() - getImage().getTransparency()/4);
d.setScore(d.getScore() + d.getPPM() * 2);
scTxt.setText("Score: " + d.getScore());
} else {
d.setScore(d.getScore() + d.getPPM() * 8);
scTxt.setText("Score: " + d.getScore());
Particle p = new Particle();
getWorld().addObject(p, getX(), getY());
destroy();
}
}
public void onPowerLaserHit(){
//super.onHit();
MyWorld s = (MyWorld)getWorld();
Score scTxt = getWorld().getObjects(Score.class).get(0);
//if (getHits() == 1){
s.setScore(s.getScore() + s.getPPM() * 3);
scTxt.setText("Score: " + s.getScore());
destroy();
//}
}
public void destroy(){
MyWorld h = (MyWorld)getWorld();
h.setDestroyed(h.getDestroyed() + 1);
getWorld().removeObject(this);
}
}
And here's the MyWorld class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
//import java.lang.String;
/**
* Write a description of class MyWorld here.
*
* @author (Aditya Bakshi)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
public static final int GRIDSIZE = 20;
public int yPos = getHeight() * 4/5;
private int numDestroyed;
private State state;
private boolean pDown = false;
private boolean isPaused = false;
private boolean isGameOver = false;
private int numLives = 3;
private int points;
private int level = 1;
private int multiplier = 50;
private boolean hasPointPowerup = false;
private boolean hasPowerUp = false;
private int ppC = 100;
private int ppF = 200;
private int ppM = 1;
private int ppS = 500;
private int ppSp = 200;
//private boolean has
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(400, 600, 1, false);
setState(new TitleState(this));
points = 0;
numDestroyed = 0;
}
public void setState(State s){
if (state != null){
state.onRemove();
}
state = s;
s.onSet();
}
public void act(){
state.onAct();
if (Greenfoot.isKeyDown("p")){
//pDown = true;
//Greenfoot.stop();
isPaused = true;
}
if (Greenfoot.isKeyDown("escape")){
// pDown = false;
//Greenfoot.start();
isPaused = false;
}
}
public boolean getPaused(){
return isPaused;
}
public void setPaused(boolean v){
isPaused = v;
}
public boolean getGameOver(){
return isGameOver;
}
public void setGameOver(boolean b){
isGameOver = b;
}
public int getDestroyed(){
return numDestroyed;
}
public void setDestroyed(int val){
numDestroyed = val;
}
public int getScore(){
return points;
}
public void setScore(int num){
points = num;
}
public int getLvl(){
return level;
}
public void setLvl(int m){
level = m;
}
public int getMulti(){
return multiplier;
}
public void setMulti(int q){
multiplier = q;
}
public void setPointPowerup(boolean m){
hasPointPowerup = m;
}
public boolean getPointPowerup(){
return hasPointPowerup;
}
public boolean hasPower(){
return hasPowerUp;
}
public void setPower(boolean bool){
hasPowerUp = bool;
}
public int getPPC(){
return ppC;
}
public void setPPC(int well){
ppC = well;
}
public int getPPF(){
return ppF;
}
public void setPPF(int nice){
ppC = nice;
}
public int getPPM(){
return ppM;
}
public void setPPM(int good){
ppM = good;
}
public int getPPS(){
return ppS;
}
public void setPPS(int jod){
ppS = jod;
}
public int getPPSP(){
return ppSp;
}
public void setPPSP(int will){
ppSp = will;
}
}