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
|
#define PORTC3 0
#define PORTC2 0
#include "U8glib.h"
#include <Wire.h>
#include <wiinunchuck.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 JOY_MAX 100
#define LCD_0X 42
#define LCD_0Y 24
char x,y=0;
char drift_x,drift_y=0;
char score=0;
void draw_game() {
u8g.setFont(u8g_font_5x7);
u8g.setPrintPos(0,8);
u8g.print(score,DEC);
u8g.drawCircle(42,24,24);
u8g.drawCircle(42,24,16);
u8g.drawCircle(42,24,8);
u8g.drawVLine(x+LCD_0X,y-4+LCD_0Y,9);
u8g.drawHLine(x-4+LCD_0X,y+LCD_0Y,9);
}
void draw_score() {
u8g.setFont(u8g_font_5x7);
u8g.setPrintPos(0,8);
u8g.print(score,DEC);
u8g.drawCircle(42,24,24);
u8g.drawCircle(42,24,16);
u8g.drawCircle(42,24,8);
u8g.drawDisc(x+LCD_0X,y+LCD_0Y,3);
}
void draw_title() {
u8g.setFont(u8g_font_9x15);
u8g.setPrintPos(0,24);
u8g.print("Arcerino");
u8g.setFont(u8g_font_5x7);
u8g.setPrintPos(30,46);
u8g.print("Loading...");
}
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,LCD_MAX) + drift_x;
y = map(nunchuk_cjoy_y(),-JOY_MAX,JOY_MAX,LCD_MAX,-LCD_MAX) + drift_y;
}
char get_score() {
int d = sqrt(pow(x,2) + pow(y,2)) / 8;
Serial.println(d,DEC);
switch(d) {
case 0:
return 10;
break;
case 1:
return 5;
break;
case 2:
return 1;
break;
default:
return 0;
}
}
void setup() {
nunchuk_init();
u8g.setColorIndex(1);
u8g.setContrast(192);
nunchuk_get_data();
u8g.firstPage();
do {
draw_title();
} while(u8g.nextPage());
delay(4000);
}
void loop() {
nunchuk_get_data();
if (nunchuk_zbutton()) {
score = score + get_score();
u8g.firstPage();
do {
draw_score();
} while(u8g.nextPage());
delay(3000);
} else {
update_position();
u8g.firstPage();
do {
draw_game();
} while(u8g.nextPage());
delay(100);
}
}
|