Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int gameScreen = 0;
- int ballX, ballY;
- int ballSize = 20;
- int ballColor = color(0);
- float gravity = 1;
- float ballSpeedVert = 0;
- float airFriction = 0.0001;
- float friction = 0.1;
- color racketColor = color(0);
- float racketWidth = 100;
- float racketHeight = 10;
- int racketBounceRate = 20;
- float ballSpeedHorizon = 10;
- int wallSpeed = 5;
- int wallInterval = 1000;
- float lastAddTime = 0;
- int minGapHeight = 200;
- int maxGapHeight = 300;
- int wallWidth = 80;
- color wallColors = color(0);
- ArrayList<int[]> walls = new ArrayList<int[]>();
- void setup()
- {
- size(500,500);
- ballX=width/4;
- ballY=height/5;
- }
- void draw()
- {
- if(gameScreen == 0) initScreen();
- if(gameScreen == 1) gameScreen();
- if(gameScreen == 2) gameOverScreen();
- }
- void initScreen()
- {
- background(0);
- textAlign(CENTER);
- text("Click to start", height/2, width/2);
- }
- void gameScreen()
- {
- background(255);
- drawBall();
- applyGravity();
- watchRacketBounce();
- keepInScreen();
- drawRacket();
- applyHorizontalSpeed();
- wallAdder();
- wallHandler();
- }
- void gameOverScreen()
- {
- }
- public void mousePressed()
- {
- if(gameScreen == 0) startGame();
- }
- void startGame()
- {
- gameScreen=1;
- }
- void drawBall()
- {
- fill(ballColor);
- ellipse(ballX, ballY, ballSize, ballSize);
- }
- void applyGravity()
- {
- ballSpeedVert += gravity;
- ballY += ballSpeedVert;
- ballSpeedVert -= ballSpeedVert * airFriction;
- }
- void keepInScreen()
- {
- if(ballY+(ballSize/2) > height) makeBounceBottom(height);
- if(ballY-(ballSize/2) < 0) makeBounceTop(0);
- if(ballX-(ballSize/2) < 0) makeBounceLeft(0);
- if(ballX+(ballSize/2) > width) makeBounceRight(width);
- }
- void makeBounceBottom(int surface)
- {
- ballY = surface - (ballSize/2);
- ballSpeedVert *= -1;
- ballSpeedVert -= ballSpeedVert * friction;
- }
- void makeBounceTop(int surface)
- {
- ballY = surface + (ballSize/2);
- ballSpeedVert *= -1;
- ballSpeedVert -= ballSpeedVert * friction;
- }
- void drawRacket()
- {
- fill(racketColor);
- rectMode(CENTER);
- rect(mouseX,mouseY,racketWidth,racketHeight);
- }
- void watchRacketBounce()
- {
- float overhead = mouseY - pmouseY;
- if((ballX+(ballSize/2) > mouseX - (racketWidth/2)) &&
- (ballX-(ballSize/2) < mouseX + (racketWidth/2)))
- {
- if(dist(ballX,ballY, ballX, mouseY) <=(ballSize/2)+abs(overhead))
- {
- makeBounceBottom(mouseY);
- ballSpeedHorizon = (ballX-mouseX)/5;
- if(overhead<0)
- {
- ballY+=overhead;
- ballSpeedVert+=overhead;
- }
- }
- }
- }
- void applyHorizontalSpeed()
- {
- ballX += ballSpeedHorizon;
- ballSpeedHorizon -= (ballSpeedHorizon * airFriction);
- }
- void makeBounceLeft(int surface)
- {
- ballX = surface +(ballSize/2);
- ballSpeedHorizon *= -1;
- ballSpeedHorizon -= ballSpeedHorizon * friction;
- }
- void makeBounceRight(int surface)
- {
- ballX = surface -(ballSize/2);
- ballSpeedHorizon *= -1;
- ballSpeedHorizon -= ballSpeedHorizon * friction;
- }
- void wallAdder()
- {
- if(millis()-lastAddTime>wallInterval)
- {
- int randHeight = round(random(minGapHeight, maxGapHeight));
- int randY = round(random(0,height-randHeight));
- int[] randWall = {width, randY, wallWidth, randHeight};
- walls.add(randWall);
- lastAddTime = millis();
- }
- }
- void wallHandler()
- {
- for(int i = 0; i<walls.size(); i++)
- {
- wallRemover(i);
- wallMover(i);
- watchWallCollision(i);
- wallDrawer(i);
- }
- }
- void wallRemover(int index)
- {
- int[] wall = walls.get(index);
- if(wall[0] + wall[2] <=0) walls.remove(index);
- }
- void wallMover(int index)
- {
- int[] wall = walls.get(index);
- wall[0]-=wallSpeed;
- }
- void wallDrawer(int index)
- {
- int[] wall = walls.get(index);
- int gapWallX = wall[0];
- int gapWallY = wall[1];
- int gapWallWidth = wall[2];
- int gapWallHeight = wall[3];
- rectMode(CORNER);
- fill(wallColors);
- rect(gapWallX, 0, gapWallWidth, gapWallY);
- rect(gapWallX, gapWallY+gapWallHeight,gapWallWidth,
- height-gapWallY-gapWallHeight);
- }
- void watchWallCollision(int index)
- {
- int[] wall = walls.get(index);
- int gapWallX = wall[0];
- int gapWallY = wall[1];
- int gapWallWidth = wall[2];
- int gapWallHeight = wall[3];
- int wallTopX = gapWallX;
- int wallTopY = 0;
- int wallTopWidth = gapWallWidth;
- int wallTopHeight = gapWallY;
- int wallBottomX = gapWallX;
- int wallBottomY = gapWallY + gapWallHeight;
- int wallBottomWidth = gapWallWidth;
- int wallBottomHeight = height-gapWallY-gapWallHeight;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement