We are about to switch to a new forum software. Until then we have removed the registration on this forum.
The piece of code I am trying to get working is commented out in draw().
import processing.serial.*;
import cc.arduino.*;
import static javax.swing.JOptionPane.*;
import javax.swing.JPasswordField;
final StringDict accounts = new StringDict(
new String[] {
"john", "tony", "amy"
}
, new String[] {
"ilikedogs", "husky", "pepsi"
}
);
final JPasswordField pwd = new JPasswordField();
{
println(accounts);
}
PImage startGameBackground;
PImage kitchen;
PImage dog;
float dogx=800, dogy=600;
HealthBar hb0 = new HealthBar();
boolean isWashed=false;
PImage achievment2;
int stage =1;
Serial port;
Fountain f = new Fountain();
//Ball b = new Ball(x,y,d,xs,ys);
ArrayList<Ball>ballList;
void setup() {
size(1800, 900);
String comPort = "COM3";
stage =1;
port = new Serial(this, comPort, 9600);
f.fountainSetup();
startGameBackground = loadImage("startGameBackground.png");
kitchen = loadImage("kitchen.png");
dog = loadImage("washDog.png");
startGameBackground.resize(1800, 900);
kitchen.resize(1800, 900);
achievment2 = loadImage("achievment2.png");
}
void draw() {
if (stage ==1) {
background(startGameBackground);
if (frameCount == 10) surface.setVisible(true);
String user = askUser();
if (user == null) confirmQuit();
else if (!"".equals(user)) askPass(user);
} else if (stage ==2) {
background(kitchen);
image(dog, dogx, dogy);
hb0.draw();
isWashed =true;
f.fountainDraw();
//if (hb0.value <100) {
// //delay(1000);
// hb0.value += .5;
// }
// else if(hb0.value == hb0.max){
// println("Here is the achievment");
// image(achievment2, 500,100);
}
}
}
String askUser() {
String id = showInputDialog("Please enter user:");
if (id == null)
showMessageDialog(null, "You've canceled login operation!"
, "Alert", ERROR_MESSAGE);
else if ("".equals(id))
showMessageDialog(null, "Empty user input!"
, "Alert", ERROR_MESSAGE);
else if (!accounts.hasKey(id = id.toLowerCase()))
showMessageDialog(null, "Unknown \"" + id + "\" user!" + (id = "")
, "Alert", ERROR_MESSAGE);
return id;
}
boolean askPass(String id) {
boolean isLogged = false;
pwd.setText("");
int action = showConfirmDialog(null, pwd
, "Now enter password:", OK_CANCEL_OPTION);
if (action != OK_OPTION) {
showMessageDialog(null, "Password input canceled!"
, "Alert", ERROR_MESSAGE);
return false;
}
String phrase = pwd.getText();
//String phrase = new String(pwd.getPassword());
if ("".equals(phrase))
showMessageDialog(null, "Empty password input!"
, "Alert", ERROR_MESSAGE);
else if (accounts.get(id).equals(phrase)) {
showMessageDialog(null, "Welcome \"" + id + "\"!\nYou're logged in!"
, "Info", INFORMATION_MESSAGE);
isLogged = true;
stage=2;
} else
showMessageDialog(null, "Password \"" + phrase + "\" mismatch!"
, "Alert", ERROR_MESSAGE);
return isLogged;
}
void confirmQuit() {
if (showConfirmDialog(null, "Wanna quit then?", "Exit"
, OK_CANCEL_OPTION) == OK_OPTION) exit();
}
public class Ball {
float x, y, xSpeed, ySpeed, diameter;
int dropColour;
boolean isAlive;
int stepsAlive;
HealthBar hb0 = new HealthBar();
public Ball(float x, float y, float d, float xs, float ys) {
this.x=x;
this.y =y;
this.diameter =d;
xSpeed =xs;
ySpeed =ys;
isAlive = true;
int stepsAlive =0 ;
}
public void move() {
stepsAlive++;
x = x + xSpeed;
y = y + ySpeed;
if (x >=800) {
xSpeed = -xSpeed;
}
if (x <=950) {
xSpeed = -xSpeed;
}
ySpeed = ySpeed +0.1F;
if (y > 800) {
ySpeed = -ySpeed;
}
}
public void isDead() {
if (stepsAlive >= 400) {
isAlive = false;
}
if (dist(x, y, dogx, dogy) <100 ) {
isAlive = false;
}
if (y > 800) {
isAlive =false;
}
}
}
import ddf.minim.*;
public class Fountain {
ArrayList<Ball> ballList;
AudioPlayer player;
Minim minim;
HealthBar hb0 = new HealthBar();
void fountainSetup() {
//size(600,600);
ballList = new ArrayList<Ball>();
//minim = new Minim(this);
}
public void fountainDraw() {
//background(255,255,255);
// if (mousePressed){
if (mousePressed ==true) {
float xSpeed = (float)(-3 + Math.random()*6);
Ball b = new Ball(880, 400, 20, xSpeed, -2);
b.dropColour = color(204, 255, 255);
ballList.add(b);
}
for (int i =0; i <ballList.size(); i++) {
Ball b2 = ballList.get(i);
fill(b2.dropColour);
ellipse(b2.x, b2.y, b2.diameter, b2.diameter);
b2.move();
//println("working");
b2.isDead();
if (b2.isAlive == false)
ballList.remove(b2);
println(dogy + ""+ "This thing is broke");
}
}
}
class HealthBar {
float value, max, x, y, w, h;
color backing, bar;
HealthBar(){
value = 0;
max = 100;
x = 800;
y = 60;
w = 200;
h = 30;
backing = color(255,0,0);
bar = color(0,255,0);
}
void draw(){
fill(backing);
stroke(0);
rect(x,y,w,h);
fill(bar);
rect(x,y,map(value,0,max,0,w),h);
}
}
Answers
hb0.value == hb0.max
Don't use==
nor!=
in order to compare floating point values! [-XIn this case, use
>=
operator:hb0.value >= hb0.max
Ok thanks I changed that...Would u know how I could change my code so when a ball hits my image the health bar goes up?
This is circle vs rectangle collision detection. I dunno that formula.
If precision isn't that important, you can consider your rectangle an ellipse or vice-versa. :D
Yeah I do have a collision detection done with the ellipse but I cannot seem to figure out how to draw the increase of the health in the draw() function...I basically want the health to increase on screen when the function isDead() is activated.