Створення та оптимізація засобів штучного інтелекту на основі генетичних алгоритмів



Сторінка9/10
Дата конвертації12.04.2017
Розмір0.65 Mb.
1   2   3   4   5   6   7   8   9   10

ДОДАТКИ

Додаток А


(обов’язковий)

Додаток Б


(обов’язковий)

Додаток В


(обов’язковий)

Додаток Г


Лістинг програми

package com.example.waros.app;


import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Random;


import objects.Blood;

import objects.Enemy;


import android.annotation.SuppressLint;

import android.annotation.TargetApi;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.BitmapShader;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.DrawFilter;

import android.graphics.Matrix;

import android.graphics.Paint;

import android.graphics.PaintFlagsDrawFilter;

import android.graphics.Path;

import android.graphics.Point;

import android.graphics.Rect;

import android.graphics.Shader;

import android.media.MediaPlayer;

import android.os.Build;

import android.os.Handler;

import android.util.AttributeSet;

import android.view.Display;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.Window;

import android.widget.RelativeLayout.LayoutParams;

import android.widget.TextView;

public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
private Canvas cacheCanvas;

private Bitmap backBuffer;

public static int width;

public static int height;

private int clientHeight;

private Activity context;

private SurfaceHolder mHolder;
private DrawFilter mFastDF;

private Shader mShader;

private Paint backgroundPaint,

paint,


upgradePaint,

pEnergy,


pShield,

timer;
private Matrix rotator,

laserRotator,

laserRotator1,

laserRotator2,

laserRotator3,

klesh_rotator_1,

matBullet,

lezoMatrix,

lezoMatrix1;


private boolean isSurfaceCreated;
private List tracesOfRed, deletedBlood;

private int bloodCount = 0;


private List enemies;
public static final int [] backgrounds = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, R.drawable.pic9};

public int [] heroes = {R.drawable.ic_launcher, R.drawable.apple_lit};

public int [] lasers = {R.drawable.laser_long, R.drawable.laser_long_first, R.drawable.laser_long_second, R.drawable.laser_long_third};

private int backgroundId;


public float controlPosX = 0,

comboX = 0,

comboY = 0,

comboRotate = 0,

comboXbegin = -1,

comboYbegin = -1,

comboAngleBegin = -1,

comboAngelBeginDegrees = -1,


appleX = 300,

appleY = 300,

appleSpeed = 3,

heroSpeed = 0,

attackRadius = 0,

clawSizeCoef = 1,

bulletSpeed = 1.8f,
appXcenter,

bulletXcenter,

appYcenter,

bulletYcenter,

lezoSpeed = 0f,

lezoAngle = 0f,

lezoRadius = 0f,
textSize = 10;
private int comboTimeShift = 0,

skillPoint = 0,

energy = 100,

shield = 100,


currRazorSpeed = 0,
hitsForNextEnemy = 5,

hitPoints = 0,

enemiesOnScreen = 1,
speedUpgrade = 1,

bullSpeedUpgrade = 1,

generatorUpgrade = 1,
currentWeapon;
private String text;

public Rect rect, textBounds;


public boolean isTextWidhest;
private MediaPlayer mediaPlayer;

int media = 0;


public static Bitmap myBitmap,

joy_bg,


joy,

Hero,


kleshnia_1,

kleshnia_2,

apple,

upgrade,


pBlood,

fireButton,

bullet,

laser,


laser1,

laser2,


laser3,

laser_t,


laser_l,

laser_p,


fireButton1,

fireButton2,

blik,

lezo;
private GameThread _thread;



private GameControls coordinates;
float deltaX = 0;

float deltaY = 0;


private int counter;

public boolean isDownOnJoy = false,

isCombo = false,

isLaserActive = false,

wasLaserctive = false,

isComboRunning = false,

isComboFinishing = false,

isRazorActive,

isHitThisTime = false,

isHitSecondTime = false,


isAppleMovingTop = false,

isAppleMovingRight = true,


