I am experiencing a glitch with my zombie fighter game where the zombies will be affected by the player's knockback. Here's my player code:
Here's my zombie code:
Does anyone know what I'm doing wrong?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
public static final int DEFAULT_SPEED = 2;
public int walkProgress = 10;
public boolean isWalking = false;
public int speed = DEFAULT_SPEED;
public int swordTimer = 0;
public boolean backSwing = false;
public int spinProgress = 0;
public int spinTimer = 0;
public int gunTimer = 0;
public int knifeTimer = 0;
public int joustTimer = 0;
public int lives = 10;
public int invincibilityTimer = 0;
public int effectTimer = 0;
public int knockbackLeft = 0;
public int knockbackSpeed = 0;
public int combo = 0;
public int comboTimer = 0;
public int currentWeapon = 1;
public int currentItem = 1;
public String leftKey = "";
public String rightKey = "";
public String upKey = "";
public String downKey = "";
public String weaponKey = "";
public String specialKey = "";
public String itemKey = "";
public Color defaultColourLight = new Color(140, 157, 255);
public Color defaultColour = new Color(0, 38, 255);
public Color defaultColourDark = new Color(0, 19, 130);
public Color nextColourLight = new Color(255, 140, 140);
public Color nextColour = new Color(255, 0, 0);
public Color nextColourDark = new Color(127, 0, 0);
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Board board = (Board)getWorld();
if (board.playing)
{
if (lives > 0)
{
if (knockbackLeft <= 0)
{
moveAround();
knockbackSpeed = 0;
}
else
{
if (invincibilityTimer > 0)
{
setImage("player_blue_walk" + ((walkProgress / 10) - 1) + "_hurt.png");
}
turn(180);
move(knockbackSpeed);
knockbackLeft -= knockbackSpeed;
turn(-180);
}
useWeapons();
// setColour(defaultColour, defaultColourDark, nextColour, nextColourDark);
}
else
{
}
if (lives > 0)
{
if (lives > 10)
{
lives = 10;
}
if (invincibilityTimer > 0)
{
invincibilityTimer--;
}
if (effectTimer > 0)
{
effectTimer--;
}
if (comboTimer > 0)
{
comboTimer--;
}
else
{
combo = 0;
}
updateArms();
}
else
{
if (currentItem == 1)
{
getWorld().addObject(new Medpack(2, getRotation(), 30, false), getX(), getY());
}
else if (currentItem == 2)
{
getWorld().addObject(new Bomb(2, getRotation(), 50, false), getX(), getY());
}
die();
}
}
}
public void moveAround()
{
if (joustTimer > 40)
{
speed *= 3;
if (effectTimer <= 0)
{
getWorld().addObject(new Placeholder(getRotation(), 1, getImage(), 150), getX(), getY());
getWorld().addObject(new Placeholder(getRotation(), 1, getArms().getImage(), 150), getX(), getY());
effectTimer = 3;
}
}
if (Greenfoot.isKeyDown(leftKey))
{
setRotation(180);
move(speed);
}
if (Greenfoot.isKeyDown(rightKey))
{
setRotation(0);
move(speed);
}
if (Greenfoot.isKeyDown(upKey))
{
setRotation(270);
move(speed);
}
if (Greenfoot.isKeyDown(downKey))
{
setRotation(90);
move(speed);
}
if (Greenfoot.isKeyDown(downKey) && Greenfoot.isKeyDown(rightKey))
{
setRotation(45);
}
if (Greenfoot.isKeyDown(downKey) && Greenfoot.isKeyDown(leftKey))
{
setRotation(135);
}
if (Greenfoot.isKeyDown(upKey) && Greenfoot.isKeyDown(leftKey))
{
setRotation(225);
}
if (Greenfoot.isKeyDown(upKey) && Greenfoot.isKeyDown(rightKey))
{
setRotation(315);
}
if (!Greenfoot.isKeyDown(leftKey) && !Greenfoot.isKeyDown(rightKey) && !Greenfoot.isKeyDown(upKey) && !Greenfoot.isKeyDown(downKey))
{
isWalking = false;
}
else
{
isWalking = true;
}
speed = DEFAULT_SPEED;
if (!isWalking)
{
walkProgress = 20;
if (invincibilityTimer > 0)
{
setImage("player_blue_hurt.png");
}
else
{
setImage("player_blue.png");
}
}
else
{
walkProgress += speed / 2;
if (invincibilityTimer > 0)
{
setImage("player_blue_walk" + ((walkProgress / 10) - 1) + "_hurt.png");
}
else
{
setImage("player_blue_walk" + ((walkProgress / 10) - 1) + ".png");
}
}
if (walkProgress >= 45)
{
walkProgress = 10;
}
World world = getWorld();
Board board = (Board)world;
board.playerBlueX = getX();
board.playerBlueY = getY();
}
public void useWeapons()
{
if (Greenfoot.isKeyDown(weaponKey))
{
if (currentWeapon == 1 && swordTimer <= 0)
{
//getWorld().addObject(new Sword(this, getRotation(), backSwing, false), getX(), getY());
if (backSwing)
{
backSwing = false;
}
else
{
backSwing = true;
}
swordTimer = 20;
}
else if (currentWeapon == 2 && gunTimer <= 0)
{
getWorld().addObject(new Bullet(0, 4, 20, 10, 8, 600, getArms()), getX(), getY());
gunTimer = 5;
}
else if (currentWeapon == 3 && gunTimer <= 0)
{
for (int i = 0; i < 7; i++)
{
getWorld().addObject(new Bullet(1, 5, 75, 8, 30, 25, getArms()), getX(), getY());
}
gunTimer = 50;
knockbackLeft = 30;
knockbackSpeed = 3;
}
else if (currentWeapon == 4 && gunTimer <= 0)
{
getWorld().addObject(new Grenade(6, getRotation(), getArms()), getX(), getY());
gunTimer = 50;
}
else if (currentWeapon == 5 && gunTimer <= 0)
{
for (int k = 0; k < 3; k++)
{
Bullet bullet = new Bullet(2, 5, 10, 10, 1, 600, getArms());
getWorld().addObject(bullet, getX(), getY());
bullet.setRotation(getRotation() - 30 + k * 30);
}
gunTimer = 40;
}
else if (currentWeapon == 6 && isWalking && joustTimer <= 0)
{
joustTimer = 80;
}
}
if (Greenfoot.isKeyDown(specialKey))
{
if (currentWeapon == 1 && swordTimer <= 0 && spinTimer <= 0)
{
//getWorld().addObject(new Sword(this, getRotation(), false, true), getX(), getY());
backSwing = false;
swordTimer = 50;
spinTimer = 620;
spinProgress = getRotation();
// getWorld().addObject(new SpinAttackBar(this, spinTimer), getX(), getY());
}
else if (currentWeapon == 2 && spinTimer <= 0)
{
gunTimer = 30;
spinTimer = 300;
spinProgress = getRotation();
}
else if (currentWeapon == 5 && knifeTimer <= 0)
{
for (int k = 0; k < 5; k++)
{
Bullet bullet = new Bullet(3, 10, 10, 10, 1, 600, getArms());
getWorld().addObject(bullet, getX(), getY());
bullet.setRotation(getRotation() - 30 + k * 15);
}
knifeTimer = 300;
}
}
if (Greenfoot.isKeyDown(itemKey))
{
if (currentItem == 1)
{
getWorld().addObject(new Medpack(4, getRotation(), 30, true), getX(), getY());
currentItem = 0;
}
else if (currentItem == 2)
{
getWorld().addObject(new Bomb(4, getRotation(), 50, true), getX(), getY());
currentItem = 0;
}
}
if (currentWeapon == 1 && spinTimer > 584)
{
spinProgress += 30;
setRotation(spinProgress);
spinUpdate();
}
if (currentWeapon == 2 && spinTimer > 282)
{
spinProgress += 20;
setRotation(spinProgress);
spinUpdate();
getWorld().addObject(new Bullet(0, 10, 20, 10, 8, 600, getArms()), getX(), getY());
}
/*
if (Greenfoot.isKeyDown("shift") && shieldTimer <= 0)
{
shieldTimer = 2;
swordTimer = 1;
getWorld().addObject(new Shield(this, getRotation(), shieldTimer), getX(), getY());
}
*/
if (swordTimer > 0)
{
swordTimer--;
}
if (spinTimer > 0)
{
spinTimer--;
}
if (gunTimer > 0)
{
gunTimer--;
}
if (knifeTimer > 0)
{
knifeTimer--;
}
if (joustTimer > 0)
{
joustTimer--;
}
}
public Player_Arms getArms()
{
return null;
}
public void updateArms()
{
}
public void spinUpdate()
{
}
public void die()
{
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Zombie here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Zombie extends Actor
{
public int walkProgress = 10;
public int speed = 1;
public int lives = 10;
public int invincibilityTimer = 0;
public int knockbackLeft = 0;
public int knockbackSpeed = 0;
public boolean droppedBomb = false;
public int lungeRange = 50;
public boolean grenade = false;
public int grenadeTimer = 0;
public Player_Arms arms;
private static final GreenfootImage[] walkingFrames = {
new GreenfootImage("zombie_walk0.png"),
new GreenfootImage("zombie_walk1.png"),
new GreenfootImage("zombie_walk2.png"),
new GreenfootImage("zombie_walk3.png"),
new GreenfootImage("zombie_walk0_hurt.png"),
new GreenfootImage("zombie_walk1_hurt.png"),
new GreenfootImage("zombie_walk2_hurt.png"),
new GreenfootImage("zombie_walk3_hurt.png"),
new GreenfootImage("zombie_scratch0.png"),
new GreenfootImage("zombie_scratch1.png"),
new GreenfootImage("zombie_scratch2.png"),
new GreenfootImage("zombie_scratch3.png"),
};
/**
* Act - do whatever the Zombie wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Board board = (Board)getWorld();
if (board.playing)
{
if (lives > 0)
{
if (!grenade)
{
arms = null;
}
if (knockbackLeft <= 0)
{
moveAround();
}
else
{
setImage(walkingFrames[(walkProgress / 10) - 1 + 4]);
turn(180);
move(knockbackSpeed);
knockbackLeft -= knockbackSpeed;
turn(-180);
}
hurtPlayers();
if (invincibilityTimer > 0)
{
invincibilityTimer--;
}
if (grenadeTimer > 0)
{
grenadeTimer--;
}
else if (grenade)
{
board.addObject(new ExplosionSmall(false), getX(), getY());
for (int s = 0; s < 20; s++)
{
board.addObject(new Bullet(1, 10, 75, 8, 180, 20, arms), getX(), getY());
}
arms.player.combo += 1;
arms.player.comboTimer = 60;
if (arms.comboDisplayTimer <= 0)
{
board.addObject(new ComboDisplay(arms.player), arms.player.getX(), arms.player.getY());
arms.comboDisplayTimer = 10;
}
lives = 0;
}
else
{
arms = null;
}
}
else
{
arms = null;
int weaponDrop = Greenfoot.getRandomNumber(6) + 1;
while (weaponDrop == board.playerBlueWeapon && weaponDrop == board.playerRedWeapon)
{
weaponDrop = Greenfoot.getRandomNumber(4) + 1;
}
if (Greenfoot.getRandomNumber(20) < 1 && !board.weaponsInWorld())
{
board.addObject(new Weapon(weaponDrop), getX(), getY());
}
else if (Greenfoot.getRandomNumber(100) < 1)
{
board.addObject(new Medpack(0, 0, 0, false), getX(), getY());
}
else if (Greenfoot.getRandomNumber(1000) < 5)
{
board.addObject(new Bomb(0, 0, 0, false), getX(), getY());
}
board.removeObject(this);
}
}
}
public void moveAround()
{
Board board = (Board)getWorld();
int playerBlueDistance = (int)Math.sqrt((getX() - board.playerBlueX)*(getX() - board.playerBlueX)+(getY() - board.playerBlueY)*(getY() - board.playerBlueY));
int playerRedDistance = (int)Math.sqrt((getX() - board.playerRedX)*(getX() - board.playerRedX)+(getY() - board.playerRedY)*(getY() - board.playerRedY));
if (board.playerBlueAlive && board.playerRedAlive)
{
if (playerRedDistance < playerBlueDistance)
{
if (!grenade)
{
turnTowards(board.playerRedX, board.playerRedY);
move(speed);
}
walkProgress += speed;
}
else
{
if (!grenade)
{
turnTowards(board.playerBlueX, board.playerBlueY);
move(speed);
}
walkProgress += speed;
}
if (playerRedDistance < lungeRange || playerBlueDistance < lungeRange)
{
if (!grenade)
{
move(speed);
move(speed);
walkProgress += speed;
}
}
}
else if (board.playerBlueAlive && !board.playerRedAlive)
{
if (!grenade)
{
turnTowards(board.playerBlueX, board.playerBlueY);
move(speed);
}
walkProgress += speed;
if (playerBlueDistance < lungeRange)
{
if (!grenade)
{
move(speed);
move(speed);
walkProgress += speed;
}
}
}
else if (!board.playerBlueAlive && board.playerRedAlive)
{
if (!grenade)
{
turnTowards(board.playerRedX, board.playerRedY);
move(speed);
}
walkProgress += speed;
if (playerRedDistance < lungeRange)
{
if (!grenade)
{
move(speed);
move(speed);
walkProgress += speed;
}
}
}
if (invincibilityTimer > 0)
{
setImage(walkingFrames[(walkProgress / 10) - 1 + 4]);
}
else if (grenade)
{
setImage(walkingFrames[(walkProgress / 10) - 1 + 8]);
}
else
{
setImage(walkingFrames[(walkProgress / 10) - 1]);
}
if (walkProgress >= 45)
{
walkProgress = 10;
}
}
public void hurtPlayers()
{
Board board = (Board)getWorld();
Player playerBlue = (Player)getOneIntersectingObject(Player_Blue.class);
Player playerRed = (Player)getOneIntersectingObject(Player_Red.class);
Shield shield = (Shield)getOneIntersectingObject(Shield.class);
if (withinRange(board.playerBlueX, board.playerBlueY, 10, 10))
{
if (playerBlue != null && playerBlue.lives > 0)
{
knockbackLeft = 20;
knockbackSpeed = 8;
playerBlue.lives--;
playerBlue.comboTimer = 0;
playerBlue.invincibilityTimer = 10;
playerBlue.setRotation(getRotation());
playerBlue.turn(180);
playerBlue.knockbackLeft = 20;
playerBlue.knockbackSpeed = 8;
getWorld().addObject(new AttackEffectRed(playerBlue.getRotation()), playerBlue.getX(), playerBlue.getY());
}
}
if (withinRange(board.playerRedX, board.playerRedY, 10, 10))
{
if (playerRed != null && playerRed.lives > 0)
{
knockbackLeft = 20;
knockbackSpeed = 8;
playerRed.lives--;
playerRed.comboTimer = 0;
playerRed.invincibilityTimer = 10;
playerRed.setRotation(getRotation());
playerRed.turn(180);
playerRed.knockbackLeft = 20;
playerRed.knockbackSpeed = 8;
getWorld().addObject(new AttackEffectRed(playerRed.getRotation()), playerRed.getX(), playerRed.getY());
}
}
}
public boolean withinRange(int posX, int posY, int radiusX, int radiusY)
{
if (getX() > posX - radiusX && getX() < posX + radiusX && getY() > posY - radiusY && getY() < posY + radiusY)
{
return true;
}
else
{
return false;
}
}
public boolean shieldBlocking(int rotation, int range)
{
rotation += 180;
if (rotation > 360)
{
rotation -= 360;
}
if (getRotation() > rotation - range && getRotation() < rotation + range)
{
return true;
}
else
{
return false;
}
}
}

