Hi guys, currently working on a uni project where we have to incorporate a piece of hardware into our code. I have tried to create a code based on the flappy bird game and found someone who had created a ‘flappy code’ game, i have changed it so my slider controls the Cave Raider (what my game is called) but i am having trouble with the collision of my raider and my pillars, i was hoping someone might be able to help, and could potentially change the way i have used the collision in my code because i currently don’t understand it. Any help would be much appreciated!
Harry - code below
import cc.arduino.*;
import com.tinkerkit.*;
pillar[] p = new pillar[3];
boolean end=false;
boolean intro=true;
int score=0;
Arduino arduino;
//declare the potentiometer
TKPotentiometer pot;
float minVal = 1;
float maxVal = 700;
float val;
PImage raider;
PImage background;
raider r;
String gameScene;
void setup() {
size(500, 700);
arduino = new Arduino(this, Arduino.list()[1], 57600);
//for every tinkerkit component we have to pass
//the arduino and the port
pot = new TKPotentiometer(arduino, TK.I0);
noStroke();
raider = loadImage("caveraiderimg.png");
background = loadImage("background.png");
for (int i = 0; i<3; i++) {
p[i]=new pillar(i);
r =new raider();
gameScene = "Intro";
}
}
void draw() {
background(0);
if(gameScene =="Intro"){
introScene();
}else if(gameScene=="Play"){
playScene();
}else if(gameScene=="Lose"){
loseScene();
}
if(gameScene=="Play"){
fill(0);
stroke(255);
textSize(32);
rect(20,20,100,50);
fill(255);
text(score,30,58);
}
//get the potentiometer values
float val = pot.read();
//calibration
if (val < minVal) minVal = val;
if (val > maxVal) maxVal = val;
//draw the raider
imageMode(CENTER);
image(raider, 100, val, 50, 50);
r.displayRaider();
}
// a void which will enable the slider value
void getSliderValue(){
val = map(val, minVal, maxVal, 600, height);
r.setRaiderx(val);
}
//creating classes - separate to the rest of the code so that they can reference them
class raider {
float Raiderx;
float xPos,yPos,ySpeed;
raider(){
xPos = 250;
yPos = 400;
}
void displayRaider(){
}
void setRaiderx(float val){
Raiderx = val;
}
void move(){
yPos+=ySpeed;
for(int i = 0;i<3;i++){
p[i].xPos-=3;
}
}
void checkCollisions(){
if(yPos>800){
end=false;
}
for(int i = 0;i<3;i++){
if((xPos<p[i].xPos+50&&xPos>p[i].xPos-50)&&((float)val<p[i].opening-100||(float)val>p[i].opening+100)){
end=false;
}
}
}
}
class pillar {
float xPos, opening;
boolean cashed = false;
pillar(int i) {
xPos = 100+(i*200);
opening = random(600)+100;
}
void drawPillar(){
line(xPos,0,xPos,opening-100);
line(xPos,opening+100,xPos,800);
}
void checkPosition(){
if(xPos<0){
xPos+=(200*3);
opening = random(600)+100;
cashed=false;
}
if(xPos<250&&cashed==false){
cashed=true;
score++;
}
}
}
void reset(){
end=true;
score=0;
r.yPos=400;
for(int i = 0;i<3;i++){
p[i].xPos+=550;
p[i].cashed = false;
}
}
void introScene(){
background(0);
image(background, 250, 350);
if(mousePressed==true)
if(mouseX > 0 && mouseX < 500 && mouseY>0 && mouseY<700){
gameScene = "Play";
}
}
void playScene(){
background(0);
r.move();
r.displayRaider();
r.checkCollisions();
for(int i = 0;i<3;i++){
p[i].drawPillar();
p[i].checkPosition();
}
}
void loseScene(){
background(0);
} '''