0% found this document useful (0 votes)
38 views19 pages

Code

Uploaded by

Edrian Lait
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views19 pages

Code

Uploaded by

Edrian Lait
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

activity_main.

xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.example.snake.GameView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:background="#0C2200"
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:weightSum="2"
android:layout_alignParentBottom="true"
android:alpha="0.8">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_apple"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/apple"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txt_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x 0"
android:textSize="30sp"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_cup"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_baseline_emoji_events_24"
android:scaleType="fitXY"
app:tint="#fff0"/>
<TextView
android:id="@+id/txt_best_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x 0"
android:textSize="30sp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<ImageView
android:id="@+id/img_swipe"
android:visibility="invisible"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/swipe"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.snake">

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher_foreground"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SnakeGame">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>
MainActivity.java
package com.example.snake;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


public static ImageView img_swipe;
public static Dialog dialogScore;
private GameView gv;
public static TextView txt_score, txt_best_score, txt_dialog_score,
txt_dialog_best_score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
Constants.SCREEN_WIDTH = dm.widthPixels;
Constants.SCREEN_HEIGHT = dm.heightPixels;
setContentView(R.layout.activity_main);
img_swipe = findViewById(R.id.img_swipe);
gv = findViewById(R.id.gv);
txt_score = findViewById(R.id.txt_score);
txt_best_score = findViewById(R.id.txt_best_score);
dialogScore();
}

private void dialogScore() {


int bestScore = 0;
SharedPreferences sp = this.getSharedPreferences("gamesetting",
Context.MODE_PRIVATE);
if(sp!=null){
bestScore = sp.getInt("bestscore",0);
}
MainActivity.txt_best_score.setText(bestScore+"");
dialogScore = new Dialog(this);
dialogScore.setContentView(R.layout.dialog_start);
txt_dialog_score = dialogScore.findViewById(R.id.txt_dialog_score);
txt_dialog_best_score =
dialogScore.findViewById(R.id.txt_dialog_best_score);
txt_dialog_best_score.setText(bestScore + "");
dialogScore.setCanceledOnTouchOutside(false);
RelativeLayout rl_start = dialogScore.findViewById(R.id.rl_start);
rl_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img_swipe.setVisibility(View.VISIBLE);
gv.reset();
dialogScore.dismiss();
}
});
dialogScore.show();
}
}

Apple.java
package com.example.snake;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Apple {


private Bitmap bm;
private int x, y;
private Rect r;

public Apple(Bitmap bm, int x, int y) {


this.bm = bm;
this.x = x;
this.y = y;
}

public void draw(Canvas canvas){


canvas.drawBitmap(bm, x, y, null);
}

public void reset(int nx, int ny){


this.x = nx;
this.y = ny;
}

public Bitmap getBm() {


return bm;
}

public void setBm(Bitmap bm) {


this.bm = bm;
}

public int getX() {


return x;
}
public void setX(int x) {
this.x = x;
}

public int getY() {


return y;
}

public void setY(int y) {


this.y = y;
}

public Rect getR() {


return new Rect(this.x, this.y, this.x+ GameView.sizeElementMap,
this.y+GameView.sizeElementMap);
}

public void setR(Rect r) {


this.r = r;
}
}

Constant.java
package com.example.snake;

public class Constants {


public static int SCREEN_WIDTH;
public static int SCREEN_HEIGHT;
}

Gameview.java
package com.example.snake;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Random;

public class GameView extends View {


private Bitmap bmGrass1, bmGrass2, bmSnake1, bmApple;
private ArrayList<Grass> arrGrass = new ArrayList<>();
private int w = 12, h=21;
public static int sizeElementMap = 75*Constants.SCREEN_WIDTH/1080;
private Snake snake;
private Apple apple;
private Handler handler;
private Runnable r;
private boolean move = false;
private float mx, my;
public static boolean isPlaying = false;
public static int score = 0, bestScore = 0;
private Context context;
private int soundEat, soundDie;
private float volume;
private boolean loadedsound;
private SoundPool soundPool;

public GameView(Context context, @Nullable AttributeSet attrs) {


super(context, attrs);
this.context = context;
SharedPreferences sp = context.getSharedPreferences("gamesetting",
Context.MODE_PRIVATE);
if(sp!=null){
bestScore = sp.getInt("bestscore",0);
}
bmGrass1 = BitmapFactory.decodeResource(this.getResources(),
R.drawable.grass);
bmGrass1 = Bitmap.createScaledBitmap(bmGrass1, sizeElementMap,
sizeElementMap, true);
bmGrass2 = BitmapFactory.decodeResource(this.getResources(),
R.drawable.grass03);
bmGrass2 = Bitmap.createScaledBitmap(bmGrass2, sizeElementMap,
sizeElementMap, true);
bmSnake1 = BitmapFactory.decodeResource(this.getResources(),
R.drawable.snake1);
bmSnake1 = Bitmap.createScaledBitmap(bmSnake1, 14*sizeElementMap,
sizeElementMap, true);
bmApple = BitmapFactory.decodeResource(this.getResources(),
R.drawable.apple);
bmApple = Bitmap.createScaledBitmap(bmApple, sizeElementMap,
sizeElementMap, true);
for(int i = 0; i < h; i++){
for (int j = 0; j < w; j++){
if((j+i)%2==0){
arrGrass.add(new Grass(bmGrass1, j*bmGrass1.getWidth() +
Constants.SCREEN_WIDTH/2 - (w/2)*bmGrass1.getWidth(), i*bmGrass1.getHeight()
+50*Constants.SCREEN_HEIGHT/1920, bmGrass1.getWidth(), bmGrass1.getHeight()));
}else{
arrGrass.add(new Grass(bmGrass2, j*bmGrass2.getWidth() +
Constants.SCREEN_WIDTH/2 - (w/2)*bmGrass2.getWidth(), i*bmGrass2.getHeight()
+50*Constants.SCREEN_HEIGHT/1920, bmGrass2.getWidth(), bmGrass2.getHeight()));
}
}
}
snake = new
Snake(bmSnake1,arrGrass.get(126).getX(),arrGrass.get(126).getY(), 4);
apple = new Apple(bmApple, arrGrass.get(randomApple()[0]).getX(),
arrGrass.get(randomApple()[1]).getY());
handler = new Handler();
r = new Runnable() {
@Override
public void run() {
invalidate();
}
};
if(Build.VERSION.SDK_INT>=21){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
SoundPool.Builder builder = new SoundPool.Builder();
builder.setAudioAttributes(audioAttributes).setMaxStreams(5);
this.soundPool = builder.build();
}else{
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
}
this.soundPool.setOnLoadCompleteListener(new
SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int
status) {
loadedsound = true;
}
});
soundEat = this.soundPool.load(context, R.raw.eat, 1);
soundDie = this.soundPool.load(context, R.raw.die, 1);
}

