First game in Android/sample program in android/simple android game program
main.xml
By following the tutorials given in android basics, you can now develop a simple, but funny game. I think, you can now easily handle button and its click events. Ok, first create a new Android Project. As said earlier, first click on “New Project”, select “Android” and then fill out the form with suitable details.
First add an image of your favorite actor’s or actress ‘s photo in the drawable folder(Just copy the photo and right click on the drawable folder and click on “paste”). Then go to main.xml. Now create a layout and add four image views to the main.xml.
I have created a table layout and added four image views. Then make each button clickable as we did in our first application. Then add your favorite actor/actress images as background.
<ImageView
android:text=""
android:id="@+id/image1"
android:layout_width="100dip"
android:layout_height="100dip"
android:background="@drawable/pic"
android:clickable="true"
android:onClick="button1"
</ImageView>
Then select your activity class. To access the ImageView created in xml file to your activity class, you need to call findViewById method. Add argument “R.layout.idofimageview”.
ImageView imageview=(ImageView)findViewById(R.layout.imageview1);
Similarly create instance of four ImageView created in main.xml. To make each image clickable, create methods and pass the instance of the class View
Here, the important part of the code comes. We need some random values to load the image dynamically. For that, put some pure java code to calculate some random number.
int a=Math.random();
int b=a*10;
String num=b+"";
If you know any other method to do this, you can implement that code. .
To display the score create a dynamic text view and load the mark…
By integrating all the above code, we get
GameProjectActivity.java
package game.project; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class GameProjectActivity extends Activity { double a,b; String num; ImageView imgone; int mark; ImageView imgtwo,imgthree,imgfour; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imgone=(ImageView)findViewById(R.id.image1); imgtwo=(ImageView)findViewById(R.id.image2); imgthree=(ImageView)findViewById(R.id.image3); imgfour=(ImageView)findViewById(R.id.image4); a=Math.random(); b=a*10; num=b+""; } public void display() { TextView textView=new TextView(this); textView.setText("Your score 4.."); setContentView(textView); } public void button1(View v) { if(num.startsWith("0") || num.startsWith("3") || num.startsWith("8")) { imgone.setImageResource(R.drawable.pic); mark=4; display(); } else{imgone.setImageResource(R.drawable.crazy);} } public void button2(View v) { if(num.startsWith("1") || num.startsWith("5") ) { imgtwo.setImageResource(R.drawable.pic); mark=4; display(); }else{imgtwo.setImageResource(R.drawable.crazy);} } public void button3(View v) { if(num.startsWith("2") || num.startsWith("4") || num.startsWith("6")) { imgthree.setImageResource(R.drawable.pic); mark=4; display(); }else{imgthree.setImageResource(R.drawable.crazy);} } public void button4(View v) { if(num.startsWith("7") || num.startsWith("9")) { imgfour.setImageResource(R.drawable.pic); mark=4; display();} else{imgfour.setImageResource(R.drawable.crazy);} } } |
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="Which one is orginal..?" android:layout_width="wrap_content"> </TextView> <TableLayout android:id="@+id/tableLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginLeft="30dip"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dip" > <ImageView android:text="" android:id="@+id/image1" android:layout_width="100dip" android:layout_height="100dip" android:background="@drawable/pic" android:clickable="true" android:onClick="button1"> </ImageView> <ImageView android:id="@+id/image2" android:layout_width="100dip" android:layout_height="100dip" android:layout_marginLeft="30dip" android:background="@drawable/pic" android:clickable="true" android:onClick="button2"> </ImageView> </TableRow> |
Comments
Post a Comment