public void newGame() { gameObject1.clear(); gameObject2.clear(); gameObject3.clear(); gameObject4.clear(); gameObjectadd(); timer.resetTimer(); score1 = 0; score2 = 0; totalWinner = 0; winner = 0; money.randomize(); state = 1; round = 0; choose = "Nope"; choose2 = "Nope"; newMoney(scaledMoney, money.drawUpdate(money.vals[0])); newMoney(Money1, score11.drawUpdate(0)); newMoney(Money2, score22.drawUpdate(0)); newMoney(scaledTimer, timer.drawUpdate()); score11.setBitmap(Money1.get(0), Money1.get(1)); score22.setBitmap(Money2.get(0), Money2.get(1)); money.setBitmap(scaledMoney.get(0), scaledMoney.get(1)); timer.setBitmap(scaledTimer.get(0), scaledTimer.get(1)); }
@Override public void surfaceCreated(SurfaceHolder holder) { scaleFactorX = getWidth() / (WIDTH * 1.f); scaleFactorY = getHeight() / (HEIGHT * 1.f); createPics(); scaledMoney.add(BitmapFactory.decodeResource(getResources(), R.drawable.money)); scaledMoney.add(BitmapFactory.decodeResource(getResources(), R.drawable.kingword)); Money1.add(BitmapFactory.decodeResource(getResources(), R.drawable.money)); Money1.add(BitmapFactory.decodeResource(getResources(), R.drawable.kingword)); Money2.add(BitmapFactory.decodeResource(getResources(), R.drawable.money)); Money2.add(BitmapFactory.decodeResource(getResources(), R.drawable.kingword)); scaledTimer.add(BitmapFactory.decodeResource(getResources(), R.drawable.civilian)); scaledTimer.add(BitmapFactory.decodeResource(getResources(), R.drawable.captain)); setupVar(); newMoney(scaledMoney, money.drawUpdate(money.vals[0])); newMoney(Money1, score11.drawUpdate(0)); newMoney(Money2, score22.drawUpdate(0)); newMoney(scaledTimer, timer.drawUpdate()); score11.setBitmap(Money1.get(0), Money1.get(1)); score22.setBitmap(Money2.get(0), Money2.get(1)); money.setBitmap(scaledMoney.get(0), scaledMoney.get(1)); timer.setBitmap(scaledTimer.get(0), scaledTimer.get(1)); thread = new MainThread(getHolder(), this); // we can safely start the game loop thread.setRunning(true); thread.start(); }
@Override public boolean onTouchEvent(MotionEvent event) { ArrayList<GameObject> gameObject = new ArrayList<GameObject>(); String player = null; GameWord gameWord = gameWord1; boolean playerDiamond = false; if (state == 1) { gameObject = gameObject1; playerDiamond = usedDiamond; player = choose; gameWord = gameWord1; } if (state == 2) { gameObject = gameObject2; playerDiamond = usedDiamond2; player = choose2; gameWord = gameWord2; } if (state == 3) { gameObject = gameObject3; } if (state == 4) { gameObject = gameObject4; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: playerDiamond = false; xTouch = event.getX(); yTouch = event.getY(); // System.out.println("x: "+ (int)(xTouch/scaleFactorX)+" "+scaleFactorX); // System.out.println("y: "+ (int)(yTouch/scaleFactorY)+" "+scaleFactorY); for (int i = 0; i < gameObject.size(); i++) { gameObjectName = collision( (int) (xTouch / scaleFactorX), (int) (yTouch / scaleFactorY), gameObject.get(i)); if (gameObjectName != null) { imageWord(gameWord, gameObjectName); System.out.println(gameObjectName); num = i; // gameObject.get(i).setX((int)(xTouch/scaleFactorX)); // gameObject.get(i).setY((int)(yTouch/scaleFactorY)); break; } } break; case MotionEvent.ACTION_MOVE: if (state == 1 || state == 2) { if (num != -1) { if (gameObjectName != "Done") { xTouch = event.getX(); // System.out.println("X: " + xTouch); yTouch = event.getY(); // System.out.println("Y: " + yTouch); if (num >= 0) { gameObject .get(num) .setX((int) (xTouch / scaleFactorY) - (gameObject.get(num).getWidth() / 2)); gameObject .get(num) .setY((int) (yTouch / scaleFactorY) - (gameObject.get(num).getHeight() / 2)); } } } } break; case MotionEvent.ACTION_UP: /*if (collBetObj(gameObject.get(num),mid)){ System.out.println("collide"); gameObject.remove(num); overlayBitmap.eraseColor(Color.TRANSPARENT); }*/ if (num != -1) { if ((collBetObj(gameObject.get(num), mid) && player == "Nope") || (collBetObj(gameObject.get(num), mid)) && (gameObject.get(num).getString() == "Diamond")) { if (gameObject.get(num).getString() != "Diamond") { player = gameObject.get(num).getString(); // System.out.println("Choose: " + player); gameObject.remove(num); } else if (gameObject.get(num).getString() == "Diamond" && player == "Nope") { // System.out.println("Choose: " + player); gameObject.get(num).setX(gameObject.get(num).getInitialX()); gameObject.get(num).setY(gameObject.get(num).getInitialY()); } else { playerDiamond = true; // System.out.println("Diamond used: " + playerDiamond); gameObject.remove(num); } if (state == 1) { usedDiamond = playerDiamond; choose = player; } else if (state == 2) { usedDiamond2 = playerDiamond; choose2 = player; } // gameObject.get(num).getBit().eraseColor(Color.TRANSPARENT); } else { if (gameObject.get(num).getString() != "Done") { gameObject.get(num).setX(gameObject.get(num).getInitialX()); gameObject.get(num).setY(gameObject.get(num).getInitialY()); } else { if (state == 1) { // usedDiamond = playerDiamond; // choose = player; state = 2; scaledGameWord = Bitmap.createScaledBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.player2), 192, 64, true); gameWord2.setBitmap(scaledGameWord); timer.resetTimer(); } else if (state == 2) { // usedDiamond2 = playerDiamond; // choose2 = player; state = 3; timer.resetTimer(); compare.setCompare(choose, usedDiamond, choose2, usedDiamond2); winner = compare.comparator(); System.out.println("Winner: " + winner); choose = "Nope"; choose2 = "Nope"; usedDiamond = false; usedDiamond2 = false; } else if (state == 3) { state = 1; scaledGameWord = Bitmap.createScaledBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.player1), 192, 64, true); gameWord1.setBitmap(scaledGameWord); // gameWord2.setBitmap(scaledGameWord); if (winner == 1) { score1 = score1 + money.vals[round]; newMoney(Money1, score11.drawUpdate(score1)); score11.setBitmap(Money1.get(0), Money1.get(1)); } else if (winner == 2) { score2 = score2 + money.vals[round]; newMoney(Money2, score22.drawUpdate(score2)); score22.setBitmap(Money2.get(0), Money2.get(1)); } if (round < 5) { round++; if (round == 5) { state = 4; } else { newMoney(scaledMoney, money.drawUpdate(money.vals[round])); money.setBitmap(scaledMoney.get(0), scaledMoney.get(1)); } } int remaining = 0; for (int i = 0; i < ((money.vals.length) - round); i++) { remaining = remaining + money.vals[4 - i]; System.out.println("Money: " + money.vals[4 - i]); } System.out.println(remaining); System.out.println((score1 + remaining) < score2); System.out.println((score2 + remaining) < score1); if (((score1 + remaining) < score2) || ((score2 + remaining) < score1)) { state = 4; System.out.println("Sad"); } timer.resetTimer(); winner = 0; } else if (state == 4) { newGame(); } // System.out.println("State: "+ state); // System.out.println("Score1: "+ score1); // System.out.println("Score2: "+ score2); } } num = -1; gameObjectName = null; } break; } return true; }