我正在为我的编程课写一个游戏。这是一个飞机射击游戏,有其他飞机作为“敌人”进来。射击的飞机发射小椭圆。我不知道如何将被击中的敌人从游戏中移除。我知道如何移除“敌人”,但不知道当他们碰撞时如何做到这一点。这是我目前的代码。
ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
ArrayList enemies;
PVector player, playerSpeed;
float maxSpeed = 3;
PImage jet;
PImage enemy;
PImage laser;
void setup() {
size(600, 600);
player = new PVector(300, 550);
playerSpeed = new PVector();
noCursor();
noStroke();
smooth();
String jeturl = "http://s1.postimg.org/dhe38w1rv/fighter_jet_md_20.png";
String enemyurl = "http://s29.postimg.org/cdaj0d7z7/fighter_jet_md_20.png";
String laserurl = "http://s13.postimg.org/fq00vsl37/red_Laser_Ray.png";
// Load image from a web server
jet = loadImage(jeturl, "png");
enemy = loadImage(enemyurl, "png");
laser = loadImage(laserurl, "png");
enemies = new ArrayList();
}
void draw() {
background(255);
player.add(playerSpeed);
//fill(255, 0, 0);
image(jet, player.x, player.y);
PVector mouse = new PVector(mouseX, mouseY);
fill(10);
ellipse(mouse.x, mouse.y, 5, 5);
if (frameCount%7==0 && mousePressed) {
PVector dir = PVector.sub(mouse, player);
dir.normalize();
dir.mult(maxSpeed*3);
Bullet b = new Bullet(player, dir);
bullets.add(b);
}
for(int i = enemies.size()-1; i>=0; i--) {
Enemy b = (Enemy) enemies.get(i);
b.move();
b.draw();
}
if (frameCount%50==0) {
enemies.add(new Enemy());
}
for (Bullet b : bullets) {
b.update();
b.display();
}
}
class Bullet extends PVector {
PVector vel;
Bullet(PVector loc, PVector vel) {
super(loc.x, loc.y);
this.vel = vel.get();
}
void update() {
add(vel);
}
void display() {
fill(0, 0, 255);
ellipse(x, y, 3, 3);
}
}
class Enemy {
float x, y;
Enemy() {
x = random(20, 580);
y = random(-20, -580);
}
void move() {
y = y + random(1,3);
}
void draw() {
image(enemy, x, y);
}
}
void keyPressed() {
if (keyCode == LEFT) {
playerSpeed.x = -maxSpeed;
}
if (keyCode == RIGHT) {
playerSpeed.x = maxSpeed;
}
}
void keyReleased() {
if (keyCode == LEFT || keyCode == RIGHT) {
playerSpeed.x = 0;
}
}
你知道子弹和敌人的坐标,因此你可以检查碰撞。有一个内置的dist()函数可以计算两点之间的欧几里得距离。
虽然不是非常有效,也不准确,但你可以这样做:
ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
ArrayList enemies;
PVector player, playerSpeed;
float maxSpeed = 3;
PImage jet;
PImage enemy;
PImage laser;
int lives = 3;
int score = 0;
void setup() {
size(600, 600);
player = new PVector(300, 550);
playerSpeed = new PVector();
noCursor();
noStroke();
smooth();
String jeturl = "http://s1.postimg.org/dhe38w1rv/fighter_jet_md_20.png";
String enemyurl = "http://s29.postimg.org/cdaj0d7z7/fighter_jet_md_20.png";
String laserurl = "http://s13.postimg.org/fq00vsl37/red_Laser_Ray.png";
// Load image from a web server
jet = loadImage(jeturl, "png");
enemy = loadImage(enemyurl, "png");
laser = loadImage(laserurl, "png");
enemies = new ArrayList();
}
void draw() {
background(255);
player.add(playerSpeed);
//fill(255, 0, 0);
image(jet, player.x, player.y);
PVector mouse = new PVector(mouseX, mouseY);
fill(10);
ellipse(mouse.x, mouse.y, 5, 5);
if (frameCount%7==0 && mousePressed) {
PVector dir = PVector.sub(mouse, player);
dir.normalize();
dir.mult(maxSpeed*3);
Bullet b = new Bullet(player, dir);
bullets.add(b);
}
for (int i = enemies.size ()-1; i>=0; i--) {
Enemy b = (Enemy) enemies.get(i);
b.move();
b.draw();
//check enemy to bullet collisions
//for each bullet
for(Bullet blt : bullets) {
//if the distance between the bullet and enemy is less than the enemy's width
if(dist(blt.x,blt.y,b.x,b.y) < enemy.width){
//remove the enemy
enemies.remove(b);
//optional, add score
score += 10;
}
}
//check enemy to player collision
if(dist(player.x,player.y,b.x,b.y) < jet.width){
enemies.remove(b);//remove current enely
//optional, update lives, reset score/game if needed
lives--;
println("player hit: " + lives + " lives left");
if(lives < 1){
println("Game Over!\nscore:" + score);
lives = 3;
score = 0;
}
}
}
if (frameCount%50==0) {
enemies.add(new Enemy());
}
for (Bullet b : bullets) {
b.update();
b.display();
}
}
class Bullet extends PVector {
PVector vel;
Bullet(PVector loc, PVector vel) {
super(loc.x, loc.y);
this.vel = vel.get();
}
void update() {
add(vel);
}
void display() {
fill(0, 0, 255);
ellipse(x, y, 3, 3);
}
}
class Enemy {
float x,y;
Enemy() {
x = random(20, 580);
y = random(-20, -580);
}
void move() {
y = y + random(1, 3);
}
void draw() {
image(enemy, x, y);
}
}
void keyPressed() {
if (keyCode == LEFT) {
playerSpeed.x = -maxSpeed;
}
if (keyCode == RIGHT) {
playerSpeed.x = maxSpeed;
}
}
void keyReleased() {
if (keyCode == LEFT || keyCode == RIGHT) {
playerSpeed.x = 0;
}
}
上面的代码不准确的原因是因为它是根据距离检查碰撞。想象一个被检查对象周围的圆。如果你的对象是圆,这将是完美的,但是你大部分是矩形形状(PImages)。
上面的代码很慢的原因与dist()函数的工作原理有关。想象一下屏幕上两个对象之间的一条线。从一个对象到另一个对象绘制两条直线,仅限于水平和垂直,这将为您提供直角三角形的两侧,其中连接对象的原始线是次方。要找到距离,您需要求解次方,即两边相加的平方根。您不必太担心这个问题,因为dist()为您做了这个数学运算,但请记住它使用的是平方根,这在CPU上可能会很昂贵。解决这个问题的一种方法是使用平方距离(并对照半径平方进行检查),但这不会太准确。
另一种选择是进行简单的矩形边界检查。你知道每个盒子(PImage)在屏幕上的位置以及每个子弹的绘制位置,因此你可以判断每个子弹是否在敌人内部。同样,你可以检查两个矩形是否相交(玩家PImage和敌人PImage更新了坐标)。
这是矩形交叉点的一个最小示例,利用了java. awt.Rectgle类(它已经提供了交叉点检查):
import java.awt.Rectangle;
//based on jet image from previous code
int jetWidth = 20;
int jetHeight = 28;
Rectangle playerBox,enemyBox;
void setup(){
size(600, 600);
noFill();strokeWeight(3);
//bounding boxes
playerBox = new Rectangle(width / 2, height-jetHeight*2,jetWidth,jetHeight);
enemyBox = new Rectangle((int)random(width),-jetHeight,jetWidth,jetHeight);
}
void draw(){
//update enemy
enemyBox.y += 3;
//reset enemy position if out of screen
if(enemyBox.y > height) {
enemyBox.y = -jetHeight;
}
playerBox.x = (int)(mouseX - jetWidth * .5);
//check collision
if(playerBox.intersects(enemyBox)){
strokeWeight(10);
}else{
strokeWeight(3);
}
background(255);
stroke(192,0,0);
rect(enemyBox.x,enemyBox.y,enemyBox.width,enemyBox.height);
stroke(0,192,0);
rect(playerBox.x,playerBox.y,playerBox.width,playerBox.height);
}
如果您想要像素精度,您可以利用加载图像的alpha通道,并且:
作为接下来的步骤,我建议基于边界框实现冲突检测。稍后,您可能需要查看基本状态机来跟踪游戏的状态(例如介绍屏幕、level 1、evel2、…、屏幕上的游戏、高分屏幕等)
玩得开心!:)
你需要给你的敌人类添加一个侦听器。它应该是子弹列表上的无限循环。当子弹坐标等于敌人坐标时,它应该有执行on_delete回调的逻辑。我不是一个Java的开发者,但我会给你写一些伪代码,应该给你指明正确的方向。需要一个小的改变敌人类需要用敌人数组上元素的索引初始化,这样它就可以用回调正确地销毁。
Class Enemy (int idx){
void on_delete(){
enemies = ArrayUtils.removeElement(idx)};
void listen(){
cycle{
for(i=0; bullets.count()-1;i++){
bullet_x = bullets(i).getX
bullet_y = bullets(i).getY
if (bullet_x==x) && (bullet_y==y)
on_delete();
}
}
}