[Android] 开心消消乐代码(写的比较简单)
突然想要在android上写一个消消乐的代码,在此之前没有系统地学过java的面向对象,也没有任何android相关知识,不过还是会一点C++。8月初开始搭建环境,在这上面花了相当多的时间,然后看了一些视频和电子书,对android有了一个大概的了解,感觉差不多了的时候就开始写了。
疯狂地查阅各种资料,反反复复了好几天后,也算是写出了个成品。原计划有很多地方还是可以继续写下去的,比如UI设计,比如动画特效,时间设计,关卡设计,以及与数据库的连接,如果可以的话还能写个联网功能,当然因为写到后期内心感到格外的疲倦,所以以上内容全部被砍掉了。
所以最后的成果就只有这样了……因为完全没有设计过所以非常难看,功能也只有消方块而已,图片还是从之前写的连连看里搬过来的。反正就算一个小的demo好了。
布局
private void initBtn(int i,int j){btn[8*i+j].setBackgroundDrawable(this.getResources().getDrawable(getStyle()));point p = new point(i,j,btn[8*i+j],id);btn[8*i+j].setTag(p);map[i][j] = num;}//……LinearLayout vlayout = (LinearLayout)findViewById(R.id.vlayout);TableLayout tlayout = new TableLayout(this);TableRow row[] = new TableRow[8];for(int i=0;i<8;i++){row[i] = new TableRow(this);row[i].setGravity(Gravity.CENTER);for(int j=0;j<8;j++){btn[8*i+j] = new ImageButton(this);btn[8*i+j].setLayoutParams(new TableRow.LayoutParams(38,38));initBtn(i,j);btn[8*i+j].setOnClickListener(listener);row[i].addView(btn[8*i+j]);}tlayout.addView(row[i]);}
//……
其中getStyle()函数返回了按钮的样式id,该id是随机生成的。
point p存储了关于按钮的信息,它在按钮点击事件中会被使用。
android中的按钮有三种状态:点击态、普通态、焦点态。最后一个的意思是用户按方向键盘(或类似功能键)来控制哪个按钮处于焦点,这样就可以不通过鼠标对按钮进行操作。当然在这里并没有用到这个状态,只用到了前面两种,但是因为已经有现成图片了,就把三种状态都写入了btn?.xml(放在drawable文件夹下),使用的时候直接引用这个xml文件就可以了。
btn1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/a1_2" android:state_pressed="true"/> <item android:drawable="@drawable/a1" android:state_focused="false" android:state_pressed="false"/> <item android:drawable="@drawable/a1_1" android:state_focused="true"/> <item android:drawable="@drawable/a1" android:state_focused="false"/> </selector>
判断是否可消去
判断地图是否还存在解
多线程
@Override public void run() { if(find()){flag = false;int n = 10;alpha = 255;while(n--!=0){wait(30);mHandler.sendEmptyMessage(0);}wait(100);mHandler.sendEmptyMessage(1);}else if(flag==true){swapMap(p1.x,p1.y,p2.x,p2.y);wait(300);mHandler.sendEmptyMessage(2);}else if(flag==false){p1 = new point(-2,-2);p2 = new point(-2,-2);if(check()==false){mHandler.sendEmptyMessage(3);}}}
代码
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:background="@drawable/background" android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/vlayout"><TextViewandroid:id="@+id/text"android:layout_width="fill_parent"android:layout_height="40dip"android:textSize="18sp"android:autoText="true"android:textColor="#000000"android:capitalize="sentences"android:text="开心消消乐" />
</LinearLayout>
package com.example.android.market.licensing; import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Gravity;
import android.view.View; @SuppressLint("NewApi")
public class MainActivity extends Activity implements Runnable { private static final String TAG = "App"; private point p1 = new point(-2,-2); private point p2 = new point(-2,-2);; private boolean isPoint1 = true; private int map[][] = new int[8][8]; private int id; private int mark[][] = new int[8][8]; private Thread thread; private int num; private int alpha = 255; boolean flag = false; private int score = 0; private TextView text; ImageButton[] btn = new ImageButton[64]; private int getStyle(){ num = (int)(1+Math.random()*(7-1+1)); switch(num){ case 1:return id = R.drawable.btn1; case 2:return id = R.drawable.btn2; case 3:return id = R.drawable.btn3; case 4:return id = R.drawable.btn4; case 5:return id = R.drawable.btn5; case 6:return id = R.drawable.btn6; case 7:return id = R.drawable.btn7; default:return id = 0; } } public MainActivity() { } private void initBtn(int i,int j) { btn[8*i+j].setBackgroundDrawable (this.getResources().getDrawable(getStyle())); point p = new point(i,j,btn[8*i+j],id); btn[8*i+j].setTag(p); map[i][j] = num; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout vlayout = (LinearLayout)findViewById(R.id.vlayout); TableLayout tlayout = new TableLayout(this); TableRow row[] = new TableRow[8]; for(int i=0;i<8;i++){ row[i] = new TableRow(this); row[i].setGravity(Gravity.CENTER); for(int j=0;j<8;j++){ btn[8*i+j] = new ImageButton(this); btn[8*i+j].setLayoutParams(new TableRow.LayoutParams(38,38)); initBtn(i,j); btn[8*i+j].setOnClickListener(listener); row[i].addView(btn[8*i+j]); } tlayout.addView(row[i]); } text = new TextView(this); text.setText("分数 : 0"); vlayout.addView(tlayout); vlayout.addView(text); while(find()){ updateState(); } thread = new Thread(this); } private void wait(int time) { try { Thread.sleep(time); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { if(find()){ flag = false; int n = 10; alpha = 255; while(n--!=0){ wait(30); mHandler.sendEmptyMessage(0); } wait(100); mHandler.sendEmptyMessage(1); } else if(flag==true){ swapMap(p1.x,p1.y,p2.x,p2.y); wait(300); mHandler.sendEmptyMessage(2); } else if(flag==false){ p1 = new point(-2,-2); p2 = new point(-2,-2); if(check()==false){ mHandler.sendEmptyMessage(3); } } } void swapImage() { p1.v.setBackgroundDrawable (MainActivity.this.getResources().getDrawable(p2.id)); p2.v.setBackgroundDrawable (MainActivity.this.getResources().getDrawable(p1.id)); int tmp =p1.id; p1.id = p2.id; p2.id = tmp; p1.v.setTag(p1); p2.v.setTag(p2); } private void updateBtn(int x1,int y1,int x2,int y2) { map[x1][y1] = map[x2][y2]; point p = (point)(btn[8*x1+y1].getTag()); p.id = ((point)(btn[8*x2+y2].getTag())).id; btn[8*x1+y1].setTag(p); btn[8*x1+y1].setBackgroundDrawable (MainActivity.this.getResources().getDrawable(p.id)); } private void updateBtn(int i,int j) { btn[8*i+j].setBackgroundDrawable (MainActivity.this.getResources().getDrawable(getStyle())); map[i][j] = num; point p = (point)(btn[8*i+j].getTag()); p.id = id; btn[8*i+j].setTag(p); } private void hideBtn(){ alpha -= 25; for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ if(mark[i][j]!=0&&mark[i][j]!=2){ btn[8*i+j].getBackground().setAlpha(alpha); } } } } private void updateState() { for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ btn[8*i+j].getBackground().setAlpha(255); } } for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ if(mark[i][j]==1){ for(int k=i;k>0;k--){ updateBtn(k,j,k-1,j); } updateBtn(0,j); } else if(mark[i][j]>=3){ if(i-mark[i][j]>=0) { updateBtn(i,j,i-mark[i][j],j); updateBtn(i-mark[i][j],j); } else{ updateBtn(i,j); } } else if(mark[i][j]==2){updateBtn(i,j);}} } } public Handler mHandler=new Handler() { public void handleMessage(Message msg) { switch(msg.what){ case 0:{ hideBtn(); break; } case 1:{ updateState(); text.setText("分数 " + ((Integer)score).toString()); thread.start(); break; } case 2:{ swapImage(); p1 = new point(-2,-2); p2 = new point(-2,-2); break; } case 3:{ Toast.makeText(MainActivity.this, "已自动生成新地图", Toast.LENGTH_SHORT).show(); for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ initBtn(i,j); } } while(find()){ updateState(); } break; } } super.handleMessage(msg); } }; private boolean find() { for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ mark[i][j] = 0; } } boolean flag = false; // heng for(int i=0;i<8;i++){ int count = 1; for(int j=0;j<7;j++){ if(map[i][j]==map[i][j+1]){ count++; if(count==3){ flag = true; mark[i][j-1] = 1; mark[i][j] = 1; mark[i][j+1] = 1; score += 15; } else if(count>3){ mark[i][j+1] = 1; score += 5; } } else count = 1; } } //shu for(int j=0;j<8;j++){ int count = 1; for(int i=0;i<7;i++){ if(map[i][j]==map[i+1][j]){ count++; if(count==3){ flag = true; if(mark[i][j]==1)score+=10; else score +=15; mark[i-1][j] = 3; mark[i][j] = 3; mark[i+1][j] = 3; for(int k=i-5;k>=0;k--){mark[k][j] = 2; }} else if(count>3){ mark[i+1][j] = count; for(int k=1;k<count;k++){ mark[i-k+1][j]++; } if(i-2*count+2>=0)mark[i-2*count+2][j] = 0;score += 5; } } else count = 1; } } return flag; } private boolean check(int i,int j) { //shu int count = 1; if(i>=1&&map[i-1][j]==map[i][j]){ count++; if(i>=2&&map[i-2][j]==map[i][j]){ count++; } } if(count>=3)return true; if(i+1<8&&map[i+1][j]==map[i][j]){ count++; if(i+2<8&&map[i+2][j]==map[i][j]){ count++; } } if(count>=3)return true; //heng count = 1; if(j>=1&&map[i][j-1]==map[i][j]){ count++; if(j>=2&&map[i][j-2]==map[i][j]){ count++; } } if(count>=3)return true; if(j+1<8&&map[i][j+1]==map[i][j]){ count++; if(j+2<8&&map[i][j+2]==map[i][j]){ count++; } } if(count>=3)return true; return false; } private void swapMap(int x1,int y1,int x2,int y2) { int tmp = map[x1][y1]; map[x1][y1] = map[x2][y2]; map[x2][y2] = tmp; } private boolean check() { //heng for(int i=0;i<8;i++){ for(int j=0;j<7;j++){ swapMap(i,j,i,j+1); if(check(i,j)){ swapMap(i,j,i,j+1); return true; } if(check(i,j+1)){ swapMap(i,j,i,j+1); return true; } swapMap(i,j,i,j+1); } } //shu for(int j=0;j<8;j++){ for(int i=0;i<7;i++){ swapMap(i,j,i+1,j); if(check(i,j)){ swapMap(i,j,i+1,j); return true; } if(check(i+1,j)){ swapMap(i,j,i+1,j); return true; } swapMap(i,j,i+1,j); } } return false; } ImageButton.OnClickListener listener = new ImageButton.OnClickListener(){//创建监听对象 @SuppressLint("NewApi") public void onClick(View v) { if(isPoint1){ p1 = (point)v.getTag(); isPoint1 = false; } else { p2 = (point)v.getTag(); isPoint1 = true; } if(((p1.x-p2.x==1||p1.x-p2.x==-1)&&p1.y==p2.y)|| (p1.y-p2.y==1||p1.y-p2.y==-1)&&p1.x==p2.x){ flag = true; swapMap(p1.x,p1.y,p2.x,p2.y); swapImage(); thread.start(); } } };
}
package com.example.android.market.licensing;
import android.view.View;
public class point {public int x;public int y;View v;int id;point(int _x,int _y,View _v,int _id){x = _x;y = _y;v = _v;id = _id;}point(int _x,int _y){x = _x;y = _y;}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source ProjectLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.android.market.licensing"android:versionCode="2"android:versionName="1.1"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".MainActivity"android:label="@string/app_name"android:configChanges="orientation|keyboardHidden"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application><!-- Devices >= 3 have version of Android Market that supports licensing. --><uses-sdk android:minSdkVersion="3" /><!-- Required permission to check licensing. --><uses-permission android:name="com.android.vending.CHECK_LICENSE" />
</manifest>
[Android] 开心消消乐代码(写的比较简单)相关推荐
- 参考df代码写的一个简单的df命令的源代码
作者:飞空静渡 df命令可以列出加载的磁盘或各种文件的信息: 下面给出一个简单的df的命令的源码,这个源码根据df的源码进行改写,很简单,就一个主文件 :) #include <stdio.h& ...
- python自制简易二维码代码_Python用5行代码写一个自定义简单二维码
{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...
- python代码写开心消消乐
♥️作者:小刘在C站 ♥️个人主页:小刘主页 ♥️每天分享云计算网络运维课堂笔记,努力不一定有回报,但一定会有收获加油!一起努力,共赴美好人生! ♥️夕阳下,是最美的绽放,树高千尺,落叶归根人生不易, ...
- android星星爆炸效果图,Android_Android仿开心消消乐大树星星无限循环效果,啥都不说先上效果图,这个是 - phpStudy...
Android仿开心消消乐大树星星无限循环效果 啥都不说先上效果图,这个是我项目里的效果: 下面的是我抽取出来的 demo 适配啥的我基本上都做好了没做其他的 ok 下面 说一下思路把 首先 说一下原 ...
- java开心消消乐代码_Vue实现开心消消乐游戏算法
摘要:这篇Vue栏目下的"Vue实现开心消消乐游戏算法",介绍的技术点是"开心消消乐.Vue.开心.游戏.算法.实现",希望对大家开发技术学习和问题解决有帮助. ...
- java开心消消乐代码_Java小项目之:教你做开心消消乐!
Java小项目之:教你做个开心消消乐! 今天给大家带来的小项目是开心消消乐(连连看),还是一样的惯例.操作简单,适合java初学者练手.想看其他java小项目的朋友可以关注我,我会持续更新,也可以私信 ...
- java开心消消乐代码_Java小项目之:教你做个开心消消乐!
Java小项目之:教你做个开心消消乐! 今天给大家带来的小项目是开心消消乐(连连看),还是一样的惯例.操作简单,适合java初学者练手.想看其他java小项目的朋友可以关注我,我会持续更新,也可以私信 ...
- 闲暇时间我用C语言写出来一个开心消消乐
是这样,当我突然无聊决定把开心消消乐这款游戏下回来救村长的时候,我脑海里灵机一动,决定自己上号写一个 先来看看效果图吧 玩法就是跟消消乐的一样,让三个或更多的相同图案排列成一行或者一列即可消除,但是开 ...
- 消消乐实现下坠_教你用Vue写一个开心消消乐
之前做过一个算法题,算法要求就是写一个开心消消乐的逻辑算法,当时也是考虑了一段时间才做出来.后来想了想,既然核心算法都有了,能不能实现一个开心消消乐的小游戏呢,于是花了两天时间做了一个小游戏出来. 效 ...
最新文章
- FCKEditor最新上传漏洞(ASP),允许上传“.asp;jpg”类型文件解决方案
- python3.7和3.8的区别-Python 3.8 有什么新变化
- MVC Html.AntiForgeryToken() 防止CSRF***
- 微信红包问题:找出某个出现次数超过红包总数一半的红包的金额(面试题)
- exists查询慢_我大意了!这些问题让我的MySQL慢了十倍。
- 没有找到dllregisterserver输入点_Excel教程:框内打的几种输入方法,值得收藏
- Intel安装框架爆出带本地提权的任意代码执行漏洞CVE-2017-5688
- 命令行插入时显示不存在_成年人的世界里,不存在“容易”两个字没人心疼时自己要学会坚强...
- 知识图谱嵌入的一点小事 - ICLR2021
- VS中,一个头文件使用另外一个头文件的静态变量,要谨慎
- QCC300x hello world
- 清明上河图对计算机技术的启发,走进清明上河图沉浸式数字音画项目在京发布-微软亚洲研究院.PDF...
- rfc2544_tput_test脚本中增加丢包率功能
- url怎么隐藏html后缀,去除url后缀(.html,.jsp等)的有效方法
- php 压缩gif 不动,PHP怎么进行GIF动图压缩
- 警告: Establishing SSL connection without server
- PyTorch 学习笔记:transforms的二十二个方法(transforms用法非常详细)
- 系统错误无法连接到服务器失败怎么办啊,SQL Server无法连接到服务器怎么办
- Java文本控件型号_小博老师解析Java核心技术 ——JSwing文本型控件
- MSSQL数据导入导出Excel的相关代码
热门文章
- 超全曲谱 谷歌Doodle电吉他带你玩转音乐(转自:http://pcedu.pconline.com.cn/softnews/yejie/1106/2439215.html)
- nltk_data下载错误 Error loading stopwords urlopen error [Errno 11004] 解决办法
- lua:lua与C/C++程序的整合
- Layui .render() 元素更新记得使用
- 计算机信息管理招聘笔试题,计算机信息管理专业卫生事业单位招聘考试笔试模拟题(八)...
- 高德机器人的名字是怎么呼叫的_语音呼叫名字打电话怎么设置
- 2022-4-2 Leetcode 25.K个一组翻转
- 网络科学导论 - network Science:an introduction 汪小帆版本
- 智能交互黑板做MTBF报告,智能交互黑板做MTBF认证
- 2021-08-05hit-oslab1操作系统的引导