isLoose;
public static int hits;
public DrawingSurface(Activity context) {

super(context);

this.context = context;
init();

}
public DrawingSurface(Activity context, AttributeSet attrs) {

super(context, attrs);

this.context = context;

init();

}
private void init() {



mHolder = getHolder();

mHolder.addCallback(this);

width = getClientWidth();

height = getClientHeight();

Random r = new Random();

backgroundId = r.nextInt(backgrounds.length);

deletedBlood = new ArrayList();

tracesOfRed = new ArrayList();

enemies = new ArrayList();

enemies.add(new Enemy(appleX, appleY, appleSpeed, isAppleMovingTop, isAppleMovingRight, 0));

for (int i = 0; i<15; i++){

addEnemy();

}

counter = 0;



_thread = new GameThread(mHolder, context, new Handler(),this);

setFocusable(true);

mediaPlayer = new MediaPlayer();

coordinates = new GameControls();

currentWeapon = context.getIntent().getIntExtra(Splash.WEAPON, 0);

setShipParams(currentWeapon);

coordinates.setCurrentWaepon(currentWeapon);

setOnTouchListener(coordinates);

initializeMatrix();

initializePaints();

initializeBitmaps();

}

@Deprecated



public Canvas getCanvas() {

return cacheCanvas;

}

private void onPause() {



// _thread.interrupt();

_thread.stop();

}

private void onResume() {



// TODO Auto-generated method stub

}

