Kamis, 21 Mei 2015

Membuat Permainan Sederhana

Haloo Semua,
Kali ini saya akan membagikan bagaimana cara membuat simple game atau permainan sederhana. Di simple game ini saya hanya membuat halaman untuk permainan nya saja, untuk itu silahkan kembangkan sendiri yaa …
di permainan ini, saya buat beberapa fitur seperti :
1. Random Images/gambar
2. Random Jawaban
3. Score/ Nilai
Jadi, permainan ini sangat simple, user di haruskan menjawab dari gambar yang tersedia. Misalnya : jika pada gambar 1 menunjukan gambar sapi, maka user harus menjawab sapi pada kolom inputan “Gambar 1″. Setelah user menjawab semua gambar, maka user akan mendapatkan score. Pada simple game ini juga terdapat dua level. Dimana level 1 setiap jawaban yang benar akan mendapat 100 point, sedangkan di level 2 akan mendapat 150 point per jawaban yang benar.



Berikut screenshoot aplikasinya :
Dihalaman ini, user berada pada level 1 dan harus menjawab sesuai dengan gambar yang tersedia.
kreativdeveloper_random_image_1
Dihalaman ini, user berada masih pada level 1. Jika semua data di isi dan menekan tombol jawab, maka score akan ditampilkan.
kreativdeveloper_random_image_2
Dihalaman ini, user berada pada level 2 dan harus menjawab sesuai dengan gambar yang tersedia.
kreativdeveloper_random_image_3
Dihalaman ini, user berada masih pada level 2. Jika semua data di isi dan menekan tombol jawab, maka score akan ditampilkan. Mulai dari score level 1 dan leve 2 serta total scorenya
kreativdeveloper_random_image_4
Baik, sekang langsung ke kodingnya aja. Pertama kalian harus menyiapkanactivity_main.xml untuk layout game nya. Berikut kodenya :
<!-- @author yudirahmat -->
<!-- @email yudirahmat7@gmail.com -->
<!-- @wordpress kreativdeveloper.wordpress.com -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >

 <LinearLayout
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="50"
 android:orientation="vertical" >

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_margin="15dp"
 android:layout_weight="1"
 android:orientation="horizontal" >

 <RelativeLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1" >

 <ImageView
 android:id="@+id/iv_gambar1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:contentDescription="@string/app_name"
 android:src="@drawable/icon_cat" />

 <TextView
 android:id="@+id/label_1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingLeft="5dp"
 android:text="1"
 android:textColor="#3e3e3e"
 android:textSize="13sp" />
 </RelativeLayout>

 <RelativeLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1" >

 <ImageView
 android:id="@+id/iv_gambar2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:contentDescription="@string/app_name"
 android:src="@drawable/icon_cow" />

 <TextView
 android:id="@+id/label_2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingLeft="5dp"
 android:text="2"
 android:textColor="#3e3e3e"
 android:textSize="13sp" />
 </RelativeLayout>
 </LinearLayout>

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_margin="15dp"
 android:layout_weight="1"
 android:orientation="horizontal" >

 <RelativeLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1" >

 <ImageView
 android:id="@+id/iv_gambar3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:contentDescription="@string/app_name"
 android:src="@drawable/icon_dog" />

 <TextView
 android:id="@+id/label_3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingLeft="5dp"
 android:text="3"
 android:textColor="#3e3e3e"
 android:textSize="13sp" />
 </RelativeLayout>

 <RelativeLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1" >

 <ImageView
 android:id="@+id/iv_gambar4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:contentDescription="@string/app_name"
 android:src="@drawable/icon_fish" />

 <TextView
 android:id="@+id/label_4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingLeft="5dp"
 android:text="4"
 android:textColor="#3e3e3e"
 android:textSize="13sp" />
 </RelativeLayout>
 </LinearLayout>

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_margin="15dp"
 android:layout_weight="1"
 android:orientation="horizontal" >

 <RelativeLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1" >

 <ImageView
 android:id="@+id/iv_gambar5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:contentDescription="@string/app_name"
 android:src="@drawable/icon_lion" />

 <TextView
 android:id="@+id/label_5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingLeft="5dp"
 android:text="5"
 android:textColor="#3e3e3e"
 android:textSize="13sp" />
 </RelativeLayout>

 <RelativeLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1" >

 <ImageView
 android:id="@+id/iv_gambar6"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:contentDescription="@string/app_name"
 android:src="@drawable/icon_monkey" />

 <TextView
 android:id="@+id/label_6"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingLeft="5dp"
 android:text="6"
 android:textColor="#3e3e3e"
 android:textSize="13sp" />
 </RelativeLayout>
 </LinearLayout>
 </LinearLayout>

 <ScrollView
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="50" >

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:padding="10dp" >

 <EditText
 android:id="@+id/edt_1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:hint="Gambar 1"
 android:textSize="17sp" />

 <EditText
 android:id="@+id/edt_2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="5dp"
 android:hint="Gambar 2"
 android:textSize="17sp" />

 <EditText
 android:id="@+id/edt_3"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="5dp"
 android:hint="Gambar 3"
 android:textSize="17sp" />

 <EditText
 android:id="@+id/edt_4"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="5dp"
 android:hint="Gambar 4"
 android:textSize="17sp" />

 <EditText
 android:id="@+id/edt_5"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="5dp"
 android:hint="Gambar 5"
 android:textSize="17sp" />

 <EditText
 android:id="@+id/edt_6"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="5dp"
 android:hint="Gambar 6"
 android:textSize="17sp" />

 <Button
 android:id="@+id/btnSend"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_margin="7dp"
 android:text="Jawab" />
 </LinearLayout>
 </ScrollView>

