Hi All,
I am playing around with building an incredibly simple two-person competitive game. Here’s the concept:
One player uses the mouse, EI the ‘spaceship’ that shoots a ‘light beam’ when the mouse is clicked. The other player is an ‘alien’ which toggles back and forth using the Left and Right arrow keys. The point is to hit the alien three times before the timer is up, a classic “beat the clock” game.
How would I go about the interaction triggering the 'life" to decrement when the light beam and the alien intersect? The light beam is a simple rect() drawn in Processing, and the alien is a jpg file.
Can anyone help me troubleshoot coding this intersection function using a boolean statement? Again, my goal is to code beating the clock, if life > 0 before the timer reaches 0 the alien wins and the game ends, if life = 0 before the timer reaches 0, the spaceship wins and the game ends.
Thanks for your feedback!
// images
PImage planet; //https://forums.frontier.co.uk/threads/frontier-is-really-capable-to-truly-change-the-face-of-the-planets-in-2018.384087/page-6
PImage alien;
PImage spaceship;
//Global Variable Objects
int x = 10; //alien x
int beamWidth = 15;
int beamHeight = 250;
//timer variables
int begin;
int duration = 60;
int time = 60;
//life variables
int life = 3;
//true false statement ** NEED HELP SETTING UP CORRECT BOOLEAN PARAMETERS
boolean touching = false;
void setup() {
size (600, 400);
planet = loadImage("planet.jpg");
alien = loadImage("alien.jpg");
spaceship = loadImage("spaceship.JPG");
}
void draw() {
background(0);
imageMode(CENTER);
image(planet, 90,0, 1200, 800);
image(alien, x, height/1.3, 50, 80);
image(spaceship, mouseX, 50, 70, 40);
//timer counts down 60 seconds
if (time > 0){
time = duration - (millis() - begin)/1000;
text(time, 550, height - 25);
fill(255);
textSize(30);
}
// life decrements if beam and alien intersect
if (life > 0) {
text(life, 50, height - 25);
fill(255);
textSize(30);
}
// if (touching) { **** NEED TO CREATE CORRECT BOOLEAN FOR INTERACTION
// life = life - 1;
// }
}
//Keyboard interaction - move alien left and right with arrow keys
void keyPressed() {
if (key == CODED) {
if (keyCode == RIGHT) {
x = x + 20;
} else if (keyCode == LEFT) {
x = x - 20;
}
// Check horizontal edges - alien runs off right side of window and shows up on left
if (x > width || x < 0) {
x = 0;
}
}
}
//Mouse interaction - light beam displays when mouse is pressed
void mousePressed () {
if (mousePressed) {
//draw beam rectangle
rect(mouseX, 60, beamWidth, beamHeight);
}
}