-->

Tutorial membuat game tikus lapar bagian 1

Post a Comment
Oke gan langusng aja jika kalian pernah melihat atau memainkan game super mario kira kira gambaran aplikasinya kayak gitu,
oke langung saja kita membuat projek baru di eclipse dan ini mungkin sudah tidak perlu di jelaskan lagi karna saya sudah pernah membahasnya di postingan sebelumnya.

Nama Pack "com.basithsteviejhei.mouse"
Ubah Nama pack MainActivity menjadi "SampleGame"
setelah projek eclipse terbuka

1. Buka folder Value "Streing.xml" edit menjadi seperti kodingan di bawah
<resources>

    <string name="app_name">Hungry Mouse</string>

</resources>

2. Buatlah fonder baru di "Res" dan berinama "raw" di dalam folder "raw " buat file baru dengan nama "map1.txng

! level 1 map
! stage environment begin from line 11 to 22
! # = Tile Legend
! 5 = TileDirt
! 8 = GrassTop
! 2 = GrassBot
! 4 = GrassLeft
! 6 = GrassRight
! Hungry Mouse Level 1 Environment
!
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
                                                                                                                                                                                                                         
                                                                                                                                                                                                                         
                                                                                                                                                                                                                         
                                                                                                                                                                                                                         
                                                                                                                                                                       
                                                                                                                                                                   
                                                                                                                                                                     
                                                                                                                                         
88888888888888888888    88888888888888888888   8888888888888   8888888888888888888888888888888888888    88888888888888   888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888                                                                                                                            
55555555555555555555    55555555555555555555   5555555555555   5555555555555555555555555555555555555    55555555555555   555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555                                                    
55555555555555555555    55555555555555555555   5555555555555   5555555555555555555555555555555555555    55555555555555   555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555


3. Buka folder "src" folder "com.basithsteviejhei.mouse" edit file "SampleGame.java" menjadi seperti ini
package com.basithsteviejhei.mouse;

import com.basithsteviejhei.fremework.Screen;
import com.basithsteviejhei.fremework.implement.AndroidGame;
//java libraries
import java.io.BufferedReader;//wraps and existing reader and buffers the input
import java.io.IOException;//signal that i/o exception has occurred
import java.io.InputStream;//represent input stream of bytes
import java.io.InputStreamReader;//turn a byte stream to character stream

//android libraries stored in SDK platform
import android.util.Log;//API for sending log output

public class SampleGame extends AndroidGame {

public static String map;
boolean firstTimeCreate = true;

//check if its the first time that class is opened.
@Override
public Screen getInitScreen() {

if (firstTimeCreate) {
Assets.load(this);//load assets
firstTimeCreate = false;
}

//load map-level
InputStream is = getResources().openRawResource(R.raw.map1);

//create the level
map = convertStreamToString(is);

//we return to the splash screen
return new SplashLoadingScreen(this);

}

//go back at every screen when key is pressed
@Override
public void onBackPressed() {
getCurrentScreen().backButton();
}

//get txt file and return string
private static String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
Log.w("LOG", e.getMessage());
} finally {
try {
is.close();
} catch (IOException e) {
Log.w("LOG", e.getMessage());
}
}
return sb.toString();
}

@Override
public void onResume() {
super.onResume();

}

@Override
public void onPause() {
super.onPause();
Assets.theme.pause();

}


}

4. buat file baru di pack "com.basithsteviejhei.mouse"

  1. AboutScreen.java
  2. Animation.java
  3. Assets.java
  4. Background.java
  5. Bomb.java 
  6. Cheese,java
  7. Enemy.java
  8. GameScreen.java
  9. HelpScreen1.java
  10. HelpScreen2.java
  11. HelpScreen3.java
  12. HelpScreen4.java
  13. Kamikazi.java 
  14. LevelSelectorScreen.java
  15. LoadingScreen.java 
  16. MainMenuScreen.java 
  17. Mouse.java
  18. Projectile.java
  19. Rewards.java
  20. Settings.java 
  21. Sign.java 
  22. SplashLoading.java 
  23. Tile.java  

AboutScreen.java
//Name: AboutScreen.java
//Purpose: create the about screen that include project information

