-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscalar.c
246 lines (201 loc) · 5.45 KB
/
scalar.c
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
//
// File: scalar.c
// Author: Sebastian Mihai
// Info: Not really a game, but a tech demo of programming the Vectrex.
// It shows how to use scaling, animation, collision detection.
//
// If you have any questions regarding the code, contact me
// via the method specified at http://www.sebastianmihai.com
// (operational in 2012), and I will try my best to help.
//
// Converted to Classics Coder and CMOC by Roger Boesch
//
#include <vectrex.h>
#pragma vx_copyright "2020"
#pragma vx_title_pos 0,-80
#pragma vx_title_size -8, 80
#pragma vx_title "g SCALAR"
#pragma vx_music vx_music_1
#define MAX_BRIGHTNESS (0x7f)
#define SCALE 120
#define DOT_BRIGHTNESS 5
#define ENEMY_SIZE 40
#define ANIMATION_FREQUENCY 30
#define ANIMATION_FRAMES 3
#define PLAYER_SPEED 2
#define BULLET_SPEED 10
static void line_to(uint8_t x, uint8_t y) {
intensity_a(MAX_BRIGHTNESS);
draw_line_d(y, x);
}
static void move_to(uint8_t x, uint8_t y) {
moveto_d(y, x);
}
static void draw_player(int8_t x) {
move_to(x, -120);
move_to(0, -30);
move_to(-5, 0);
line_to(5, 15);
line_to(5, -15);
line_to(-10, 0);
// back to middle and bottom of triangle
move_to(5, 0);
// and back to origin
move_to(-x, 120);
move_to(0, 30);
}
static void draw_bullet(int8_t x, int8_t y) {
move_to(x, y);
line_to(0, 5);
// back to origin
move_to(0, -5);
move_to(-x, -y);
}
static void draw_enemy(int8_t x, int8_t y, uint8_t frame) {
move_to(x, y);
set_scale(frame*2);
if (frame < ANIMATION_FREQUENCY) {
move_to(-ENEMY_SIZE/2, ENEMY_SIZE/2);
line_to(ENEMY_SIZE, 0);
line_to(0, -ENEMY_SIZE);
line_to(-ENEMY_SIZE, 0);
line_to(0, ENEMY_SIZE);
// back to center
move_to(ENEMY_SIZE/2, -ENEMY_SIZE/2);
}
else if ( frame < 2*ANIMATION_FREQUENCY ) {
line_to(-ENEMY_SIZE/2, ENEMY_SIZE/2);
line_to(ENEMY_SIZE, 0);
line_to(-ENEMY_SIZE/2, -ENEMY_SIZE/2);
line_to(ENEMY_SIZE/2, -ENEMY_SIZE/2);
line_to(-ENEMY_SIZE, 0);
line_to(ENEMY_SIZE/2, ENEMY_SIZE/2);
}
else {
move_to(-ENEMY_SIZE/2, ENEMY_SIZE/2);
line_to(ENEMY_SIZE/2, -ENEMY_SIZE/2);
line_to(ENEMY_SIZE/2, ENEMY_SIZE/2);
line_to(0, -ENEMY_SIZE);
line_to(-ENEMY_SIZE/2, ENEMY_SIZE/2);
line_to(-ENEMY_SIZE/2, -ENEMY_SIZE/2);
line_to(0, ENEMY_SIZE);
// back to center
move_to(ENEMY_SIZE/2, -ENEMY_SIZE/2);
}
set_scale(SCALE);
// back to origin
move_to( -x, -y );
}
static int game_loop() {
uint8_t i;
int8_t x; // player x
int8_t enemyX[5];
int8_t enemyY[5];
int8_t enemyAlive[5];
int8_t bulletX, bulletY;
uint8_t frame = ANIMATION_FREQUENCY;
int8_t frameDelta = 2;
uint8_t atLeastOneEnemyAlive;
x = 0;
bulletY = 127;
bulletX = x;
// enemy positions
enemyX[0] =-100; enemyX[1] =-50; enemyX[2] = 0; enemyX[3] = 50; enemyX[4] = 100;
enemyY[0] = 100; enemyY[1] = 80; enemyY[2] = 90;enemyY[3] = 80; enemyY[4] = 100;
// As terrible as this repetition is, I could not get simple loops to work
// It seems that during the later iterations in loops, the counter cannot be
// used to index into arrays... couldn't figure out why.
enemyAlive[0] = 1;
enemyAlive[1] = 1;
enemyAlive[2] = 1;
enemyAlive[3] = 1;
enemyAlive[4] = 1;
while (1) {
frame += frameDelta;
//set_dp_c8();
wait_recal();
intensity_a(MAX_BRIGHTNESS);
zero_beam();
set_scale(SCALE);
draw_player( x );
if (bulletY <= 119) {
draw_bullet(bulletX, bulletY);
bulletY+=BULLET_SPEED;
}
else {
bulletX = -125;
}
// did bullet hit an enemy?
for (i=0; i<5; i++) {
if (bulletY + 5 > enemyY[i] &&
bulletX > enemyX[i] - ENEMY_SIZE/3 &&
bulletX < enemyX[i] + ENEMY_SIZE/3) {
enemyAlive[i] = 0;
}
}
if (enemyAlive[0] == 1 ) draw_enemy(enemyX[0], enemyY[0], frame);
if (enemyAlive[1] == 1 ) draw_enemy(enemyX[1], enemyY[1], frame);
if (enemyAlive[2] == 1 ) draw_enemy(enemyX[2], enemyY[2], frame);
if (enemyAlive[3] == 1 ) draw_enemy(enemyX[3], enemyY[3], frame);
if (enemyAlive[4] == 1 ) draw_enemy(enemyX[4], enemyY[4], frame);
if (controller_joystick_1_x() > 0 && x < 127-PLAYER_SPEED) {
x += PLAYER_SPEED;
}
else if (controller_joystick_1_x()<0 && x > -127 + PLAYER_SPEED) {
x -= PLAYER_SPEED;
}
if (( controller_joystick_1_y() > 0 || controller_buttons_pressed()) && bulletY > 119) {
// fire a new bullet!
bulletY = -120;
bulletX = x;
}
// reset frame to reset animation
if (frame >= ANIMATION_FRAMES * ANIMATION_FREQUENCY ) {
frameDelta = -2;
}
else if (frame <= ANIMATION_FREQUENCY) {
frameDelta = 2;
}
// bring beam to centre again, and print messages
zero_beam();
print_str_c(127, -50, "SCALAR");
joy_digital();
read_btns();
// As terrible as this repetition is, I could not get simple loops to work
// It seems that during the later iterations in loops, the counter cannot be
// used to index into arrays... couldn't figure out why.
if (enemyAlive[0] == 0
&& enemyAlive[1] == 0
&& enemyAlive[2] == 0
&& enemyAlive[3] == 0
&& enemyAlive[4] == 0) {
return 0;
}
}
}
static int show_intro(void) {
while(1) {
wait_recal();
controller_enable_1_x();
controller_enable_1_y();
controller_check_joysticks();
move_to(0, 0);
intensity_a(MAX_BRIGHTNESS);
print_str_c(0, -90, "-- PRESS FIRE --");
joy_digital();
read_btns();
if (controller_buttons_pressed()) {
while (controller_buttons_pressed()) {
read_btns();
}
return 0;
}
}
}
int main(void) {
while (1) {
show_intro();
game_loop();
}
return 0;
}