Berikut ini adalah lanjutan dari Tutorial membuat game hangman di android Part 1
Setelah temen-temen mengikuti step by stepnya pada Tutorial membuat game hangman di android Part 1 kemudian ikuti step by step pada Part 2 ini
1. masuk ke "MainActivity.java" ganti dengan source code dibawah ini
package com.example.hangman;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
//import android.view.Menu;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RatingBar;
/**
* - Android SDK: Create a Hangman Game
*
* Setia budi - July 2014
*/
public class MainActivity extends Activity implements OnClickListener {
MediaPlayer audioBackground;
Button help;
Button rate;
private AlertDialog helpAlert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button playBtn = (Button)findViewById(R.id.playBtn);
playBtn.setOnClickListener(this);
audioBackground = MediaPlayer.create(this, R.raw.seram);
audioBackground.setLooping(true);
audioBackground.setVolume(1,1);
//Memulai audio
audioBackground.start();
help=(Button) findViewById(R.id.help1);
help.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showHelp();
}
});
rate=(Button) findViewById(R.id.rate1);
rate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showRate();
}
});
}
public void showHelp(){
AlertDialog.Builder helpBuild = new AlertDialog.Builder(this);
helpBuild.setTitle("Help");
helpBuild.setMessage("Menebak kata dengan memilih huruf.\n\n" + "Anda hanya memiliki 7 kesempatan jika anda salah \n\n"
+ "maka satu persatu gambar akan keluar mulai dari kepala sampai kaki,\n\n"
+ " maka permainan akan berakhir!");
helpBuild.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
helpAlert.dismiss();
}});
helpAlert = helpBuild.create();
helpBuild.show();
}
public void showRate(){
AlertDialog.Builder rateBuild = new AlertDialog.Builder(this);
rateBuild.setTitle("Rate");
final RatingBar rating = new RatingBar(this);
rating.setMax(6);
rateBuild.setIcon(android.R.drawable.btn_star_big_on);
rateBuild.setView(rating);
//t1.setTitle(String.valueOf(rating));
rateBuild.setMessage("Please Rate Us");
rateBuild.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
helpAlert.dismiss();
}});
helpAlert = rateBuild.create();
rateBuild.show();
}
public void onBackPressed() {
exit();//Pergi ke method exit
}
private void exit()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exit !!!");
builder.setMessage("Yakin ingin Keluar")
.setCancelable(false)
//jika pilih ya
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
audioBackground.stop();
finish();
}
})
//jika pilih tidak
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
@Override
public void onClick(View view) {
//handle clicks
if(view.getId()==R.id.playBtn){
Intent playIntent = new Intent(this, GameActivity.class);
this.startActivity(playIntent);
{
audioBackground.setVolume(0, 0);
}
}
}
}
2.create class baru beri nama "GameActivity.java" kemudian pastekan source code dibawah
package com.example.hangman;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
//import android.widget.RatingBar;
import android.widget.TextView;
/**
* - Android SDK: Create a Hangman Game
*
* Setia budi - July 2014
*/
public class GameActivity extends Activity {
MediaPlayer audioBackground;
//the words
private String[] words;
//random for word selection
private Random rand;
//store the current word
private String currWord;
//the layout holding the answer
private LinearLayout wordLayout;
//text views for each letter in the answer
private TextView[] charViews;
//letter button grid
private GridView letters;
//letter button adapter
private LetterAdapter ltrAdapt;
//body part images
private ImageView[] bodyParts;
//total parts
private int numParts=6;
//current part
private int currPart;
//num chars in word
private int numChars;
//num correct so far
private int numCorr;
//help
private AlertDialog helpAlert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Resources res = getResources();
words = res.getStringArray(R.array.words);
rand = new Random();
currWord="";
wordLayout = (LinearLayout)findViewById(R.id.word);
letters = (GridView)findViewById(R.id.letters);
bodyParts = new ImageView[numParts];
bodyParts[0] = (ImageView)findViewById(R.id.head);
bodyParts[1] = (ImageView)findViewById(R.id.body);
bodyParts[2] = (ImageView)findViewById(R.id.arm1);
bodyParts[3] = (ImageView)findViewById(R.id.arm2);
bodyParts[4] = (ImageView)findViewById(R.id.leg1);
bodyParts[5] = (ImageView)findViewById(R.id.leg2);
playGame();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_help:
showHelp();
return true;
}
return super.onOptionsItemSelected(item);
}
//play a new game
private void playGame(){
String newWord = words[rand.nextInt(words.length)];
while(newWord.equals(currWord)) newWord = words[rand.nextInt(words.length)];
currWord = newWord;
charViews = new TextView[currWord.length()];
wordLayout.removeAllViews();
for(int c=0; c<currWord.length(); c++){
charViews[c] = new TextView(this);
charViews[c].setText(""+currWord.charAt(c));
charViews[c].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
charViews[c].setGravity(Gravity.CENTER);
charViews[c].setTextColor(Color.WHITE);
charViews[c].setBackgroundResource(R.drawable.letter_bg);
wordLayout.addView(charViews[c]);
}
ltrAdapt=new LetterAdapter(this);
letters.setAdapter(ltrAdapt);
currPart=0;
numChars=currWord.length();
numCorr=0;
for(int p=0; p<numParts; p++){
bodyParts[p].setVisibility(View.INVISIBLE);
}
}
//letter pressed method
public void letterPressed(View view){
//find out which letter was pressed
String ltr=((TextView)view).getText().toString();
char letterChar = ltr.charAt(0);
view.setEnabled(false);
view.setBackgroundResource(R.drawable.letter_down);
boolean correct=false;
for(int k=0; k<currWord.length(); k++){
if(currWord.charAt(k)==letterChar){
correct=true;
numCorr++;
charViews[k].setTextColor(Color.BLUE);
}
}
if(correct){
if(numCorr==numChars){
//disable all buttons
disableBtns();
AlertDialog.Builder winBuild = new AlertDialog.Builder(this);
winBuild.setTitle("YOU WIN");
winBuild.setMessage("Jawaban : "+currWord);
winBuild.setPositiveButton("Main Lagi",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.playGame();
}});
winBuild.setNegativeButton("Keluar",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.finish();
}});
winBuild.setIcon(R.drawable.sukses);
winBuild.show();
}
}
//check if user still has guesses
else if(currPart<numParts){
bodyParts[currPart].setVisibility(View.VISIBLE);
currPart++;
}
else{
//user has lost
disableBtns();
AlertDialog.Builder loseBuild = new AlertDialog.Builder(this);
loseBuild.setTitle("YOU LOSE");
loseBuild.setMessage("Jawaban : "+currWord);
loseBuild.setPositiveButton("Main Lagi",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.playGame();
}});
loseBuild.setNegativeButton("Keluar",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.finish();
}});
loseBuild.setIcon(R.drawable.fail);
loseBuild.show();
}
}
//disable letter buttons
public void disableBtns(){
int numLetters = letters.getChildCount();
for(int l=0; l<numLetters; l++){
letters.getChildAt(l).setEnabled(false);
}
}
//show help information
public void showHelp(){
AlertDialog.Builder helpBuild = new AlertDialog.Builder(this);
helpBuild.setTitle("Help");
helpBuild.setMessage("Menebak kata dengan memilih huruf.\n\n"
+ "Anda hanya memiliki 7 kesempatan jika anda salah \n\n"
+ "maka satu persatu gambar akan keluar mulai dari kepala sampai kaki,\n\n"
+ " maka permainan akan berakhir!");
helpBuild.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
helpAlert.dismiss();
}});
helpAlert = helpBuild.create();
helpBuild.show();
}
}
3.create class baru lagi beri nama "LatterAdapter.java" kemudian pastekan source code dibawahpackage com.example.hangman;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
/**
* - Android SDK: Create a Hangman Game
*
* setia budi - July 2014
*/
public class LetterAdapter extends BaseAdapter {
//store letters
private String[] letters;
//inflater for button layout
private LayoutInflater letterInf;
public LetterAdapter(Context c) {
//instantiate alphabet array
letters=new String[26];
for(int a=0; a<letters.length; a++){
letters[a]=""+(char)(a+'A');
}
//specify layout to inflate
letterInf = LayoutInflater.from(c);
}
@Override
public int getCount() {
return letters.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//create a button for the letter at this position in the alphabet
Button letterBtn;
if (convertView == null) {
//inflate the button layout
letterBtn = (Button)letterInf.inflate(R.layout.letter, parent, false);
} else {
letterBtn = (Button) convertView;
}
//set the text to this letter
letterBtn.setText(letters[position]);
return letterBtn;
}
}
4.kemudian masuk ke "AndroidManifest" ganti source code tersebut dengan source code dibawah ini
5. Running project tersebut jangan lupa save all terlebih dahulu
screenshot :
semoga bermanfaat :)
NOTE :
Jika sobat blogger ingin full project hangman ini bisa komen dibawah ini sertakan email kalian.Terima kasih :)








gan boleh minta full projectnya..
ReplyDeleteemail saya beardw8@gmail.com
terimakasih