private void onDestroy() {



_thread.interrupt();

}
protected void draw(int lShift, int rShift) {


if (clientHeight == 0) {

clientHeight = getClientHeight();

height = clientHeight;

backBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

cacheCanvas.setBitmap(backBuffer);

cacheCanvas.setDrawFilter(mFastDF);

cacheCanvas.drawPaint(backgroundPaint);

}
Canvas canvas = null;

try{


canvas = mHolder.lockCanvas(null);

coordinates.update(null);


if (counter > 360000){

counter = 0;

}
isCombo = coordinates.getCombo();

isLaserActive = coordinates.isLaserActive;

isRazorActive = coordinates.isLezoActive;

counter++;

if(counter%2 == 0 && energy<100){

energy++;

}

// showUpgrades();



updateHits();
if (shield <=0){

isLoose = true;

}

//back buffer


matrixReset();

//rotation

rotator.preTranslate(coordinates.pictureX, coordinates.pictureY);

rotator.postRotate((float)cal_angle(coordinates.speedH, coordinates.speedV) + 90,coordinates.pictureX + myBitmap.getWidth() / 2,coordinates.pictureY + myBitmap.getHeight() / 2);

//rotation end


//moving apple on screen

moveEnemy();


klesh_rotator_1.preTranslate(coordinates.pictureX + 40, coordinates.pictureY + 40);

klesh_rotator_1.postRotate((float)cal_angle(coordinates.speedH, coordinates.speedV) - 90 + lShift,coordinates.pictureX + myBitmap.getWidth() / 2,coordinates.pictureY + myBitmap.getHeight() / 2);

//bullet
if(energy<10 && isCombo){

isCombo = false;

}

calculateBullet();



//bullet
if (isRazorActive && energy>3){

if(currRazorSpeed == 0){

currRazorSpeed++;

} else {


if (currRazorSpeed < 50 && counter % 3 == 0){

currRazorSpeed ++;

energy = energy - 3;

}

}



}
if(!isRazorActive){

if(currRazorSpeed>0 && counter % 3 == 0){

currRazorSpeed--;

}

}



lezoSpeed = (float) currRazorSpeed;

//changing text and sound of hits

// updateHits();
pEnergy.setColor(Color.RED);

pShield.setColor(Color.BLUE);


if(energy > 0){

cacheCanvas.drawRect(5, 5, 5+energy*2, 30, pEnergy);

}

if(shield > 0){



cacheCanvas.drawRect(5, 35, 5+shield*2, 60, pShield);

}

deletedBlood = new ArrayList();


for (Blood blood : tracesOfRed) {

timer.setAlpha(blood.getTime());

if(blood.getTime() > 10){

blood.setTime(blood.getTime() - 5);

cacheCanvas.drawBitmap(pBlood, blood.getX(), blood.getY(), timer);

} else {


if (counter%10 == 10) {

deletedBlood.add(blood);

// tracesOfRed.remove(blood);

}

}



}
if (deletedBlood.size()>0) {

Iterator dIterator = deletedBlood.iterator();

while (dIterator.hasNext()) {

Blood blood = dIterator.next();

tracesOfRed.remove(blood);

}

}



cacheCanvas.drawBitmap(fireButton1, width - 200, height - 200, null);

// cacheCanvas.drawBitmap(fireButton2, width - 100, height - 250, null);

for(Enemy enemy : enemies){

cacheCanvas.drawBitmap(apple, enemy.getX(), enemy.getY(), null);

if(enemy.getHp()>0) {

cacheCanvas.drawRect(enemy.getX(), enemy.getY() + apple.getHeight(), enemy.getX() + apple.getWidth() * (float)((float)enemy.getHp() / (float)enemy.getMaxHp()), enemy.getY() + apple.getHeight() + 5, pEnergy);

}

}

//lezo



if(currRazorSpeed > 0) {

lezoRadius = currRazorSpeed / 5;

lezoAngle = (lezoAngle + lezoSpeed)%360;

lezoMatrix.preTranslate(coordinates.pictureX + myBitmap.getWidth() / 2 + 20,coordinates.pictureY + myBitmap.getHeight() / 2 - lezo.getHeight() / 2);

lezoMatrix.postRotate(lezoAngle,coordinates.pictureX + myBitmap.getWidth() / 2,coordinates.pictureY + myBitmap.getHeight() / 2);

lezoMatrix1.preTranslate(coordinates.pictureX + myBitmap.getWidth() / 2 + 20,coordinates.pictureY + myBitmap.getHeight() / 2 - lezo.getHeight() / 2);

lezoMatrix1.postRotate(lezoAngle + 180,coordinates.pictureX + myBitmap.getWidth() / 2,coordinates.pictureY + myBitmap.getHeight() / 2);

for (Enemy enemy : enemies) {

appXcenter = enemy.getX() + apple.getWidth() / 2;

appYcenter = enemy.getY() + apple.getHeight() / 2;

float heroXCenter = coordinates.pictureX + myBitmap.getWidth() / 2;

float heroYCenter = coordinates.pictureY + myBitmap.getHeight() / 2;


boolean isSlashed = isSlashed(cal_angle(appXcenter - (coordinates.pictureX + myBitmap.getWidth() / 2), appYcenter - (coordinates.pictureY + myBitmap.getHeight() / 2)));

boolean isInRadius = (Math.abs(appXcenter - heroXCenter) < 125) && (Math.abs(appYcenter - heroYCenter) < 125);

if (isSlashed && isInRadius) {

hits++;


hitPoints++;

hitsForNextEnemy++;

addBlood(enemy.getX(), enemy.getY());

Random r = new Random();

appleX = r.nextInt(width - 100);

appleY = r.nextInt(height - 100);

enemy.setX(appleX);

enemy.setY(appleY);

}

}

}



if (isComboFinishing || isComboRunning){

cacheCanvas.drawBitmap(bullet, matBullet, null);

}
// if(!isLaserActive){

// wasLaserctive = false;

// }

// if(energy<=30){



// isLaserActive = false;

// }
//WORKING ALGORITHM AFTER 30 ENERGY STOP

// if(isLaserActive && energy>30){

// wasLaserctive = true;

// }

// if(!isLaserActive){



// wasLaserctive = false;

// }


//END OF ALGORITHM
if(currRazorSpeed>0){

cacheCanvas.drawBitmap(lezo, lezoMatrix, null);

cacheCanvas.drawBitmap(lezo, lezoMatrix1, null);

}
//llaser

// cacheCanvas.drawText("" + isLaserActive, 10, 5 + textSize + textSize + textSize, paint);

if (isLaserActive){

energy--;

// if(energy < 1){

// wasLaserctive = false;

// }


// matLaser.preTranslate(coordinates.pictureX + myBitmap.getWidth() / 2 - laser_l.getWidth() / 2,

// coordinates.pictureY + myBitmap.getHeight() / 2 - laser_l.getHeight() / 2);

// matLaser.preTranslate(appleX, appleY);

// matLaser.postRotate((float)cal_angle(coordinates.speedH, coordinates.speedV),

// coordinates.pictureX + myBitmap.getWidth() / 2,

// coordinates.pictureY + myBitmap.getHeight() / 2);

double angle = cal_angle(coordinates.speedH, coordinates.speedV) + 90;

laserRotator.preTranslate(coordinates.pictureX + myBitmap.getWidth() / 2 - 5, coordinates.pictureY + myBitmap.getHeight() / 2);

// laserRotator1.preTranslate(coordinates.pictureX + myBitmap.getWidth() / 2 - 5, coordinates.pictureY + myBitmap.getHeight() / 2);

// laserRotator2.preTranslate(coordinates.pictureX + myBitmap.getWidth() / 2 - 5, coordinates.pictureY + myBitmap.getHeight() / 2);

// laserRotator3.preTranslate(coordinates.pictureX + myBitmap.getWidth() / 2 - 5, coordinates.pictureY + myBitmap.getHeight() / 2);

laserRotator.postRotate((float)cal_angle(coordinates.speedH, coordinates.speedV) - 90,coordinates.pictureX + myBitmap.getWidth() / 2,coordinates.pictureY + myBitmap.getHeight() / 2);

// laserRotator1.postRotate((float)cal_angle(coordinates.speedH, coordinates.speedV),coordinates.pictureX + myBitmap.getWidth() / 2,coordinates.pictureY + myBitmap.getHeight() / 2);

// laserRotator2.postRotate((float)cal_angle(coordinates.speedH, coordinates.speedV) + 90,coordinates.pictureX + myBitmap.getWidth() / 2,coordinates.pictureY + myBitmap.getHeight() / 2);

// laserRotator3.postRotate((float)cal_angle(coordinates.speedH, coordinates.speedV) - 180,coordinates.pictureX + myBitmap.getWidth() / 2,coordinates.pictureY + myBitmap.getHeight() / 2);

// cacheCanvas.drawText("" + (angle - 90), 10, 5 + textSize + textSize + textSize, paint);


if (lShift == 0){

cacheCanvas.drawBitmap(laser, laserRotator, null);

// cacheCanvas.drawBitmap(laser, laserRotator1, null);

// cacheCanvas.drawBitmap(laser, laserRotator2, null);

// cacheCanvas.drawBitmap(laser, laserRotator3, null);

}

if (lShift == 1){



cacheCanvas.drawBitmap(laser1, laserRotator, null);

// cacheCanvas.drawBitmap(laser1, laserRotator1, null);

// cacheCanvas.drawBitmap(laser1, laserRotator2, null);

// cacheCanvas.drawBitmap(laser1, laserRotator3, null);

}

if (lShift == 2){



cacheCanvas.drawBitmap(laser2, laserRotator, null);

// cacheCanvas.drawBitmap(laser2, laserRotator1, null);

// cacheCanvas.drawBitmap(laser2, laserRotator2, null);

// cacheCanvas.drawBitmap(laser2, laserRotator3, null);

}

if (lShift == 3){



cacheCanvas.drawBitmap(laser3, laserRotator, null);

// cacheCanvas.drawBitmap(laser3, laserRotator1, null);

// cacheCanvas.drawBitmap(laser3, laserRotator2, null);

// cacheCanvas.drawBitmap(laser3, laserRotator3, null);

}

// energy--;



hitEnemyLaser(angle);

}

// if (angle>0 && angle < 90){



// cacheCanvas.drawBitmap(blik, 0, 0, null);

// }


if (hitsForNextEnemy > 30){

if(enemiesOnScreen<50) {

addEnemy();

}

hitsForNextEnemy=0;



}

cacheCanvas.drawBitmap(myBitmap, rotator, null);

// cacheCanvas.drawText("" + comboAngleBegin, width - 300, 200, upgradePaint);

// if (skillPoint > 0){

// cacheCanvas.drawText("HERO SPEED", width - 320, 45, upgradePaint);

// cacheCanvas.drawBitmap(upgrade, width - 65, 0, null);

// cacheCanvas.drawText("BULLET SPEED", width - 320, 100, upgradePaint);

// cacheCanvas.drawBitmap(upgrade, width - 65, 55, null);

// cacheCanvas.drawText("POWER GENERATOR", width - 320, 155, upgradePaint);

// cacheCanvas.drawBitmap(upgrade, width - 65, 110, null);

// }
cacheCanvas.drawText("energy: " + energy, width - 300, 200, upgradePaint);

cacheCanvas.drawText(text, 10, 110, paint);

cacheCanvas.drawText("Enemies : " + enemiesOnScreen, 10, 150, paint);
// cacheCanvas.drawText("generator = " + generatorUpgrade, 10, 200, paint);

// cacheCanvas.drawText("speed = " + speedUpgrade, 10, 250, paint);

// cacheCanvas.drawText("bullet = " + bullSpeedUpgrade, 10, 300, paint);
if (isLoose){

cacheCanvas.drawText("YOU LOOSE!!!", 0, 0, upgradePaint);

}
if (coordinates.isDownOnJoy) {

cacheCanvas.drawBitmap(joy_bg, coordinates.downX + 7 - (joy_bg.getWidth() / 2), coordinates.downY + 6 - (joy_bg.getHeight() / 2), null);

double angle = cal_angle(coordinates.speedH, coordinates.speedV);

float x,y;

if (coordinates.distance < 100) {

x = coordinates.speedH;

y = coordinates.speedV;

} else {


x = (float) (Math.cos(Math.toRadians(angle)) * 100);

y = (float) (Math.sin(Math.toRadians(angle)) * 100);

}

cacheCanvas.drawBitmap(joy, x + coordinates.downX - (joy.getWidth() / 2), y + coordinates.downY - (joy.getHeight() / 2), null);



}
canvas.drawBitmap(backBuffer, 0, 0, paint);

if (mHolder != null)

mHolder.unlockCanvasAndPost(canvas);

} catch(Exception ex) {

ex.printStackTrace();

} finally {


}

}
protected void updateCanvas(int lShift, int rShift) {

cacheCanvas.drawPaint(backgroundPaint);

draw(lShift, rShift);

}

