-->

Tutorial membuat game tikus lapar bagian 6

Post a Comment
Tutorial membuat game tikus lapar bagian 6

MainMenuScreen.java 
//Name: MainMenuScreen.java
//Purpose: create the menu screen with many options

package com.basithsteviejhei.mouse;

import com.basithsteviejhei.fremework.Game;
import com.basithsteviejhei.fremework.Graphics;
import com.basithsteviejhei.fremework.Screen;
import com.basithsteviejhei.fremework.Input.TouchEvent;
import com.basithsteviejhei.mouse.Assets;
import com.basithsteviejhei.mouse.AboutScreen;

//java libraries
import java.util.List;//a collection which maintain an ordering for its elements

public class MainMenuScreen extends Screen {

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

//touch event checking
@Override
public void update(float deltaTime) {
Graphics g = game.getGraphics();
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);

if (event.type == TouchEvent.TOUCH_UP) {

//sound options
if (inBounds(event, 16, 368, 96, 96)) {
                 Settings.soundEnabled = !Settings.soundEnabled;
                  if (Settings.soundEnabled)
                  Assets.click.play(1);
}
//gyroscope options

if (inBounds(event, 112, 368, 96, 96)) {
                 Settings.gyroscopeEnabled = !Settings.gyroscopeEnabled;
                  if (Settings.soundEnabled)
                  Assets.click.play(1);
                  }

if (inBounds(event, 224, 176, 352, 128)) {
                   game.setScreen(new LevelSelectorScreen(game));//change screen when button pressed
                   if (Settings.soundEnabled)
                   Assets.click.play(1);
}
if (inBounds(event, 688, 368, 96, 96)) {
game.setScreen(new AboutScreen(game));//change screen when button pressed
if (Settings.soundEnabled)
Assets.click.play(1);
}
if (inBounds(event, 592, 368, 96, 96)) {
game.setScreen(new HelpScreen1(game));//change screen when button pressed
if (Settings.soundEnabled)
Assets.click.play(1);
}

}

}
Settings.save(game.getFileIO());//save the changes
}

//create rectangles with coordinates that will be used to track touches
private boolean inBounds(TouchEvent event, int x, int y, int width,
int height) {
if (event.x > x && event.x < x + width - 1 && event.y > y
&& event.y < y + height - 1)
return true;
else
return false;
}

//draw graphics
@Override
public void paint(float deltaTime) {
Graphics g = game.getGraphics();
g.drawImage(Assets.menu, 0, 0);

g.drawImage(Assets.logo,  224, 16);
g.drawImage(Assets.menu_buttons,  224, 176, 0, 0, 352, 128);//play button (show the 1/3 of image)

if (Settings.soundEnabled){
g.drawImage(Assets.sound_on_buttons,  16, 368, 0, 0, 96, 96);
} else {
g.drawImage(Assets.sound_on_buttons,  16, 368, 0, 96, 96, 96);
}


if (Settings.gyroscopeEnabled){
g.drawImage(Assets.gyroscope_buttons,  112, 368, 0, 96, 96, 96);
} else
{
g.drawImage(Assets.gyroscope_buttons,  112, 368, 0, 0, 96, 96);
}

g.drawImage(Assets.info_buttons,  592, 368, 0, 0, 96, 96);
g.drawImage(Assets.info_buttons,  688, 368, 0, 96, 96, 96);

}

@Override
public void pause() {
Settings.save(game.getFileIO());//save the changes
}

@Override
public void resume() {

}

@Override
public void dispose() {

}

//kill all process including all activities
//prefered than finish() to free up memory
@Override
public void backButton() {

//kill the process with the given PID and free up memory
        android.os.Process.killProcess(android.os.Process.myPid());

}
}


Mouse.java
//Name: Mouse.java
//Purpose: the main character that player will control with attributes and its functions

package com.basithsteviejhei.mouse;

//java libraries
import java.util.ArrayList;

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

