float pacX,pacY,pacXS,pacYS,pacAng; float ballX, ballY; float ballXS, ballYS; float PACSPEED; float mouth = PI/4; float mouthspeed = -.1; void setup() { size(200, 200); strokeCap(SQUARE); frameRate(30); resetBall(); pacX = width*3/4; pacY = width/2; pacXS = width/80; pacYS = 0; // noCursor(); PACSPEED = width/100; textAlign(CENTER,CENTER); textSize(16); lowLag.init(); lowLag.load("blip1.mp3"); lowLag.load("blip2.mp3"); lowLag.load("pac.mp3"); lowLag.load("gameend.mp3"); } String showScore=""; int score = 0; float scorePos; void doScore(){ showScore = " last score: "+ score; scorePos = width *1.5; score = 0; } boolean virgin = true; void mousePressed(){ virgin = false; } void resetBall(){ ballX = width/2; ballY = height/2; ballXS = -width/100; ballYS = random(-height/100,height/100); } int clicksTilTurn = 20; void draw() { background(0); strokeWeight(width/40); stroke(255); line(0, height/20, width, height/20); line(0, height*19/20, width, height*19/20); fill(255); text(showScore,scorePos,height/2); scorePos -= width/50; float s = 15; for (int i = 0; i <= s; i++) { float y = lerp(height/20, height*18/20, i/s) ; line(width/2, y, width/2,y + ceil(height/(s*3))); } float pos = constrain(mouseY,height/20+height/16,height*19/20-height/16); line(width/20,pos - height/16, width/20,pos+height/16); line(width*19/20,pos - height/16, width*19/20,pos+height/16); if(virgin){ text("CLICK TO PLAY\nGAME JAM PAC PONG",width/2,height/2); return; } fill(255,255,0); noStroke(); if(dist(ballX,ballY,pacX,pacY)< width/24){ //pacman got it doScore(); resetBall(); lowLag.play("pac.mp3"); } pacX += pacXS; pacY += pacYS; pushMatrix(); translate(pacX,pacY); rotate(pacAng); arc(0,0,width/8, height/8,mouth,2*PI-mouth); popMatrix(); turnPac(); mouth += mouthspeed; if(mouth < 0){ mouth = 0; mouthspeed = .2; } if(mouth > PI/4){ mouth = PI/4; mouthspeed = -.2; } fill(255); strokeWeight(width/10); ballX += ballXS; ballY += ballYS; if(ballY < height/20){ ballY = height/20; ballYS *= -1; } if(ballY > height*18.5/20){ ballY = height*18.5/20; ballYS *= -1; } if(ballY < height*1.5/20){ ballY = height*1.5/20; ballYS *= -1; } //bounce on right if(abs(ballX - width*19/20) < height/30 && abs(ballY - pos) < height/16){ ballXS = -abs(ballXS) ; ballX = width*19/20 - height/30; ballYS = (ballY-pos)/(height/30); score++; lowLag.play("blip1.mp3"); } //bounce on left if(abs(ballX - width/20) < height/30 && abs(ballY - pos) < height/16){ ballXS = abs(ballXS); ballX = width/20 + height/30; ballYS = (ballY-pos)/(height/30); score++; lowLag.play("blip2.mp3"); } if(ballX <0){ resetBall(); doScore(); lowLag.play("gameend.mp3"); } if(ballX > width){ resetBall(); doScore(); lowLag.play("gameend.mp3"); } ellipse(ballX,ballY,width/20,width/20); } void turnPac(){ clicksTilTurn--; if(clicksTilTurn <= 0){ clicksTilTurn = ceil(random(5,20)); if(abs(pacX-ballX) > abs(pacY-ballY)){ if(ballX > pacX) pacAng = 0; else pacAng = PI; } else { if(ballY > pacY) pacAng = PI/2; else pacAng = PI*3/2; } pacXS = PACSPEED * cos(pacAng); pacYS = PACSPEED * sin(pacAng); // println(clicks); } }