• Android 使用线性布局LinearLayout和Button实现一个点红块游戏


    这个游戏的功能类似打地鼠。

    项目地址:https://github.com/moonlightpoet/RedBlock

    程序下载试玩地址:https://github.com/moonlightpoet/RedBlock/blob/master/bin/RedPoint.apk?raw=true

    主要代码:

    package com.example.redpoint;
    
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.os.Build;
    
    public class MainActivity extends ActionBarActivity {
    
        private int redId;
        private int score = 0;
        private TextView textView1;
        private Button[] buttons = new Button[9];
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            textView1 = (TextView) findViewById(R.id.textView1);
            textView1.setText("当前得分: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 (int i = 0; i < 9; i ++) {
                buttons[i].setOnClickListener(new MyOnClickListener(this, i));
            }
        }
        
        class MyOnClickListener implements OnClickListener {
            
            private Activity context;
            private int id;
            
            public MyOnClickListener(Activity context, int id) {
                this.context = context;
                this.id = id;
            }
    
            @Override
            public void onClick(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("当前得分:" + score);
            }
            
        }
    }
    MainActivity.java
    <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="vertical"
        >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:text="当前分数:0"
            android:layout_weight="1" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_weight="2"
            android:orientation="horizontal"
            >
            
            <Button
            android:id="@+id/button0"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#EEEEEE"
            />
            
            <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#EEEEEE"
            />
            
            <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#EEEEEE"
            />
            
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_weight="2"
            android:orientation="horizontal"
            >
            
             <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="left"
            android:layout_weight="1"
            android:background="#EEEEEE"
            />
            
            <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#EEEEEE"
            />
            
            <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="right"
            android:layout_weight="1"
            android:background="#EEEEEE"
            />
            
        </LinearLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_weight="2"
            android:gravity="bottom"
            >
            
             <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#EEEEEE"
            />
            
            <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#EEEEEE"
            />
            
            <Button
            android: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

    效果:

  • 相关阅读:
    素数筛代码
    stringsream用法
    MySQL学习(四)——外键
    MySQL学习(三)——Java连接MySQL数据库
    MySQL学习(二)——SQL语句创建删除修改以及中文乱码问题
    MySQL学习(一)——启动和登录MySql遇到的问题及解决
    BootStrap学习(三)——重写首页之导航栏和轮播图
    BootStrap学习(二)——重写首页之topbar
    BootStrap学习(一)——BootStrap入门
    jQuery学习(八)——使用JQ插件validation进行表单校验
  • 原文地址:https://www.cnblogs.com/moonlightpoet/p/5405796.html
Copyright © 2020-2023  润新知