aboutsummaryrefslogtreecommitdiff
path: root/arduino_sketch/arcerino/arcerino.ino
blob: 80f08a3367f5d93ecf258f6b4c214c02dbe33f8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#define PORTC3 0
#define PORTC2 0

#include <Wire.h>
#include <wiinunchuck.h>

#if 0
#define LCD
#endif

#define JOY_MAX 100

#ifdef LCD

#include "U8glib.h"

U8GLIB_TLS8204_84X48 u8g(15, 16, 13, 4, 5);             // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 4, Reset = 5

#define LCD_MAX 24
#define LCD_0X 42
#define LCD_0Y 24

#define TOP_TEXT_Y 8
#define MIDDLE_TEXT_Y 24
#define BOTTOM_TEXT_Y 46

#define LOAD_TEXT_X 30

#define FONT_SMALL u8g_font_5x7
#define FONT_BIG u8g_font_9x15

#else /* TV */

#include <TVout.h>
#include <fontALL.h>

TVout TV;

#define LCD_MAX 48
#define LCD_0X 60
#define LCD_0Y 48

#define TOP_TEXT_Y 8
#define MIDDLE_TEXT_Y 48
#define BOTTOM_TEXT_Y 96

#define LOAD_TEXT_X 60

#define FONT_SMALL font6x8
#define FONT_BIG font8x8

#endif /* LCD/TV */


char x,y=0;
char drift_x,drift_y=0;

char score=0;
char arrows=10;
bool play=true;

#ifdef LCD

void output_init() {
    u8g.setColorIndex(1);
    u8g.setContrast(192);
}

void draw_board() {
    u8g.setFont(FONT_SMALL);
    u8g.setPrintPos(0,TOP_TEXT_Y);
    u8g.print(score,DEC);
    u8g.setPrintPos(0,BOTTOM_TEXT_Y);
    u8g.print("->");
    u8g.print(arrows,DEC);
    u8g.drawCircle(LCD_0X,LCD_0Y,LCD_MAX);
    u8g.drawCircle(LCD_0X,LCD_0Y,LCD_MAX*2/3);
    u8g.drawCircle(LCD_0X,LCD_0Y,LCD_MAX/3);
    u8g.drawCircle(LCD_0X,LCD_0Y,LCD_MAX/6);
}

void draw_game() {
    u8g.firstPage();
    do {
        draw_board();
        u8g.drawVLine(x+LCD_0X,y-4+LCD_0Y,9);
        u8g.drawHLine(x-4+LCD_0X,y+LCD_0Y,9);
    } while(u8g.nextPage());
    delay(100);
}

void draw_score() {
    u8g.firstPage();
    do {
        draw_board();
        u8g.drawDisc(x+LCD_0X,y+LCD_0Y,3);
    } while(u8g.nextPage());
    delay(3000);
}

void draw_title() {
    u8g.firstPage();
    do {
        u8g.setFont(FONT_BIG);
        u8g.setPrintPos(0,MIDDLE_TEXT_Y);
        u8g.print("Arcerino");
        u8g.setFont(FONT_SMALL);
        u8g.setPrintPos(LOAD_TEXT_X,BOTTOM_TEXT_Y);
        u8g.print("Loading...");
    } while(u8g.nextPage());
    delay(4000);
}

void draw_wait() {
    u8g.firstPage();
    do {
        u8g.setFont(FONT_SMALL);
        u8g.setPrintPos(0,TOP_TEXT_Y);
        u8g.print("Your score: ");
        u8g.print(score,DEC);
        u8g.setPrintPos(0,BOTTOM_TEXT_Y);
        u8g.print("Press Z to start");
        u8g.setPrintPos(0,MIDDLE_TEXT_Y);
        u8g.print("Rank: ");
        u8g.setFont(FONT_BIG);
        if (score >= 100) {
            u8g.print("A++");
        } else if (score >= 90) {
            u8g.print("A");
        } else if (score >= 80) {
            u8g.print("B");
        } else if (score >= 60) {
            u8g.print("C");
        } else if (score >= 30) {
            u8g.print("D");
        } else {
            u8g.print("E");
        }
    } while(u8g.nextPage());
    delay(100);
}

void draw_clean() {
}

#else /* TV */

void output_init() {
    nunchuk_setpowerpins();
    TV.begin(PAL,120,96);
}

