Created
September 22, 2019 22:31
-
-
Save niiicolai/0a732ec65aa1ada6a74db2ea5e172e7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // A reference to the ball | |
| Ball ball; | |
| // ball radius | |
| float ballRadius = 25; | |
| // ball movement speed | |
| PVector ballSpeedRange = new PVector(3, 7); | |
| // the ball's stroke color | |
| color ballStrokeColor = color(255); | |
| // ball fill colors | |
| color ballFillColors[] = new color[] { | |
| color(255, 5, 9), | |
| color(255, 247, 5), | |
| color(5, 255, 36), | |
| }; | |
| // A reference to the player | |
| Player player; | |
| // player start position | |
| PVector playerStartPosition = new PVector(0, 40); | |
| // player size | |
| PVector playerSize = new PVector(80, 10); | |
| // the player's stroke color | |
| color playerStrokeColor = color(255); | |
| // display window's background color | |
| color backgroundColor = color(0); | |
| // top rect height | |
| float topRectHeight = 10; | |
| // rect color scheme start position | |
| PVector rectColorSchemeRectStartPos = new PVector(40, 50); | |
| // space between rects | |
| float rectColorSchemeRectSpace = 10; | |
| // rect color scheme rect size | |
| PVector rectColorSchemeRectSize = new PVector(10, 10); | |
| // // rect color scheme rect stroke color | |
| color rectColorSchemeRectColor = color(255); | |
| // points counter | |
| int points; | |
| // points text position | |
| PVector pointsTextPosition = new PVector(40, 40); | |
| // points text label | |
| String pointsLabel = "SCORE: "; | |
| // points text color | |
| color pointsTextColor = color(5, 119, 255); | |
| // game description text position | |
| PVector gameDescTextPosition = new PVector(40, 80); | |
| // points text color | |
| color gameDescTextColor = color(255); | |
| // points text label | |
| String gameDescText = "How to win:\n"+ | |
| "- You can't, it's endless\n"+ | |
| "- Increase your score by hitting the ball when\n"+ | |
| " the rect and ball have the same colors\n\n"+ | |
| "How to lose:\n"+ | |
| "- Hit the ball with the rect when they don't\n"+ | |
| "have the same colors"; | |
| void setup() { | |
| // set display window size | |
| size(500, 500); | |
| // create an instance of a ball | |
| ball = new Ball(ballRadius, ballSpeedRange, ballStrokeColor, ballFillColors); | |
| // reset ball | |
| ball.reset(); | |
| // set the player position centered to the left side of the display window | |
| playerStartPosition = new PVector((width/2)-playerSize.x, height-playerStartPosition.y-playerSize.y); | |
| // create an instance of a player | |
| player = new Player(playerStartPosition, playerSize, playerStrokeColor, ballFillColors); | |
| // setup a random color on the player | |
| player.setRandomColor(); | |
| } | |
| void draw() { | |
| // set display window background color | |
| background(backgroundColor); | |
| stroke(rectColorSchemeRectColor); | |
| for (int i = 0; i < ballFillColors.length; i++) { | |
| fill(ballFillColors[i]); | |
| float xPos = rectColorSchemeRectStartPos.x; | |
| if (i > 0) { | |
| xPos += ((rectColorSchemeRectSpace+rectColorSchemeRectSize.x)*i); | |
| } | |
| rect(xPos, rectColorSchemeRectStartPos.y, | |
| rectColorSchemeRectSize.x, rectColorSchemeRectSize.y); | |
| } | |
| // render the player | |
| player.render(); | |
| // set the player to follow the mouse on y axis | |
| player.position = new PVector(mouseX, playerStartPosition.y); | |
| // render the ball | |
| ball.render(); | |
| // check if the ball collide with the player | |
| ball.playerCollisionCheck(player); | |
| // draw rect color changer | |
| rect(0, 0, width, topRectHeight); | |
| // set points text color | |
| fill(pointsTextColor); | |
| // draw points text | |
| text(pointsLabel+points, pointsTextPosition.x, pointsTextPosition.y); | |
| // set game description text color | |
| fill(gameDescTextColor); | |
| // draw game description text | |
| text(gameDescText, gameDescTextPosition.x, gameDescTextPosition.y); | |
| } | |
| void resetGame() { | |
| points = 0; | |
| ball.reset(); | |
| } | |
| class Player { | |
| public PVector position; | |
| PVector size; | |
| color fillColor; | |
| color strokeColor; | |
| private color[] fillColors; | |
| Player(PVector _position, PVector _size, color _strokeColor, color[] _fillColors) { | |
| position = _position; | |
| size = _size; | |
| strokeColor = _strokeColor; | |
| fillColors = _fillColors; | |
| } | |
| public void setRandomColor() { | |
| fillColor = color(fillColors[(int)random(0, fillColors.length)]); | |
| } | |
| public void render() { | |
| // set the balls stroke color | |
| stroke(strokeColor); | |
| // set the balls fill color | |
| fill(fillColor); | |
| // draws the palyer rect | |
| rect(position.x, position.y, size.x, size.y); | |
| } | |
| } | |
| class Ball { | |
| private PVector position; | |
| private PVector velocity; | |
| private float radius; | |
| private PVector speedRange; | |
| private color fillColor; | |
| private color strokeColor; | |
| private color[] fillColors; | |
| Ball(float _radius, PVector _speedRange, color _strokeColor, color[] _fillColors) { | |
| radius = _radius; | |
| speedRange = _speedRange; | |
| strokeColor = _strokeColor; | |
| fillColors = _fillColors; | |
| } | |
| public void render() { | |
| // apply velocity to the position | |
| // to make the ball move | |
| position.add(velocity); | |
| // set the balls stroke color | |
| stroke(strokeColor); | |
| // set the balls fill color | |
| fill(fillColor); | |
| // draws the ball | |
| circle(position.x, position.y, radius); | |
| // Check display window collisions | |
| displayCollisionCheck(); | |
| } | |
| public void reset() { | |
| position = new PVector(width/2, height/2); | |
| float randomX = random(speedRange.x, speedRange.y); | |
| randomX = (random(-1,1) > 0 ? randomX : -randomX); | |
| float randomY = random(speedRange.x, speedRange.y); | |
| velocity = new PVector(randomX, randomY); | |
| setRandomColor(); | |
| } | |
| private void setRandomColor() { | |
| fillColor = color(fillColors[(int)random(0, fillColors.length)]); | |
| } | |
| private void displayCollisionCheck() { | |
| // Use a randomSpeed to make the ball move different sometimes | |
| float randomSpeed = random(speedRange.x, speedRange.y); | |
| // check if the ball hit one of the sides of the display window | |
| // and apply random speed in the opposite direction | |
| if (position.x+radius >= width) { | |
| velocity.x = -randomSpeed; | |
| } else if (position.x-radius <= 0) { | |
| velocity.x = randomSpeed; | |
| } else if (position.y-radius <= topRectHeight) { | |
| velocity.y = randomSpeed; | |
| setRandomColor(); | |
| } else if (position.y+radius >= height) { | |
| velocity.y = -randomSpeed; | |
| } | |
| } | |
| public void playerCollisionCheck(Player _player) { | |
| boolean betweenYPositions = (position.y-radius >= _player.position.y && | |
| position.y-radius <= _player.position.y+_player.size.y) || | |
| (position.y+radius >= _player.position.y && | |
| position.y+radius <= _player.position.y+_player.size.y); | |
| boolean betweenXPositions = (position.x-radius >= _player.position.x && | |
| position.x-radius <= _player.position.x+_player.size.x) || | |
| (position.x+radius >= _player.position.x && | |
| position.x+radius <= _player.position.x+_player.size.x) || | |
| (position.x >= _player.position.x && | |
| position.x <= _player.position.x+_player.size.x); | |
| if (betweenXPositions && betweenYPositions) { | |
| // Use a randomSpeed to make the ball move different sometimes | |
| float randomSpeed = random(speedRange.x, speedRange.y); | |
| // Move the ball in the opposite direction as the player | |
| velocity.y = -randomSpeed; | |
| // if the ball and the player is the same color | |
| if (fillColor == _player.fillColor) { | |
| // add 1 point | |
| points++; | |
| // set the player to a new color | |
| _player.setRandomColor(); | |
| } else { | |
| resetGame(); | |
| } | |
| // Set the ball to a random color | |
| setRandomColor(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment