开发工具:
文件大小: 1mb
下载次数: 0
上传时间: 2015-10-11
详细说明:
package com.example.game2048; import java.util.ArrayList; public class GameView extends GridLayout { private List emptyPoints=new ArrayList(); public GameView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub initGameView(); } public GameView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub initGameView(); } public GameView(Context context) { super(co ntext); // TODO Auto-generated constructor stub initGameView(); } /*public void initGameView(){ setColumnCount(4); setBackgroundColor(0xffbbada0); setOnTouchListener(new View.OnTouchListener() { private float startX,startY,offsetX, offsetY; @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX=event.getX(); startY=event.getY(); break; case MotionEvent.ACTION_UP: offsetX=event.getX()-startX; offsetY=event.getY()-startY; if(Math.abs(offsetX)>Math.abs(offsetY)){ if(offsetX<-5){ swipeLeft(); System.out.println("left"); } else if(offsetX>5){ swipeRight(); System.out.println("right"); } }else{ if(offsetY<-5){ swipeUp(); System.out.println("up"); } else if(offsetY>5){ swipeDown(); System.out.println("down"); } } break; } return true; } }); }*/ private void initGameView(){ setColumnCount(4); setBackgroundColor(0xffbbada0); setOnTouchListener(new View.OnTouchListener() { private float startX,startY,offsetX,offsetY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = event.getX(); startY = event.getY(); break; case MotionEvent.ACTION_UP: offsetX = event.getX()-startX; offsetY = event.getY()-startY; if (Math.abs(offsetX)>Math.abs(offsetY)) { if (offsetX<-5) { swipeLeft(); }else if (offsetX>5) { swipeRight(); } }else{ if (offsetY<-5) { swipeUp(); }else if (offsetY>5) { swipeDown(); } } break; } return true; } }); } protected void onSizeChanged(int w, int h, int oldw, int oldh) { // TODO Auto-generated method stub super.onSizeChanged(w, h, oldw, oldh); int cardWidth=(Math.min(w,h)-10)/4; addCards(cardWidth,cardWidth); startGame(); } private void startGame() { // TODO Auto-generated method stub for(int y=0;y<4;y++){ for(int x=0;x<4;x++){ cardsMap[x][y].setNum(0); } } addRandomNum(); addRandomNum(); addRandomNum(); addRandomNum(); addRandomNum(); addRandomNum(); addRandomNum(); addRandomNum(); } private void addCards(int cardWidth, int cardHeight) { // TODO Auto-generated method stub Card c; for(int y=0;y<4;y++){ for(int x=0;x<4;x++){ c=new Card(getContext()); c.setNum(0); addView(c, cardWidth, cardHeight); cardsMap[x][y]=c; } } } private void addRandomNum(){ emptyPoints.clear(); for(int y=0;y<4;y++){ for(int x=0;x<4;x++){ if(cardsMap[x][y].getNum()<=0) emptyPoints.add(new Point(x,y)); } } int a=(int)(Math.random()*emptyPoints.size()); Point p=emptyPoints.remove(a);//(int)(Math.random()*emptyPoints.size()) cardsMap[p.x][p.y].setNum(Math.random()>0.2?2:4); } private void swipeLeft(){ for(int y=0;y<4;y++){ for(int x=0;x<4;x++){ for(int x1=x+1;x1<4;x++){ if(cardsMap[x1][y].getNum()>0){ if(cardsMap[x][y].getNum()<=0){ cardsMap[x][y].setNum(cardsMap[x1][y].getNum()); cardsMap[x1][y].setNum(0); x--; break; }else if( cardsMap[x][y].equals(cardsMap[x1][y])){ cardsMap[x][y].setNum(cardsMap[x1][y].getNum()*2); cardsMap[x1][y].setNum(0); break; } } } } } } private void swipeRight(){ for(int y=0;y<4;y++){ for(int x=3;x>=0;x--){ for(int x1=x-1;x1>=0;x--){ if(cardsMap[x1][y].getNum()>0){ if(cardsMap[x][y].getNum()<=0){ cardsMap[x][y].setNum(cardsMap[x1][y].getNum()); cardsMap[x1][y].setNum(0); x++; break; }else if( cardsMap[x][y].equals(cardsMap[x1][y])){ cardsMap[x][y].setNum(cardsMap[x1][y].getNum()*2); cardsMap[x1][y].setNum(0); break; } } } } } } private void swipeUp(){ for(int x=0;x<4;x++){ for(int y=0;y<4;y++){ for(int y1=y+1;y1<4;y1++){ if(cardsMap[x][y1].getNum()>0){ if(cardsMap[x][y].getNum()<=0){ cardsMap[x][y].setNum(cardsMap[x][y1].getNum()); cardsMap[x][y1].setNum(0); y--; break; }else if( cardsMap[x][y].equals(cardsMap[x][y1])){ cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2); cardsMap[x][y1].setNum(0); break; } } } } } } private void swipeDown(){ for(int x=0;x<4;x++){ for(int y=3;y>=0;y--){ for(int y1=y-1;y1>=0;y1--){ if(cardsMap[x][y1].getNum()>0){ if(cardsMap[x][y].getNum()<=0){ cardsMap[x][y].setNum(cardsMap[x][y1].getNum()); cardsMap[x][y1].setNum(0); y++; break; }else if( cardsMap[x][y].equals(cardsMap[x][y1])){ cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2); cardsMap[x][y1].setNum(0); break; } } } } } } private Card[][] cardsMap=new Card[4][4]; } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.