使用している画像
実現したいこと
レーザーとボス、自機と弾幕の当たり判定がうまくいきません
発生している問題・エラーメッセージ
動くことは動くが当たり判定が出ない
該当のソースコード
PImage pStart;
PImage pGameback;
PImage pGameover;
PImage pGameclear;
Bullet bullet;
Ship ship;
Boss boss;
GameStage gamestage;
private ArrayList<Bullet> danmaku = new ArrayList<Bullet>();
enum GameScene {
START, PLAY, CLEAR, OVER;
}
private GameScene scene = GameScene.START;
void setup() {
size(320, 480);
frameRate(30);
noCursor(); // clear mouse cursor
pStart = loadImage("start.png");
pGameback = loadImage("back.png");
pGameover = loadImage("gameover.png");
pGameclear = loadImage("gameclear.png");
bullet = new Bullet(width/2, height/2, 20, 0, 0 );//クラスからインスタンスを生成
ship = new Ship(mouseX,mouseY);
gamestage = new GameStage() ;
boss = new Boss(160, 150);
}
//GameStage class
class GameStage {
void update() { boss.doShinking(); //画面切り替え if (ship.hp == 0) { scene = GameScene.OVER; } if (boss.hp == 0) { scene = GameScene.CLEAR; } }
}
// ship(自機) class
class Ship {
private int sx, sy;
private int hp = 1;
boolean is_Alive() {
//return is_alive;
return hp != 0;
}
Ship(int x, int y) {
sx = x;
sy = y;
}
void update(int x, int y) {
sx = x;
sy = y;
stroke(255, 255, 255);
noFill();
} //hit check void damage() { if (abs(ship.sx -boss.tx) < (boss.bw/2)) { boss.hp--; } }
}
// Boss class
class Boss {
private int tx, ty;
private int dx, dy;
private int bw = 20;
private int bh = 10;
private long routine = 0;
private int hp = 10;
boolean isAlive() {
//return is_alive;
return hp != 0;
}
Boss(int x, int y) { tx = x; ty = y; dx = 10; dy = -8; } void move() { tx += dx; ty += dy; if (tx < 0 || tx > width) { dx*= -1; } if (ty < 0 || ty > height/3) { dy*= -1; } stroke(255, 204, 0); } void doShinking() { routine++; if (routine % 10 == 0) { move(); } if (routine % 30 == 0) { danmaku.addAll(this.shot()); println("#shot!!" +routine + ":" + tx + "," + ty); } } //hit check void damage() { if (dist(tx, ty, ship.sx, ship.sy) < bullet.tr/2) { ship.hp--; } } ArrayList<Bullet> shot() { ArrayList<Bullet> danmaku = new ArrayList(); for (int i = 0; i < 360; i+= 10) { //インスタンス(弾幕)の作成 double rad = radians(i); danmaku.add(new Bullet(this.tx, this.ty, 10, Math.cos(rad), Math.sin(rad))); } return danmaku;
}
// Bullet(弾幕) class
class Bullet {
private double tx, ty;
private final double tr;
private double dx, dy;
private boolean is_alive = true;
Bullet(double x, double y, double r, double temp_dx, double temp_dy ) {
tx = x;
ty = y;
tr = r;
dx = temp_dx;
dy = temp_dy;
}
boolean isAlive() {
return is_alive;
}
void update() {
tx += dx;
ty += dy;
//area check
if (Math.min(tx, ty) < 0) {
is_alive = false;
//println("#" + tx + "," + ty);
return ;
}
if (tx > width || ty > height) {
is_alive = false;
return ;
}
stroke(255, 0, 0); ellipse((float)tx, (float)ty, (float)tr, (float)tr); }
}
void draw() {
rect(boss.tx - boss.bw/2, boss.ty + boss.bh/2, boss.bw, boss.bh);
fill(255,0,0);
text("boss.hp",100,100);
fill(255,255,255);
background(0); // clear triangle(mouseX, mouseY -7, mouseX - 10, mouseY + 7, mouseX + 10, mouseY + 7); boss.damage(); ship.damage(); //レーザーの描写 if (mousePressed) { line(mouseX, mouseY - 7, mouseX, 0); } for (int i = danmaku.size() -1; i >= 0; i--) { Bullet b = (Bullet)danmaku.get(i); if (!b.isAlive()) { danmaku.remove(i); continue; } b.update(); } if (scene == GameScene.START) { image(pStart, 0, 0); } if (mousePressed) { if (mouseButton == LEFT) { scene = GameScene.PLAY; } } if (scene == GameScene.PLAY) { gamestage.update(); //image(pGameback,0,0); } if (scene == GameScene.OVER) { image(pGameover, 0, 0); } if (scene == GameScene.CLEAR) { image(pGameclear, 0, 0); }
}
回答1件
あなたの回答
tips
プレビュー