  Android:      
  


         ""    Android     Java.     ,        ,      .     ,    ,        ,   ,      .  ,     ,          .     ,          .





 

  Android:      



    

       ,   ,     .         ,   ,     ,     ,      .        ,   ""  "",      70- .    ,         .         ,      .      :         .      ,   .

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

 ,   Java, C#, C++  Python,      .     Android, Java      ,       .       .  ,   ,    ,          .    ,             . ,           ,    .            Android Studio,          .             ,        .

   Android Studio         Android,       Android Studio.       ,    ,          .     Android Studio   .   https://developer.android.com/studio   ,       Windows, macOS  Linux. ,      ,     .      .  Windows , ,       .        ,      ,   Android SDK,   .

       ,    .      ,         .  ,     Java Development Kit (JDK),      Java.       ,    .     Android Studio.          .      :   ,     .       .

         ,       ,    Android   .      SDK,       .          .        ""    ,       .    ,     Android Studio.      ,  ,       .           .          .      AVD Manager   .      ,   Android   .          .

  ,     Android     .        ,       .      "AndroidManifest.xml",       Android Studio.      ,           .            . ,    ,  ,      .     Android Studio ,           Android.

        ,     ,      .     ,         .         Android Studio,       ,           "".     ,        ,     .  Android Studio          .          .  , , "SnakeGame",   ,     . ,           ,       .         .    ""    "Empty Activity",         .           .     , , "MainActivity".   Android Studio    ,      ,   ,  ,     .      ,      ,      ,      .  Finish,  Android Studio   .

    ,         .           .          ,   .     "app",      .      "src",  ,   ,  "main"      .  "MainActivity.java"      ,      .  ,      ,     .    ,    ,      USB  .    ,          . Android Studio         .

  ,   ,      "Hello World!"   ,    .     ,      .   ,       "".       ,    .   ,   "activity_main.xml",     "res/layout".        .    XML    ,    ,   Android Studio.          .   TextView    .          "".

    :

```xml <TextView

android:layout_width="match_parent" android:layout_height="match_parent"

android:gravity="center"

android:text=""

android:textSize="30sp" /> ```

         .      ""  .            .           .         ,   ,     "".              ,   Android-.  ,   ,      .            Android,             (UI).

    ,      ,           .         ,      "".

  . Android  XML    . ,       ,      .xml,    res/layout.          .   activity_game.xml,         .     :  ,           .

  XML      :

```xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/tvScore"



android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text=": 0"

android:textSize="24sp"

android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/>

<SurfaceView

android:id="@+id/surfaceView"

android:layout_width="match_parent" android:layout_height="match_parent"

android:layout_below="@id/tvScore"/>

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"

android:orientation="horizontal"

android:gravity="center">

<Button

android:id="@+id/btnLeft"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="" />

<Button android:id="@+id/btnRight" android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="" />

<Button

android:id="@+id/btnUp"



android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="" />

<Button

android:id="@+id/btnDown"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="" />

</LinearLayout> </RelativeLayout> ```



       RelativeLayout,      ,   SurfaceView    . SurfaceView    ,         ,      .      ,          .      (MainActivity),            :



```java public class MainActivity extends AppCompatActivity

{ private TextView tvScore;

private SurfaceView surfaceView;

@Override protected void onCreate(Bundle savedInstanceState)

{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_game);

tvScore = findViewById(R.id.tvScore);

surfaceView = findViewById(R.id.surfaceView);

//    } } ```



      ,  ,       .    ,         ,       .        .          .

     onCreate  MainActivity: Button btnLeft = findViewById(R.id.btnLeft);

Button btnRight = findViewById(R.id.btnRight);

Button btnUp = findViewById(R.id.btnUp);

Button btnDown = findViewById(R.id.btnDown);

btnLeft.setOnClickListener(v -> {

//     }); btnRight.setOnClickListener(v -> {

//     }); btnUp.setOnClickListener(v -> {

//     }); btnDown.setOnClickListener(v -> {

//     });

       ,         .     ,       ,        .  ,        ,           .          .       .   ,             .           (, drawable-mdpi, drawable-hdpi  ..)   .

   ,        ,       .              "".    ,    ,    .          ,        .            ,      .        ,    .  :   

   ""   Android    ,    ,     .     -  (),       ,  ,  .     ,        ,      .  ,   ,    .       SnakeGame.       ,    ,     .

     SnakeGame: public class SnakeGame { private Snake snake; private Food food; private int score; public SnakeGame() { this.snake = new Snake(); this.food = new Food(); this.score = 0; }

public void moveSnake(Direction direction) {

snake.move(direction);

//       }

public void increaseScore() {

score++; //    UI }

//     }

 Snake   .           ,   ,     :

public class Snake { private List<Point> body;

private Direction direction; public Snake() {

body = new ArrayList<>();

//   }

public void move(Direction direction) {

//           }

public void grow() {

//     }

//     }

    Snake        .  move    ,   grow    ,   "" .  ,   ,   Food,     .

            :

public class Food {

private Point position;

public Food() {

spawn(); }

public void spawn() {

//      

}

//     }

 Food          .  spawn        .

        Direction:

public enum Direction { UP, DOWN, LEFT, RIGHT }

           ,       .     , , 

SnakeGame     Snake        Food   .

,    ,       ,     ,         .        ,       .  ,     , ,           .     ,           .       .       ,        ,        .   ,               .  ,         ""      .

        ,      .            ,     ,      ,    .  :   SnakeGame   SnakeGame         "".

      ,       .     ,    SnakeGame,     ,        ,     .   SnakeGame       :    ,   ,       .     : public class SnakeGame { private Snake snake; private Food food;

private int score; private int width;

private int height; private boolean isGameOver;

public SnakeGame(int width, int height)

{ this.width = width;

this.height = height;

this.snake = new Snake();

this.food = new Food();

this.score = 0;

this.isGameOver = false;

spawnFood();

} public void spawnFood()

{ food.spawn(width, height, snake.getBody());

} public void update(Direction direction)

{ if (!isGameOver) {

snake.move(direction);

checkCollision(); } }

private void checkCollision() {

if (snake.hasCollidedWithWall(width, height) || snake.hasCollidedWithItself())

{ isGameOver = true;

} else if (snake.headEquals(food.getPosition())) {

score++;

snake.grow();

spawnFood(); } } public boolean isGameOver() { return isGameOver; }public int getScore() {

return score; } }

      ,       ,      ,  ,    .    ,          ,     .

 spawnFood       ,      .      ,    Food,         ,   .  update    .

              .   checkCollision          .     ,       .

      hasCollidedWithWall, hasCollidedWithItself  headEquals,      .

  Snake,  hasCollidedWithWall    :

public boolean hasCollidedWithWall(int width, int height) {

Point head = body.get(0);

return head.x < 0 || head.x >= width || head.y < 0 || head.y >= height; }

  hasCollidedWithItself       : public boolean hasCollidedWithItself()

{ Point head = body.get(0);

for (int i = 1; i < body.size(); i++) {

if (body.get(i).equals(head)) {

return true; } } return false; }

   headEquals   Food,  ,        :

public boolean headEquals(Point foodPosition) {

Point head = body.get(0);

return head.equals(foodPosition); }

       ,     .      SnakeGame           ,     .

   ,         ,      ,     . ,       SnakeGame,           ,        .

       ,        .              ,      .               "".           ,     .

    ,            Android.        "".          .           ,      .              .

      .   GameView,    ,       .      onTouchEvent,     .  ,    :




  .


   .

   ,     (https://www.litres.ru/pages/biblio_book/?art=72003268)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