private int[] randomApple(){


int []xy = new int[2];
Random r = new Random();
xy[0] = r.nextInt(arrGrass.size()-1);
xy[1] = r.nextInt(arrGrass.size()-1);
Rect rect = new Rect(arrGrass.get(xy[0]).getX(),
arrGrass.get(xy[1]).getY(), arrGrass.get(xy[0]).getX()+sizeElementMap,
arrGrass.get(xy[1]).getY()+sizeElementMap);
boolean check = true;
while (check){
check = false;
for (int i = 0; i < snake.getArrPartSnake().size(); i++){
if(rect.intersect(snake.getArrPartSnake().get(i).getrBody())){
check = true;
xy[0] = r.nextInt(arrGrass.size()-1);
xy[1] = r.nextInt(arrGrass.size()-1);
rect = new Rect(arrGrass.get(xy[0]).getX(),
arrGrass.get(xy[1]).getY(), arrGrass.get(xy[0]).getX()+sizeElementMap,
arrGrass.get(xy[1]).getY()+sizeElementMap);
}
}
}
return xy;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int a = event.getActionMasked();
switch (a){
case MotionEvent.ACTION_MOVE:{
if(move==false){
mx = event.getX();
my = event.getY();
move = true;
}else{
if(mx - event.getX() > 100 && !snake.isMove_right()){
mx = event.getX();
my = event.getY();
this.snake.setMove_left(true);
isPlaying = true;
MainActivity.img_swipe.setVisibility(INVISIBLE);
}else if(event.getX() - mx > 100 &&!snake.isMove_left()){
mx = event.getX();
my = event.getY();
this.snake.setMove_right(true);
isPlaying = true;
MainActivity.img_swipe.setVisibility(INVISIBLE);
}else if(event.getY() - my > 100 && !snake.isMove_up()){
mx = event.getX();
my = event.getY();
this.snake.setMove_down(true);
isPlaying = true;
MainActivity.img_swipe.setVisibility(INVISIBLE);
}else if(my - event.getY() > 100 && !snake.isMove_down()){
mx = event.getX();
my = event.getY();
this.snake.setMove_up(true);
isPlaying = true;
MainActivity.img_swipe.setVisibility(INVISIBLE);
}
}
break;
}
case MotionEvent.ACTION_UP:{
mx = 0;
my = 0;
move = false;
break;
}
}
return true;
}

public void draw(Canvas canvas){


super.draw(canvas);
canvas.drawColor(0xFF065700);
for(int i = 0; i < arrGrass.size(); i++){
canvas.drawBitmap(arrGrass.get(i).getBm(), arrGrass.get(i).getX(),
arrGrass.get(i).getY(), null);
}
if(isPlaying){
snake.update();
if(snake.getArrPartSnake().get(0).getX() <
this.arrGrass.get(0).getX()
||snake.getArrPartSnake().get(0).getY() <
this.arrGrass.get(0).getY()
||snake.getArrPartSnake().get(0).getY()
+sizeElementMap>this.arrGrass.get(this.arrGrass.size()-1).getY() +
sizeElementMap
||snake.getArrPartSnake().get(0).getX()
+sizeElementMap>this.arrGrass.get(this.arrGrass.size()-1).getX() +
sizeElementMap){
gameOver();
}
for (int i = 1; i < snake.getArrPartSnake().size(); i++){
if
(snake.getArrPartSnake().get(0).getrBody().intersect(snake.getArrPartSnake().g
et(i).getrBody())){
gameOver();
}
}
}
snake.drawSnake(canvas);
apple.draw(canvas);
if(snake.getArrPartSnake().get(0).getrBody().intersect(apple.getR())){
if(loadedsound){
int streamId = this.soundPool.play(this.soundEat, (float)0.5,
(float)0.5, 1, 0, 1f);
}
apple.reset(arrGrass.get(randomApple()[0]).getX(),
arrGrass.get(randomApple()[1]).getY());
snake.addPart();
score++;
MainActivity.txt_score.setText(score+"");
if(score > bestScore){
bestScore = score;
SharedPreferences sp =
context.getSharedPreferences("gamesetting", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("bestscore", bestScore);
editor.apply();
MainActivity.txt_best_score.setText(bestScore+"");
}
}
handler.postDelayed(r, 100);
}

private void gameOver() {


isPlaying = false;
MainActivity.dialogScore.show();
MainActivity.txt_dialog_best_score.setText(bestScore+"");
MainActivity.txt_dialog_score.setText(score+"");
if(loadedsound){
int streamId = this.soundPool.play(this.soundDie, (float)0.5,
(float)0.5, 1, 0, 1f);
}
}

public void reset(){


for(int i = 0; i < h; i++){
for (int j = 0; j < w; j++){
if((j+i)%2==0){
arrGrass.add(new Grass(bmGrass1, j*bmGrass1.getWidth() +
Constants.SCREEN_WIDTH/2 - (w/2)*bmGrass1.getWidth(), i*bmGrass1.getHeight()
+50*Constants.SCREEN_HEIGHT/1920, bmGrass1.getWidth(), bmGrass1.getHeight()));
}else{
arrGrass.add(new Grass(bmGrass2, j*bmGrass2.getWidth() +
Constants.SCREEN_WIDTH/2 - (w/2)*bmGrass2.getWidth(), i*bmGrass2.getHeight()
+50*Constants.SCREEN_HEIGHT/1920, bmGrass2.getWidth(), bmGrass2.getHeight()));
}
}
}
snake = new
Snake(bmSnake1,arrGrass.get(126).getX(),arrGrass.get(126).getY(), 4);
apple = new Apple(bmApple, arrGrass.get(randomApple()[0]).getX(),
arrGrass.get(randomApple()[1]).getY());
score = 0;
}
}

