This game features similar hits to the gopher.
Project Address: Https://github.com/moonlightpoet/RedBlock
Program Download demo Address: https://github.com/moonlightpoet/RedBlock/blob/master/bin/RedPoint.apk?raw=true
Main code:
PackageCom.example.redpoint;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.support.v7.app.ActionBar;Importandroid.support.v4.app.Fragment;Importandroid.app.Activity;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportAndroid.os.Build; Public classMainactivityextendsactionbaractivity {Private intredId; Private intScore = 0; PrivateTextView TextView1; Privatebutton[] buttons =NewButton[9]; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView1=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Textview1.settext ("Current score: 0"); buttons[0] =(Button) Findviewbyid (R.id.button0); buttons[1] =(Button) Findviewbyid (R.id.button1); buttons[2] =(Button) Findviewbyid (R.id.button2); buttons[3] =(Button) Findviewbyid (R.id.button3); buttons[4] =(Button) Findviewbyid (R.ID.BUTTON4); buttons[5] =(Button) Findviewbyid (R.ID.BUTTON5); buttons[6] =(Button) Findviewbyid (R.ID.BUTTON6); buttons[7] =(Button) Findviewbyid (r.id.button7); buttons[8] =(Button) Findviewbyid (R.ID.BUTTON8); RedId= (int) (Math.random () * 9)% 9; Buttons[redid].setbackgroundcolor (Color.rgb (255, 0, 0)); for(inti = 0; I < 9; i + +) {Buttons[i].setonclicklistener (NewMyonclicklistener ( This, i)); } } classMyonclicklistenerImplementsOnclicklistener {PrivateActivity context; Private intID; PublicMyonclicklistener (Activity context,intID) { This. Context =context; This. ID =ID; } @Override Public voidOnClick (View arg0) {if(id = =redId) {Score+ = 10; Buttons[redid].setbackgroundcolor (Color.rgb (238, 238, 238)); RedId= (int) (Math.random () * 9)% 9; Buttons[redid].setbackgroundcolor (Color.rgb (255, 0, 0)); } Else{score-= 10; } textview1.settext ("Current score:" +score); } }}
Mainactivity.java
<LinearLayoutxmlns: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= "vertical" > <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"Android:text= "Current score: 0"Android:layout_weight= "1" /> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "2"android:orientation= "Horizontal" > <ButtonAndroid:id= "@+id/button0"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:background= "#EEEEEE" /> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:background= "#EEEEEE" /> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:background= "#EEEEEE" /> </LinearLayout> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "2"android:orientation= "Horizontal" > <ButtonAndroid:id= "@+id/button3"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"android:gravity= "Left"Android:layout_weight= "1"Android:background= "#EEEEEE" /> <ButtonAndroid:id= "@+id/button4"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:background= "#EEEEEE" /> <ButtonAndroid:id= "@+id/button5"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"android:gravity= "Right"Android:layout_weight= "1"Android:background= "#EEEEEE" /> </LinearLayout> <LinearLayoutandroid:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "2"android:gravity= "Bottom" > <ButtonAndroid:id= "@+id/button6"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:background= "#EEEEEE" /> <ButtonAndroid:id= "@+id/button7"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:background= "#EEEEEE" /> <ButtonAndroid:id= "@+id/button8"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:background= "#EEEEEE" /> </LinearLayout></LinearLayout>
Activity_main.xml
Effect:
Android uses linear layout linearlayout and button to implement a point red block game