【文件属性】:
文件名称:安卓游戏2048
文件大小:1.9MB
文件格式:ZIP
更新时间:2018-10-31 05:56:44
2048
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(context);
// 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];
}
【文件预览】:
Game2048b
----.project(845B)
----bin()
--------Game2048b.apk(274KB)
--------AndroidManifest.xml(931B)
--------jarlist.cache(120B)
--------Game2048.apk(274KB)
--------dexedLibs()
--------res()
--------resources.ap_(40KB)
--------classes()
--------R.txt(3KB)
--------classes.dex(679KB)
----.settings()
--------org.eclipse.jdt.core.prefs(177B)
----ic_launcher-web.png(50KB)
----lint.xml(155B)
----AndroidManifest.xml(931B)
----proguard-project.txt(781B)
----libs()
--------android-support-v4.jar(607KB)
----src()
--------com()
----project.properties(608B)
----res()
--------values()
--------values-sw720dp-land()
--------layout()
--------drawable-ldpi()
--------drawable-hdpi()
--------values-v11()
--------drawable-mdpi()
--------values-sw600dp()
--------drawable-xhdpi()
--------drawable-xxhdpi()
--------menu()
--------values-v14()
----.classpath(475B)
----gen()
--------com()
----assets()