Проблема в том, что мой кактус за кадром. Я уже пытаюсь поместить спрайты немного выше нижней части экрана, как вы можете видеть фотографию, но как сделать так, чтобы кактус находился на том же уровне
Я пытался изменить screenX при инициализации Кактус внутри l oop, а также изменяющая переменную y в классе sprite, но, похоже, ничего не работает.
как динозавр?
Класс GameActivity
package android.ab.t_rex;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceView;
import java.lang.reflect.Array;
import java.util.Random;
public class GameView extends SurfaceView implements Runnable {
public Thread thread;
public boolean isPlaying, gameOver = false;
private Background background1, background2;
private Dino dino;
private Cactus[] cacts;
public static float screenRatioX, screenRatioY;
private Paint paint;
private int screenX, screenY;
private Random random;
private GameActivity activity;
public GameView(Context context, int screenX, int screenY) {
super(context);
this.screenX = screenX;
this.screenY = screenY;
screenRatioX = 1920f / screenX;
screenRatioY = 1080f / screenY;
random = new Random();
background1 = new Background(screenX, screenY, getResources());
background2 = new Background(screenX, screenY, getResources());
background2.x = screenX;
dino = new Dino(screenY, getResources());
cacts = new Cactus[4];
// for loop to run two times, inside bird class object and each bird is added to the array
for(int i=0;i<4;i++){
Cactus cactus = new Cactus(screenX, getResources());
cacts[i] = cactus;
}
paint = new Paint();
}
@Override
public void run() {
while (isPlaying) {
update();
draw();
sleep();
}
}
private void update() {
background1.x -= 10 * screenRatioX;
background2.x -= 10 * screenRatioX;
if(background1.x + background1.background.getWidth() < 0) {
background1.x = screenX;
}
if(background2.x + background2.background.getWidth() < 0) {
background2.x = screenX;
}
if(dino.isGoingUp)
dino.y -= 30 * screenRatioY;
else
dino.y += 30 * screenRatioY;
if(dino.y < 0)
dino.y = 0;
if(dino.y >= (screenY / 2) + (128 * (int) screenRatioX))
dino.y = (screenY / 2) + (128 * (int) screenRatioX);
for(Cactus cactus : cacts){
cactus.x -= cactus.speed;
if(cactus.x + cactus.width < 0 ){
int leap = (int) (30 * screenRatioX);
cactus.speed = random.nextInt(leap);
if(cactus.speed < (int) (10 * screenRatioX)){
cactus.speed = (int) (10 * screenRatioX);
}
cactus.x = screenX;
}
if(Rect.intersects(dino.getCollision(), cactus.getCollision())){
gameOver = true;
}
}
}
private void draw() {
if (getHolder().getSurface().isValid()) {
// returns current canvas that is being displayed on the screen
Canvas canvas = getHolder().lockCanvas();
canvas.drawBitmap(background1.background, background1.x, background1.y, paint);
canvas.drawBitmap(background2.background, background2.x, background2.y, paint);
// for the dino, position of the dino
canvas.drawBitmap(dino.getDino(), dino.x, dino.y, paint);
for (Cactus cactus: cacts) {
canvas.drawBitmap(cactus.cactus1, cactus.x, cactus.y, paint);
}
if (gameOver) {
isPlaying = false;
canvas.drawBitmap(dino.getDead(), dino.x, dino.y, paint);
getHolder().unlockCanvasAndPost(canvas);
return;
}
getHolder().unlockCanvasAndPost(canvas);
}
}
private void sleep() {
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void resume () {
isPlaying = true;
thread = new Thread(this);
thread.start();
}
public void pause () {
try {
isPlaying = false;
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (event.getX() < screenX / 2) {
dino.isGoingUp = true;
}
break;
case MotionEvent.ACTION_UP:
dino.isGoingUp = false;
}
return true;
}
}
Это мой класс спрайтов
package android.ab.t_rex;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import static android.ab.t_rex.GameView.screenRatioX;
import static android.ab.t_rex.GameView.screenRatioY;
public class Cactus {
public int speed = 20;
int x, y, width, height;
Bitmap cactus1;
Cactus(int screenY, Resources res) {
cactus1 = BitmapFactory.decodeResource(res, R.drawable.cactus1);
width = cactus1.getWidth();
height = cactus1.getHeight();
width = (int) (width * screenRatioX);
height = (int) (height * screenRatioY);
y = (screenY / 2) + (10 * (int) screenRatioX);
x = 64 * (int) screenRatioX;
cactus1 = Bitmap.createScaledBitmap(cactus1, width, height, false);
}
Rect getCollision(){
return new Rect(x, y, x + width, y + height);
}
}