public static int getH(){



return height;

}
public static int getW(){

return width;

}
private double cal_angle(float x, float y) {

if(x > 0 && y >= 0)

return Math.toDegrees(Math.atan(y / x));

else if(x < 0 && y >= 0)

return Math.toDegrees(Math.atan(y / x)) + 180;

else if(x < 0 && y < 0)

return Math.toDegrees(Math.atan(y / x)) + 180;

else if(x > 0 && y < 0)

return Math.toDegrees(Math.atan(y / x)) + 360;

else if(x == 0 && y > 0)

return 90;

return 0;

}
private double cal_angle_rad(float x, float y) {

if(x >= 0 && y >= 0)

return Math.atan(y / x);

else if(x < 0 && y >= 0)

return Math.atan(y / x) + Math.PI;

else if(x < 0 && y < 0)

return Math.atan(y / x) + Math.PI;

else if(x >= 0 && y < 0)

return Math.atan(y / x) + 2*Math.PI;

return 0;

}
private Path drawEnergy(int energ){

Path line = new Path();

line.moveTo(5, 5);

line.lineTo(5+energ, 5);

line.lineTo(5+energ, 15);

line.lineTo(5, 15);

line.lineTo(5, 5);

return line;

}
private Path drawShield(int shiel){

Path line = new Path();

line.moveTo(5, 5);

line.lineTo(5+shiel, 5);

line.lineTo(5+shiel, 15);

line.lineTo(5, 15);

line.lineTo(5, 5);

return line;

}
private Path drawArrow(float x, float y, float width, float height, float angle){

float[] values = {

x, y + height / 4,

x + width / 3 * 2, y + height / 4,

x + width / 3 * 2, y,

x + width, y + height / 2,

x + width / 3 * 2, y + height,

x + width / 3 * 2, y + height / 4 * 3,

x, y + height / 4 * 3,

x, y + height / 4

};
float x0 = x, y0 = y + height / 2;

double a = Math.toRadians(-angle);

Path arrow = new Path();

arrow.moveTo((float)(x0 + (values[0] - x0) * Math.cos(a) - (values[1] - y0) * Math.sin(a)), (float)(y0 + (values[1] - y0) * Math.cos(a) + (values[0] - x0) * Math.sin(a)));

arrow.lineTo((float)(x0 + (values[2] - x0) * Math.cos(a) - (values[3] - y0) * Math.sin(a)), (float)(y0 + (values[3] - y0) * Math.cos(a) + (values[2] - x0) * Math.sin(a)));

arrow.lineTo((float)(x0 + (values[4] - x0) * Math.cos(a) - (values[5] - y0) * Math.sin(a)), (float)(y0 + (values[5] - y0) * Math.cos(a) + (values[4] - x0) * Math.sin(a)));

arrow.lineTo((float)(x0 + (values[6] - x0) * Math.cos(a) - (values[7] - y0) * Math.sin(a)), (float)(y0 + (values[7] - y0) * Math.cos(a) + (values[6] - x0) * Math.sin(a)));

arrow.lineTo((float)(x0 + (values[8] - x0) * Math.cos(a) - (values[9] - y0) * Math.sin(a)), (float)(y0 + (values[9] - y0) * Math.cos(a) + (values[8] - x0) * Math.sin(a)));

arrow.lineTo((float)(x0 + (values[10] - x0) * Math.cos(a) - (values[11] - y0) * Math.sin(a)), (float)(y0 + (values[11] - y0) * Math.cos(a) + (values[10] - x0) * Math.sin(a)));

arrow.lineTo((float)(x0 + (values[12] - x0) * Math.cos(a) - (values[13] - y0) * Math.sin(a)), (float)(y0 + (values[13] - y0) * Math.cos(a) + (values[12] - x0) * Math.sin(a)));

arrow.lineTo((float)(x0 + (values[0] - x0) * Math.cos(a) - (values[1] - y0) * Math.sin(a)), (float)(y0 + (values[1] - y0) * Math.cos(a) + (values[0] - x0) * Math.sin(a)));

return arrow;
}
public void androidVSios(){

heroes[0] = R.drawable.ic_launcher;

heroes[1] = R.drawable.apple_lit;

newGame();

}
public void iosVSandroid(){

heroes[0] = R.drawable.hero_android;

heroes[1] = R.drawable.android_lit;

newGame();

}