public class Mouse {

//constant
final int JUMPSPEED = -19;
final int MOVESPEED = 5;

//local variables
private int centerX = 100;//start x axis coordinates
private int centerY = 297;//start y axis coordinates
private boolean jumped = false;
private boolean movingLeft = false;
private boolean movingRight = false;
private boolean ducked = false;//sit
private boolean readyToFire = true;

private int health=100;//health of mouse
private int kamikaziKilled;//how many kamikazi the mouse killed
private int collectedCheeses;//how many cheeses the mouse collected
private int levelPassed;

private int speedX = 0;//start speed of x axis
private int speedY = 0;//start speed of y axis



//rectangles for collision
public static Rect rect = new Rect(0, 0, 0, 0);
public static Rect rect2 = new Rect(0, 0, 0, 0);
public static Rect rect3 = new Rect(0, 0, 0, 0);
public static Rect rect4 = new Rect(0, 0, 0, 0);
public static Rect yellowRed = new Rect(0, 0, 0, 0);

public static Rect footleft = new Rect(0,0,0,0);
public static Rect footright = new Rect(0,0,0,0);


private Background bg1 = GameScreen.getBg1();
private Background bg2 = GameScreen.getBg2();

private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();

//update position and speed of mouse
public void update() {

// move mouse and scroll background
if (speedX < 0) {
centerX += speedX;
}
if (speedX == 0 || speedX < 0) {
if (bg1 != null)
bg1.setSpeedX(0);
if (bg2 != null)
bg2.setSpeedX(0);

}
if (centerX <= 200 && speedX > 0) {
centerX += speedX;
}
if (speedX > 0 && centerX > 200) {
bg1.setSpeedX(-MOVESPEED / 5);
bg2.setSpeedX(-MOVESPEED / 5);
}

//update y axis
centerY += speedY;

//handles jumping

speedY += 1;

//if the speed is bigger than 3, flag jumped
if (speedY > 3){// jump height
jumped = true;
}

// Prevents going beyond X coordinate of 0
if (centerX + speedX <= 60) {
centerX = 61;
}

rect.set(centerX - 34, centerY - 63, centerX + 34, centerY);
rect2.set(rect.left, rect.top + 63, rect.left+68, rect.top + 128);
rect3.set(rect.left - 26, rect.top+32, rect.left, rect.top+52);
rect4.set(rect.left + 68, rect.top+32, rect.left+94, rect.top+52);
yellowRed.set(centerX - 110, centerY - 110, centerX + 70, centerY + 70);
footleft.set(centerX - 50, centerY + 20, centerX, centerY + 35);
footright.set(centerX, centerY + 20, centerX+50, centerY+35);


}

//set horizontal speed of mouse
public void moveRight() {
if (ducked == false) {
speedX = MOVESPEED;
}
}

//set horizontal speed of mouse
public void moveLeft() {
if (ducked == false) {
speedX = -MOVESPEED;
}
}

//stop moving right
public void stopRight() {
setMovingRight(false);
stop();
}

//stop moving left
public void stopLeft() {
setMovingLeft(false);
stop();
}

//where to move check
private void stop() {
if (isMovingRight() == false && isMovingLeft() == false) {
speedX = 0;
}

if (isMovingRight() == false && isMovingLeft() == true) {
moveLeft();
}

if (isMovingRight() == true && isMovingLeft() == false) {
moveRight();
}

}

//set vertical speed of mouse
public void jump() {
if (jumped == false) {
//if  sound settings are on
if (Settings.soundEnabled)
Assets.jump.play(1);
speedY = JUMPSPEED;
jumped = true;
}

}

//create new bullet and add to the list
public void shoot() {
if (readyToFire) {
//if  sound settings are on
if (Settings.soundEnabled)
Assets.fire.play(1);
Projectile p = new Projectile(centerX, centerY+34);//make the bullets appear from the gun
projectiles.add(p);
}
}

public boolean isJumped() {
return jumped;
}

public boolean isDucked() {
return ducked;
}

public boolean isReadyToFire() {
return readyToFire;
}

public void addCollectedCheeses(int collectedCheeses){
this.collectedCheeses+=collectedCheeses;
}

public void addKamikaziKilled(int kamikaziKilled)
{
this.kamikaziKilled+=kamikaziKilled;
}
public void subtractHealth(int health){
this.health-=health;
}



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

public int getCenterY() {
return centerY;
}

public int getSpeedX() {
return speedX;
}

public int getSpeedY() {
return speedY;
}
public int getHealth(){
return health;
}

public int getKamikaziKilled()
{
return kamikaziKilled;
}

public int getCollectedCheeses()
{
return collectedCheeses;
}

public int  getLevelPassed()
{
return levelPassed;
}
//reference to the bullets
public ArrayList getProjectiles() {
return projectiles;
}

//setters//
public void setDucked(boolean ducked) {
this.ducked = ducked;
}

public boolean isMovingRight() {
return movingRight;
}

public void setMovingRight(boolean movingRight) {
this.movingRight = movingRight;
}

public boolean isMovingLeft() {
return movingLeft;
}

public void setMovingLeft(boolean movingLeft) {
this.movingLeft = movingLeft;
}

public void setReadyToFire(boolean readyToFire) {
this.readyToFire = readyToFire;
}

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

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

public void setJumped(boolean jumped) {
this.jumped = jumped;
}

public void setSpeedX(int speedX) {
this.speedX = speedX;
}

public void setSpeedY(int speedY) {
this.speedY = speedY;
}
public void setLevelPassed(int levelPassed) {
this.levelPassed = levelPassed;
}

}


