fiorina
Junior Member
Offline
Posts: 51
Re: keyPressed()?
Reply #1 - Jul 22nd , 2009, 9:24pm
One other q, when I throw in PFont for the scores... it winds up all slow... Delete the PFont code blocks and it all runs smooth again... *shrugs* float pad1 = 0; //player 1 bat y pos float pad2 = 0; //player 2 bat y pos int d = 30; //ball diameter float x1 = 400; //ball x pos float x2 = 200;//ball y pos float direction = +0.0; float direction2 = -0.0; float h = 40.0; //bat diff int s1 = 0; //p1 score int s2 = 0; //p2 score void setup() { size(800, 400); frameRate(100); } void draw() { smooth(); background(50); strokeWeight(6); stroke(255); line(400, 0, 400, height); // playboard net/line PFont score1; score1 = loadFont("Perpetua-Bold-48.vlw"); textFont(score1, 100); text(s1, 175, 100); PFont score2; score2 = loadFont("Perpetua-Bold-48.vlw"); textFont(score2, 100); text(s2, 600, 100); x1 = (x1 + direction); // ball movement x2 = (x2 - direction2); if(x2 > height) { direction2 = +5.0; } if (x2 < 0 && direction2 == +5.0) { direction2 = -5.0; } if (x2 < pad1+h && x2 > pad1-h && x1 > width-10) { direction = -5.0; } if (x2 < mouseY+h && x2 > mouseY-h && x1 < 1) { direction = +5.0; } if (x1 < -10) { x1 = 400; x2 = 200; direction = +0.0; direction2 = -0.0; s2 = s2 + 1; } if (x1 > width+10) { x1 = 400; x2 = 200; direction = +0.0; direction2 = -0.0; s1 = s1 + 1; } strokeWeight(6); //ball stroke(70, 80, 90, 255); ellipse(x1, x2, d, d); strokeWeight(1); rectMode(CENTER); rect(width, pad1, 20, 80); // player 2 bat rect(0, mouseY, 20, 80); // player 1 bat if((keyPressed == true) && (key == CODED)) { // controls for arrow key if (keyCode == UP) { pad1 = pad1 - 7; } else if (keyCode == DOWN) { pad1 = pad1 + 7; } if(pad1 > height) { pad1 = height; } else { if (pad1 <= -1) { pad1 = 0; } } } } void mouseClicked() { //click starts ball direction = +5.0; direction2 = -4.0; } void keyPressed() { if(key == ' ') { //spacebar starts ball direction = +5.0; direction2 = -4.0; } }