Grass.java
package com.example.snake;

import android.graphics.Bitmap;

public class Grass {


private Bitmap bm;
private int x, y, width, height;

public Grass(Bitmap bm, int x, int y, int width, int height) {


this.bm = bm;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

public Bitmap getBm() {


return bm;
}

public void setBm(Bitmap bm) {


this.bm = bm;
}

public int getX() {


return x;
}

public void setX(int x) {


this.x = x;
}

public int getY() {


return y;
}

public void setY(int y) {


this.y = y;
}

public int getWidth() {


return width;
}

public void setWidth(int width) {


this.width = width;
}

public int getHeight() {


return height;
}

public void setHeight(int height) {


this.height = height;
}
}

PartSnake.java
package com.example.snake;

import android.graphics.Bitmap;
import android.graphics.Rect;

public class PartSnake {


private Bitmap bm;
private int x, y;
private Rect rBody, rTop, rBottom, rRight, rLeft;

public PartSnake(Bitmap bm, int x, int y) {


this.bm = bm;
this.x = x;
this.y = y;
}

public Bitmap getBm() {


return bm;
}
public void setBm(Bitmap bm) {
this.bm = bm;
}

public int getX() {


return x;
}

public void setX(int x) {


this.x = x;
}

public int getY() {


return y;
}

public void setY(int y) {


this.y = y;
}

public Rect getrTop() {


return new Rect(this.x, this.y-10*Constants.SCREEN_HEIGHT/1920,
this.x+GameView.sizeElementMap, this.y);
}

public void setrTop(Rect rTop) {


this.rTop = rTop;
}

public Rect getrBottom() {


return new Rect(this.x, this.y + GameView.sizeElementMap, this.x +
GameView.sizeElementMap, this.y +
GameView.sizeElementMap+10*Constants.SCREEN_HEIGHT/1920);
}

public void setrBottom(Rect rBottom) {


this.rBottom = rBottom;
}

public Rect getrRight() {


return new Rect(this.x + GameView.sizeElementMap, this.y, this.x +
GameView.sizeElementMap+10*Constants.SCREEN_WIDTH/1080,
this.y+GameView.sizeElementMap);
}

public void setrRight(Rect rRight) {


this.rRight = rRight;
}

public Rect getrLeft() {


return new Rect(this.x - 10*Constants.SCREEN_WIDTH/1080, this.y,
this.x, this.y + GameView.sizeElementMap);
}

public void setrLeft(Rect rLeft) {


this.rLeft = rLeft;
}
public Rect getrBody() {
return new Rect(this.x, this.y, this.x + GameView.sizeElementMap,
this.y + GameView.sizeElementMap);
}

public void setrBody(Rect rBody) {


this.rBody = rBody;
}
}

Snake.java
package com.example.snake;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import java.util.ArrayList;

public class Snake {


private Bitmap bm, bm_head_down, bm_head_left, bm_head_right, bm_head_up,
bm_body_vertical, bm_body_horizontal, bm_body_bottom_left,
bm_body_bottom_right, bm_body_top_left, bm_body_top_right,
bm_tail_up, bm_tail_down, bm_tail_right, bm_tail_left;
private ArrayList<PartSnake> arrPartSnake = new ArrayList<>();
private int length;
private boolean move_left, move_right, move_up, move_down;

public Snake(Bitmap bm, int x, int y, int length) {


this.bm = bm;
this.length = length;
bm_body_bottom_left = Bitmap.createBitmap(bm, 0, 0, bm.getWidth()/14,
bm.getHeight());
bm_body_bottom_right = Bitmap.createBitmap(bm, bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_body_horizontal = Bitmap.createBitmap(bm, 2*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_body_top_left = Bitmap.createBitmap(bm, 3*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_body_top_right = Bitmap.createBitmap(bm, 4*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_body_vertical = Bitmap.createBitmap(bm, 5*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_head_down = Bitmap.createBitmap(bm, 6*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_head_left = Bitmap.createBitmap(bm, 7*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_head_right = Bitmap.createBitmap(bm, 8*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_head_up = Bitmap.createBitmap(bm, 9*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_tail_up = Bitmap.createBitmap(bm, 10*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_tail_right = Bitmap.createBitmap(bm, 11*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_tail_left = Bitmap.createBitmap(bm, 12*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
bm_tail_down = Bitmap.createBitmap(bm, 13*bm.getWidth()/14, 0,
bm.getWidth()/14, bm.getHeight());
setMove_right(true);
arrPartSnake.add(new PartSnake(bm_head_right, x, y));
for (int i = 1; i < length-1; i++){
this.arrPartSnake.add(new PartSnake(bm_body_horizontal,
this.arrPartSnake.get(i-1).getX()-GameView.sizeElementMap, y));
}
arrPartSnake.add(new PartSnake(bm_tail_right, arrPartSnake.get(length-
2).getX()-GameView.sizeElementMap, arrPartSnake.get(length-2).getY()));
}

public void update(){


for(int i = length-1; i > 0; i--){
arrPartSnake.get(i).setX(arrPartSnake.get(i-1).getX());
arrPartSnake.get(i).setY(arrPartSnake.get(i-1).getY());
}
if(move_right){
arrPartSnake.get(0).setX(arrPartSnake.get(0).getX()
+GameView.sizeElementMap);
arrPartSnake.get(0).setBm(bm_head_right);
}else if(move_down){
arrPartSnake.get(0).setY(arrPartSnake.get(0).getY()
+GameView.sizeElementMap);
arrPartSnake.get(0).setBm(bm_head_down);
}else if(move_up){
arrPartSnake.get(0).setY(arrPartSnake.get(0).getY()-
GameView.sizeElementMap);
arrPartSnake.get(0).setBm(bm_head_up);
}else{
arrPartSnake.get(0).setX(arrPartSnake.get(0).getX()-
GameView.sizeElementMap);
arrPartSnake.get(0).setBm(bm_head_left);
}
for (int i = 1; i < length - 1; i++){

if(arrPartSnake.get(i).getrLeft().intersect(arrPartSnake.get(i+1).getrBody())

&&arrPartSnake.get(i).getrBottom().intersect(arrPartSnake.get(i-1).getrBody())
||
arrPartSnake.get(i).getrBottom().intersect(arrPartSnake.get(i+1).getrBody())

&&arrPartSnake.get(i).getrLeft().intersect(arrPartSnake.get(i-1).getrBody())){
arrPartSnake.get(i).setBm(bm_body_bottom_left);
}else if
(arrPartSnake.get(i).getrLeft().intersect(arrPartSnake.get(i+1).getrBody())

&&arrPartSnake.get(i).getrTop().intersect(arrPartSnake.get(i-1).getrBody())
||
arrPartSnake.get(i).getrLeft().intersect(arrPartSnake.get(i-1).getrBody())

&&arrPartSnake.get(i).getrTop().intersect(arrPartSnake.get(i+1).getrBody())){
arrPartSnake.get(i).setBm(bm_body_top_left);
}else if
(arrPartSnake.get(i).getrRight().intersect(arrPartSnake.get(i+1).getrBody())

&&arrPartSnake.get(i).getrTop().intersect(arrPartSnake.get(i-1).getrBody())
||
arrPartSnake.get(i).getrRight().intersect(arrPartSnake.get(i-1).getrBody())

&&arrPartSnake.get(i).getrTop().intersect(arrPartSnake.get(i+1).getrBody())) {
arrPartSnake.get(i).setBm(bm_body_top_right);
}else
if(arrPartSnake.get(i).getrRight().intersect(arrPartSnake.get(i+1).getrBody())

&&arrPartSnake.get(i).getrBottom().intersect(arrPartSnake.get(i-1).getrBody())
||
arrPartSnake.get(i).getrRight().intersect(arrPartSnake.get(i-1).getrBody())

&&arrPartSnake.get(i).getrBottom().intersect(arrPartSnake.get(i+1).getrBody())
){
arrPartSnake.get(i).setBm(bm_body_bottom_right);
}else
if(arrPartSnake.get(i).getrLeft().intersect(arrPartSnake.get(i-1).getrBody())

&&arrPartSnake.get(i).getrRight().intersect(arrPartSnake.get(i+1).getrBody())
||
arrPartSnake.get(i).getrLeft().intersect(arrPartSnake.get(i+1).getrBody())

&&arrPartSnake.get(i).getrRight().intersect(arrPartSnake.get(i-1).getrBody()))
{
arrPartSnake.get(i).setBm(bm_body_horizontal);
}else
if(arrPartSnake.get(i).getrTop().intersect(arrPartSnake.get(i-1).getrBody())

&&arrPartSnake.get(i).getrBottom().intersect(arrPartSnake.get(i+1).getrBody())
||
arrPartSnake.get(i).getrTop().intersect(arrPartSnake.get(i+1).getrBody())

&&arrPartSnake.get(i).getrBottom().intersect(arrPartSnake.get(i-
1).getrBody())){
arrPartSnake.get(i).setBm(bm_body_vertical);
}else{
if(move_right){
arrPartSnake.get(i).setBm(bm_body_horizontal);
}else if(move_down){
arrPartSnake.get(i).setBm(bm_body_vertical);
}else if(move_up){
arrPartSnake.get(i).setBm(bm_body_vertical);
}else{
arrPartSnake.get(i).setBm(bm_body_horizontal);
}
}
}
if(arrPartSnake.get(length-
1).getrRight().intersect(arrPartSnake.get(length-2).getrBody())){
arrPartSnake.get(length-1).setBm(bm_tail_right);
}else if(arrPartSnake.get(length-
1).getrLeft().intersect(arrPartSnake.get(length-2).getrBody())){
arrPartSnake.get(length-1).setBm(bm_tail_left);
}else if(arrPartSnake.get(length-
1).getrBottom().intersect(arrPartSnake.get(length-2).getrBody())){
arrPartSnake.get(length-1).setBm(bm_tail_down);
}else{
arrPartSnake.get(length-1).setBm(bm_tail_up);
}
}

public void drawSnake(Canvas canvas){


for(int i = length-1; i >= 0; i--){
canvas.drawBitmap(arrPartSnake.get(i).getBm(),
arrPartSnake.get(i).getX(), arrPartSnake.get(i).getY(), null);
}
}

public Bitmap getBm() {


return bm;
}

public void setBm(Bitmap bm) {


this.bm = bm;
}

public Bitmap getBm_head_down() {


return bm_head_down;
}

public void setBm_head_down(Bitmap bm_head_down) {


this.bm_head_down = bm_head_down;
}

public Bitmap getBm_head_left() {


return bm_head_left;
}

public void setBm_head_left(Bitmap bm_head_left) {


this.bm_head_left = bm_head_left;
}

public Bitmap getBm_head_right() {


return bm_head_right;
}

public void setBm_head_right(Bitmap bm_head_right) {


this.bm_head_right = bm_head_right;
}

public Bitmap getBm_head_up() {


return bm_head_up;
}

public void setBm_head_up(Bitmap bm_head_up) {


this.bm_head_up = bm_head_up;
}
public Bitmap getBm_body_vertical() {
return bm_body_vertical;
}

public void setBm_body_vertical(Bitmap bm_body_vertical) {


this.bm_body_vertical = bm_body_vertical;
}

public Bitmap getBm_body_horizontal() {


return bm_body_horizontal;
}

public void setBm_body_horizontal(Bitmap bm_body_horizontal) {


this.bm_body_horizontal = bm_body_horizontal;
}

public Bitmap getBm_body_bottom_left() {


return bm_body_bottom_left;
}

public void setBm_body_bottom_left(Bitmap bm_body_bottom_left) {


this.bm_body_bottom_left = bm_body_bottom_left;
}

public Bitmap getBm_body_bottom_right() {


return bm_body_bottom_right;
}

public void setBm_body_bottom_right(Bitmap bm_body_bottom_right) {


this.bm_body_bottom_right = bm_body_bottom_right;
}

public Bitmap getBm_body_top_left() {


return bm_body_top_left;
}

public void setBm_body_top_left(Bitmap bm_body_top_left) {


this.bm_body_top_left = bm_body_top_left;
}

public Bitmap getBm_body_top_right() {


return bm_body_top_right;
}

public void setBm_body_top_right(Bitmap bm_body_top_right) {


this.bm_body_top_right = bm_body_top_right;
}

public Bitmap getBm_tail_up() {


return bm_tail_up;
}

public void setBm_tail_up(Bitmap bm_tail_up) {


this.bm_tail_up = bm_tail_up;
}
public Bitmap getBm_tail_down() {
return bm_tail_down;
}

public void setBm_tail_down(Bitmap bm_tail_down) {


this.bm_tail_down = bm_tail_down;
}

public Bitmap getBm_tail_right() {


return bm_tail_right;
}

public void setBm_tail_right(Bitmap bm_tail_right) {


this.bm_tail_right = bm_tail_right;
}

public Bitmap getBm_tail_left() {


return bm_tail_left;
}

public void setBm_tail_left(Bitmap bm_tail_left) {


this.bm_tail_left = bm_tail_left;
}

public ArrayList<PartSnake> getArrPartSnake() {


return arrPartSnake;
}

public void setArrPartSnake(ArrayList<PartSnake> arrPartSnake) {


this.arrPartSnake = arrPartSnake;
}

public int getLength() {


return length;
}

public void setLength(int length) {


this.length = length;
}

public boolean isMove_left() {


return move_left;
}

public void setMove_left(boolean move_left) {


this.setup();
this.move_left = move_left;
}

public boolean isMove_right() {


return move_right;
}

public void setMove_right(boolean move_right) {


this.setup();
this.move_right = move_right;
}
public boolean isMove_up() {
return move_up;
}

public void setMove_up(boolean move_up) {


this.setup();
this.move_up = move_up;
}

public boolean isMove_down() {


return move_down;
}

public void setMove_down(boolean move_down) {


this.setup();
this.move_down = move_down;
}

public void setup(){


this.move_right = false;
this.move_down = false;
this.move_left = false;
this.move_up = false;
}

public void addPart() {


PartSnake p = this.arrPartSnake.get(length-1);
this.length += 1;
if(p.getBm()==bm_tail_right){
this.arrPartSnake.add(new PartSnake(bm_tail_right, p.getX()-
GameView.sizeElementMap, p.getY()));
}else if(p.getBm()==bm_tail_left){
this.arrPartSnake.add(new PartSnake(bm_tail_left, p.getX()
+GameView.sizeElementMap, p.getY()));
}else if(p.getBm()==bm_tail_up){
this.arrPartSnake.add(new PartSnake(bm_tail_up, p.getX(), p.getY()
+GameView.sizeElementMap));
}else if(p.getBm()==bm_tail_down){
this.arrPartSnake.add(new PartSnake(bm_tail_up, p.getX(),
p.getY()-GameView.sizeElementMap));
}
}
}

You might also like