VGA Game
Dependencies: 4DGL LSM9DS0 mbed
main.cpp@0:93d2929d7b4c, 2015-10-19 (annotated)
- Committer:
- fengspot
- Date:
- Mon Oct 19 18:45:37 2015 +0000
- Revision:
- 0:93d2929d7b4c
uVGA Game Code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fengspot | 0:93d2929d7b4c | 1 | // |
fengspot | 0:93d2929d7b4c | 2 | // TFT_4DGL is a class to drive 4D Systems TFT touch screens |
fengspot | 0:93d2929d7b4c | 3 | // |
fengspot | 0:93d2929d7b4c | 4 | // Copyright (C) <2010> Stephane ROCHON <stephane.rochon at free.fr> |
fengspot | 0:93d2929d7b4c | 5 | // |
fengspot | 0:93d2929d7b4c | 6 | // TFT_4DGL is free software: you can redistribute it and/or modify |
fengspot | 0:93d2929d7b4c | 7 | // it under the terms of the GNU General Public License as published by |
fengspot | 0:93d2929d7b4c | 8 | // the Free Software Foundation, either version 3 of the License, or |
fengspot | 0:93d2929d7b4c | 9 | // (at your option) any later version. |
fengspot | 0:93d2929d7b4c | 10 | // |
fengspot | 0:93d2929d7b4c | 11 | // TFT_4DGL is distributed in the hope that it will be useful, |
fengspot | 0:93d2929d7b4c | 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
fengspot | 0:93d2929d7b4c | 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
fengspot | 0:93d2929d7b4c | 14 | // GNU General Public License for more details. |
fengspot | 0:93d2929d7b4c | 15 | // |
fengspot | 0:93d2929d7b4c | 16 | // You should have received a copy of the GNU General Public License |
fengspot | 0:93d2929d7b4c | 17 | // along with TFT_4DGL. If not, see <http://www.gnu.org/licenses/>. |
fengspot | 0:93d2929d7b4c | 18 | |
fengspot | 0:93d2929d7b4c | 19 | #include "mbed.h" |
fengspot | 0:93d2929d7b4c | 20 | #include "LSM9DS0.h" |
fengspot | 0:93d2929d7b4c | 21 | #include <stdio.h> |
fengspot | 0:93d2929d7b4c | 22 | #include <stdlib.h> |
fengspot | 0:93d2929d7b4c | 23 | #include <time.h> |
fengspot | 0:93d2929d7b4c | 24 | #include <string> |
fengspot | 0:93d2929d7b4c | 25 | #include "TFT_4DGL.h" |
fengspot | 0:93d2929d7b4c | 26 | #include <iostream> |
fengspot | 0:93d2929d7b4c | 27 | |
fengspot | 0:93d2929d7b4c | 28 | // overwrite 4DGL library screen size settings in TFT_4DGL.h |
fengspot | 0:93d2929d7b4c | 29 | #define SIZE_X 479 |
fengspot | 0:93d2929d7b4c | 30 | #define SIZE_Y 639 |
fengspot | 0:93d2929d7b4c | 31 | // |
fengspot | 0:93d2929d7b4c | 32 | |
fengspot | 0:93d2929d7b4c | 33 | // SDO_XM and SDO_G are pulled up, so our addresses are: |
fengspot | 0:93d2929d7b4c | 34 | #define LSM9DS0_XM_ADDR 0x1D // Would be 0x1E if SDO_XM is LOW |
fengspot | 0:93d2929d7b4c | 35 | #define LSM9DS0_G_ADDR 0x6B // Would be 0x6A if SDO_G is LOW |
fengspot | 0:93d2929d7b4c | 36 | |
fengspot | 0:93d2929d7b4c | 37 | #define REFRESH_TIME_MS 200 |
fengspot | 0:93d2929d7b4c | 38 | |
fengspot | 0:93d2929d7b4c | 39 | TFT_4DGL lcd(p13,p14,p11); // serial tx, serial rx, reset pin; |
fengspot | 0:93d2929d7b4c | 40 | LSM9DS0 imu(p9, p10, LSM9DS0_G_ADDR, LSM9DS0_XM_ADDR); |
fengspot | 0:93d2929d7b4c | 41 | |
fengspot | 0:93d2929d7b4c | 42 | class Brick { |
fengspot | 0:93d2929d7b4c | 43 | public: |
fengspot | 0:93d2929d7b4c | 44 | int x; |
fengspot | 0:93d2929d7b4c | 45 | int y; |
fengspot | 0:93d2929d7b4c | 46 | int color; |
fengspot | 0:93d2929d7b4c | 47 | int width; |
fengspot | 0:93d2929d7b4c | 48 | int height; |
fengspot | 0:93d2929d7b4c | 49 | bool exist; |
fengspot | 0:93d2929d7b4c | 50 | |
fengspot | 0:93d2929d7b4c | 51 | Brick(){ |
fengspot | 0:93d2929d7b4c | 52 | width = 10 * 2; |
fengspot | 0:93d2929d7b4c | 53 | height = 5 * 2; |
fengspot | 0:93d2929d7b4c | 54 | exist = true; |
fengspot | 0:93d2929d7b4c | 55 | } |
fengspot | 0:93d2929d7b4c | 56 | |
fengspot | 0:93d2929d7b4c | 57 | void set(int x, int y){ |
fengspot | 0:93d2929d7b4c | 58 | this->x = x; |
fengspot | 0:93d2929d7b4c | 59 | this->y = y; |
fengspot | 0:93d2929d7b4c | 60 | } |
fengspot | 0:93d2929d7b4c | 61 | }; |
fengspot | 0:93d2929d7b4c | 62 | |
fengspot | 0:93d2929d7b4c | 63 | int main() { |
fengspot | 0:93d2929d7b4c | 64 | |
fengspot | 0:93d2929d7b4c | 65 | //Initial setup |
fengspot | 0:93d2929d7b4c | 66 | uint16_t status = imu.begin(); |
fengspot | 0:93d2929d7b4c | 67 | lcd.baudrate(128000); |
fengspot | 0:93d2929d7b4c | 68 | // added - Set Display to 640 by 480 mode |
fengspot | 0:93d2929d7b4c | 69 | lcd.display_control(0x0c, 0x01); |
fengspot | 0:93d2929d7b4c | 70 | lcd.background_color(0); |
fengspot | 0:93d2929d7b4c | 71 | lcd.cls(); |
fengspot | 0:93d2929d7b4c | 72 | |
fengspot | 0:93d2929d7b4c | 73 | int score = 0; |
fengspot | 0:93d2929d7b4c | 74 | Brick* wall[32]; |
fengspot | 0:93d2929d7b4c | 75 | srand(time(0)); |
fengspot | 0:93d2929d7b4c | 76 | for(int i = 0; i < 8; i ++){ |
fengspot | 0:93d2929d7b4c | 77 | for( int j = 0; j < 4; j++){ |
fengspot | 0:93d2929d7b4c | 78 | Brick * brick = new Brick(); |
fengspot | 0:93d2929d7b4c | 79 | brick->set(5 + i * brick->width, 20 + j * brick->height); |
fengspot | 0:93d2929d7b4c | 80 | int c = 0; |
fengspot | 0:93d2929d7b4c | 81 | int num = rand() % 8 + 1; |
fengspot | 0:93d2929d7b4c | 82 | switch (num) { |
fengspot | 0:93d2929d7b4c | 83 | case 1: |
fengspot | 0:93d2929d7b4c | 84 | c = 0xff0000; |
fengspot | 0:93d2929d7b4c | 85 | break; |
fengspot | 0:93d2929d7b4c | 86 | case 2: |
fengspot | 0:93d2929d7b4c | 87 | c = 0xffff00; |
fengspot | 0:93d2929d7b4c | 88 | break; |
fengspot | 0:93d2929d7b4c | 89 | case 3: |
fengspot | 0:93d2929d7b4c | 90 | c = 0x00FF00; |
fengspot | 0:93d2929d7b4c | 91 | break; |
fengspot | 0:93d2929d7b4c | 92 | case 4: |
fengspot | 0:93d2929d7b4c | 93 | c = 0x0000FF; |
fengspot | 0:93d2929d7b4c | 94 | break; |
fengspot | 0:93d2929d7b4c | 95 | case 5: |
fengspot | 0:93d2929d7b4c | 96 | c = 0xFFC0CB; |
fengspot | 0:93d2929d7b4c | 97 | break; |
fengspot | 0:93d2929d7b4c | 98 | case 6: |
fengspot | 0:93d2929d7b4c | 99 | c = 0xA52A2A; |
fengspot | 0:93d2929d7b4c | 100 | break; |
fengspot | 0:93d2929d7b4c | 101 | case 7: |
fengspot | 0:93d2929d7b4c | 102 | c = 0xd3d3d3; |
fengspot | 0:93d2929d7b4c | 103 | break; |
fengspot | 0:93d2929d7b4c | 104 | case 8: |
fengspot | 0:93d2929d7b4c | 105 | c = 0x800080; |
fengspot | 0:93d2929d7b4c | 106 | break; |
fengspot | 0:93d2929d7b4c | 107 | default: |
fengspot | 0:93d2929d7b4c | 108 | c = 0x445566; |
fengspot | 0:93d2929d7b4c | 109 | } |
fengspot | 0:93d2929d7b4c | 110 | brick->color = c; |
fengspot | 0:93d2929d7b4c | 111 | wall[i*4 + j] = brick; |
fengspot | 0:93d2929d7b4c | 112 | } |
fengspot | 0:93d2929d7b4c | 113 | } |
fengspot | 0:93d2929d7b4c | 114 | |
fengspot | 0:93d2929d7b4c | 115 | float player_x = 60.0 * 2; |
fengspot | 0:93d2929d7b4c | 116 | float player_y = 120.0 * 2; |
fengspot | 0:93d2929d7b4c | 117 | float p_width = 30.0; |
fengspot | 0:93d2929d7b4c | 118 | |
fengspot | 0:93d2929d7b4c | 119 | float ball_x = 23.0 * 2; |
fengspot | 0:93d2929d7b4c | 120 | float ball_y = 49.0 * 2; |
fengspot | 0:93d2929d7b4c | 121 | float ball_vx = 15.0; |
fengspot | 0:93d2929d7b4c | 122 | float ball_vy = 15.0; |
fengspot | 0:93d2929d7b4c | 123 | float ball_r = 2.0; |
fengspot | 0:93d2929d7b4c | 124 | float width = 110.0 * 2; |
fengspot | 0:93d2929d7b4c | 125 | float height = 120.0 * 2; |
fengspot | 0:93d2929d7b4c | 126 | bool end = false; |
fengspot | 0:93d2929d7b4c | 127 | bool win = false; |
fengspot | 0:93d2929d7b4c | 128 | |
fengspot | 0:93d2929d7b4c | 129 | while (!end && !win) |
fengspot | 0:93d2929d7b4c | 130 | { |
fengspot | 0:93d2929d7b4c | 131 | lcd.cls(); |
fengspot | 0:93d2929d7b4c | 132 | imu.readMag(); |
fengspot | 0:93d2929d7b4c | 133 | imu.readAccel(); |
fengspot | 0:93d2929d7b4c | 134 | |
fengspot | 0:93d2929d7b4c | 135 | for(int i = 0; i < 32; i++ ){ |
fengspot | 0:93d2929d7b4c | 136 | if(wall[i]->exist){ |
fengspot | 0:93d2929d7b4c | 137 | lcd.rectangle(wall[i]->x, wall[i]->y, wall[i]->x + wall[i]->width, wall[i]->y + wall[i]->height, wall[i]->color); |
fengspot | 0:93d2929d7b4c | 138 | } |
fengspot | 0:93d2929d7b4c | 139 | } |
fengspot | 0:93d2929d7b4c | 140 | |
fengspot | 0:93d2929d7b4c | 141 | lcd.circle(ball_x, ball_y, ball_r, RED); |
fengspot | 0:93d2929d7b4c | 142 | lcd.line(player_x, player_y, player_x + p_width, player_y, BLUE); |
fengspot | 0:93d2929d7b4c | 143 | |
fengspot | 0:93d2929d7b4c | 144 | ball_x += ball_vx * 0.7; |
fengspot | 0:93d2929d7b4c | 145 | ball_y += ball_vy * 0.7; |
fengspot | 0:93d2929d7b4c | 146 | player_x = player_x - imu.ax_raw * 0.003; |
fengspot | 0:93d2929d7b4c | 147 | |
fengspot | 0:93d2929d7b4c | 148 | if(player_x + p_width > width){ |
fengspot | 0:93d2929d7b4c | 149 | player_x = width - p_width; |
fengspot | 0:93d2929d7b4c | 150 | } |
fengspot | 0:93d2929d7b4c | 151 | |
fengspot | 0:93d2929d7b4c | 152 | if(player_x < 0){ |
fengspot | 0:93d2929d7b4c | 153 | player_x = 0; |
fengspot | 0:93d2929d7b4c | 154 | } |
fengspot | 0:93d2929d7b4c | 155 | |
fengspot | 0:93d2929d7b4c | 156 | if(ball_x + ball_r > width){ |
fengspot | 0:93d2929d7b4c | 157 | ball_x = width - ball_r; |
fengspot | 0:93d2929d7b4c | 158 | ball_vx *= -1; |
fengspot | 0:93d2929d7b4c | 159 | } |
fengspot | 0:93d2929d7b4c | 160 | |
fengspot | 0:93d2929d7b4c | 161 | if(ball_x - ball_r < 0){ |
fengspot | 0:93d2929d7b4c | 162 | ball_x = ball_r; |
fengspot | 0:93d2929d7b4c | 163 | ball_vx *= -1; |
fengspot | 0:93d2929d7b4c | 164 | } |
fengspot | 0:93d2929d7b4c | 165 | |
fengspot | 0:93d2929d7b4c | 166 | if(ball_y + ball_r > height){ |
fengspot | 0:93d2929d7b4c | 167 | if(ball_x < player_x || ball_x > player_x + p_width){ |
fengspot | 0:93d2929d7b4c | 168 | end = true; |
fengspot | 0:93d2929d7b4c | 169 | } else { |
fengspot | 0:93d2929d7b4c | 170 | ball_y = height - ball_r; |
fengspot | 0:93d2929d7b4c | 171 | ball_vy *= -1; |
fengspot | 0:93d2929d7b4c | 172 | } |
fengspot | 0:93d2929d7b4c | 173 | } |
fengspot | 0:93d2929d7b4c | 174 | |
fengspot | 0:93d2929d7b4c | 175 | if(ball_y - ball_r < 0){ |
fengspot | 0:93d2929d7b4c | 176 | ball_y = ball_r; |
fengspot | 0:93d2929d7b4c | 177 | ball_vy *= -1; |
fengspot | 0:93d2929d7b4c | 178 | } |
fengspot | 0:93d2929d7b4c | 179 | |
fengspot | 0:93d2929d7b4c | 180 | for(int i = 0; i < 32; i++ ){ |
fengspot | 0:93d2929d7b4c | 181 | if(wall[i]->exist){ |
fengspot | 0:93d2929d7b4c | 182 | if (!(ball_x > wall[i]->x + wall[i]->width || |
fengspot | 0:93d2929d7b4c | 183 | ball_x + ball_r < wall[i]->x || |
fengspot | 0:93d2929d7b4c | 184 | ball_y > wall[i]->y + wall[i]->height || |
fengspot | 0:93d2929d7b4c | 185 | ball_y + ball_r < wall[i]->y)){ |
fengspot | 0:93d2929d7b4c | 186 | wall[i]->exist = false; |
fengspot | 0:93d2929d7b4c | 187 | score++; |
fengspot | 0:93d2929d7b4c | 188 | } |
fengspot | 0:93d2929d7b4c | 189 | } |
fengspot | 0:93d2929d7b4c | 190 | } |
fengspot | 0:93d2929d7b4c | 191 | if(score == 32){ |
fengspot | 0:93d2929d7b4c | 192 | win = true; |
fengspot | 0:93d2929d7b4c | 193 | } |
fengspot | 0:93d2929d7b4c | 194 | wait_ms(REFRESH_TIME_MS); |
fengspot | 0:93d2929d7b4c | 195 | } |
fengspot | 0:93d2929d7b4c | 196 | lcd.cls(); |
fengspot | 0:93d2929d7b4c | 197 | |
fengspot | 0:93d2929d7b4c | 198 | if(win){ |
fengspot | 0:93d2929d7b4c | 199 | lcd.text_string("You Win!!!", 20, 60, FONT_8X8, WHITE); |
fengspot | 0:93d2929d7b4c | 200 | } else { |
fengspot | 0:93d2929d7b4c | 201 | lcd.text_string("Game Over...", 20, 60, FONT_8X8, WHITE); |
fengspot | 0:93d2929d7b4c | 202 | } |
fengspot | 0:93d2929d7b4c | 203 | |
fengspot | 0:93d2929d7b4c | 204 | } |