Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 6ec9e46b30
Fetching contributors…

Cannot retrieve contributors at this time

166 lines (137 sloc) 5.264 kB
// Tomer Elmalem
#include "mylib.h"
#include "screens.h"
#include "vehicles.h"
#include "invaders.h"
#include "text.h"
#include <string.h>
void initialize_everything() {
init_invaders(); // create the invaders
init_invader_bullets(); // create the invader bullets
init_tank(); // create the tank
init_tank_bullets(); // create the tank bullets
invaders_left = INVADERS_AMT;
}
int main() {
*(u16 *) REG_DISPCNT = 1027; // set the display mode
/* This variable determines whether or not to
display the start screen */
int start = 0;
/* Keep track of how many frames have
elapsed since the game started */
int frame = 0;
/* How many times the invaders have
moved or 'stepped' */
int invaders_step = 0;
/* The distance of the invader movement. */
int invaders_delta = 10;
/* Initialize everything else needed
for the game */
initialize_everything();
// Game loop
while(1) {
wait_for_vblank();
// Start Screen
if (!start) {
drawImage3(0,0,START_SCREEN_WIDTH,START_SCREEN_HEIGHT,start_screen);
/* Note: If you want this to act like a pause instead of reset
uncomment out the next line.
*/
// if (tank.lives == 0 || invaders_left == 0) {
/* reinitialize all the things! */
initialize_everything();
frame = 0;
invaders_step = 0;
invaders_delta = 10;
}
// Or the game
else {
blank_screen();
frame++;
/* Tank has 0 lives or there are
0 invaders left, game over! */
if (tank.lives == 0 || invaders_left == 0) {
drawImage3(0,0,WAR_OVER_WIDTH,WAR_OVER_HEIGHT,war_over);
if (KEY_DOWN_NOW(BUTTON_SELECT) || KEY_DOWN_NOW(BUTTON_START)) {
start = 0;
}
continue;
}
/* Draw the tank at its current position */
drawImage3(140,tank.position,TANK_WIDTH,TANK_HEIGHT,tank_img);
/* Movement for the invaders happens after
100 frames. */
if (frame % 100 == 0) {
move_invaders(invaders_delta);
if (invaders_delta > 0) invaders_step++;
else if (invaders_delta < 0) invaders_step--;
}
/* Determine if it's time to reverse directions
based on where their current step is */
if (invaders_step == INVADER_MOVES) invaders_delta = -10;
else if (invaders_step == 0) invaders_delta = 10;
/* Draw the currently living invaders now that
they may have moved */
for (int i=0; i<INVADERS_AMT; i++) {
if (invaders[i].alive) {
drawImage3(invaders[i].r,invaders[i].c, invaders[i].width, invaders[i].height, alien1);
}
}
/* All of the logic for the tank bullets. Black out
the old ones, move them up, and then redraw them.
Afterwards check to see if they hit anything or
if they're on the screen anymore. */
for (int i=0; i<TANK_BLT; i++) {
if (tank_bullets[i].fired) {
drawRect(tank_bullets[i].r, tank_bullets[i].c, 1, 3, RGB(0,0,0));
tank_bullets[i].r -= 3;
drawRect(tank_bullets[i].r, tank_bullets[i].c, 1, 3, RGB(31,31,31));
// Check if the tank bullets hit any invaders
for (int j=0; j<INVADERS_AMT; j++) {
check_invader_collision(&tank_bullets[i], &invaders[j]);
}
// Check to see if it's still on screen
if (tank_bullets[i].r <= 0) tank_bullets[i].fired = 0;
}
}
/* Logic for the invader's bullets. Similarly to
the tank, black out the old ones, move them down,
and redraw. Then check for collision or if still
on screen. */
for (int i=0; i<INVADERS_BLT; i++) {
if (invader_bullets[i].fired) {
drawRect(invader_bullets[i].r, invader_bullets[i].c, 1, 3, RGB(0,0,0));
invader_bullets[i].r += 3;
drawRect(invader_bullets[i].r, invader_bullets[i].c, 1, 3, RGB(31,31,31));
// Check if the invader bullets hit the tank
check_tank_collision(&invader_bullets[i], &tank);
// Check if still on screen
if (invader_bullets[i].r >= 160) invader_bullets[i].fired = 0;
}
}
/* Controls the motion of the tank. */
if (KEY_DOWN_NOW(BUTTON_LEFT)) {
if (tank.position > 0) tank.position--; // left
} else if (KEY_DOWN_NOW(BUTTON_RIGHT)) {
if (tank.position < 225) tank.position++; // right
}
/* Fire ze bullets! */
fire_invader_bullet();
if (KEY_DOWN_NOW(BUTTON_A)) fire_tank_bullet();
/* Draw the lives left on the screen */
char lives[] = "Lives left ";
char lives_amt = (char)(tank.lives + 48); // the ascii code for representing the current lives left
char *lives_ptr = &lives_amt;
char *lives_str = strcat(lives, lives_ptr); // concatenate the str and the amount
drawString(150,165,lives_str, RGB(31,31,31));
}
/* If the start button is pressed then start the game.
Go go go! Otherwise if the select button is pressed
reset the game. */
if (KEY_DOWN_NOW(BUTTON_START)) {
start = 1;
} else if (KEY_DOWN_NOW(BUTTON_SELECT)) {
start = 0;
}
}
}
Jump to Line
Something went wrong with that request. Please try again.