void draw_board() {
    TV.select_font(FONT_SMALL);
    TV.print(0,TOP_TEXT_Y,score,DEC);
    TV.print(0,BOTTOM_TEXT_Y,"->");
    TV.print(arrows,DEC);
    TV.draw_circle(LCD_0X,LCD_0Y,LCD_MAX,WHITE);
    TV.draw_circle(LCD_0X,LCD_0Y,LCD_MAX*2/3,WHITE);
    TV.draw_circle(LCD_0X,LCD_0Y,LCD_MAX/3,WHITE);
    TV.draw_circle(LCD_0X,LCD_0Y,LCD_MAX/6,WHITE);
}

void draw_game() {
    draw_board();
    TV.draw_line(x+LCD_0X,y-4+LCD_0Y,x+LCD_0X,y+4+LCD_0Y,INVERT);
    TV.draw_line(x-4+LCD_0X,y+LCD_0Y,x+4+LCD_0X,y+LCD_0Y,INVERT);
    TV.delay(100);
    TV.draw_line(x+LCD_0X,y-4+LCD_0Y,x+LCD_0X,y+4+LCD_0Y,INVERT);
    TV.draw_line(x-4+LCD_0X,y+LCD_0Y,x+4+LCD_0X,y+LCD_0Y,INVERT);
}

void draw_score() {
    TV.clear_screen();
    draw_board();
    TV.draw_circle(x+LCD_0X,y+LCD_0Y,6,WHITE,WHITE);
    TV.delay(3000);
    TV.clear_screen();
}

void draw_title() {
    TV.clear_screen();
    TV.select_font(FONT_BIG);
    TV.print(0,MIDDLE_TEXT_Y,"Arcerino...");
    TV.select_font(FONT_SMALL);
    TV.print(LOAD_TEXT_X,BOTTOM_TEXT_Y,"Loading...");
    TV.delay(4000);
    TV.clear_screen();
}

void draw_wait() {
    TV.select_font(FONT_SMALL);
    TV.print(0,TOP_TEXT_Y,"Your score: ");
    TV.print(0,TOP_TEXT_Y+13,score,DEC);
    TV.print(0,BOTTOM_TEXT_Y,"Press Z to start");
    TV.print(0,MIDDLE_TEXT_Y,"Rank: ");
    TV.select_font(FONT_BIG);
        if (score >= 100) {
            TV.print("A++");
        } else if (score >= 90) {
            TV.print("A");
        } else if (score >= 80) {
            TV.print("B");
        } else if (score >= 60) {
            TV.print("C");
        } else if (score >= 30) {
            TV.print("D");
        } else {
            TV.print("E");
        }
    TV.delay(100);
}

void draw_clean() {
    TV.clear_screen();
}

#endif /* LCD/TV */

void update_position() {
    drift_x = drift_x + random(-1,2);
    drift_x = constrain(drift_x,-LCD_MAX,LCD_MAX);
    drift_y = drift_y + random(-1,2);
    drift_y = constrain(drift_y,-LCD_MAX,LCD_MAX);
    x = map(nunchuk_cjoy_x(),-JOY_MAX,JOY_MAX,-LCD_MAX*2,LCD_MAX*2) + drift_x;
    y = map(nunchuk_cjoy_y(),-JOY_MAX,JOY_MAX,LCD_MAX*2,-LCD_MAX*2) + drift_y;
}

char get_score() {
    int d = sqrt(pow(x,2) + pow(y,2)) / 4;
    switch(d) {
        case 0:
            return 10;
            break;
        case 1:
            return 6;
            break;
        case 2:
        case 3:
            return 3;
            break;
        case 4:
        case 5:
            return 1;
            break;
        default:
            return 0;
    }
}

void new_arrow() {
    arrows = arrows--;
    drift_x = random(-LCD_MAX/2,LCD_MAX/2) + random(-LCD_MAX/2,LCD_MAX/2);
    drift_y = random(-LCD_MAX/2,LCD_MAX/2) + random(-LCD_MAX/2,LCD_MAX/2);
}

void new_round() {
    arrows = 10;
    score = 0;
    draw_clean();
}

void setup() {
    output_init();
    nunchuk_init();
    nunchuk_get_data();
    draw_title();
    new_arrow();
    new_round();
}

void loop() {
    nunchuk_get_data();

    if (play) {
        if (nunchuk_zbutton()) {
            score = score + get_score(); 
            new_arrow();
            draw_score();
            if ( arrows <= 0 ) {
                play = false;
            }
        } else {
            update_position();
            draw_game();
        }
    } else {
        if (nunchuk_zbutton()) {
            new_round();
            play=true;
            delay(200);
        } else {
            draw_wait();
        }
    }
}