package com.basithsteviejhei.mouse;

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

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

public class AboutScreen extends Screen{

//constructor
public AboutScreen(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) {
if (inBounds(event, 16, 368, 96, 96)) {
game.setScreen(new MainMenuScreen(game));//change screen when button pressed
                  if (Settings.soundEnabled)
                      Assets.click.play(1);
return;
}

}
}
}

//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.about, 0, 0);
g.drawImage(Assets.logo,  224, 16);
g.drawImage(Assets.arrow_buttons,  16, 368, 0, 0, 96, 96);

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {

}

@Override
public void backButton() {
// TODO Auto-generated method stub

}


}


Animation.java
//Name: Animation.java
//Purpose: display different images in sequence based on the duration specified

package com.basithsteviejhei.mouse;

import com.basithsteviejhei.fremework.Image;

//java libraries
import java.util.ArrayList;//implementation of list with additional operations add,remove,replace

public class Animation {

//local variables
private ArrayList frames;//contain animframe objects
private int currentFrame;//numerical location in the list
//long is more accurate than int
private long animTime;//keep track of time elapsed since last image
private long totalDuration;//how long each image will be displayed

//constructor
public Animation() {
frames = new ArrayList();
totalDuration = 0;

//threads are called together
synchronized (this) {
animTime = 0;
currentFrame = 0;
}
}

//add animframe object to animation list
//threads are called together
public synchronized void addFrame(Image image, long duration) {
totalDuration += duration;
frames.add(new AnimFrame(image, totalDuration));
}

//update current frame with its appropriate image
public synchronized void update(long elapsedTime) {
if (frames.size() > 1) {

//add elapsed time to anime time
animTime += elapsedTime;

//if animation time exceeds total duration we change frame
if (animTime >= totalDuration) {
animTime = animTime % totalDuration;
currentFrame = 0;

}

while (animTime > getFrame(currentFrame).endTime) {
currentFrame++;

}
}
}

//get current frame`s image to paint(present) it
//threads are called together
public synchronized Image getImage() {
if (frames.size() == 0) {
return null;
} else {
return getFrame(currentFrame).image;
}
}

//get the frame`s location from the list
private AnimFrame getFrame(int i) {
return (AnimFrame) frames.get(i);
}

//create objects to contain current images and duration to be displayed
private class AnimFrame {

Image image;
long endTime;

//nested class
public AnimFrame(Image image, long endTime) {
this.image = image;
this.endTime = endTime;//the end time to be displayed
}
}
}


Assets.java
//Name: Assets.java
//Purpose: assign reference variables for all the assets,images sound and music
// loadingscreen.java will load all the assets for the game during the splash screen

package com.basithsteviejhei.mouse;

import com.basithsteviejhei.fremework.Image;
import com.basithsteviejhei.fremework.Music;
import com.basithsteviejhei.fremework.Sound;

public class Assets {

//use static so there is no need to create object to access them
//also static is 15%-20% faster since there is no need of an object

//rectangle buttons
public static Image menu_buttons;

//small buttons
public static Image pause_button;

public static Image sound_on_buttons,music_on_buttons,info_buttons,help,logo,play_buttons;
public static Image about,help1,help2,help3,help4,arrow_buttons,control_buttons,gyroscope_buttons,level_buttons,level1_button,level2_button,level3_button;
public static Image cheese_reward,bomb,bomb2,sign;
public static Image menu, splash, background, character, character2, character3, Kamikazi, Kamikazi2, Kamikazi3, Kamikazi4, Kamikazi5;
public static Image tiledirt, tilegrassTop, tilegrassBot, tilegrassLeft, tilegrassRight, characterJump, characterDown;
public static Sound click,fire,jump,collision,gameover,passed,collect;
public static Music theme;

public static void load(SampleGame sampleGame) {
// TODO Auto-generated method stub
theme = sampleGame.getAudio().createMusic("menutheme.mp3");//get the asset
theme.setLooping(true);//repeat background music
theme.setVolume(0.80f);//volume set to 80%
theme.play();//play
}

}



Related Posts

Post a Comment

Subscribe Our Newsletter