-->

Tutorial membuat game tikus lapar bagian 7

Post a Comment
Tutorial membuat game tikus lapar bagian 7

Rewards.java
//Name: Rewards.java
//Purpose: all the attributes and functions of the rewards like collision and rewards collected

package com.basithsteviejhei.mouse;

import java.util.ArrayList;
import android.graphics.Rect;//hold 4 integer coordinates for rectangle

public class Rewards {

//local variables
private int power, centerX, speedX, centerY;
private int movementSpeed;

private Background bg = GameScreen.getBg1();
private Mouse Mouse = GameScreen.getMouse();

public Rect r = new Rect(0, 0, 0, 0);
public boolean collected=false;

//Behavioral Method, update coordinates and intersects
public void update() {

centerX += speedX;
speedX = bg.getSpeedX() * 5 + movementSpeed;
r.set(centerX - 25, centerY - 25, centerX + 25, centerY + 35);

if (Rect.intersects(r, Mouse.yellowRed)) {
checkCollision();
}


}

//check if mouse and reward had collision
private void checkCollision() {
if (Rect.intersects(r, Mouse.rect)|| Rect.intersects(r, Mouse.rect2) || Rect.intersects(r, Mouse.rect3) || Rect.intersects(r, Mouse.rect4)) {
Mouse.addCollectedCheeses(1);
this.collected=true;
this.setCenterX(-100);

if (Settings.soundEnabled)
Assets.collect.play(1);
}
}

//when background moves, the enemy must move to the same direction
public Background getBg() {
return bg;
}
public void setBg(Background bg) {
this.bg = bg;
}

//getters//
public boolean getColleceted(){
return collected;
}
public int getCenterX() {
return centerX;
}

public int getCenterY() {
return centerY;
}


//setters//
public void setCenterX(int centerX) {
this.centerX = centerX;
}

public void setCenterY(int centerY) {
this.centerY = centerY;
}
}

Settings.java 
//Name: Settings.java
//Purpose: save and load settings like sounds on/off gyroscope on/off and save high scores
package com.basithsteviejhei.mouse;

import com.basithsteviejhei.fremework.FileIO;

//java libraries
import java.io.BufferedReader;//wraps and existing reader and buffers the input
import java.io.BufferedWriter;//wraps an existing Writer and buffers the output
import java.io.IOException;//signal that i/o exception has occurred
import java.io.InputStreamReader;//turn a byte stream to character stream
import java.io.OutputStreamWriter;



public class Settings {
 
    // Create variables that will hold the values you want to save in your game.
    // Default values:
 
    public static boolean soundEnabled = true;
    public static boolean gyroscopeEnabled = false;
    public static int currentLevel = 0;
    public static int theLevelPassed = 0;
    public static int level1CollectedCheeses, level2CollectedCheeses, level3CollectedCheeses;
    public static int level1KilledKamikazis, level2KilledKamikazis, level3KilledKamikazis;
 
