-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvga_game_by_dhruv.ino
173 lines (145 loc) · 3.42 KB
/
vga_game_by_dhruv.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
#define nop __asm__("nop \n")
#define SCREEN_W 40
#define SCREEN_H 30
char PXL_DATA [SCREEN_H][SCREEN_W];
int lineCounter;
char skipLine;
char PXL_OUT;
char dy;
char enemyX;
char enemyY;
float score;
ISR(TIMER1_OVF_vect)
{lineCounter = 0;}
ISR(TIMER2_OVF_vect)
{
sei();
__asm__("sleep \n");
}
ISR(TIMER2_COMPB_vect)
{
register char columnCounter;
register char* rowPtr;
switch (PXL_OUT){
case 1:
rowPtr = &(PXL_DATA[(lineCounter - 35)>>4][0]);
columnCounter = SCREEN_W;
while(columnCounter--){
PORTD = (*(rowPtr++)<<PD2);
nop;
}
nop;
nop;
nop;
PORTD = 0;
break;
}
lineCounter++;
skipLine = !skipLine;
if ( skipLine && (lineCounter >= 35) && (lineCounter < 515) ){
PXL_OUT = 1;
}
else{
PXL_OUT = 0;
}
}
void setup() {
cli(); //DISABLE INTERRUPTS
PORTD = 0;
pinMode(10,OUTPUT);
pinMode(3,OUTPUT);
pinMode(2,OUTPUT);
pinMode(A0,INPUT);
pinMode(12, INPUT);
TCCR1A = 0b00100011; //PIN D10 OUTPUT, FAST PWM MODE
TCCR1B = 0b00011101; //FAST PWM, PRESCALER OF 1024
TIMSK1 = 0b00000001; //OVERFLOW INTERRUPT ENABLE
TIFR1 = 0b00000001; //CLEAR OVERFLOW FLAG
//TCNT1 = 0; //rst t1
TCCR2A = 0b00100011; //PIN D3 OUTPUT, FAST PWM MODE
TCCR2B = 0b00001010; //FAST PWM, PRESCALER OF 8
TIMSK2 = 0b00000101; //OVERFLOW INTERRUPT ENABLE
TIFR2 = 0b00000101; //CLEAR OVERFLOW FLAG
//TCNT2 = 0; //rst t2
OCR1A = 259; //SET FREQ OF T1
OCR1B = 0; //VSYNC PULSE DURATION
OCR2A = 63; //SET FREQ OF T2
OCR2B = 7; //HSYNC PULSE DURATUIN
SMCR = bit(SE);
memset(PXL_DATA, 0, sizeof(PXL_DATA));
lineCounter = 0;
enemyX =random(1,36);
enemyY =random(3,25);
score=0;
sei();
}
void gameCode(){
}
void loop() {
char col = (analogRead(A0)/29);
bool shoot = digitalRead(12);
bool bulletFired;
char bulletX;
char bulletY;
if (shoot == 1){
bulletFired = 1;
dy = 0.0;
bulletX = col;
}
if ((bulletFired==1)&&(dy<=27)){
bulletY = 27-dy;
dy = dy + 1;
}
if ( (bulletY<=0)&&(dy>=27) ){
bulletFired = 0;
bulletY=31;
}
if ( (bulletX==(enemyX-1))&&(bulletY==enemyY) ){
enemyX=random(1,36);
enemyY=random(3,25);
bulletFired = 0;
dy = 27;
score = score + 4;
if (score>=37){
score=0;
}
}
char r, c;
for (r = 0; r < SCREEN_H; r++) {
for (c = 0; c < SCREEN_W; c++) {
if ((r >= 28) &&
(r < 28 + 1) &&
(c >= col) &&
(c < col + 3)) {
PXL_DATA[r][c] = 0;
}
else if ((r >= 27) &&
(r < 27 + 1) &&
(c >= col+1) &&
(c < col + 2)) {
PXL_DATA[r][c] = 0;
}
else if ((bulletFired==1)&&(r >= bulletY) &&
(r < bulletY + 1) &&
(c >= bulletX+1) &&
(c < bulletX + 2)) {
PXL_DATA[r][c] = 0;
}
else if ((r >= enemyY) &&
(r < enemyY + 1) &&
(c >= enemyX) &&
(c < enemyX + 1)) {
PXL_DATA[r][c] = 0;
}
else if ((r >= 0) &&
(r < 0 + 1) &&
(c >= 0) &&
(c < 0 + score)) {
PXL_DATA[r][c] = 0;
}
else {
PXL_DATA[r][c] = 1;
}
}
}
}