Android编写2048小游戏
程序员文章站
2024-03-01 17:10:34
先来说说2048游戏规则:
开始时棋盘内随机出现两个数字,出现的数字仅可能为2或4
玩家可以选择上下左右四个方向,若棋盘内的数字出现位移或合并,视为有效移动
玩家选择...
先来说说2048游戏规则:
开始时棋盘内随机出现两个数字,出现的数字仅可能为2或4
玩家可以选择上下左右四个方向,若棋盘内的数字出现位移或合并,视为有效移动
玩家选择的方向上若有相同的数字则合并,每次有效移动可以同时合并,但不可以连续合并
合并所得的所有新生成数字想加即为该步的有效得分
玩家选择的方向行或列前方有空格则出现位移
每有效移动一步,棋盘的空位(无数字处)随机出现一个数字(依然可能为2或4)
棋盘被数字填满,无法进行有效移动,判负,游戏结束
棋盘上出现2048,判胜,游戏结束
下面代码
mainactivity类
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(new gameview(this)); } }
定义一个卡片类显示数据
/卡片类型 public class card extends framelayout { //2 textview tv; private int number = 0; int width; public card(context context, int width) { super(context); this.width = width; init(); } private void init() { tv = new textview(getcontext()); setpadding(5, 5, 5, 5); framelayout.layoutparams lp = new layoutparams(width - 10, width - 10); tv.setlayoutparams(lp); tv.setgravity(gravity.center); tv.settextcolor(color.white); tv.settextsize(48); this.addview(tv); setcolor(); } public void setnumber(int number) { this.number = number; if (number == 0) tv.settext(""); else tv.settext(number + ""); setcolor(); } @override public string tostring() { return "card{" + "tv=" + tv + ", number=" + number + ", width=" + width + '}'; } private void setcolor() { switch (number) { case 0: tv.setbackgroundcolor(getresources().getcolor(r.color.c0)); break; case 2: tv.setbackgroundcolor(getresources().getcolor(r.color.c2)); break; case 4: tv.setbackgroundcolor(getresources().getcolor(r.color.c4)); break; case 8: tv.setbackgroundcolor(getresources().getcolor(r.color.c8)); break; case 16: tv.setbackgroundcolor(getresources().getcolor(r.color.c16)); break; case 32: tv.setbackgroundcolor(getresources().getcolor(r.color.c32)); break; case 64: tv.setbackgroundcolor(getresources().getcolor(r.color.c64)); break; case 128: tv.setbackgroundcolor(getresources().getcolor(r.color.c128)); break; case 256: tv.setbackgroundcolor(getresources().getcolor(r.color.c256)); break; case 512: tv.setbackgroundcolor(getresources().getcolor(r.color.c512)); break; case 1024: tv.setbackgroundcolor(getresources().getcolor(r.color.c1024)); break; case 2048: tv.setbackgroundcolor(getresources().getcolor(r.color.c2048)); break; } } public int getnumber() { return number; } }
自己定义一下上面的几种颜色 在values下的colors中
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorprimary">#3f51b5</color> <color name="colorprimarydark">#303f9f</color> <color name="coloraccent">#ff4081</color> <color name="c0">#43382a</color> <color name="c2">#2a8618</color> <color name="c4">#3d6319</color> <color name="c8">#f2b179</color> <color name="c16">#f59563</color> <color name="c32">#f57c5f</color> <color name="c64">#5b69b7</color> <color name="c128">#b37d3e</color> <color name="c256">#3a9096</color> <color name="c512">#d7882c</color> <color name="c1024">#5c1b78</color> <color name="c2048">#640548</color> </resources>
布局类 和里面的运算方法
public class gameview extends gridlayout { int[][] values = new int[4][4]; card[][] cards = new card[4][4]; // integer -128-127 == 大于这个数 两个对象 public gameview(context context) { super(context); init(); } public gameview(context context, attributeset attrs) { super(context, attrs); init(); } private void init() { setcolumncount(4); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); newgame(); } private void newgame() { //viewgroup.layoutparams lp = new viewgroup.layoutparams((int) (getresources().getdisplaymetrics().widthpixels / 4), (int) (getresources().getdisplaymetrics().widthpixels / 4)); gridlayout.layoutparams params = new gridlayout.layoutparams(); // params.width = getresources().getdisplaymetrics().widthpixels / 4; // params.height = getresources().getdisplaymetrics().widthpixels / 4; //log.e("tag", params.width + " " + params.height); this.removeallviews(); // gridlayout.layoutparams lpa = new gridlayout.layoutparams(lp); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { values[i][j] = 0; cards[i][j] = new card(getcontext(), getresources().getdisplaymetrics().widthpixels / 4); // cards[i][j].setlayoutparams(params); //cards[i][j].uplv(); this.addview(cards[i][j]); } } //创建初始的两张卡 int i = (int) (math.random() * 16); int j = 0; do { j = (int) (math.random() * 16);//0-15 15 3 3 } while (j == i); log.e("tag", i + " " + j); values[i / 4][i % 4] = math.random() * 20 < 1 ? 4 : 2; values[j / 4][j % 4] = math.random() * 20 < 1 ? 4 : 2; setvalues(); } float oldx, oldy; int move = -1; @override public boolean ontouchevent(motionevent event) { float x = event.getx(); float y = event.gety(); switch (event.getaction()) { case motionevent.action_down: move = -1; oldx = x; oldy = y; break; case motionevent.action_move: if (math.abs(oldx - x) > math.abs(oldy - y)) { if (oldx - x > 15) { //左 log.e("tag", "---------->>>"); move = 1; } else if (oldx - x < -15) {//右 log.e("tag", "---------->>>"); move = 2; } } else { if (oldy - y > 15) { move = 3; } else if (oldy - y < -15) { move = 4; } } break; case motionevent.action_up: //记录之前的数组 int[][] temp = new int[4][4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { temp[i][j] = values[i][j]; } } switch (move) { case 1: left(); break; case 2: right(); break; case 3: up(); break; case 4: down(); break; } setvalues(); if (move != -1) { //比对当前的数组 for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (values[i][j] != temp[i][j]) { addcard(); return true; } } } } //判断游戏胜利还是结束4 if (iswin()) { toast.maketext(getcontext(), "游戏胜利", toast.length_short).show(); } if (isover()) { this.removeallviews(); textview tv = new textview(getcontext()); tv.settext("游戏结束,点击从新开始"); this.addview(tv); tv.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { newgame(); } }); } break; } return true; } private void addcard() { while (true) { int j = (int) (math.random() * 16); if (values[j / 4][j % 4] == 0) { values[j / 4][j % 4] = 2; cards[j / 4][j % 4].setnumber(2); return; } } } public void left() { //取出 4个 集合 for (int i = 0; i < 4; i++) { list<integer> list = new arraylist<>(); for (int j = 0; j < 4; j++) { int value = values[i][j]; if (value != 0) list.add(value); } //比较 log.e("tag", list.tostring()); if (list.size() == 0) continue; else if (list.size() == 1) { values[i][0] = list.get(0); for (int j = 0; j < 3; j++) { values[i][j + 1] = 0; } } else if (list.size() == 2) { if (list.get(0).equals(list.get(1))) { values[i][0] = list.get(0) * 2; //三个值补0 for (int j = 0; j < 3; j++) { values[i][j + 1] = 0; } } else { values[i][0] = list.get(0); values[i][1] = list.get(1); values[i][2] = 0; values[i][3] = 0; } } else if (list.size() == 3) { if (list.get(0).equals(list.get(1))) { values[i][0] = list.get(0) * 2; values[i][1] = list.get(2); values[i][2] = 0; values[i][3] = 0; } else if (list.get(1).equals(list.get(2))) { values[i][0] = list.get(0); values[i][1] = list.get(2) * 2; values[i][2] = 0; values[i][3] = 0; } else { values[i][0] = list.get(0); values[i][1] = list.get(1); values[i][2] = list.get(2); values[i][3] = 0; } } else { if (list.get(0).equals(list.get(1))) { if (list.get(3).equals(list.get(2))) { values[i][0] = list.get(0) * 2; values[i][1] = list.get(2) * 2; values[i][2] = 0; values[i][3] = 0; } else { values[i][0] = list.get(0) * 2; values[i][1] = list.get(2); values[i][2] = list.get(3); values[i][3] = 0; } } else { //1和2不相等 //先比对2 3 相等,不等 if (list.get(1).equals(list.get(2))) { values[i][0] = list.get(0); values[i][1] = list.get(1) * 2; values[i][2] = list.get(3); values[i][3] = 0; } else { if (list.get(2).equals(list.get(3))) { values[i][0] = list.get(0); values[i][1] = list.get(1); values[i][2] = list.get(2) * 2; values[i][3] = 0; } } } } } } private void delete() { // log.e("tag", "--------------------执行"); // //遍历 // for (int i = 0; i < 4; i++) { // for (int j = 0; j < 3; j++) { // card card = cards[i][j]; // log.e("tag", "i:" + i + " j:" + j + " num:" + card.getnumber()); // if (card.getnumber() == 0) { // boolean issub = false; // for (int k = j; k < 3; k++) { // cards[i][k].setnumber(cards[i][k + 1].getnumber()); // if (cards[i][k + 1].getnumber() != 0) { // issub = true; // } // } // if (issub) // j--; // cards[i][3].setnumber(0); // } else if (card.getnumber() == cards[i][j + 1].getnumber()) { // card.uplv(); // cards[i][j + 1].setnumber(0); // //后面的往前搬 // for (int k = j + 1; k < 3; k++) { // cards[i][k].setnumber(cards[i][k + 1].getnumber()); // } // cards[i][3].setnumber(0); // j--; // } // } // } // for (int j = 0; j < 4; j++) { //列 // for (int i = 3; i >= 1; i--) { // card card = cards[j][i]; // if (card.getnumber() == 0) { // //全行左移 // //要将 // //如果是最后一个,不需要理会 // continue; // } else { // //判断左边一个 // if (cards[j][i - 1].getnumber() == 0) { // //从i --》i-1 // for (int k = i - 1; k < 3; k++) { // cards[j][k].setnumber(cards[j][k + 1].getnumber()); // } // cards[j][3].setnumber(0); // // } else if (cards[j][i - 1].getnumber() == card.getnumber()) { // cards[j][i - 1].uplv(); // card.setnumber(0); // for (int k = i; k < 3; k++) { // cards[j][k].setnumber(cards[j][k + 1].getnumber()); // } // cards[j][3].setnumber(0); // } // } // } // } } public void right() { mirrorh(); left(); mirrorh(); } private void mirrorh() { for (int i = 0; i < 4; i++) { int temp = values[i][0]; values[i][0] = values[i][3]; values[i][3] = temp; temp = values[i][1]; values[i][1] = values[i][2]; values[i][2] = temp; } } public void down() { //左旋 int[][] temp = new int[4][4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { temp[i][j] = values[3 - j][i]; } } values = temp; left(); temp = new int[4][4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { temp[i][j] = values[j][3 - i]; } } values = temp; } public void up() { mirrorv(); down(); mirrorv(); } private void mirrorv() { for (int j = 0; j < 4; j++) { int temp = values[0][j]; values[0][j] = values[3][j]; values[3][j] = temp; temp = values[1][j]; values[1][j] = values[2][j]; values[2][j] = temp; } } public void setvalues() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { system.out.print(values[i][j] + " "); cards[i][j].setnumber(values[i][j]); } system.out.println(); } } public boolean iswin() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (values[i][j] == 2048) return true; } } return false; } public boolean isover() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (values[i][j] == 0) return false; } } //满了 for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { int value = values[i][j]; if (i > 1 && value == values[i - 1][j]) return false; else if (i < 3 && value == values[i + 1][j]) return false; else if (j > 1 && value == values[i][j - 1]) return false; else if (j < 3 && value == values[i][j + 1]) return false; } } return true; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: CSS如何使DIV层居中