I’m coding for a two-player game where each player has a colour which is changed when the beam shot out from their opponent touches them. i.e. When Red player’s beam hits blue player, the the tint of the blue player supposedly changes to purple and is slowly converted to red. When it reaches the full saturation of red (something like fill(255,0,0)), the game ends.
Currently I’m having trouble accessing the variables of the x positions of the beams shot from a PVector array list in order to code its contact with the other play in my draw method. I would greatly appreciate it if anyone can help me out! I currently have 3 tabs.
main tab
PImage buildings;
PImage monL;
PImage monR;
MonL mL;
MonR mR;
boolean gameOver;
void setup() {
size(1024, 768);
buildings = loadImage(“building.jpg”);
monL = loadImage(“monsterleft.png”);
mL = new MonL();
monR = loadImage(“monsterright.png”);
mR = new MonR();
}
void draw() {
background(0);
image(buildings, 0, 0);
mL.aniMonL();
mR.aniMonR();
if (keyPressed) {
//moving MonL
if (key == 'w' || key == 'W') {
mL.moveupMonL();
}
if (key == 's' || key == 'S') {
mL.movedownMonL();
}
if (key == 'a' || key == 'A') {
mL.moveleftMonL();
}
if (key == 'd' || key == 'D') {
mL.moverightMonL();
}
//shoot MonLbeam
if (key == ' ') {
mL.createMonLBeam();
}
}
//moving MonR
if (key == CODED) {
if (keyCode == UP) {
mR.moveupMonR();
}
if (keyCode == DOWN) {
mR.movedownMonR();
}
if (keyCode == LEFT) {
mR.moveleftMonR();
}
if (keyCode == RIGHT) {
mR.moverightMonR();
}
if (keyCode == CONTROL) {
mR.createMonRBeam();
}
}
mL.drawMonLbeam();
mR.drawMonRbeam();
mL.restrictMonL();
mR.restrictMonR();
}
//monsters interacting with beams
void contactMonLbeam() {
if ((PVector).get(monLp.x) > mR.monR_x) {
mR.MonRred = mR.MonRred + 10;
mR.MonRblue = mR.MonRblue - 10;
}
}
void contactMonRbeam() {
if ((PVector).get(monRp.x) < mL.monL_x) {
mL.MonLred = mL.MonLred - 10;
mL.MonLblue = mL.MonLblue + 10;
}
}
/*void gameOver() {
gameOver = true;
if (mL.MonLred == 0 || mR.MonRblue == 0) {
reset();
}
}
void reset() {
size(1024, 768);
buildings = loadImage(“building.jpg”);
monL = loadImage(“monsterleft.png”);
mL = new MonL();
monR = loadImage(“monsterright.png”);
mR = new MonR();
}*/
Left player:
class MonL {
PImage monLArray;
float monL_x = 20;
float monL_y = height/2;
int numberOfFrames = 50;
float monL_s = 5;
float monL_restrict = 5;
float mon_bspeed = 3;
ArrayList monLBeams;
float MonLred=255;
float MonLblue=0;
MonL() {
monLBeams = new ArrayList<PVector> ();
monLArray = new PImage[5];
tint(0, 0, 255);
for (int i =0; i< 5; i++) {
monLArray[i] = monL.get(100*i, 10, 100, 100);
tint(255, 0, 0);
image(monL, 0, 0);
}
noTint();
}
void aniMonL() {
frameRate(80);
tint(255, 0, 0);
image(monLArray[frameCount%numberOfFrames/10], monL_x, monL_y);
noTint();
}
//movingmonLsprite
void moveupMonL() {
monL_y = monL_y - monL_s;
}
void movedownMonL() {
monL_y = monL_y + monL_s;
}
void moveleftMonL() {
monL_x = monL_x - monL_s;
}
void moverightMonL() {
monL_x = monL_x + monL_s;
}
//restricting monL
void restrictMonL() {
if (monL_y < 0) {
monL_y = monL_y + monL_restrict;
}
if (monL_y + 100 > 768) {
monL_y = monL_y - monL_restrict;
}
if (monL_x < 0) {
monL_x = monL_x + monL_restrict;
}
if (monL_x + 100 > width) {
monL_x = monL_x - monL_restrict;
}
}
void createMonLBeam(){
PVector Lbeam = new PVector(monL_x + 50, monL_y + 45);
monLBeams.add(Lbeam);
}
void drawMonLbeam(){
ArrayList Lbeams = new ArrayList();
for (PVector monLp: monLBeams){
fill(MonLred,0,MonLblue);
strokeWeight(0);
ellipse(monLp.x,monLp.y,20,10);
monLp.x = monLp.x + mon_bspeed;
if(monLp.x>width){
Lbeams.add(monLp);
}
}
for (PVector monLp: Lbeams){
monLBeams.remove(monLp);
}
}
}
Right player:
class MonR {
PImage monRArray;
float monR_x = 904;
float monR_y = height/2;
int numberOfFrames = 50;
float monR_s = 5;
boolean upR, downR;
float monR_restrict = 5;
float MonRred = 0;
float MonRblue = 255;
float mon_bspeed = 3;
ArrayList monRBeams;
MonR() {
monRBeams = new ArrayList<PVector> ();
monRArray = new PImage[5];
for (int i =0; i< 5; i++) {
monRArray[i] = monR.get(100*i, 10, 100, 100);
tint(0, 0, 255);
image(monR, 0, 0);
}
noTint();
}
void aniMonR() {
frameRate(80);
tint(0, 0, 255);
image(monRArray[frameCount%numberOfFrames/10], monR_x, monR_y);
noTint();
}
//movingmonRsprite
void moveupMonR() {
monR_y = monR_y - monR_s;
}
void movedownMonR() {
monR_y = monR_y + monR_s;
}
void moveleftMonR() {
monR_x = monR_x - monR_s;
}
void moverightMonR() {
monR_x = monR_x + monR_s;
}
//restricting monL
void restrictMonR() {
if (monR_y < 0) {
monR_y = monR_y + monR_restrict;
}
if (monR_y + 100 > 768) {
monR_y = monR_y - monR_restrict;
}
if (monR_x < 0) {
monR_x = monR_x + monR_restrict;
}
if (monR_x + 100 > width) {
monR_x = monR_x - monR_restrict;
}
}
void createMonRBeam() {
PVector Rbeam = new PVector(monR_x+50, monR_y + 45);
monRBeams.add(Rbeam);
}
void drawMonRbeam() {
ArrayList Rbeams = new ArrayList();
for (PVector monRp : monRBeams) {
fill(MonRred, 0, MonRblue);
strokeWeight(0);
ellipse(monRp.x, monRp.y, 20, 10);
monRp.x = monRp.x - mon_bspeed;
if (monRp.x<0) {
Rbeams.add(monRp);
}
}
}
}
Thank you very much in advance!