[ ] : 2D |
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
public static boolean isLeftPressed = false; //
public static boolean isRightPressed = false; //
GameView gameView = new GameView(this); // gameView
LinearLayout gameLayout = (LinearLayout) findViewById(R.id.gameLayout); // gameLayout
gameLayout.addView(gameView); // gameView
Button leftButton = (Button) findViewById(R.id.leftButton); //
Button rightButton = (Button) findViewById(R.id.rightButton);
leftButton.setOnTouchListener(this); // ( onTouch)
rightButton.setOnTouchListener(this);
public boolean onTouch(View button, MotionEvent motion) {
switch(button.getId()) { //
case R.id.leftButton:
switch (motion.getAction()) { //
case MotionEvent.ACTION_DOWN:
isLeftPressed = true;
break;
case MotionEvent.ACTION_UP:
isLeftPressed = false;
break;
}
break;
case R.id.rightButton:
switch (motion.getAction()) { //
case MotionEvent.ACTION_DOWN:
isRightPressed = true;
break;
case MotionEvent.ACTION_UP:
isRightPressed = false;
break;
}
break;
}
return true;
}
package com.spaceavoider.spaceavoider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
public static boolean isLeftPressed = false; //
public static boolean isRightPressed = false; //
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GameView gameView = new GameView(this); // gameView
LinearLayout gameLayout = (LinearLayout) findViewById(R.id.gameLayout); // gameLayout
gameLayout.addView(gameView); // gameView
Button leftButton = (Button) findViewById(R.id.leftButton); //
Button rightButton = (Button) findViewById(R.id.rightButton);
leftButton.setOnTouchListener(this); // ( onTouch)
rightButton.setOnTouchListener(this);
}
public boolean onTouch(View button, MotionEvent motion) {
switch(button.getId()) { //
case R.id.leftButton:
switch (motion.getAction()) { //
case MotionEvent.ACTION_DOWN:
isLeftPressed = true;
break;
case MotionEvent.ACTION_UP:
isLeftPressed = false;
break;
}
break;
case R.id.rightButton:
switch (motion.getAction()) { //
case MotionEvent.ACTION_DOWN:
isRightPressed = true;
break;
case MotionEvent.ACTION_UP:
isRightPressed = false;
break;
}
break;
}
return true;
}
}
public static int maxX = 20; //
public static int maxY = 28; //
public static float unitW = 0; //
public static float unitH = 0; //
private boolean firstTime = true;
private boolean gameRunning = true;
private Ship ship;
private Thread gameThread = null;
private Paint paint;
private Canvas canvas;
private SurfaceHolder surfaceHolder;
public GameView(Context context) {
super(context);
//
surfaceHolder = getHolder();
paint = new Paint();
//
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void run() {
while (gameRunning) {
update();
draw();
control();
}
}
private void update() {
if(!firstTime) {
ship.update();
}
}
private void draw() {
if (surfaceHolder.getSurface().isValid()) { // surface
if(firstTime){ //
firstTime = false;
unitW = surfaceHolder.getSurfaceFrame().width()/maxX; //
unitH = surfaceHolder.getSurfaceFrame().height()/maxY;
ship = new Ship(getContext()); //
}
canvas = surfaceHolder.lockCanvas(); // canvas
canvas.drawColor(Color.BLACK); //
ship.drow(paint, canvas); //
surfaceHolder.unlockCanvasAndPost(canvas); // canvas
}
}
private void control() { // 17
try {
gameThread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected float x; //
protected float y;
protected float size; //
protected float speed; //
protected int bitmapId; // id
protected Bitmap bitmap; //
void init(Context context) { //
Bitmap cBitmap = BitmapFactory.decodeResource(context.getResources(), bitmapId);
bitmap = Bitmap.createScaledBitmap(
cBitmap, (int)(size * GameView.unitW), (int)(size * GameView.unitH), false);
cBitmap.recycle();
}
void update(){ //
}
void drow(Paint paint, Canvas canvas){ //
canvas.drawBitmap(bitmap, x*GameView.unitW, y*GameView.unitH, paint);
}
public Ship(Context context) {
bitmapId = R.drawable.ship; //
size = 5;
x=7;
y=GameView.maxY - size - 1;
speed = (float) 0.2;
init(context); //
}
@Override
public void update() { //
if(MainActivity.isLeftPressed && x >= 0){
x -= speed;
}
if(MainActivity.isRightPressed && x <= GameView.maxX - 5){
x += speed;
}
}
private int radius = 2; //
private float minSpeed = (float) 0.1; //
private float maxSpeed = (float) 0.5; //
public Asteroid(Context context) {
Random random = new Random();
bitmapId = R.drawable.asteroid;
y=0;
x = random.nextInt(GameView.maxX) - radius;
size = radius*2;
speed = minSpeed + (maxSpeed - minSpeed) * random.nextFloat();
init(context);
}
@Override
public void update() {
y += speed;
}
public boolean isCollision(float shipX, float shipY, float shipSize) {
return !(((x+size) < shipX)||(x > (shipX+shipSize))||((y+size) < shipY)||(y > (shipY+shipSize)));
}
private ArrayList asteroids = new ArrayList<>(); //
private final int ASTEROID_INTERVAL = 50; // ( )
private int currentTime = 0;
private void checkCollision(){ //
for (Asteroid asteroid : asteroids) {
if(asteroid.isCollision(ship.x, ship.y, ship.size)){
//
gameRunning = false; //
// TODO
}
}
}
private void checkIfNewAsteroid(){ // 50
if(currentTime >= ASTEROID_INTERVAL){
Asteroid asteroid = new Asteroid(getContext());
asteroids.add(asteroid);
currentTime = 0;
}else{
currentTime ++;
}
}
@Override
public void run() {
while (gameRunning) {
update();
draw();
checkCollision();
checkIfNewAsteroid();
control();
}
}
private void update() {
if(!firstTime) {
ship.update();
for (Asteroid asteroid : asteroids) {
asteroid.update();
}
}
}
private void draw() {
if (surfaceHolder.getSurface().isValid()) { // surface
if(firstTime){ //
firstTime = false;
unitW = surfaceHolder.getSurfaceFrame().width()/maxX; //
unitH = surfaceHolder.getSurfaceFrame().height()/maxY;
ship = new Ship(getContext()); //
}
canvas = surfaceHolder.lockCanvas(); // canvas
canvas.drawColor(Color.BLACK); //
ship.drow(paint, canvas); //
for(Asteroid asteroid: asteroids){ //
asteroid.drow(paint, canvas);
}
surfaceHolder.unlockCanvasAndPost(canvas); // canvas
}
}