Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PImage myDinoHat;
- PImage myBackground;
- int dinox = 0;
- int ballSize;
- // Paddle Variables
- float paddleX;
- int paddleSize = 70;
- int x = 0;
- int y = 0;
- int ySpeed = 5;
- int xSpeed = 5;
- void setup() {
- size(500,500);
- myDinoHat = loadImage("DinoHat.png");
- myBackground = loadImage("MiddleEast.png");
- background(255);
- // Reset game
- newGame();
- }
- void draw ()
- {
- x = x + xSpeed;
- y = y + ySpeed;
- // Check if the ball hits any wall
- if (x > width) {
- xSpeed = -5;
- //soundWall.rewind();
- //soundWall.play();
- }
- if (x < 0) {
- xSpeed = 5;
- //soundWall.rewind();
- //soundWall.play();
- }
- if (y < 0) {
- ySpeed = 5;
- //soundWall.rewind();
- //soundWall.play();
- }
- if (y > (height - 35)) {
- // GAME OVER MAN
- // Ball hit bottom of the screen
- // Restart game
- newGame();
- //soundLose.rewind();
- //soundLose.play();
- }
- // Check if ball hit paddle
- if (ballHitPaddle()) {
- ySpeed = -ySpeed;
- // This line resets the ball position just above the paddle
- y = (height - 50) - (ballSize / 2);
- //soundPaddle.rewind();
- //soundPaddle.play();
- }
- //dinox = mouseX; // this just has the picture of the dinosaur become the mouse, and the new one at the bottom just has the speed of the mouse slow down
- dinox += (mouseX - dinox)/ 20; //the higher the number the slower it is, the lower the number the faster it is
- image(myBackground, 0, 0, 900, 800); // (x, y, width, height)
- image(myDinoHat, dinox, height - 50);
- //Drawing the ball
- noStroke();
- ballSize = 18;
- //background(255, 153, 204);
- ellipse(x, y, ballSize, ballSize);
- }
- // This functions checks if a ball has hit the paddle
- boolean ballHitPaddle() {
- // We set up a bunch of helper variables
- int ballLeft = x - (ballSize / 2);
- int ballRight = x + (ballSize / 2);
- int ballTop = y - (ballSize / 2);
- int ballBottom = y + (ballSize / 2);
- int padLeft = int(paddleX);
- int padRight = int(paddleX) + paddleSize;
- int padTop = height - 50;
- // Eliminating all cases where a collision would be impossible
- if (ballBottom < padTop) {
- return false;
- }
- if (ballRight < padLeft) {
- return false;
- }
- if (ballLeft > padRight) {
- return false;
- }
- // We checked against all negative cases
- // A collision has probably occured
- return true;
- }
- //Restarts Game
- void newGame() {
- x = int(random(50, width - 50));
- y = 50;
- xSpeed = 5;
- ySpeed = 5;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement