This site requires JavaScript, please enable it in your browser!
Greenfoot back
Çðæyœn
Çðæyœn wrote ...

2019/6/7

Mass to health ratio

Çðæyœn Çðæyœn

2019/6/7

#
I've created an asteroid game where a user must control spaceships to dodge incoming asteroids. For anyone who's previously helped me with this game, my other errors have been fixed. The last feature I am trying to implement into my program is related to shooting. Currently, when one of my two spaceships on the screen shoots an incoming asteroid, both that bullet and asteroid being hit are removed from the world. My goal is to make asteroids that are bigger have more health (more bullets needed to explode), and I hypothesize that a ratio will be needed. If I say it takes 10 bullets to destroy an asteroid with the size of 100, I suggest I can use that to make a ratio (10 bullets / size100 = ? bullets / getSize of asteroid being shot at), in order to make size and health (of an asteroid) proportional. Not exactly sure how to go about this, any ideas?
Super_Hippo Super_Hippo

2019/6/7

#
When applying a size to the asteroid, you can also set its health based on that. When the asteroid is damaged by a bullet, it will lose one health and will split when it reaches health=0.
Çðæyœn Çðæyœn

2019/6/8

#
Thanks, I was able to figure it out. I also had another issue with storing high scores, where scores would be written to an external file, but would be replaced if the game was played again. I solved this issue by using a file writer and buffered writer, which would allow scores to be written to the file without replacing anything. I am already aware that in a separate world where high scores will be displayed, I must read from the text file and use a scanner, although this has not been done yet. What I am still trying to do is match up scores with the user (ie. at the end of the game, popup message that prompts users to enter in their name, match up their name with their high score, NOTE: if 2 player mode, 2 names will be entered). I have no idea how to do this, but it is the one of the last obstacles to the completion of my game, which must be done before the end of tomorrow. Thank you for your help. MyWorld class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
import java.util.*;
/**
 * Write a description of class MyWorld here.
 * 
 * @author Matthew Benson
 * @version 1.0
 */
public class MyWorld extends World
{
    private SpaceBackground Image1, Image2;                             //images merged together to create illusion of scrolling background
    GreenfootSound backgroundMusic = new GreenfootSound("GameScreenMusic.wav");             //background music assigned to a variable
    private int delay;                                          //time between how fast asteroids spawn
    private boolean gameOver;                                           //tests whether game is over or not
    private int wait = 240;                                         //4 second delay (240/60) for explosion gif to be seen
    private double gameTimer1 = 0;                              //survived time of spaceship1
    private double gameTimer2 = 0;                              //survived time of spaceship2
    /**
     * Initialization and creation of objects
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 800, 1, false); 

        Image1 = new SpaceBackground();
        addObject(Image1, getWidth()/2, getHeight()/2);
        Image2 = new SpaceBackground();
        addObject(Image2, getWidth() + getWidth()/2, getHeight()/2);

        if (StartScreen.mode == 1) { // if the mode variable is 1 (meaning one player mode), only spawn 1P ship
            Spaceship ship = new Spaceship();
            addObject(ship, 100, getHeight()/4);
        }
        if (StartScreen.mode == 2) { // if the mode variable is 2 (meaning 2 player mode), spawn both ships.
            Spaceship ship = new Spaceship();
            addObject(ship, 100, getHeight()/4);
            Spaceship2 ship2 = new Spaceship2();
            addObject(ship2, 100, (getHeight() / 2) + (getHeight() / 4));
        }
    }

    /**
     * Spawns asteroids at right edge of screen every half a second.
     * Y-location of asteroids are random.
     */
    public void spawn() {
        delay = (delay + 1)%30;         //spawns asteroids every 0.5 seconds
        if (delay == 0) addObject(new Asteroid((Greenfoot.getRandomNumber(19)-20), 2), 0, 0);
    }

    /**
     * Stop scrolling screen once game is over.
     * The game is over once both spaceships explode.
     */
    public void gameOver() {
        Image1.setLocation((this.getWidth() / 2) + 10, this.getHeight() / 2);
        Image2.setLocation((this.getWidth() / 2) + 10, this.getHeight() / 2);
    }

    /**
     * Write scores of both spaceships to an external file.
     * Store (gameTimer1/60) and (gameTimer2/60) here to store time survived (s)
     */
    public void writeToFile() {
        double score1 = gameTimer1/60;                  //convert act cycle increments to seconds
        double score2 = gameTimer2/60;                  //convert act cycle increments to seconds
        String score1s = String.valueOf(score1);        //convert double to string
        String score2s = String.valueOf(score2);        //convert double to string
        try {
            File highscores = new File("highscores.txt");       //assign file to variable
            if (!highscores.exists()) {                         //if file does not exist, create new one
                highscores.createNewFile();
            }
            FileWriter fw = new FileWriter(highscores,true);        //create new filewriter
            BufferedWriter bw = new BufferedWriter(fw);             //create new bufferedwriter, to avoid replacement when writing to a file
            bw.write(score1s);
            bw.newLine();
            bw.write(score2s);
            bw.newLine();
            bw.close();                 //close the bufferedwriter
        } 
        catch (IOException e) {         //vital part of storing, catch exceptions in case file does not exist etc.
            e.printStackTrace();
        }
    }

    /**
     * Executes following information several times per second during the course of runtime.
     * Dictates the actions of objects after game has started.
     */
    public void act() {
        if (getObjects(Spaceship.class).size() != 0 || getObjects(Spaceship2.class).size() != 0) {              //spawn asteroids if at least one spaceship is alive
            gameOver = false;
            spawn();
        }

        if (getObjects(Spaceship.class).isEmpty() && getObjects(Spaceship2.class).isEmpty()) {                  //if both spaceships are dead, end game
            gameOver = true;
            gameOver();
        }

        backgroundMusic.playLoop();    //play background music on run()

        Image1.ActivateScroll();
        Image2.ActivateScroll();

        if (gameOver) {
            if (wait>0)                 //after game is over, wait x amount of seconds based on (wait/60)
            {
                wait--;
                if(wait == 0) {
                    backgroundMusic.stop();
                    writeToFile();                          //write scores to file here to avoid repeats
                    Greenfoot.setWorld(new GameOver());
                }
            }
        }

        if (getObjects(Spaceship.class).size() != 0) {          //incrementing time survived for spaceship1
            gameTimer1++;
        }
        if (getObjects(Spaceship2.class).size() != 0) {         //incrementing time survived for spaceship2
            gameTimer2++;
        }

    }
}
Super_Hippo Super_Hippo

2019/6/8

#
I have no experience with using external files, so I can't help you with that one. What I would do instead would be to use the UserInfo class. Which obviously doesn't work easily for two-play-games like you described because you can only read other user's data but don't write anything to their files. Sending it over is probably more complex than figuring that I/O out.
You need to login to post a reply.