这是一个比较简单的android小游戏,实现的思路比较简单。
实现效果图:
下面是来看看具体实现的过程源码:
package com.example.administrator.mygame;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView text;
private ImageView pic0;
private ImageView pic1;
private ImageView pic2;
int[] imageIds = new int[]{R.drawable.shoe_ok, R.drawable.shoe_sorry,
R.drawable.shoe_sorry};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
pic0 = (ImageView) findViewById(R.id.pic0);
pic1 = (ImageView) findViewById(R.id.pic1);
pic2 = (ImageView) findViewById(R.id.pic2);
Button butplay = (Button) findViewById(R.id.butplay);
text = (TextView) findViewById(R.id.text);
pic0.setOnClickListener(this);
pic1.setOnClickListener(this);
pic2.setOnClickListener(this);
butplay.setOnClickListener(this);
replay();//开局先将鞋子顺序打乱
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.pic0:
//设置已打乱顺序的鞋子图片
pic0.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, imageIds[0]));
pic1.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, imageIds[1]));
pic2.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, imageIds[2]));
if (imageIds[0] == R.drawable.shoe_ok) {
//判断是否猜中有鸡蛋的鞋子
text.setText("恭喜你,猜对了,祝你幸福!");
} else
text.setText("很抱歉,猜错了,要不要再试一次?");
break;
case R.id.pic1:
pic0.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, imageIds[0]));
pic1.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, imageIds[1]));
pic2.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, imageIds[2]));
if (imageIds[1] == R.drawable.shoe_ok) {
text.setText("恭喜你,猜对了,祝你幸福!");
} else
text.setText("很抱歉,猜错了,要不要再试一次?");
break;
case R.id.pic2:
pic0.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, imageIds[0]));
pic1.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, imageIds[1]));
pic2.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, imageIds[2]));
if (imageIds[2] == R.drawable.shoe_ok) {
text.setText("恭喜你,猜对了,祝你幸福!");
} else
text.setText("很抱歉,猜错了,要不要再试一次?");
break;
case R.id.butplay:
replay();
default:
break;
}
}
public void replay() {
//点击再玩一次,恢复原有标题和鞋子图片
text.setText("猜猜鸡蛋在哪只鞋子里?");
pic0.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.shoe_default));
pic1.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.shoe_default));
pic2.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.shoe_default));
for (int i = 0; i < 3; i++) {
int temp = imageIds[i]; // 将数组元素i保存到临时变量中
int index = (int) (Math.random() * 2); // 生成一个随机数
imageIds[i] = imageIds[index]; // 将随机数指定的数组元素的内容赋给数组元素i
imageIds[index] = temp; // 将临时变量的值赋值给随机数组指定的那个数组元素
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="255dp"
>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="猜猜鸡蛋在哪只鞋子里?"
android:textSize="45dp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:id="@+id/pic0"
android:src="@drawable/shoe_default"
android:layout_weight="1"/>
<ImageView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:id="@+id/pic1"
android:src="@drawable/shoe_default"
android:layout_weight="1"/>
<ImageView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:id="@+id/pic2"
android:src="@drawable/shoe_default"
android:layout_marginRight="15dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/butplay"
android:text="再玩一次"
android:layout_gravity="center_vertical"
android:layout_marginLeft="145dp"/>
</LinearLayout>
</LinearLayout>