有一种游戏,在我们还小的时候,会经常和小伙伴玩耍,拿出几张扑克牌,其中包含红桃,游戏中有两种身份,洗牌者和猜测者,洗牌者通过洗牌打乱顺序,然后将所有纸牌背部朝向猜测者,由猜测者从中选择,如果抽出红桃,则猜测成功,否则失败。这应该是最简单的扑克牌游戏了。那么如何在Android程序中编写这个游戏功能呢?在本篇博客中,我将就这个需求完成寻觅红桃一个的纸牌游戏。
<强>逻辑分析:强>
1。纸牌应该体现在ImageView控件上,在猜测时,统一显示纸牌背部花色,猜测之前应先随机改变纸牌的位置,猜测之后将纸牌翻转,提示用户成功或者失败。
2。可将纸牌内容放置到一个容器中进行统一管理,然后随机改变容器内元素的位置。
3。当用户点击选中某张纸牌时,设置三张纸牌翻转到正面,查看是否选中红桃a .
那么我们在代码中实现这个需求逻辑。
首先编写布局文件:activity_main.xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" match_parent " android: layout_height=" match_parent " android:取向=按怪薄? android:重力=" center_horizontal " android:填充=" 20 dp”比; & lt; TextView android: id=癅 + id/多行文字” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android:文本="猜猜紅心一个是哪一張” android: textSize=" 18 sp "/比; LinearLayout & lt; android: layout_width=" match_parent " android: layout_height=" wrap_content " android: layout_margin=" 40 dp " 面向android:="水平"比; & lt; ImageView android: id=癅 + id/mImage01” android: layout_width=" 0 dp” android: layout_height=" match_parent " android: layout_weight=" 1 " android: src=" https://www.yisu.com/zixun/@mipmap p04 "/比; & lt; ImageView android: id=癅 + id/mImage02” android: layout_width=" 0 dp” android: layout_height=" match_parent " android: layout_weight=" 1 " android: src=" https://www.yisu.com/zixun/@mipmap p04 "/比; & lt; ImageView android: id=癅 + id/mImage03” android: layout_width=" 0 dp” android: layout_height=" match_parent " android: layout_weight=" 1 " android: src=" https://www.yisu.com/zixun/@mipmap p04 "/比; & lt;/LinearLayout> & lt;按钮 android: id=癅 + id/mButton” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android:文本="重玩一次“/比; & lt;/LinearLayout>
效果图如下:
接下来,将处理逻辑的代码写入到活动界面当中。
进口android.app.Activity; 进口android.os.Bundle; 进口android.view.View; 进口android.view.View.OnClickListener; 进口android.widget。*; 公开课MainActivity延伸活动{ 私人ImageView myImageView; 私人ImageView myImageView2; 私人ImageView myImageView3; 私人按钮色; 私人布尔isPlay=false;//判断是否为猜测游戏过程的标志位 私人int [] myImage={ R.mipmap。p01 R.mipmap。p02、R.mipmap.p03};/* *为每一个ImageView设置点击事件*/视图。OnClickListener myimageClistener=new View.OnClickListener () { @Override 公共空间alt=" Android实现寻觅红桃一个的翻牌游戏">好了,这就是红桃一翻牌游戏了,大家编程时可以增加纸牌的数量提高游戏的难度,代码并不会有太大的改变,不信你试试!感谢您的阅读~
点击此处可下载相关项目代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
Android实现寻觅红桃一个的翻牌游戏