source: rtos_arduino/trunk/arduino_lib/libraries/TFT/examples/Arduino/TFTPong/TFTPong.ino@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

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