-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] : 2D

, 11 2017 . 19:46 +
, . 2D - . Android Studio, .

1.
:

. . . . . .



2.
Android Studio File -> New -> New Project.



, . Next.



. . . . Next.



Empty Activity. Next.



Finish. . .

3.

.

drawable .



.

4. layout

activity_main.xml, Text :



    
    
        
        
    

Design layout .



, Left Right. layout , . . .

5. MainActivity

implements View.OnTouchListener. :

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {

( ):

public static boolean isLeftPressed = false; //   
public static boolean isRightPressed = false; //   

protected void onCreate(Bundle savedInstanceState) {
:

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);

LinearLayout, Button .. Import.
Import Alt+Enter.
GameView - . .

:

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;
}

- - MainActivity :

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;
    }
}

, MainActivity ! GameView. isLeftPressed = true, isRightPressed = true. .

, . .

6. GameView

- GameView. . extends SurfaceView implements Runnable. . 480x800, 1800x2560. 20 28 . . . , .

public static int maxX = 20; //   
public static int maxY = 28; //   
public static float unitW = 0; //     
public static float unitH = 0; //     

unitW unitW . :

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();
}

run() . update()
. draw() . control() 17 . 17 run() . gameRunning == true. :

@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();
    }
}

. . . .

7. SpaceBody

Ship ( ) Asteroid (). . :

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);
}

8. Ship

Ship ( ). SpaceBody extends SpaceBody.

:

public Ship(Context context) {
    bitmapId = R.drawable.ship; //   
    size = 5;
    x=7;
    y=GameView.maxY - size - 1;
    speed = (float) 0.2;

    init(context); //  
}

update()

@Override
public void update() { //       
    if(MainActivity.isLeftPressed && x >= 0){
        x -= speed;
    }
    if(MainActivity.isRightPressed && x <= GameView.maxX - 5){
        x += speed;
    }
}

! . . . . .

9. Asteroid

Asteroid (). SpaceBody extends SpaceBody.

:

private int radius = 2; // 
private float minSpeed = (float) 0.1; //  
private float maxSpeed = (float) 0.5; //  

. x speed .

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);
}

. update() x .

@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)));
}

. . . .

((x+size) < shipX) .
(x > (shipX+shipSize)) .
((y+size) < shipY) .
(y > (shipY+shipSize)) .

|| (). ( ) .

!. true . .

.

10. GameView

GameView :

private ArrayList asteroids = new ArrayList<>(); //    
private final int ASTEROID_INTERVAL = 50; //      ( )
private int currentTime = 0;

2 :

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 ++;
    }
}

run() control().

@Override
public void run() {
    while (gameRunning) {
        update();
        draw();
        checkCollision();
        checkIfNewAsteroid();
        control();
    }
}

update() update().

private void update() {
    if(!firstTime) {
        ship.update();
        for (Asteroid asteroid : asteroids) {
            asteroid.update();
        }
    }
}

draw().

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
    }
}

! 2D . , !
- - .

, , . , . . , , , . , .

. , , .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/330686/

:  

: [1] []
 

:
: 

: ( )

:

  URL