Pong again! (returning values)
in
Contributed Library Questions
•
10 months ago
I really need help here, esp with the return number bit in bold, I am trying to return a number depending on where the ball is in the pong grid (yes i went a long way around with the grid). The eventual idea is the number will be sent to Max and then logic to make certain chords go off etc. When i run the code it tells me "unexpected token float" but without it, it tells me that it needs a float. the underlined solutions were suggested to me, but I don't really understand what's going on. I'm a beginner, so if you do help, please assume i know NOTHING. i'm just trying to get it to work, i'm not worried if it's a bit unwieldy and too long.
import oscP5.*; //import libraries
import netP5.*;
//paddle variables
int paddlerightX, paddlerightY;
int paddleleftX, paddleleftY;
int widthpaddle = 10, heightpaddle = 50;
// ellipse (ball) variables
float ellipseX, ellipseY;//
float speedX = 1, speedY = 1;
float ellipseSize = 10;
float directionX = 1, directionY = 1;
//declaring global variable to start pong
boolean startPong=false;
//objects
OscP5 oscP5Connect;
NetAddress sendingLocation;//declaring the objects for later
OscMessage messageToSendToMax;
void setup() //background/game interface, runs and stays
{
size (400, 400);
// variable values needed here for the declared variables above
paddlerightX = width - 10;
//paddlerightY;
paddleleftX = 0;
// paddleleftY;
ellipseX = width/2;
ellipseY = 0;
oscP5Connect = new OscP5(this, 9000); // incoming from ipad
sendingLocation = new NetAddress ("127.0.0.1", 8000); // processing to max
}
void draw()
{
background(0);
if (startPong ==true)//boolean to start pong
{
ellipseBounce();
}
fill (0, 255, 0); //colours ball and rects
ellipse(ellipseX, ellipseY, ellipseSize, ellipseSize);//draws the ball
drawInterface();//function to draw interface called here
paddlePosition();
drawPaddles();
ellipseBounce();
tellMaxToStart();
tellSpeed();
startUltra();
// ballSpeed(); //this function has an argument, float s, and is called here and created below.
EvenOrNot();
//makes the ball move
}
void ellipseBounce ()
{
//y radius
ellipseY = ellipseY + (directionY*speedY); //flips the ball back if it goes outside the 400 x 400 frame
if (ellipseY > height || ellipseY < 0)
directionY = directionY * -1;
// x radius
ellipseX = ellipseX + (directionX*speedX);//flips the ball back if it goes outside the 400 x 400 frame
if (ellipseX > width || ellipseX < 0)
directionX = directionX * -1;
}
void oscEvent(OscMessage receivedMessage) //I adapted laura Maye's patch here.
{
if (receivedMessage.checkAddrPattern("/1/ballspeed"))
{
float receivedSpeed = receivedMessage.get(0).floatValue();
ballSpeed(receivedSpeed);
println("the current ballspeed is:" + receivedSpeed);
}
if (receivedMessage.checkAddrPattern("/1/pongtoggleout"))
{
float begin = receivedMessage.get(0).floatValue();
if (begin == 1.0)
startPong = true;
else
startPong = false;
println("StartPong is set to:" + startPong);
}
}
void ballSpeed(float s)
{
speedY = s*3 + 1;
speedX = s*3 + 1;
}
void drawInterface()//draws the screen,
{
strokeWeight(1); //makes the line more visible cos it has an outline
for (int i = 0; i<height; i+=10) // for loop to tell it to draw the rects continually and at a space of 10 until it goes the lenght of the defined screen
{
rect (200, i + 10, 5, 15); // line down screen centre this for loop tells it to keep drawing as long as it is less than the height
}
}
void drawPaddles() //draws the paddles
{
fill (247, 15, 42);
rect (paddleleftX, paddleleftY, widthpaddle, heightpaddle);
rect (paddlerightX, paddlerightY, widthpaddle, heightpaddle);
}
void paddlePosition()//makes them move and not disappear
{
//RIGHT PADDLE
paddlerightY = paddlerightY + 3;
if (paddlerightY > height - 50) //ensuring the paddle doesn't go off the bottom of the screen
{
paddlerightY = paddlerightY * -1 ;//
}
else if (paddlerightY < 0 || ellipseY > paddlerightY && ellipseY < paddlerightY + heightpaddle)
// ball bounce
paddlerightY = 0;
//LEFT PADDLE
paddleleftY = paddleleftY + 3; //moving it down
if (paddleleftY > height - 50) //ensuring the paddle doesn't go off the bottom of the screen
{
paddleleftY = paddleleftY * -1;//
}
else if (paddleleftY < 0 || ellipseY > paddleleftY && ellipseY < paddleleftY + heightpaddle)
//ensuring the paddle doesn't go off the top of the screen
paddleleftY = 0;
}
// _____________________________________________________________________ //
//This bit figures out where the ball is, and returns a number depending on the location and then sends this number and message to max to do things over there.
float gridLayout (float px, float py)//function for the grid
{
float numberToReturn = 0
//first line of grid boxes 1-4
if (px < width/4 && py > height/4)// it's in the Q1 (Q = quadrant)
numberToReturn = 1;
return 1;
else if (px < width/2 && px < width/ 4 && py < height/4) //it's in Q2
numberToReturn = 2;
return 2;
else if (px > width/2 && px < width/4*3 && py < height/4) //it's in q3
numberToReturn = 3;
return 3;
else if (px > width && px > width/4*3 && py < height/4) //it's in Q4
numberToReturn = 4;
return 4;
//second line of grid boxes 5-8
else if (px < width/4 && py > height/2) //Q5
return 5;
else if (px > width/4 && px > width/ 4 && py > height/2)//correct q6
return 6;
else if (px > width/2 && px < width/4*3 && py < height/2) //it's in q7
return 7;
else if (px > width/4*3 && px < width/4 && py < height/4) //it's in q8
return 8;
//third line of grid boxes 9-12
else if (px < width/4 && py > height/4) //Q9
return 9;
else if (px < width/2 && px > width/4 && py > height/4) //Q10
return 10;
else if (px > width/2 && px < width/4*3 && py > height/4)//q11
return 11;
else if ( px > width/4*3 && px < width && py > height/4)//q12
return 12;
//last line of grid boxes 13 -16
else if (px < width/4 && px < height)// q13
return 13;
else if (px < width/2 && px > width/4 && px < height)// Q14
return 14;
else if (px < width/4*3 && px > width/2 && px < height)//Q15
return 15;
else if (px < width/4*3 && px > width && px < height)//Q15
return 16;
}
void EvenOrNot ()
{
float r = gridLayout (ellipseX, ellipseY);
if ( r % 2 == 1) //?/ ie it's uneven random note to nord stage called /1/unevennote in max
//i'll call this uneven note 1 and send the 1 to max with the identifier unevennote
{
messageToSendToMax = new OscMessage("/unevennote/");
messageToSendToMax.add(2);
oscP5Connect.send(messageToSendToMAx, sendinglocation);
}
else if ( r% 2 == 0 && r != 6)//ie it's EVEN send random note to xp 80 (send a message to max to) /1/evennote
{
messageToSendToMax = new OscMessage("/evennote");
messageToSendToMax.add(1);
oscP5Connect.send(messageToSendToMAx, sendinglocation);
}
else if (r == 6)
{
//message to max send message to midi to "noteon" 24 channel 8 (this is all in one box in max)
messageToSendToMax = new OscMessage("/ingrid");
messageToSendToMax.add(6);
oscP5Connect.send(messageToSendToMAx, sendinglocation);
}
}
// ______________________________________________________________________ //
// this sends the recieved messages to max//
void tellMaxToStart()
{
messageToSendToMax = new OscMessage("/1/pongtoggleout"); //check this on notes
messageToSendToMax.add(0.);//add something else /// not sure about the float number
oscP5Connect.send(messageToSendToMAx, sendinglocation);
}
void tellSpeed()
{
messageToSendToMax = new OscMessage("/1/ballspeed"); //sending back values??
// messageToSendToMax.add();//add something else //
oscP5Connect.send(messageToSendToMAx, sendinglocation);
}
void startUltra()// no idea what is going on here?? don't think i have it in max
{
messageToSendToMax = new OscMessage("/1/ballspeed"); //check this on notes
//messageToSendToMax.add();//add something else //
oscP5Connect.send(messageToSendToMAx, sendinglocation);
}
1