We are about to switch to a new forum software. Until then we have removed the registration on this forum.
``I'm having trouble detecting the bullet collide with the enemy ship in my shooter game. Here's my code so far. Any ideas on how I can make it work?
PImage background;
ship player= new ship();
int score;
PImage enemypic;
boolean enemyAlive = true;
boolean instructions = true;
boolean level1=true;
boolean gameOver=false;
enemy enemyArr [] = new enemy [11];
color rectColor = color (200);
ArrayList <Bullet> bullets;
void setup() {
size(1000, 750);
bullets = new ArrayList();
background=loadImage("shooterbackground.png");
ship=loadImage("ship.png");
enemypic = loadImage("enemy.png");
frameRate(13);
smooth();
noStroke();
float enemyY = 0;
for (int i = 0; i < 11; i++) {
enemyArr [i] = new enemy(900, 5 + enemyY);
enemyY +=65;
}
}
void draw() {
println(frameRate);
image (background, 0, 0, 1000, 750);
moveAll();
displayAll();
if (instructions) {
fill(rectColor);
//rect(0, 0, 1000, 750);
fill(0);
text("afdsf", 0, 0);
}
if (instructions) {
background(200, 200, 200);
fill(225);
rect(300, 275, 400, 200);
fill(0);
textSize(65);
text("Start", 420, 390);
}
if (enemyAlive && !instructions && !gameOver) {
player.display();
for (int i = 0; i < 11; i++) {
enemyArr[i].update();
enemyArr[i].display();
}
}
if (playerX > 244) {//player move limits
playerX = 244;
}
if (playerX < 0) {
playerX = 0;
}
if (playerY < -68) {//wrap screen
playerY = 740;
}
if (playerY > 740) {
playerY = -68;
}
}
void keyPressed() {
if (key == 'a') {
playerX -= 8;
}
if (key == 'd') {
playerX+= 8;
}
if (key == 'w') {
playerY -= 8;
}
if (key == 's') {
playerY += 8;
}
}
void mousePressed() {
if (mouseX > 300 && mouseX < 700 && mouseY > 275 && mouseY < 475 && instructions) {
instructions = false;
}
Bullet temp = new Bullet(playerX+65, playerY+34);
bullets.add(temp);
}
public class enemy {
float health, speed;
float yPos, xPos;
enemy(float XPOS, float YPOS) {
health=5;
speed=10;
xPos=XPOS;
yPos=YPOS;
}
public void display() {
image(enemypic, xPos, yPos, 75, 75);
}
public void update() {
if (enemyAlive) {
xPos -=7;
}
if (xPos < -65) {
xPos=925;
}
}
}
class Bullet{//bullet class
float x=playerX +20;
float y=playerY;
float speed;
Bullet(float tx, float ty){
x = tx;
y = ty;
}
void display(){
fill(255, 0,0);
rect(x,y, 10, 5);
}
void move(){
x += 25;
}
void moveAll(){
for (Bullet temp : bullets){
temp.move();
}
}
void displayAll(){
for (Bullet temp : bullets){
temp.display();
}
}
PImage ship;
int playerX = 100;
int playerY = 300;
public class ship {
public void display() {
image(ship, playerX, playerY, 75, 75);
}
}
Answers
It looks like you are interested in collision detection --
Either point-rect, rect-rect, or circle-rect collision, depending on how you want to model what counts as "colliding" with your bullet. In general, rect-rect (a hit-box on the bullet) is just fine, and much more performant than circle-rect. If you don't mind almost-overlapping bullets, then point-rect is even easier -- the center of the bullet must touch the ship's hit box in order for it to count as a "hit".
Shameless self-promotion: http://happycoding.io/tutorials/processing/collision-detection
Really nice use of codepens in that tutorial series, Kevin.
Thanks Jeremy!