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

一、功能与要求

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

二,UI布局设计

在这里插入图片描述

布局设计如下所示:

在这里插入图片描述

三,业务流程图

在这里插入图片描述

项目UI布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:gravity="center"
            android:layout_marginTop="100dp">
            <TextView
                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"
                />
        </TableRow>
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:gravity="center"
            android:layout_marginTop="150dp"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="130dp"
                    android:layout_height="130dp"
                    android:layout_weight="1"
                    android:src="@drawable/shoe_default"
                    />
                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="130dp"
                    android:layout_height="130dp"
                    android:layout_weight="1"
                    android:src="@drawable/shoe_default"
                    />
                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="130dp"
                    android:layout_height="130dp"
                    android:layout_weight="1"
                    android:src="@drawable/shoe_default"
                    />
            </LinearLayout>
        </TableRow>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_marginTop="150dp"
            >
            <Button
                android:id="@+id/btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="再玩一次"
                android:background="#2196F3"
                />
        </LinearLayout>
    </TableLayout>
</LinearLayout>
后台代码:
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<Integer> 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("猜错了,在玩一次?");
                }
            }
        });

    }
}

在这里插入图片描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-努力搬砖的小刘-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值