Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float ballx;
- float bally;
- float dy = -1; //direction of the ball on y
- float dx = random(5); //direction of the ball on x
- float ballSize;
- float ballSpeed;
- boolean inPlay = false;
- int score = 0;
- int highscore = 0;
- int paddleWidth;
- int paddleHeight;
- float paddlex; //paddle position
- int distWall = 15;
- int bricksNum = 10;
- Brick bricks[] = new Brick[bricksNum];
- float minDistance = 200;
- void setup(){
- size(displayWidth, displayHeight);
- rectMode(RADIUS);
- ellipseMode(RADIUS);
- noStroke();
- smooth();
- paddleWidth = displayWidth / 10;
- paddleHeight = displayHeight / 60;
- ballSize = min(displayWidth, displayHeight)/50;
- bally = 15;
- ballx = width / 2.0;
- }
- void draw(){
- if(inPlay){
- background(51);
- bally += dy * ballSpeed;
- ballx += dx;
- paddlex = constrain(mouseX, paddleWidth, width - paddleWidth);
- checkOutOfScreen();
- checkHitsBallPaddle();
- checkHitsBallBackWall();
- checkHitsBallEdges();
- checkHitsBallBrick();
- //draw
- fill(255);
- ellipse(ballx,bally,ballSize,ballSize);
- fill(153);
- rect(paddlex, height - distWall, paddleWidth, paddleHeight);
- fill(180);
- rectMode(CORNER);
- for(int i=0; i<bricksNum; i++){
- try{
- rect(bricks[i].getX(),bricks[i].getY(),bricks[i].getBrickWidth(),bricks[i].getBrickHeight());
- }
- catch(NullPointerException e){
- if(score%10==0){
- newBricks();
- ballSpeed *= 1.5;
- }
- }
- }
- rectMode(RADIUS);
- textSize(width/80);
- textAlign(LEFT);
- if(score < highscore) fill(255,0,0);
- else fill(0,255,0);
- text("highscore: "+highscore, distWall, 30);
- text("score: "+score, distWall, 60);
- }
- else{
- background(51);
- fill(0,255,0); //green
- textSize(width/40);
- textAlign(CENTER);
- text("Tap anyware to Play",width/2,height/2);
- textSize(width/80);
- textAlign(LEFT);
- if(score < highscore) fill(255,0,0);
- text("highscore: "+highscore, distWall, 30);
- text("score: "+score, distWall, 60);
- }
- }
- void checkOutOfScreen(){
- if(bally > height + ballSize){ //fuori dallo schermo
- ballx = width / 2 - ballSize;
- bally = 15;
- dx = random(5);
- inPlay = false;
- }
- }
- void checkHitsBallPaddle(){
- if(bally >= height - distWall - paddleHeight - ballSize
- && ballx > paddlex - paddleWidth - ballSize
- && ballx < paddlex + paddleWidth + ballSize){
- dy *= -1;
- if(bally >= height - distWall - paddleHeight - ballSize && bally < height){
- //Anti-Bouncing
- dy = -1;
- bally -= paddleHeight;
- bally -= distWall;
- }
- if(mouseX != pmouseX){ //abbotta system! XD
- dx = (mouseX - pmouseX) / 2.0;
- dx = constrain(dx, -5, 5);
- }
- }
- }
- void checkHitsBallBackWall(){
- if(bally < ballSize && dy == -1){ //ball hits back wall
- dy *= -1;
- }
- }
- void checkHitsBallEdges(){
- //ball hits top or bottom edges
- if(ballx > width - ballSize){
- dx *= -1;
- }
- if(ballx < ballSize){
- dx *= -1;
- }
- }
- void checkHitsBallBrick(){
- for(int i=0; i<bricksNum;i++){
- try{
- if(ballx >= bricks[i].getX() && ballx <= bricks[i].getX()+bricks[i].getBrickWidth()
- && bally >= bricks[i].getY()-ballSize && bally <= bricks[i].getY()+ballSize){
- //up collision
- dy = -1;
- score++;
- bricks[i]=null;
- }
- if(ballx >= bricks[i].getX() && ballx <= bricks[i].getX()+bricks[i].getBrickWidth()
- && bally >= bricks[i].getY()-ballSize+bricks[i].getBrickHeight()
- && bally <= bricks[i].getY()+ballSize+bricks[i].getBrickHeight()){
- //down collision
- dy = 1;
- score++;
- bricks[i]=null;
- }
- if(bally >= bricks[i].getY() && bally <= bricks[i].getY()+bricks[i].getBrickHeight()
- && ballx >= bricks[i].getX()-ballSize && ballx <= bricks[i].getX()+ballSize){
- //left collision
- dx *= -1;
- score++;
- bricks[i]=null;
- }
- if(bally >= bricks[i].getY() && bally <= bricks[i].getY()+bricks[i].getBrickHeight()
- && ballx >= bricks[i].getX()-ballSize+bricks[i].getBrickWidth()
- && ballx <= bricks[i].getX()+ballSize+bricks[i].getBrickWidth()){
- //right collision
- dx *= 1;
- score++;
- bricks[i]=null;
- }
- }
- catch(NullPointerException e){
- }
- }
- }
- void newBricks(){
- for(int i=0;i<bricksNum;i++){
- bricks[i] = new Brick(minDistance);
- }
- }
- void mousePressed(){
- if(!inPlay){
- newBricks();
- inPlay = true;
- highscore = score;
- score = 0;
- ballSpeed = height/100;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement