-
Notifications
You must be signed in to change notification settings - Fork 2
/
ArduinoInvaders20x4.ino
210 lines (182 loc) · 5.02 KB
/
ArduinoInvaders20x4.ino
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
//LCD SETUP
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//CHARACTER BYTES
byte canon[8] = {
0b00000,
0b00000,
0b00001,
0b00011,
0b11111,
0b00011,
0b00001,
0b00000
};
byte laser[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b01110,
0b00000,
0b00000,
0b00000
};
byte poof[8] = {
0b10101,
0b01110,
0b00100,
0b11000,
0b00011,
0b00100,
0b01110,
0b10101
};
byte invaderZero[8] = {
0b00110,
0b01000,
0b11111,
0b01110,
0b01110,
0b11111,
0b01000,
0b00110
};
byte invaderOne[8] = {
0b11000,
0b00100,
0b11111,
0b01110,
0b01110,
0b11111,
0b00100,
0b11000
};
byte invaderTwo[8] = {
0b00001,
0b00010,
0b01101,
0b11110,
0b11110,
0b01101,
0b00010,
0b00001
};
//INVADERS
const int invader[] = {4, 5, 6}; //these will be the 3 invaders (see CHARACTERS)
int number = 0; //number determines wich invader is used 0,1 or 2
int directionInvader = 0; //0 = left, 1 = right
//COLUMNS & LINES
int columnInvader = 0;
int lineInvader = 0;
int lineCanon = 0;
int columnLaser = 19; //the laser begins at the last column
int lineLaser = 0;
//OTHER INTS
int counter = 0; //count how many times the void loop has been completed
int level = 0; //how many invaders you have hit
int difficulty = 3; //the lower this number, the faster the invaders will go
//BUTTONS
const int left = A5;
const int right = A4;
void setup() {
//CHARACTERS
lcd.createChar(1, canon);
lcd.createChar(2, laser);
lcd.createChar(3, poof);
lcd.createChar(4, invaderZero);
lcd.createChar(5, invaderOne);
lcd.createChar(6, invaderTwo);
//STARTING LCD
lcd.begin(20, 4);
}
void loop() {
//GOING LEFT/RIGHT
int leftValue = analogRead(left); //I used analog inputs to make it easier to replace the buttons with something like a potmeter for going left/right
int rightValue = analogRead(right);
if(leftValue > 511 && rightValue < 511 && lineCanon != 0){ //left (left button pressed)
--lineCanon;
}
if(leftValue < 511 && rightValue > 511 && lineCanon != 3){ //right (right button pressed)
++lineCanon;
}
//SHOOTING
if(leftValue > 511 && rightValue > 511 && columnLaser == 19){ //shooting (both buttons pressed)
lineLaser = lineCanon; //set laser on the correct line
}
if((leftValue > 511 && rightValue > 511) || columnLaser != 19){
--columnLaser;
lcd.setCursor(columnLaser, lineLaser);
lcd.write(2); //move and write laser
}
if(columnLaser == 0){
columnLaser = 19; //reset laser
}
//ANIMATION
lcd.setCursor(19, lineCanon);
lcd.write(1); //write canon
lcd.setCursor(columnInvader, lineInvader);
lcd.write(invader[number]); //write invader
delay(200);
lcd.clear(); //delay & clear
if(counter == difficulty){
counter = 0; //reset counter
if(directionInvader == 0 && lineInvader == 3){
directionInvader = 1;
--lineInvader;
++columnInvader; //move invader down & change direction when invader is all the way at the left
}
else if(directionInvader == 1 && lineInvader == 0){
directionInvader = 0;
++lineInvader;
++columnInvader; //move invader down & change direction when invader is all the way at the right
}
else if(directionInvader == 0){
++lineInvader; //move invader left
}
else if(directionInvader == 1){
--lineInvader; //move invader right
}
}
//HITTING
if(columnLaser == columnInvader && lineLaser == lineInvader && columnLaser != 19){
lcd.setCursor(columnLaser, lineInvader);
lcd.write(3); //write poof
columnLaser = 19; //reset laser
columnInvader = 0;
lineInvader = 0; //reset invader
++level; //levelup
++number; //change invader
if(number == 3) {
number = 0; //reset number
}
delay(250); //short winners break
}
//GAME OVER
if(columnInvader == 19){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Game Over");
lcd.setCursor(0,2);
lcd.print("Score: ");
lcd.print(level); //game over screen with score
delay(5000); //wait 5 sec before restarting
columnInvader = 0;
lineInvader = 0;
counter = -1;
columnLaser = 19;
level = 0;
difficulty = 3; //reset eveything
}
//INCREASING DIFFICULTY
if(level == 10 && difficulty == 3){
--difficulty; //at level 10 the invaders will move faster
counter = 0; //reset counter
}
//COUNTING
++counter; //counter for animation
}
/*
Made by Thijs van Beers
Latest version: 27-5-2014
*/