Guys i need some help with player movement and death counter, here’s my code
Main code:
Player Player;
enemy enemy;
int score;
int deaths;
void setup() {
size ( 640, 480);
noStroke();
enemy = new enemy();
Player = new Player();
}
void draw() {
background(126);
enemy.display();
Player.display();
Player.keyPressed();
fill (0);
text ("Score: " + score, 10, 460);
text ("Deaths: " + deaths, 570, 460);
if (Player.checkHit()){
Player.die();
}
}
Classes code:
class enemy {
color c;
float xposr;
float yposr;
float xspeed;
enemy(){
c = color (0);
xposr = width/2;
yposr = height/2;
xspeed = 0;
}
void display(){
rectMode(CENTER);
fill(c);
rect(xposr, yposr, 33, 33);
}
}
class Player {
color c;
float xposb;
float yposb;
float xspeed;
Player(){
c = color (255);
xposb = 33/2;
yposb = height/2;
xspeed = 0;
}
void display(){
ellipseMode(CENTER);
fill(c);
ellipse(xposb, yposb, 33, 33);
}
boolean checkHit() {
return (xposb >= enemy.xposr-33 &&
yposb >= enemy.yposr-33 &&
xposb <= enemy.xposr+33 &&
yposb <= enemy.yposr+33);
}
void keyPressed()
{
if (key == CODED)
{
if (keyCode == LEFT)
{
xposb= xposb -2;
yposb= yposb +0;
}
if (keyCode == RIGHT)
{
xposb=xposb+2;
yposb=yposb+0;
}
if (keyCode == UP)
{
yposb=yposb-2;
xposb=xposb+0;
}
if (keyCode == DOWN)
{
yposb=yposb+2;
xposb=xposb+0;
}
}
}
void die(){
deaths++;
score = 0;
}
}
So, i was able to change the player movement to arrow keys instead of mousey and mousex values, but how do i make the ball stop everytime i release the keys or hit the scene limits (640x480).
And also, i tried with the
boolean checkHit() {
return (xposb >= enemy.xposr-33 &&
yposb >= enemy.yposr-33 &&
xposb <= enemy.xposr+33 &&
yposb <= enemy.yposr+33);
}
if (Player.checkHit()){
Player.die();
}
to make it count only 1 death instead of 17 everytime i passed through the enemy, but it did not worked… any thoughts?