New to Processing
in
Programming Questions
•
1 years ago
int x,y;
int dx=1,dy=1;
int screenHeight=500;
int screenWidth=500;
int sz = 10;
int barX1=10, barY=screenHeight/2;
int barX2=50;
int score=0;
void setup(){
size(screenWidth,screenHeight) ;
fill(255);
stroke(255);
frameRate(100);
smooth();
x=int(random(0,screenWidth));
y=int(random(0,screenHeight));
}
void draw(){
background(0);
line(barX1,barY,barX2,barY);
ellipse(x,y,sz,sz);
//detect if ball hit left and right side
if(x<=sz/2 || x>=screenWidth-sz/2)
dx=-dx;
//detect if ball hit top and bottom
if(y<=sz/2 || y>=screenHeight-sz/2)
dy=-dy;
//detect if bar hits ball
if((y==barY-sz/2 || y==barY+sz/2 ) && (x>barX1 && x<barX2))
{
dx=-dx;
dy=-dy;
println(++score);
}
x+=dx;
y+=dy;
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) {
barX1-=10;
barX2-=10;
} else if (keyCode == RIGHT) {
barX1+=10;
barX2+=10;
} else if (keyCode == UP) {
barY-=10;
} else if (keyCode == DOWN) {
barY+=10;
}
}
}
This is a sketch from my lecture on 'Motion'. We are given the additional task of modifying this set of code and transforming it into a 'Ping-Pong' game where the participation of another player is possible. However, I'm having some trouble interpreting this code, and so I've taken liberties to pose my questions to the web in hopes for guidance. Any form of help rendered will be appreciated.
Some questions I have pertaining to the aforementioned code:
Q1) Is screen.height the same as screenHeight? and additively, Is screen.width the same as screenWidth?
Q2) Why set the fill(255) in void(setup) only to set the background(0) in void(draw)?
Q3) //detect if ball hit left and right side
if(x<=sz/2 || x>=screenWidth-sz/2)
dx=-dx;
Why sz/2 of all values? ( I tried substituting this set of values with alternative values of "sz/3", "sz/4", "sz/30" and did not see any visible changes when running the programme.
I'm new to coding and programming and although technically I should be acquainted with the principles of "motion" at this point in time. Reality says otherwise. Any form of assistance will be deeply appreciated =).
On a sidenote, I'm also curious as to whether or not there are good practice habits or methodologies that should be adopted for beginners who are just starting out in programming.
int dx=1,dy=1;
int screenHeight=500;
int screenWidth=500;
int sz = 10;
int barX1=10, barY=screenHeight/2;
int barX2=50;
int score=0;
void setup(){
size(screenWidth,screenHeight)
fill(255);
stroke(255);
frameRate(100);
smooth();
x=int(random(0,screenWidth));
y=int(random(0,screenHeight));
}
void draw(){
background(0);
line(barX1,barY,barX2,barY);
ellipse(x,y,sz,sz);
//detect if ball hit left and right side
if(x<=sz/2 || x>=screenWidth-sz/2)
dx=-dx;
//detect if ball hit top and bottom
if(y<=sz/2 || y>=screenHeight-sz/2)
dy=-dy;
//detect if bar hits ball
if((y==barY-sz/2 || y==barY+sz/2 ) && (x>barX1 && x<barX2))
{
dx=-dx;
dy=-dy;
println(++score);
}
x+=dx;
y+=dy;
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) {
barX1-=10;
barX2-=10;
} else if (keyCode == RIGHT) {
barX1+=10;
barX2+=10;
} else if (keyCode == UP) {
barY-=10;
} else if (keyCode == DOWN) {
barY+=10;
}
}
}
This is a sketch from my lecture on 'Motion'. We are given the additional task of modifying this set of code and transforming it into a 'Ping-Pong' game where the participation of another player is possible. However, I'm having some trouble interpreting this code, and so I've taken liberties to pose my questions to the web in hopes for guidance. Any form of help rendered will be appreciated.
Some questions I have pertaining to the aforementioned code:
Q1) Is screen.height the same as screenHeight? and additively, Is screen.width the same as screenWidth?
Q2) Why set the fill(255) in void(setup) only to set the background(0) in void(draw)?
Q3) //detect if ball hit left and right side
if(x<=sz/2 || x>=screenWidth-sz/2)
dx=-dx;
Why sz/2 of all values? ( I tried substituting this set of values with alternative values of "sz/3", "sz/4", "sz/30" and did not see any visible changes when running the programme.
I'm new to coding and programming and although technically I should be acquainted with the principles of "motion" at this point in time. Reality says otherwise. Any form of assistance will be deeply appreciated =).
On a sidenote, I'm also curious as to whether or not there are good practice habits or methodologies that should be adopted for beginners who are just starting out in programming.
I'm currently a student of the University of London International Programme in my first year of the Creative Computing Degree. Geographically I'm located in Singapore but spiritually I belong to the web. And if there's anyone out there, like me who is just starting out on Processing and is keen to engage in collaborative learning, feel free to add me on facebook =D.
1