</LinearLayout>
Selanjutnya, kalian buat koding di file MainActivity.java. Berikut kodenya
package com.kreativedeveloper.random;

import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @author yudirahmat
 * @email yudirahmat7@gmail.com
 * @wordpress kreativdeveloper.wordpress.com
 */

@SuppressLint("ShowToast")
public class MainActivity extends Activity {
 private Button btnJawab;
 private ImageView gambar1, gambar2, gambar3, gambar4, gambar5, gambar6;
 private EditText edt1, edt2, edt3, edt4, edt5, edt6;
 
 private int Score = 0;
 
 int[] solutionArray = { R.drawable.icon_cat, R.drawable.icon_cow, R.drawable.icon_dog, R.drawable.icon_fish, R.drawable.icon_lion, R.drawable.icon_monkey };
 
 String[] ArrayName = {"KUCING", "SAPI", "ANJING", "IKAN", "SINGA", "MONYET"};

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 gambar1 = (ImageView) findViewById(R.id.iv_gambar1);
 gambar2 = (ImageView) findViewById(R.id.iv_gambar2);
 gambar3 = (ImageView) findViewById(R.id.iv_gambar3);
 gambar4 = (ImageView) findViewById(R.id.iv_gambar4);
 gambar5 = (ImageView) findViewById(R.id.iv_gambar5);
 gambar6 = (ImageView) findViewById(R.id.iv_gambar6);
 
 edt1 = (EditText) findViewById(R.id.edt_1);
 edt2 = (EditText) findViewById(R.id.edt_2);
 edt3 = (EditText) findViewById(R.id.edt_3);
 edt4 = (EditText) findViewById(R.id.edt_4);
 edt5 = (EditText) findViewById(R.id.edt_5);
 edt6 = (EditText) findViewById(R.id.edt_6);
 
 btnJawab = (Button) findViewById(R.id.btnSend);

 shuffleArray(solutionArray, ArrayName);
 setImageData(solutionArray);
 
 btnJawab.setOnClickListener(new OnClickListener() { 
 @Override
 public void onClick(View v) {
 CheckData();
 }
 });
 
 }
 
 /**
 * Pengecekan Data Ketika Menekan Tombol Jawab 
 * Apabila Gambar Sesuai Dengan Jawaban Maka,
 * User Akan Mendapat nilai 10 pada setiap jawaban yang benar*/
 @SuppressLint("DefaultLocale")
 private void CheckData() {
 String edt_1 = edt1.getText().toString().toUpperCase();
 String edt_2 = edt2.getText().toString().toUpperCase();
 String edt_3 = edt3.getText().toString().toUpperCase();
 String edt_4 = edt4.getText().toString().toUpperCase();
 String edt_5 = edt5.getText().toString().toUpperCase();
 String edt_6 = edt6.getText().toString().toUpperCase();
 
 if(edt_1.equals("") || edt_2.equals("") || edt_3.equals("") || edt_4.equals("") || edt_5.equals("") || edt_6.equals("")) {
 showDialogMessage("Peringatan", "Silahkan Isi Semua Data");
 } else {
 if(edt_1.equals(ArrayName[0]))
 Score += 100;
 if(edt_2.equals(ArrayName[1]))
 Score += 100;
 if(edt_3.equals(ArrayName[2]))
 Score += 100;
 if(edt_4.equals(ArrayName[3]))
 Score += 100;
 if(edt_5.equals(ArrayName[4]))
 Score += 100;
 if(edt_6.equals(ArrayName[5]))
 Score += 100;
 
 showDialogMessage("Level 1", "Score Anda : " + Score);
 }
 
 }
 
 @SuppressWarnings("unused")
 private void ShowToast(String text) {
 Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT);
 }
 
 private void showDialogMessage(String title, String message) {
 final AlertDialog alertDialog;
 alertDialog = new AlertDialog.Builder(this).create();
 alertDialog.setCanceledOnTouchOutside(false);
 alertDialog.setCancelable(false);
 alertDialog.setTitle(title);
 alertDialog.setMessage(message);
 alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Ok",
 new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int which) {
 dialog.dismiss();
 
 Score = 0;
 }
 });
 alertDialog.show();
 }
 /** Setelah gambar di random/ diacak, maka gambar di tampilkan*/
 private void setImageData(int[] ar) {
 gambar1.setImageResource(ar[0]);
 gambar2.setImageResource(ar[1]);
 gambar3.setImageResource(ar[2]);
 gambar4.setImageResource(ar[3]);
 gambar5.setImageResource(ar[4]);
 gambar6.setImageResource(ar[5]);
 }
 
 /** Data gambar dan jawaban di acak */
 static void shuffleArray(int[] ar, String[] arrName) {
 Random rnd = new Random();
 for (int i = ar.length - 1; i > 0; i--) {
 int index = rnd.nextInt(i + 1);
 // Simple swap
 int a = ar[index];
 ar[index] = ar[i];
 ar[i] = a;
 
 String b = arrName[index];
 arrName[index] = arrName[i];
 arrName[i] = b;
 }
 }

}
Jangan lupa untuk meng-copy gambar-gambar yang dibutuhkan dalam membuat simple game ini, silahkan download gambar-gambarnya disiniSetelah mendownload gambar-gambar nya, lalu kalian copy di “res -> drawable-hdpi”.
Untuk full source code nya silahkan download disiniJika kalian hanya ingin coba install Apk nya ada disini
Sekian duluuu yaa …
Maaf kalo ada kalimat/ kata yang salah hehe :))
Silahkan Tinggalkan Komentar n_n

Tidak ada komentar:

Posting Komentar