public void run() { //Этот метод выполняется в побочном потоке
do {
draw.postInvalidate();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
} while (MyDraw.Gameisrunning);
}
}
public class MyDraw extends View {
static public int PlayerX;
static int PlayerY;
int PlayerR;
float EndLx =0,EndLy=0; // В эти значения кидаем нажатие пользователя
static float Vy = 0, Vx = 0; // Эти значения плюсуются каждые 50 МС, в их определение собственно и есть проблема
protected boolean fl = true;
public static boolean Gameisrunning = true;
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int OnePercentHeight = canvas.getHeight() / 100; // create percents value
int OnePercentWidth = canvas.getWidth() / 100;
canvas.drawColor(getResources().getColor(R.color.BackGroundColor)); // drawing backgroung
Paint PaintPlayer = new Paint(); //Settings for ball of a player
Paint PaintMainFrame = new Paint(); //settings for form rectangle
Paint PaintWallGreen = new Paint(); //Settings for green walls
PaintMainFrame.setColor(Color.GREEN);
PaintMainFrame.setStyle(Paint.Style.STROKE);
PaintMainFrame.setStrokeWidth(OnePercentWidth);
PaintWallGreen.setColor(Color.GREEN);
PaintWallGreen.setStyle(Paint.Style.FILL);
PaintPlayer.setColor(Color.WHITE);
PaintPlayer.setStyle(Paint.Style.FILL);
frame MainFrame = new frame(0 + OnePercentWidth / 2, 0 + OnePercentWidth / 2, canvas.getWidth() - OnePercentWidth / 2, getHeight() - OnePercentWidth / 2, PaintMainFrame);
Wall RightWall = new Wall(OnePercentWidth * 40, canvas.getHeight() - OnePercentHeight, OnePercentWidth * 43, OnePercentHeight * 60, PaintWallGreen);
if (fl) {
PlayerX = OnePercentWidth * 10;
PlayerY = canvas.getHeight() - (OnePercentHeight * 8 + OnePercentWidth * 2);
PlayerR = OnePercentWidth * 4;
canvas.drawRect(RightWall.getX1(), RightWall.getY1(), RightWall.getX2(), RightWall.getY2(), RightWall.getPaint()); // drawing wall
canvas.drawRect(MainFrame.getX1(), MainFrame.getY1(), MainFrame.getX2(), MainFrame.getY2(), MainFrame.getPaint()); // drawing form
canvas.drawCircle(PlayerX, PlayerY, PlayerR, PaintPlayer); // drawing player
Paint PaintLine = new Paint();
PaintLine.setColor(Color.RED);
PaintLine.setStrokeWidth(5);
canvas.drawLine(PlayerX,PlayerY,EndLx,EndLy,PaintLine); //Пользователь нажатием выбирает координаты, ему помогает линия
}
else {
update();
Player player = new Player(PlayerX, PlayerY, PlayerR, PaintPlayer);
canvas.drawCircle(PlayerX, PlayerY, PlayerR, PaintPlayer); // drawing player
if (((player.getY1() <= MainFrame.getY1() + OnePercentWidth * 4) || (player.getY1() >= MainFrame.getY2() - OnePercentWidth * 4) || (player.getY1() + 50 >= RightWall.getY2()) && (player.getY1() + 60 <= RightWall.getY2()) && (player.getX1() - 50 < RightWall.getX2()))) {
ChangeY();
}
if ((player.getX1() <= MainFrame.getX1() + OnePercentWidth * 4) || (player.getX1() >= MainFrame.getX2() - OnePercentWidth * 4) || ((player.getX1() - PlayerR < RightWall.getX2()) && (player.getX1() + PlayerR > RightWall.getX1()) && (player.getY1() + PlayerR > RightWall.getY2()))) {
ChangeX();
}
canvas.drawRect(RightWall.getX1(), RightWall.getY1(), RightWall.getX2(), RightWall.getY2(), RightWall.getPaint()); // drawing wall
canvas.drawRect(MainFrame.getX1(), MainFrame.getY1(), MainFrame.getX2(), MainFrame.getY2(), MainFrame.getPaint()); // drawing form
canvas.drawCircle(PlayerX, PlayerY, PlayerR, PaintPlayer);