HOME> 最新活动> android简单小游戏---猜猜鸡蛋在哪只鞋里

android简单小游戏---猜猜鸡蛋在哪只鞋里

一、功能与要求

实现功能:设计一个猜猜鸡蛋在哪只鞋子里游戏。在UI上放置三只鞋子,单击其中的任意一只鞋子,将打开鞋子显示里面是否有鸡蛋,如果猜中,设置该图片为半透明显示,并提示信息“猜对了”,如果猜错,提示信息为“再玩一次?”。

指标要求:实现UI布局;业务功能应实现鸡蛋随机显示在某一只鞋子里。

二,UI布局设计

布局设计如下所示:

三,业务流程图

项目UI布局代码如下:

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:background="@drawable/background">

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/tableRow1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:gravity="center"

android:layout_marginTop="100dp">

android:id="@+id/textView1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="猜猜鸡蛋在哪只鞋子里?"

android:layout_gravity="center"

android:textSize="30sp"

android:textColor="#673AB7"

/>

android:id="@+id/tableRow2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:gravity="center"

android:layout_marginTop="150dp"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:id="@+id/imageView1"

android:layout_width="130dp"

android:layout_height="130dp"

android:layout_weight="1"

android:src="@drawable/shoe_default"

/>

android:id="@+id/imageView2"

android:layout_width="130dp"

android:layout_height="130dp"

android:layout_weight="1"

android:src="@drawable/shoe_default"

/>

android:id="@+id/imageView3"

android:layout_width="130dp"

android:layout_height="130dp"

android:layout_weight="1"

android:src="@drawable/shoe_default"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:gravity="center"

android:layout_marginTop="150dp"

>

android:id="@+id/btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="再玩一次"

android:background="#2196F3"

/>

后台代码:

package com.xs.guess_egg_game;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class MainActivity extends AppCompatActivity {

//定义对象

private TextView text;

private ImageView imageView1;

private ImageView imageView2;

private ImageView imageView3;

//定义一个列表,存储三张图片的id

List list = new ArrayList<>();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//绑定控件

imageView1 = (ImageView) findViewById(R.id.imageView1);

imageView2 = (ImageView) findViewById(R.id.imageView2);

imageView3 = (ImageView) findViewById(R.id.imageView3);

Button button = (Button) findViewById(R.id.btn);

text = (TextView) findViewById(R.id.textView1);

//在列表中添加元素

list.add(R.drawable.shoe_sorry);

list.add(R.drawable.shoe_sorry);

list.add(R.drawable.shoe_ok);

Collections.shuffle(list);//随机显示

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//点击再玩一次,恢复原有标题和鞋子图片

text.setText("猜猜鸡蛋在哪只鞋子里?");

imageView1.setAlpha(255);//设置图片透明度位完全不透明

imageView2.setAlpha(255);//设置图片透明度位完全不透明

imageView3.setAlpha(255);//设置图片透明度位完全不透明

Collections.shuffle(list);

imageView1.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));//将imageview图片设置为默认图片

imageView2.setImageDrawable(getResources( ).getDrawable(R.drawable.shoe_default));

imageView3.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));

}

});

//设置监听

imageView1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

imageView1.setImageDrawable(getResources().getDrawable(list.get(0)));

imageView2.setImageDrawable(getResources().getDrawable(list.get(1)));

imageView3.setImageDrawable(getResources().getDrawable(list.get(2)));

if(list.get(0)==R.drawable.shoe_ok){

imageView1.setAlpha((float)0.5);

imageView2.setAlpha((float)1);

imageView3.setAlpha((float)1);

text.setText("运气真好,猜对了!!");

}else {

text.setText("猜错了,在玩一次?");

}

}

});

imageView2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

imageView1.setImageDrawable(getResources().getDrawable(list.get(0)));

imageView2.setImageDrawable(getResources().getDrawable(list.get(1)));

imageView3.setImageDrawable(getResources().getDrawable(list.get(2)));

if(list.get(1)==R.drawable.shoe_ok){

imageView1.setAlpha((float)1);

imageView2.setAlpha((float)0.5);

imageView3.setAlpha((float)1);

text.setText("运气真好,猜对了!!");

}else {

text.setText("猜错了,在玩一次?");

}

}

});

imageView3.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

imageView1.setImageDrawable(getResources().getDrawable(list.get(0)));

imageView2.setImageDrawable(getResources().getDrawable(list.get(1)));

imageView3.setImageDrawable(getResources().getDrawable(list.get(2)));

if(list.get(2)==R.drawable.shoe_ok){

imageView1.setAlpha((float)1);

imageView2.setAlpha((float)1);

imageView3.setAlpha((float)0.5);

text.setText("运气真好,猜对了!!");

}else {

text.setText("猜错了,在玩一次?");

}

}

});

}

}

项目设计报告+项目源码:源码+项目设计报告