    public static void save(FileIO files) {
        BufferedWriter out = null;
        try {
         
            // Writes a file called .savedata to the SD Card
            out = new BufferedWriter(new OutputStreamWriter(
                    files.writeFile(".savethedataHungryMouse")));
         
            // Line by line ("\n" creates a new line), write the value of each variable to the file.
            out.write(Boolean.toString(soundEnabled));
            out.write("\n");
            out.write(Boolean.toString(gyroscopeEnabled));
            out.write("\n");          
            out.write(Integer.toString(currentLevel));
            out.write("\n");
            out.write(Integer.toString(theLevelPassed));
            out.write("\n");
         
            //cheeses
            out.write(Integer.toString(level1CollectedCheeses));
            out.write("\n");
            out.write(Integer.toString(level2CollectedCheeses));
            out.write("\n");
            out.write(Integer.toString(level3CollectedCheeses));
            out.write("\n");
         
            //kamikazis
            out.write(Integer.toString(level1KilledKamikazis));
            out.write("\n");
            out.write(Integer.toString(level2KilledKamikazis));
            out.write("\n");
            out.write(Integer.toString(level3KilledKamikazis));
            out.write("\n");
     
         
           // This section handles errors in file management!
         
        } catch (IOException e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }
 
    public static void load(FileIO files) {
        BufferedReader in = null;
        try {
            // Reads file called Save Data
            in = new BufferedReader(new InputStreamReader(
                    files.readFile(".savethedataHungryMouse")));

            // Loads values from the file and replaces default values.
            soundEnabled = Boolean.parseBoolean(in.readLine());
            gyroscopeEnabled = Boolean.parseBoolean(in.readLine());
            currentLevel = Integer.parseInt(in.readLine());
            theLevelPassed = Integer.parseInt(in.readLine());
         
            level1CollectedCheeses = Integer.parseInt(in.readLine());
            level2CollectedCheeses = Integer.parseInt(in.readLine());
            level3CollectedCheeses = Integer.parseInt(in.readLine());          
         
            level1KilledKamikazis = Integer.parseInt(in.readLine());
            level2KilledKamikazis = Integer.parseInt(in.readLine());
            level3KilledKamikazis = Integer.parseInt(in.readLine());
         
        } catch (IOException e) {
            // Catches errors. Default values are used.
        } catch (NumberFormatException e) {
            // Catches errors. Default values are used.
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }
}

Sign.java 
//Name: Sign.java
//Purpose: the Sign reward with the coordinates, when player have collision with it,  the level is passed

package com.basithsteviejhei.mouse;

//android libraries stored in SDK platform
import android.graphics.Rect;//hold 4 integer coordinates for rectangle

public class Sign extends Rewards {

private Mouse Mouse = GameScreen.getMouse();

//constructor
public Sign(int centerX, int centerY) {

setCenterX(centerX);//x axis coordinates
setCenterY(centerY);//y axis coordinates

}

//check if mouse and reward had collision
private void checkCollision() {
if (Rect.intersects(r, Mouse.rect)|| Rect.intersects(r, Mouse.rect2) || Rect.intersects(r, Mouse.rect3) || Rect.intersects(r, Mouse.rect4)) {
Mouse.setLevelPassed(1);
Settings.theLevelPassed=1;
this.collected=true;
//this.setCenterX(-100);
}
}

}

SplashLoadingScreen.java 


//Name: SplashScreen.java
//Purpose: show this screen when loading assets, before the main menu screen

package com.basithsteviejhei.mouse;

import com.basithsteviejhei.fremework.Game;
import com.basithsteviejhei.fremework.Graphics;
import com.basithsteviejhei.fremework.Screen;
import com.basithsteviejhei.fremework.Graphics.ImageFormat;

public class SplashLoadingScreen extends Screen {

//constructor
public SplashLoadingScreen(Game game) {
super(game);
}

//draw splash image
@Override
public void update(float deltaTime) {
Graphics g = game.getGraphics();
Assets.splash= g.newImage("splash.png", ImageFormat.RGB565);

game.setScreen(new LoadingScreen(game));

}

@Override
public void paint(float deltaTime) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {

}

@Override
public void backButton() {

}
}

Tile.java


//Name: Tile.java
//Purpose: create a 2-dimensional array with values that from text files.
// based on those values we will present specific images to the screen
// the text files and therefore the 2-dimensional array represents the level environment

package com.basithsteviejhei.mouse;

import com.basithsteviejhei.fremework.Image;

//android libraries stored in SDK platform
import android.graphics.Rect;//hold 4 integer coordinates for rectangle

public class Tile {

private int tileX, tileY, speedX;
public int type;
public Image tileImage;

private Mouse Mouse = GameScreen.getMouse();
private Background bg = GameScreen.getBg1();

private Rect r;

//initialize tiles
public Tile(int x, int y, int typeInt) {
tileX = x * 40;
tileY = y * 40;

type = typeInt;

r = new Rect();

//load suitable image based on numerical value
if (type == 5) {
tileImage = Assets.tiledirt;
} else if (type == 8) {
tileImage = Assets.tilegrassTop;
} else if (type == 4) {
tileImage = Assets.tilegrassLeft;

} else if (type == 6) {
tileImage = Assets.tilegrassRight;

} else if (type == 2) {
tileImage = Assets.tilegrassBot;
} else {
type = 0;
}

}

//update and check for collisions
public void update() {
speedX = bg.getSpeedX() * 5;
tileX += speedX;
r.set(tileX, tileY, tileX+40, tileY+40);

if (Rect.intersects(r, Mouse.yellowRed) && type != 0) {
checkVerticalCollision(Mouse.rect, Mouse.rect2);
checkSideCollision(Mouse.rect3, Mouse.rect4, Mouse.footleft, Mouse.footright);
}

}

//check the vertical collision with the environment
public void checkVerticalCollision(Rect rtop, Rect rbot) {
if (Rect.intersects(rtop, r)) {
}

if (Rect.intersects(rbot, r) && type == 8) {
Mouse.setJumped(false);
Mouse.setSpeedY(0);
Mouse.setCenterY(tileY - 63);
}
}

//check the side collision with the environment
public void checkSideCollision(Rect rleft, Rect rright, Rect leftfoot, Rect rightfoot) {
if (type != 5 && type != 2 && type != 0){
if (Rect.intersects(rleft, r)) {
Mouse.setCenterX(tileX + 102);
Mouse.setSpeedX(0);
}else if (Rect.intersects(leftfoot, r)) {
Mouse.setCenterX(tileX + 85);
Mouse.setSpeedX(0);
}

if (Rect.intersects(rright, r)) {
Mouse.setCenterX(tileX - 62);
Mouse.setSpeedX(0);
}
else if (Rect.intersects(rightfoot, r)) {
Mouse.setCenterX(tileX - 45);
Mouse.setSpeedX(0);
}
}
}

//getters//
public int getTileX() {
return tileX;
}


public int getTileY() {
return tileY;
}
public Image getTileImage() {
return tileImage;
}
//setters//
public void setTileX(int tileX) {
this.tileX = tileX;
}

public void setTileY(int tileY) {
this.tileY = tileY;
}

public void setTileImage(Image tileImage) {
this.tileImage = tileImage;
}

}

Related Posts

Post a Comment

Subscribe Our Newsletter