Projectile.java
//Name: Projectile.java
//Purpose: the functions of the bullets

package com.basithsteviejhei.mouse;

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

public class Projectile {

//local variables
private int x, y, speedX;//coordinates and speed of bullet
private boolean visible;//until bullet hit or move out of screen is visible

private Rect r;

//constructor
public Projectile(int startX, int startY){
x = startX;
y = startY;
speedX = 7;
visible = true;

r = new Rect(0, 0, 0, 0);
}

//change position of bullet and check for collision
public void update(){
x += speedX;
r.set(x, y, x+10, y+5);

//check bullet if there is inside of the screen
if (x > 800){
visible = false;
r = null;
}
//so if there is inside of the screen, we can check for collision
if (visible){
checkCollision();

}

}

//check for collision of enemies and the bullets
private void checkCollision() {

//check if the bullet hit and enemy and subtract from health

//enemy 1
if (Rect.intersects(r, GameScreen.hb.r)){
visible = false;

if (GameScreen.hb.getHealth() > 0) {
GameScreen.hb.subtractHealth(1);
}
if (GameScreen.hb.getHealth() == 0) {
GameScreen.hb.setCenterX(-100);
GameScreen.hb.checkKillsByBullets();
}

}

//enemy2
if (Rect.intersects(r, GameScreen.hb2.r)){
visible = false;

if (GameScreen.hb2.getHealth() > 0) {
GameScreen.hb2.subtractHealth(1);
}
if (GameScreen.hb2.getHealth() == 0) {
GameScreen.hb2.setCenterX(-100);
GameScreen.hb2.checkKillsByBullets();

}
}

//enemy3
if (Rect.intersects(r, GameScreen.hb3.r)){
visible = false;

if (GameScreen.hb3.getHealth() > 0) {
GameScreen.hb3.subtractHealth(1);
}
if (GameScreen.hb3.getHealth() == 0) {
GameScreen.hb3.setCenterX(-100);
GameScreen.hb3.checkKillsByBullets();

}
}
//enemy4
if (Rect.intersects(r, GameScreen.hb4.r)){
visible = false;

if (GameScreen.hb4.getHealth() > 0) {
GameScreen.hb4.subtractHealth(1);
}
if (GameScreen.hb4.getHealth() == 0) {
GameScreen.hb4.setCenterX(-100);
GameScreen.hb4.checkKillsByBullets();

}
}
//enemy5
if (Rect.intersects(r, GameScreen.hb5.r)){
visible = false;

if (GameScreen.hb5.getHealth() > 0) {
GameScreen.hb5.subtractHealth(1);
}
if (GameScreen.hb5.getHealth() == 0) {
GameScreen.hb5.setCenterX(-100);
GameScreen.hb5.checkKillsByBullets();

}
}
//enemy6
if (Rect.intersects(r, GameScreen.hb6.r)){
visible = false;

if (GameScreen.hb6.getHealth() > 0) {
GameScreen.hb6.subtractHealth(1);
}
if (GameScreen.hb6.getHealth() == 0) {
GameScreen.hb6.setCenterX(-100);
GameScreen.hb6.checkKillsByBullets();

}
}
//enemy7
if (Rect.intersects(r, GameScreen.hb7.r)){
visible = false;

if (GameScreen.hb7.getHealth() > 0) {
GameScreen.hb7.subtractHealth(1);
}
if (GameScreen.hb7.getHealth() == 0) {
GameScreen.hb7.setCenterX(-100);
GameScreen.hb7.checkKillsByBullets();

}
}
//enemy8
if (Rect.intersects(r, GameScreen.hb8.r)){
visible = false;

if (GameScreen.hb8.getHealth() > 0) {
GameScreen.hb8.subtractHealth(1);
}
if (GameScreen.hb8.getHealth() == 0) {
GameScreen.hb8.setCenterX(-100);
GameScreen.hb8.checkKillsByBullets();

}
}
//enemy9
if (Rect.intersects(r, GameScreen.hb9.r)){
visible = false;

if (GameScreen.hb9.getHealth() > 0) {
GameScreen.hb9.subtractHealth(1);
}
if (GameScreen.hb9.getHealth() == 0) {
GameScreen.hb9.setCenterX(-100);
GameScreen.hb9.checkKillsByBullets();

}
}
}

public boolean isVisible() {
return visible;
}

//getters//
public int getX() {
return x;
}

public int getY() {
return y;
}

public int getSpeedX() {
return speedX;
}



//setters//
public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

public void setSpeedX(int speedX) {
this.speedX = speedX;
}

public void setVisible(boolean visible) {
this.visible = visible;
}


}




Related Posts

Post a Comment

Subscribe Our Newsletter