public void newGame() {



heroes[0] = R.drawable.apple_lit;

heroes[1] = R.drawable.ic_launcher;

tracesOfRed = new ArrayList();

controlPosX = 0;

comboX = 0;

comboY = 0;

comboRotate = 0;

comboXbegin = -1;

comboYbegin = -1;

comboAngleBegin = -1;

appleX = 300;

appleY = 300;

appleSpeed = 3;

comboTimeShift = 0;

hits = 0;

media = 0;

isDownOnJoy = false;

isCombo = false;

isComboRunning = false;

isComboFinishing = false;

isHitThisTime = false;

isHitSecondTime = false;

isAppleMovingTop = false;

isAppleMovingRight = true;

clientHeight = 0;

skillPoint = 0;

attackRadius = 0;

heroSpeed = 0;

coordinates.speed = 5;
Random r = new Random();

backgroundId = r.nextInt(backgrounds.length);

backBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // back buffer

cacheCanvas.setBitmap(backBuffer);


Bitmap background = BitmapFactory.decodeResource(getResources(), backgrounds[backgroundId]);
Bitmap backgroundScreenParams = Bitmap.createScaledBitmap(background, width,

height, false);


mFastDF = new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG, 0);
mShader = new BitmapShader(backgroundScreenParams, Shader.TileMode.MIRROR,

Shader.TileMode.MIRROR);


backgroundPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
backgroundPaint.setShader(mShader);

updateCanvas(0, 0);

}
public int getSkillPoint(){

return skillPoint;

}
public void setSkillPoint(int skillpoint){

skillPoint = skillpoint;

}
public void addBlood(float x, float y){

Blood blood = new Blood(x, y, 250);

if (tracesOfRed.size()<40){

tracesOfRed.add(blood);

bloodCount = 0;

} else {


tracesOfRed.set(bloodCount, blood);

bloodCount++;

if (bloodCount>39){

bloodCount = 0;

}

}

}


public void matrixReset(){

rotator.reset();

laserRotator.reset();

klesh_rotator_1.reset();

matBullet.reset();

lezoMatrix.reset();

lezoMatrix1.reset();

}
public void initializeMatrix(){

rotator = new Matrix();

laserRotator = new Matrix();

laserRotator1 = new Matrix();

laserRotator2 = new Matrix();

laserRotator3 = new Matrix();

klesh_rotator_1 = new Matrix();

matBullet = new Matrix();

lezoMatrix = new Matrix();

lezoMatrix1 = new Matrix();

}
public void initializePaints(){

paint = new Paint();

upgradePaint = new Paint();

pEnergy = new Paint();

pShield = new Paint();

timer = new Paint();
upgradePaint.setColor(Color.GREEN);

upgradePaint.setTextSize(40);

}
public void initializeBitmaps(){

myBitmap = BitmapFactory.decodeResource(getResources(), heroes[0]);

joy_bg = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.joystick_bg), 200, 200, false);

