Tobias
September 27, 2020, 5:39pm
1
Hi.
I am new to processing and I am trying to make Pong. I want both the paddles to be able to move at once. Since keyPressed only allows for one button press at a time, i tried to add a boolean for each button press in hope that it would solve the problem, but now the paddles wont even move. Can someone help me get both of the paddles moving at once using two button presses at once?
Here is my code:
Paddle p1;
Paddle p2;
void setup(){
size(600, 600);
p1 = new Paddle(20);
p2 = new Paddle(width - 30);
}
void draw(){
background(0);
p1.show1();
p1.move();
p2.show2();
p2.move();
}
class Paddle{
float x;
float y1 = height/2 - 50;
float y2 = height/2 - 50;
boolean wp = false;
boolean sp = false;
boolean Up = false;
boolean Dp = false;
Paddle(float tempX){
x = tempX;
}
void show1(){
stroke(255);
fill(255);
rect(x, y1, 10, 100);
}
void show2(){
stroke(255);
fill(255);
rect(x, y2, 10, 100);
}
void move(){
if(wp == true){
y1 -= 4;
}
if(sp == true){
y1 +=4;
}
if(Up == true){
y2 -= 4;
}
if(Dp == true){
y2 += 4;
}
}
void keyPressed(){
if(key == ‘w’){
wp = true;
}
if(key == ‘s’){
sp = true;
}
if(keyCode == UP){
Up = true;
}
if(keyCode == DOWN){
Dp = true;
}
}
void keyReleased(){
if(key == ‘w’){
wp = false;
}
if(key == ‘s’){
sp = false;
}
if(keyCode == UP){
Up = false;
}
if(keyCode == DOWN){
Dp = false;
}
}
}
Chrisir
September 27, 2020, 5:48pm
2
keyPressed and keyReleased won‘t be called inside the class
Move it outside the class
say p1.wp etc. to adress the variables inside the class
1 Like
Tobias
September 27, 2020, 5:51pm
3
Thanks. It worked. I am trying to get better at writing efficient and small code. Do you se anuthing in my code that could have been better writen?
Chrisir
September 27, 2020, 5:56pm
4
Yeah, well…
I mean, first of all you could use ctrl-t in processing prior to posting
When posting: format your code.
Then: in the class you have one y for one paddle and one y for the other. This defies the principle of a class.
A class represents only ONE paddle and is oblivious to any other. And the class should be able to represent 1000 paddles with the same y for all
1 Like
Tobias
September 27, 2020, 6:01pm
5
Thank you for your time and effort.
Chrisir
September 27, 2020, 6:03pm
6
Please show your latest version
Tobias
September 27, 2020, 6:06pm
7
I wont bother to change the y variables because i only need two paddles for this project.
Paddle p1;
Paddle p2;
Ball b;
boolean wp = false;
boolean sp = false;
boolean Up = false;
boolean Dp = false;
void setup() {
size(600, 600);
p1 = new Paddle(20);
p2 = new Paddle(width - 30);
b = new Ball();
}
void draw() {
background(0);
p1.show1();
p1.move();
p2.show2();
p2.move();
b.show();
}
void keyPressed() {
if (key == 'w') {
wp = true;
}
if (key == 's') {
sp = true;
}
if (keyCode == UP) {
Up = true;
}
if (keyCode == DOWN) {
Dp = true;
}
}
void keyReleased() {
if (key == 'w') {
wp = false;
}
if (key == 's') {
sp = false;
}
if (keyCode == UP) {
Up = false;
}
if (keyCode == DOWN) {
Dp = false;
}
}
class Paddle {
float x;
float y1 = height/2 - 50;
float y2 = height/2 - 50;
Paddle(float tempX) {
x = tempX;
}
void show1() {
stroke(255);
fill(255);
rect(x, y1, 10, 100);
}
void show2() {
stroke(255);
fill(255);
rect(x, y2, 10, 100);
}
void move() {
if (wp == true) {
y1 -= 4;
}
if (sp == true) {
y1 +=4;
}
if (Up == true) {
y2 -= 4;
}
if (Dp == true) {
y2 += 4;
}
}
}
Chrisir
September 27, 2020, 6:09pm
8
Tobias:
p2.show2();
p2.move();
Here we can see you forgot to use ctrl-t
You really should AND delete one of your show functions too please. Otherwise it really looks you didn’t understand OOP.
Having two different y variables for two objects doesn’t really reflect what a class is for.
There is a nice tutorial about objects Objects / Processing.org
You should read it
Warm regards,
Chrisir
Tobias
September 27, 2020, 6:11pm
9
I used ctrl-t, but i had to edit the comment due to a error in my code so the spacing got messed up.
1 Like
Chrisir
September 28, 2020, 11:10am
10
here is a version more clean in terms of OOP
Paddle p1;
Paddle p2;
//Ball b;
//--------------------------------------------------------
void setup() {
size(600, 600);
p1 = new Paddle(20, height/2 - 50);
p2 = new Paddle(width - 30, height/2 - 50);
// b = new Ball();
} // func
void draw() {
background(0);
p1.show();
p1.move();
p2.show();
p2.move();
//b.show();
} // func
//--------------------------------------------------------
void keyPressed() {
if (key == 'w') {
p1.goUp = true;
}
if (key == 's') {
p1.goDown = true;
}
if (keyCode == UP) {
p2.goUp = true;
}
if (keyCode == DOWN) {
p2.goDown = true;
}
} // func
void keyReleased() {
if (key == 'w') {
p1.goUp = false;
}
if (key == 's') {
p1.goDown = false;
}
if (keyCode == UP) {
p2.goUp = false;
}
if (keyCode == DOWN) {
p2.goDown = false;
}
} // func
//=================================================================
class Paddle {
float x = 0; // Pos
float y = 0;
boolean goUp = false; // Flags for up/down
boolean goDown = false;
Paddle(float tempX, float tempY) {
x = tempX;
y = tempY;
} // constr
void show() {
stroke(255);
fill(255);
rect(x, y,
10, 100);
} // func
void move() {
if (goUp) {
y -= 4;
} else if (goDown) {
y += 4;
}
} // func
//
} //class
//
1 Like