We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm developing a football game for my university project, i'm trying to implement a feature where the user would press '1' to play as the first blue player and could switch on the fly by pressing '2' and '3'; in addition to this i would like to create a feature that highlights the current selected player with a red ring. thank you in advance :) heres my code;
Ball ball;
Timer startTimer;
Blue [] blue = {
new Blue (400, 400, 50, 0, 0, 255),
new Blue (200, 200, 50, 0, 0, 255),
new Blue (200, 600 ,50, 0, 0, 255)
};
int bScore = 0;
int rScore = 0;
void setup () {
size (1200, 800);
frameRate(100);
startTimer = new Timer (0);
ball = new Ball (600, 400, 15);
}
void draw () {
drawPitch ();
ballReset();
ball.display();
ball.update();
ball.bCollision();
ball.pCollision();
startTimer.countUp();
blue[0].displayBlue();
blue[0].moveBlue();
blue[0].boundaryCollision();
blue[1].displayBlue();
blue[1].moveBlue();
blue[1].boundaryCollision();
blue[2].displayBlue();
blue[2].moveBlue();
blue[2].boundaryCollision();
fill(0);
textSize (20);
text (startTimer.getTime(), 575, 20);
println (mouseX, mouseY);
}
void keyPressed() {
if (key == 'a') {
blue[0].aPressed = true;
} else if (key == 'd') {
blue[0].dPressed = true;
} else if (key == 'w') {
blue[0].wPressed = true;
} else if (key == 's') {
blue[0].sPressed = true;
}
}
void keyReleased () {
if (key == 'a') {
blue[0].aPressed = false;
} else if (key == 'd') {
blue[0].dPressed = false;
} else if (key == 'w') {
blue[0].wPressed = false;
} else if (key == 's') {
blue[0].sPressed = false;
}
}
class Ball {
PVector pos1 = new PVector (0, 0);
PVector velo;
float radius;
float m;
Ball (float x, float y, float r_) {
pos1 = new PVector (x, y);
velo = new PVector (0, 0);
velo.mult(3);
radius = r_;
m = radius * .1;
}
void update () {
pos1.add(velo);
}
void pCollision () {
PVector distanceVect = PVector.sub(blue[0].pos, pos1); //GETS DISTANCE BETWEEN THE TWO BALLS
float minDistance = radius + blue[0].radius-20; //MINIMUM DISTANCE BEFORE THEY TOUCH
float distanceVectMag = distanceVect.mag();
if (distanceVectMag < minDistance) {
float distanceCorrection = (minDistance - distanceVectMag);
PVector d = distanceVect.copy();
PVector correctionVector = d.normalize().mult(distanceCorrection);
blue[0].pos.add(correctionVector);
pos1.sub(correctionVector);
}
}
class Blue {
boolean wPressed = false;
boolean dPressed = false;
boolean sPressed = false;
boolean aPressed = false;
PVector pos;
float radius;
color Colour = color (0, 0, 255);
Blue (float x, float y, float r_, int red, int green, int blue) {
pos = new PVector(x, y);
radius = r_;
Colour = color (red, green, blue);
}
void displayBlue () {
fill (Colour);
noStroke();
ellipse(pos.x, pos.y, radius, radius);
}
void boundaryCollision () {
if (pos.x > width-75) { //RIGHT WALL
pos.x = width-75;
} else if (pos.x <75) { //LEFT WALL
pos.x = 75;
} else if (pos.y >height -45) { //BOTTOM WALL
pos.y = height-45;
} else if (pos.y <45) { //TOP WALL
pos.y = 45;
}
}
Answers
Is Blue your players?
Just have isActive as a boolean in the class or have an global int actPlayerIndex and fill it with 0,1 OR 2 and use it as index for array blue
yes Blue is my players, sorry i should make that clearer and thank you i'll give that a go now and let you know how i get on
After line 144 just say if (isActive) line(pos.x......
So there is a line under the active player
In lines 36-44 you‘d basically only move the guy with the active index
The other 2 you don’t move
I'm having trouble getting it started, im pretty new to processing ahah, i get the general idea of what im trying to do though so i think if i can get the index working i'll be okay
Just in keyPressed if key 0 Index active =0
key ==1 index active = 1
Etc.
And then evaluate indexActive whereever you need it
When i try and declare the blue array in the setup() i'm getting a 'Could not run the sketch' error. (Target VM failed to initialize) any ideas?
what do i set the isActive boolean too?
the boolean as opposed to the int is a variable inside the class.
Set it to false for all except for the active player
i still can't figure this out... i don't know how to get this section to work;
the blue[0] section for all of those will only work for my original player and i can't figure out how to use the boolean instead
I wasn’t clear
The idea is :
activeIndex
as an index for the arrayIn draw():
E.g. when it’s 1 only blue[1] moves
Additionally in the class
boolean isActive
helps you that each player itself knows internal if it’s active. So it can display a line to mark itself as active.In
keyPressed()
:here...
(I had to comment out the timer stuff etc., just remove the
//
signs where needed)You can choose active player by pressing 0,1,2 - see the blue line UNDER the blue player. The active player can be steered using
'a'
- I haven't done wsd yet.Chrisir ;-)