joy = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.joystick), 50, 50, false);

kleshnia_1 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.kleshnia), 70,105, false);

kleshnia_2 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.kleshnia2_lit), (int)(105 * clawSizeCoef), (int)(70 * clawSizeCoef), false);

apple = BitmapFactory.decodeResource(getResources(), heroes[1]);

pBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blood);

upgrade = BitmapFactory.decodeResource(getResources(), R.drawable.upgrade2);

fireButton = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.upgrade2), 100, 100, false);

fireButton1 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fire_button1), 200, 200, false);

fireButton2 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fire_button2), 100, 100, false);

bullet = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.laser_t), 20, 60, false);

laser = BitmapFactory.decodeResource(getResources(), R.drawable.laser_long);

laser1 = BitmapFactory.decodeResource(getResources(), R.drawable.laser_long_first);

laser2 = BitmapFactory.decodeResource(getResources(), R.drawable.laser_long_second);

laser3 = BitmapFactory.decodeResource(getResources(), R.drawable.laser_long_third);

blik = BitmapFactory.decodeResource(getResources(), R.drawable.blik_blik);


// laser = BitmapFactory.

// laser_l = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.laser_t), 10, 30, false);

// laser_t = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.laser), 10, 100, false);
lezo = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.lezo), 100, 46, false);
}
public void addEnemy(){

enemiesOnScreen++;

Random r = new Random();

appleX = r.nextInt(width - 100);

appleY = r.nextInt(height - 100);

enemies.add(new Enemy(appleX, appleY, appleSpeed, isAppleMovingTop, isAppleMovingRight, 0));

}
public void calculateBullet(){

if(isCombo){

isComboRunning = true;

}
if (isComboRunning){

if (comboAngleBegin == -1) {

energy = energy - 10;

comboAngleBegin = (float) Math.toRadians(cal_angle(coordinates.speedH, coordinates.speedV));

comboAngelBeginDegrees = (float)cal_angle(coordinates.speedH, coordinates.speedV);

comboXbegin = (float) (coordinates.pictureX + myBitmap.getWidth()/2 - bullet.getWidth() / 2);

comboYbegin = (float) (coordinates.pictureY + myBitmap.getHeight()/2 - bullet.getHeight() / 2);

deltaX = (float)(30 * Math.cos(comboAngleBegin));

deltaY = (float)(30 * Math.sin(comboAngleBegin));

}
matBullet.preRotate(comboAngelBeginDegrees + 90, bullet.getWidth() / 2, bullet.getHeight() / 2);

matBullet.postTranslate(comboXbegin + comboX, comboYbegin + comboY);

comboX = (float) (comboX + deltaX * bulletSpeed);

comboY = (float) (comboY + deltaY * bulletSpeed);


appXcenter = appleX + apple.getWidth() / 2;

bulletXcenter = comboXbegin + comboX + bullet.getWidth() / 2;

appYcenter = appleY + apple.getHeight() / 2;

bulletYcenter = comboYbegin + comboY + bullet.getHeight() / 2;


hitEnemy();
if ((comboXbegin + comboX)>width+100 || (comboXbegin + comboX)<-100 || (comboYbegin + comboY)>height+100 || (comboYbegin + comboY)<-100){

newBullet();

}

}

}


