[136] | 1 | /*
|
---|
| 2 |
|
---|
| 3 | Esplora TFT Pong
|
---|
| 4 |
|
---|
| 5 | This example for the Esplora with an Arduino TFT screen reads
|
---|
| 6 | the value of the joystick to move a rectangular platform
|
---|
| 7 | on the x and y axes. The platform can intersect with a ball
|
---|
| 8 | causing it to bounce. The Esplora's slider adjusts the speed
|
---|
| 9 | of the ball.
|
---|
| 10 |
|
---|
| 11 | This example code is in the public domain.
|
---|
| 12 |
|
---|
| 13 | Created by Tom Igoe December 2012
|
---|
| 14 | Modified 15 April 2013 by Scott Fitzgerald
|
---|
| 15 |
|
---|
| 16 | http://arduino.cc/en/Tutorial/EsploraTFTPong
|
---|
| 17 |
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | #include <Esplora.h>
|
---|
| 21 | #include <TFT.h> // Arduino LCD library
|
---|
| 22 | #include <SPI.h>
|
---|
| 23 |
|
---|
| 24 | // variables for the position of the ball and paddle
|
---|
| 25 | int paddleX = 0;
|
---|
| 26 | int paddleY = 0;
|
---|
| 27 | int oldPaddleX, oldPaddleY;
|
---|
| 28 | int ballDirectionX = 1;
|
---|
| 29 | int ballDirectionY = 1;
|
---|
| 30 |
|
---|
| 31 | int ballX, ballY, oldBallX, oldBallY;
|
---|
| 32 |
|
---|
| 33 | void setup() {
|
---|
| 34 |
|
---|
| 35 | Serial.begin(9600);
|
---|
| 36 |
|
---|
| 37 | // initialize the display
|
---|
| 38 | EsploraTFT.begin();
|
---|
| 39 | // set the background the black
|
---|
| 40 | EsploraTFT.background(0, 0, 0);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | void loop() {
|
---|
| 44 | // save the width and height of the screen
|
---|
| 45 | int myWidth = EsploraTFT.width();
|
---|
| 46 | int myHeight = EsploraTFT.height();
|
---|
| 47 |
|
---|
| 48 | // map the paddle's location to the joystick's position
|
---|
| 49 | paddleX = map(Esplora.readJoystickX(), 512, -512, 0, myWidth) - 20 / 2;
|
---|
| 50 | paddleY = map(Esplora.readJoystickY(), -512, 512, 0, myHeight) - 5 / 2;
|
---|
| 51 | Serial.print(paddleX);
|
---|
| 52 | Serial.print(" ");
|
---|
| 53 | Serial.println(paddleY);
|
---|
| 54 |
|
---|
| 55 | // set the fill color to black and erase the previous
|
---|
| 56 | // position of the paddle if different from present
|
---|
| 57 | EsploraTFT.fill(0, 0, 0);
|
---|
| 58 |
|
---|
| 59 | if (oldPaddleX != paddleX || oldPaddleY != paddleY) {
|
---|
| 60 | EsploraTFT.rect(oldPaddleX, oldPaddleY, 20, 5);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | // draw the paddle on screen, save the current position
|
---|
| 64 | // as the previous.
|
---|
| 65 | EsploraTFT.fill(255, 255, 255);
|
---|
| 66 | EsploraTFT.rect(paddleX, paddleY, 20, 5);
|
---|
| 67 | oldPaddleX = paddleX;
|
---|
| 68 | oldPaddleY = paddleY;
|
---|
| 69 |
|
---|
| 70 | // read the slider to determinde the speed of the ball
|
---|
| 71 | int ballSpeed = map(Esplora.readSlider(), 0, 1023, 0, 80) + 1;
|
---|
| 72 | if (millis() % ballSpeed < 2) {
|
---|
| 73 | moveBall();
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | // this function determines the ball's position on screen
|
---|
| 79 | void moveBall() {
|
---|
| 80 | // if the ball goes offscreen, reverse the direction:
|
---|
| 81 | if (ballX > EsploraTFT.width() || ballX < 0) {
|
---|
| 82 | ballDirectionX = -ballDirectionX;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | if (ballY > EsploraTFT.height() || ballY < 0) {
|
---|
| 86 | ballDirectionY = -ballDirectionY;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | // check if the ball and the paddle occupy the same space on screen
|
---|
| 90 | if (inPaddle(ballX, ballY, paddleX, paddleY, 20, 5)) {
|
---|
| 91 | ballDirectionY = -ballDirectionY;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | // update the ball's position
|
---|
| 95 | ballX += ballDirectionX;
|
---|
| 96 | ballY += ballDirectionY;
|
---|
| 97 |
|
---|
| 98 | // erase the ball's previous position
|
---|
| 99 | EsploraTFT.fill(0, 0, 0);
|
---|
| 100 |
|
---|
| 101 | if (oldBallX != ballX || oldBallY != ballY) {
|
---|
| 102 | EsploraTFT.rect(oldBallX, oldBallY, 5, 5);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | // draw the ball's current position
|
---|
| 106 | EsploraTFT.fill(255, 255, 255);
|
---|
| 107 |
|
---|
| 108 | EsploraTFT.rect(ballX, ballY, 5, 5);
|
---|
| 109 |
|
---|
| 110 | oldBallX = ballX;
|
---|
| 111 | oldBallY = ballY;
|
---|
| 112 |
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | // this function checks the position of the ball
|
---|
| 116 | // to see if it intersects with the paddle
|
---|
| 117 | boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) {
|
---|
| 118 | boolean result = false;
|
---|
| 119 |
|
---|
| 120 | if ((x >= rectX && x <= (rectX + rectWidth)) &&
|
---|
| 121 | (y >= rectY && y <= (rectY + rectHeight))) {
|
---|
| 122 | result = true;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | return result;
|
---|
| 126 | }
|
---|