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

2023/3/31

HELP ASAP

envxity envxity

2023/3/31

#
so i ruined my game that i was making and now it barely works like it was meant to here is the source code its due today. so pls help
import greenfoot.*;
public class MyWorld extends World
{
    int count;
    int spawnSpeed = 50;
    int randomSpawn;    
    int wave = 1;    
    int pause;
    public Player mainPlayer = new Player();
    Counter counter = new Counter();
    HealthBar Healthbar = new HealthBar(mainPlayer);
    WeaponButton weaponButton = new WeaponButton(counter);
    button button = new button();
    SuperPower superPower = new SuperPower();
    public MyWorld()
    {
        super(1000, 800, 1);
        mainPlayer = new Player(weaponButton, superPower);
        addObject(button,976,25);
        addObject(mainPlayer,getWidth()/2, getHeight()/2);
        addObject(counter,96,70);
        addObject(Healthbar, mainPlayer.getX()-5, mainPlayer.getY()-50);
        addObject(weaponButton, 900, 100);
        addObject(superPower, mainPlayer.getX()+10, mainPlayer.getY()-80);
        
    }
    public Player getPlayer()
    {
        return mainPlayer;
    }
    public void act(){
        count++;
        spawnZombies();
        decreaseSpawnSpeed();
    }
    public void spawnZombies(){
        if(count % spawnSpeed == 0){
            randomSpawn = Greenfoot.getRandomNumber(8);
            switch(randomSpawn){
                case 0 : addObject(new Zombie(mainPlayer, counter), 0,0); break;
                case 1 : addObject(new Zombie(mainPlayer, counter), getWidth()/2,0); break;
                case 2 : addObject(new Zombie(mainPlayer, counter), getWidth(),0); break;
                case 3 : addObject(new Zombie(mainPlayer, counter), 0,getHeight()/2); break;
                case 4 : addObject(new Zombie(mainPlayer, counter), getWidth(),getHeight()/2); break;
                case 5 : addObject(new Zombie(mainPlayer, counter), 0,getHeight()); break;
                case 6 : addObject(new Zombie(mainPlayer, counter), getWidth()/2,getHeight()); break;
                case 7 : addObject(new Zombie(mainPlayer, counter), getWidth(),getHeight()); break;
            }                
        }
    }
    public void decreaseSpawnSpeed(){
        if(count % 600 == 0)
        {
            spawnSpeed--;
        }
    }
}
import greenfoot.*;
public class HealthBar extends Actor
{
    int health = 50;
    double specificHealth = (double)health;
    Player mainPlayer;
    Zombie zombie;
    public HealthBar(Player mainPlayer)
    {
        this.mainPlayer = mainPlayer;
        createImage(); 
    }
    public void act() 
    {
        createImage();           
        World world = getWorld();
        MyWorld game = (MyWorld)world;
        setLocation(game.getPlayer().getX() - 5, game.getPlayer().getY() - 50);
        LoseHealth();
    }
    public void createImage()
    {
        setImage(new GreenfootImage(52,12));
        getImage().drawRect(0,0,51,11);
        getImage().setColor(Color.RED);
        getImage().fillRect(1,1,health,10);
        health--;
    }
    public void LoseHealth()
    {
        World world = getWorld();
        MyWorld game = (MyWorld)world;
        
        if (game.getPlayer().hitByZombie() && zombie != null && getNeighbours(getImage().getWidth()/2, false, Zombie.class).contains(zombie) && zombie.hitsPlayer(mainPlayer))
        {
            health--;
        }
    }
}
import greenfoot.*;
import greenfoot.MouseInfo;

public class Player extends Actor
{
    // all the variables for the players stats
    Counter counter;
    Zombie zombie;
    public final int speed = 3;
    public int firerate = 10;
    int health = 100;
    int stamina = 10;
    int cash = 0;
    int wave = 1;
    int ammo = 20;
    int superTimer;

    public int pause = 100;
    int kills = 0;
    int survivedTime = 0;
    private boolean reloadKeyDown = false;
    int time = 0;
    private final int reloadTime = 10,
    maxShots = 30; 
    private int reloadTimer = 0,
    shotsLeft = maxShots; 
    private boolean shooting = false;
    boolean farthest = false;
    WeaponButton weaponButton;
    SuperPower superPower;
    /* BY GARRETT*/

    // creates the player character
    public Player(){
        setImage(new GreenfootImage(70, 50));
        getImage().setColor(Color.YELLOW);
        getImage().fillOval(0, 0, 50, 50);
        getImage().setColor(Color.BLACK);
        getImage().fillRect(50, 20, 70, 10);
    }

    public Player(WeaponButton weaponButton, SuperPower superPower){
        this.superPower = superPower;
        this.weaponButton = weaponButton;
        setImage(new GreenfootImage(70, 50));
        getImage().setColor(Color.YELLOW);
        getImage().fillOval(0, 0, 50, 50);
        getImage().setColor(Color.BLACK);
        getImage().fillRect(50, 20, 70, 10);
    }
    // tells the player character to use all the methods that have been created as long as the conditions are right for the given method
    public void act()
    {
        move();
        turnAround();
        shoot();
        time++;
        hitByZombie();

    }
    // tells the actor to turn to face the mouse
    public void turnAround(){
        button button = new button();
        if (Color.BLACK.equals(button.getCurrentColor())){
            if(Greenfoot.getMouseInfo() != null){
                turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
            }
        }
    }
    // tells the actor to do if the button is black and if it isnt black
    public void shoot() {
        button button = new button();
        if (Color.BLACK.equals(button.getCurrentColor())){
            manualShoot();            
        } 
        else {
            autoShoot();
        }
    }
    // returns the buttons color to determine what the firemode is
    public static int getColor()
    {
        return button.colorValue;
    }
    // one of the firemodes that is player skilled based 
    private void manualShoot()
    {        
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(ammo>0){
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 1)
            {
                turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-0);
                bullet.move(25);
            }
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 2)
            {
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-5);
                bullet.move(25);
                Bullet bullet2 = new Bullet();
                getWorld().addObject(bullet2, getX(), getY());
                bullet2.setRotation(getRotation()+5);
                bullet2.move(25);
            }
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 3)
            {
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-5);
                bullet.move(25);
                Bullet bullet2 = new Bullet();
                getWorld().addObject(bullet2, getX(), getY());
                bullet2.setRotation(getRotation()+5);
                bullet2.move(25);
                Bullet bullet3 = new Bullet();
                getWorld().addObject(bullet3, getX(), getY());
                bullet3.setRotation(getRotation()-0);
                bullet3.move(25);
            }
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 4)
            {
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-10);
                bullet.move(25);
                Bullet bullet2 = new Bullet();
                getWorld().addObject(bullet2, getX(), getY());
                bullet2.setRotation(getRotation()+10);
                bullet2.move(25);
                Bullet bullet3 = new Bullet();
                getWorld().addObject(bullet3, getX(), getY());
                bullet3.setRotation(getRotation()-5);
                bullet3.move(25);
                Bullet bullet4 = new Bullet();
                getWorld().addObject(bullet4, getX(), getY());
                bullet4.setRotation(getRotation()+5);
                bullet4.move(25);
            }
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 5)
            {
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-10);
                bullet.move(25);
                Bullet bullet2 = new Bullet();
                getWorld().addObject(bullet2, getX(), getY());
                bullet2.setRotation(getRotation()+10);
                bullet2.move(25);
                Bullet bullet3 = new Bullet();
                getWorld().addObject(bullet3, getX(), getY());
                bullet3.setRotation(getRotation()-0);
                bullet3.move(25);
                Bullet bullet4 = new Bullet();
                getWorld().addObject(bullet4, getX(), getY());
                bullet4.setRotation(getRotation()-5);
                bullet4.move(25);
                Bullet bullet5 = new Bullet();
                getWorld().addObject(bullet5, getX(), getY());
                bullet5.setRotation(getRotation()+5);
                bullet5.move(25);
            }
        } 
    }

    public void useSuperPower()
    {
        if(superPower.superCount>99 && superTimer< 30)
        {
            superTimer++;
        }
        if(superTimer > 30)
        {
            Bullet bullet = new Bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.setRotation(getRotation()-60);
            bullet.move(25);
            Bullet bullet2 = new Bullet();
            getWorld().addObject(bullet2, getX(), getY());
            bullet.setRotation(getRotation()+60);
            bullet2.move(25);
            Bullet bullet3 = new Bullet();
            getWorld().addObject(bullet3, getX(), getY());
            bullet.setRotation(getRotation()-0);
            bullet3.move(25);
            Bullet bullet4 = new Bullet();
            getWorld().addObject(bullet4, getX(), getY());
            bullet.setRotation(getRotation()-180);
            bullet4.move(25);
            Bullet bullet5 = new Bullet();
            getWorld().addObject(bullet5, getX(), getY());
            bullet.setRotation(getRotation()+120);
            bullet5.move(25);
            Bullet bullet6 = new Bullet();
            getWorld().addObject(bullet6, getX(), getY());
            bullet.setRotation(getRotation()-120);
            bullet6.move(25);
            superTimer++;  
        }
    }
    // the second fire mode that is more of a auto fire to the nearest enemy
    private void autoShoot()
    {        
        if(pause>0){
            pause --;
        }
        if(pause == 0){
            if (ammo > 0) {
                if (getWorld().getObjects(Zombie.class).isEmpty()) return;
                Actor closest = null;
                int closeness = 9999;
                for (Object obj : getWorld().getObjects(Zombie.class)) {
                    Actor enemy = (Actor)obj;
                    int dist = (int)Math.hypot(getX()-enemy.getX(), getY()-enemy.getY());
                    if (dist < closeness) {
                        closeness = dist;
                        closest = enemy;
                    }
                }
                Actor bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.turnTowards(closest.getX(), closest.getY());
                setRotation(bullet.getRotation());
                ammo--;
                pause = firerate;
            }        
        }
    }
    // gets the users inputs and moves the player accordingly
    public void move(){
        if(Greenfoot.isKeyDown("W")||Greenfoot.isKeyDown("UP")){
            setLocation( getX(), getY()-speed);
        }
        if(Greenfoot.isKeyDown("A")||Greenfoot.isKeyDown("LEFT")){
            setLocation( getX()-speed, getY());
        }
        if(Greenfoot.isKeyDown("S")||Greenfoot.isKeyDown("DOWN")){
            setLocation( getX(), getY()+speed);
        }
        if(Greenfoot.isKeyDown("D")||Greenfoot.isKeyDown("RIGHT")){
            setLocation( getX()+speed, getY());
        }
    }
    // tells the character to reload his gun when the user hits r
    public void hitbyZombie()
    {
        Bullet bullet = (Bullet)getOneIntersectingObject(Bullet.class);
        health --;
        getWorld().removeObject(bullet);

    }

    public void youWin(){
        if(time >100 && !isTouching(Zombie.class)){
            getWorld().showText("You Win! - you Survived the 60 seconds ", getWorld().getWidth()/2, getWorld().getHeight()/2);
        }
    }

    public boolean hitByZombie()
    {
        Zombie zombie = (Zombie)getOneIntersectingObject(Zombie.class);
        if (zombie != null && getNeighbours(getImage().getWidth()/2, false, Zombie.class).contains(zombie) && zombie.hitsPlayer(this))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
import greenfoot.*;
public class Bullet extends Actor
{    
    public Bullet(){
        setImage(new GreenfootImage(10,5));
        getImage().setColor(Color.BLACK);
        getImage().fillRect(0,0,10,5);
    }
    public void act()
    {
        move(10);
        DestroyBullet();
        
        
    }
    public void move(int distance)
    {
        double radians = Math.toRadians(getRotation());
        int dx = (int) Math.round(Math.cos(radians) * distance);
        int dy = (int) Math.round(Math.sin(radians) * distance);
        setLocation(getX() + dx, getY() + dy);
    }
    private void DestroyBullet(){
        if(isAtEdge()){
            getWorld().removeObject(this);
        }
    }
    public boolean hitsZombie(Zombie zombie)
    {
        return getObjectsInRange(getImage().getWidth()/2, Zombie.class).contains(zombie);
    }
}
import greenfoot.*;
public class Zombie extends Actor
{
    int animateImage = 0;
    int animateSpeed = 5;
    int count;
    int health = 3;
    Player player;
    Counter counter;
    public Zombie(Player mainPlayer, Counter counter){
        this.counter = counter;
        player = mainPlayer;
        setImage("skeleton-idle_16.png");
        getImage().scale(80,80);    
    }
    public void act()
    {
        count++;
        animate();
        moveAround();
        hitByBullet();
    }
    // animates the zombie
    public void animate(){
        if(count % animateSpeed == 0){
            if(animateImage > 16){
                animateImage = 0;                
            }
            setImage("skeleton-move_"+ animateImage + ".png");
            animateImage++;
            getImage().scale(80,80); 
        }
    }
    private void moveAround(){
        move(1);
        turnTowards(player.getX(), player.getY());
    }
    public void hitByBullet()
    {
        Bullet bullet = (Bullet)getOneIntersectingObject(Bullet.class);
        if (bullet != null && getNeighbours(getImage().getWidth()/2, false, Bullet.class).contains(bullet) && bullet.hitsZombie(this))
        {
            health--;
        }
        if(health == 0){
            getWorld().removeObject(this);
            counter.Score++;
        }

    }
    public boolean hitsPlayer(Player mainPlayer)
    {
        return getObjectsInRange(getImage().getWidth()/2, Player.class).contains(mainPlayer);
    }
}
import greenfoot.*;
public class Counter extends Actor
{
    int Score;
    int cash;
    public Counter(){
        setImage(new GreenfootImage( "Score:"+ Score+
        "cash:" + cash, 40, Color.BLACK, new Color(0,0,0,0)));
    }
    public void act()
    {
        setImage(new GreenfootImage( "Score:"+ Score+
        "cash:" + cash, 40, Color.BLACK, new Color(0,0,0,0)));
    }
}
import greenfoot.*;
public class SuperPower extends Actor
{
    final int SUPER_POWER_LIMIT = 100;
    int superCount;
    int count;
    int superTimer;
    
   
    public SuperPower()
    {
        createImage();
    }
    public void act () 
    {
        createImage();           
        World world = getWorld();
        MyWorld game = (MyWorld)world;
        game.getPlayer();
        setLocation(game.getPlayer().getX() + 10, game.getPlayer().getY() - 80);
        useSuper();
        
    }
    public void useSuper()
    {
        count++;
        if(count % 10 == 0)
        {
            superCount++;
        }
        if(superCount >= 100)
        {
            superCount = 0;
        }
        
    }
    public void createImage()
    {
        setImage(new GreenfootImage(SUPER_POWER_LIMIT + 2,12));
        getImage().drawRect(0,0,SUPER_POWER_LIMIT + 1,11);
        getImage().setColor(Color.BLUE);
        getImage().fillRect(1,1,superCount,10);
    }
    
}
import greenfoot.*;
public class WeaponButton extends Actor
{
    int cost = 150;
    int WeaponUpgrade = 1;
    
    Counter counter;
    public WeaponButton(Counter counter)
    {
        this.counter = counter;
        setImage(new GreenfootImage("Weapon \n upgrade \n cost: " + cost, 25, Color.BLACK, new Color(0,0,0,0)));
    }
    public void act(){
        upgrade();
    }
    public void upgrade()
    {
        if(Greenfoot.mousePressed(this) && counter.cash >149)
        {
            counter.cash -= 150;
            cost +=50;
            WeaponUpgrade++;
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class button extends Actor
{
    Color[] colors = { Color.BLACK, Color.RED };
    public static int colorValue = 0; // index of colors array 
        
    public button() {
        setImage(new GreenfootImage(70, 50));
        changeColor();
        changeColor();
    }
    public Color getCurrentColor() {
        return colors[colorValue];
    }     
    public void changeColor() {
        colorValue = (colorValue+1)%2; // toggle index between 0 and 1
        getImage().setColor(getCurrentColor());
        getImage().fillOval(0, 0, 20 , 20);
    }     
    public void act() 
    {
        if (Greenfoot.mouseClicked(this)) {
            changeColor();
        }
    }
    
}
danpost danpost

2023/3/31

#
envxity wrote...
so i ruined my game that i was making and now it barely works like it was meant to here is the source code its due today. so pls help << Codes Omitted >>
I did notice that in multiple places you have images being created continuously (every act frame). Image creation/manipulation can be a hog on CPU time and may cause some lagging. Also, I noticed that you have the health bar and the counter doing things they should not. GUI objects, as such, should be prompted to do things, not do them on their own -- and only do those things when required. In other words, they should not have an act method at all; and, the methods in the class should be called or values in the class should be changed only when necessary. You are also passing a lot of things around when creating objects. That should be minimized or eliminated altogether.
You need to login to post a reply.