public boolean isSlashed(double angle){

return Math.abs(lezoAngle - angle)<(25+lezoRadius) || Math.abs((lezoAngle+180) - angle)<(25+lezoRadius);

}
public void moveEnemy(){

// Iterator eIterator = enemies.iterator();

// while (eIterator.hasNext()){

for (Enemy enemy : enemies){

// Enemy enemy = eIterator.next();

if (!enemy.isMovingRight()) {

if ((enemy.getX() + appleSpeed) < width - 45) {

enemy.setX(enemy.getX() + appleSpeed);

} else {

enemy.setMovingRight(true);

enemy.setTouches(enemy.getTouches() + 1);

if (enemy.getTouches() > 3){

shield--;

}

}



} else {

if ((enemy.getX() - appleSpeed) > 10) {

enemy.setX(enemy.getX() - appleSpeed);

} else {


enemy.setMovingRight(false);

enemy.setTouches(enemy.getTouches() + 1);

if (enemy.getTouches() > 3){

shield--;

}

}

}


if (!enemy.isMovingTop()) {

if ((enemy.getY() + appleSpeed) < height - 45) {

enemy.setY(enemy.getY() + appleSpeed);

} else {


enemy.setMovingTop(true);

enemy.setTouches(enemy.getTouches() + 1);

if (enemy.getTouches() > 3){

shield--;

}

}

} else {



if ((enemy.getY() - appleSpeed) > 10) {

enemy.setY(enemy.getY() - appleSpeed);

} else {

enemy.setMovingTop(false);

enemy.setTouches(enemy.getTouches() + 1);

if (enemy.getTouches() > 3){

shield--;

}

}



}

}

}


public void hitEnemyLaser(double angle){

//drawing blik

float x,y,A,B,C,k,b;

if (angle != 0 || angle != 180){

k = (float) Math.tan(Math.toRadians(angle - 90));

A = 0 - k;

B = 1;

b = coordinates.pictureY + myBitmap.getHeight()/2 - k * (coordinates.pictureX + myBitmap.getWidth()/2);



C = 0 - b;

if ((angle - 90)>180 && (angle - 90)<360 && ((-b)/k)0) {

cacheCanvas.drawBitmap(blik, ((-b)/k)-blik.getWidth()/2, -blik.getHeight()/2, null);

}

if ((angle - 90)>0 && (angle - 90)<180 && ((height-b)/k)0) {



cacheCanvas.drawBitmap(blik, ((height-b)/k)-blik.getWidth()/2, height-blik.getHeight()/2, null);

}

if ((angle - 90)>90 && (angle - 90)<270 && b



Поділіться з Вашими друзьями:
1   2   3   4   5   6   7   8   9   10


База даних захищена авторським правом ©divovo.in.ua 2017
звернутися до адміністрації

войти | регистрация
    Головна сторінка


загрузить материал