Space Invaders is both flicker free and resolution aware, now.

This commit is contained in:
Christian Kroll 2012-11-17 17:24:01 +00:00
parent 29c532b8b2
commit 77545ffb94
5 changed files with 216 additions and 284 deletions

View File

@ -1,10 +1,45 @@
#include <assert.h>
#include "invaders2.h" #include "invaders2.h"
/*--------------------double buffered graphics-------------------*/
static void setOffScreenPixel(offScreen_t offScreen, unsigned char x,
unsigned char y, unsigned char color)
{
assert(offScreen != 0);
assert(x < NUM_COLS);
assert(y < NUM_ROWS);
assert(color <= NUMPLANE);
offScreen[color][y][x / 8u] |= shl_table[x % 8u];
}
static void flushOffScreenBuffer(offScreen_t offScreen)
{
assert(offScreen != 0);
/* off-screen blitting borrowed from fixed point math patterns animation,
see fpmath_patterns.c (fixDrawPattern(...) for details */
unsigned char *pPixmap =
(&pixmap[NUMPLANE - 1][NUM_ROWS - 1][LINEBYTES - 1]) + 1;
unsigned char *pOffScreenDistHigh =
(&offScreen[NUMPLANE][NUM_ROWS - 1][LINEBYTES - 1]) + 1;
unsigned char *pOffScreenDistLow =
(&offScreen[NUMPLANE - 1][NUM_ROWS - 1][LINEBYTES - 1]) + 1;
while (pPixmap != &pixmap[0][0][0]) // stop at the beginning
{
*(--pPixmap) = *(--pOffScreenDistHigh);
*(--pOffScreenDistLow) |= *pOffScreenDistHigh;
*pOffScreenDistHigh = 0;
}
}
/*----------------------getter/setter----------------------------*/ /*----------------------getter/setter----------------------------*/
unsigned char getInvaderPixel(Invaders * iv, unsigned char x, unsigned char y) unsigned char getInvaderPixel(Invaders * iv, unsigned char x, unsigned char y)
{ {
if (((x - iv->pos.x) < MAX_INVADER_WIDTH) && ((x - iv->pos.x) >= 0) && ((y if (((x - iv->pos.x) < MAX_INVADER_WIDTH) && ((x - iv->pos.x) >= 0) &&
- iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0)) ((y - iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0))
{ {
return iv->map[x - iv->pos.x][y - iv->pos.y]; return iv->map[x - iv->pos.x][y - iv->pos.y];
} }
@ -14,96 +49,91 @@ unsigned char getInvaderPixel(Invaders * iv, unsigned char x, unsigned char y)
void setInvaderPixel(Invaders * iv, unsigned char x, unsigned char y, void setInvaderPixel(Invaders * iv, unsigned char x, unsigned char y,
unsigned char val) unsigned char val)
{ {
if (((x - iv->pos.x) < MAX_INVADER_WIDTH) && ((x - iv->pos.x) >= 0) && ((y if (((x - iv->pos.x) < MAX_INVADER_WIDTH) && ((x - iv->pos.x) >= 0) &&
- iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0)) ((y - iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0))
{ {
iv->map[x - iv->pos.x][y - iv->pos.y] = val; iv->map[x - iv->pos.x][y - iv->pos.y] = val;
} }
} }
void setGuardPixel(unsigned char *guards, unsigned char x, void setGuardPixel(unsigned char *guards, unsigned char x, unsigned char y,
unsigned char y, unsigned char val) unsigned char val)
{ {
if (x < BORG_WIDTH && y == GUARD_LINE && val <= 3) assert(val <= NUMPLANE);
if (x < NUM_COLS && y == GUARD_LINE)
{
guards[x] = val; guards[x] = val;
}
} }
/*----------------------drawing Method---------------------------*/ /*----------------------drawing Method---------------------------*/
void draw(Invaders * iv, Spaceship * sc, Player * pl, Cannon * cn, void draw(offScreen_t offScreen, Invaders * iv, Spaceship * sc, Player * pl,
unsigned char *guards, uPixel *st, uPixel * shot) Cannon * cn, unsigned char *guards, pixel *st, pixel * shot)
{ {
clearScreen ();
unsigned char x, y; unsigned char x, y;
/*---SPACESHIP---*/ /*---SPACESHIP---*/
if (sc->pos < RIGHT_BORDER) if (sc->pos < NUM_COLS)
{ {
setPixel (sc->pos, SPACESHIP_LINE, sc->lives); setOffScreenPixel(offScreen, sc->pos, SPACESHIP_LINE, sc->lives);
} }
if (sc->pos - 1 < RIGHT_BORDER) if ((sc->pos > 0) && ((sc->pos - 1) < NUM_COLS))
{ {
setPixel (sc->pos + 1, SPACESHIP_LINE, sc->lives); setOffScreenPixel(offScreen, sc->pos - 1, SPACESHIP_LINE, sc->lives);
} }
/*---INVADERS--*/ /*---INVADERS--*/
// for (y = 0; y < MAX_INVADER_HEIGHT; y++)
for (y = MAX_INVADER_HEIGHT; y--;) for (y = MAX_INVADER_HEIGHT; y--;)
{ {
// for (x = 0; x < MAX_INVADER_WIDTH; x++)
for (x = MAX_INVADER_WIDTH; x--;) for (x = MAX_INVADER_WIDTH; x--;)
{ {
//mal in oder statement umwandeln ;-) if ((x + iv->pos.x >= 0) && (x + iv->pos.x < NUM_COLS) &&
if (iv->map[x][y] == 0) (y + iv->pos.y < NUM_ROWS))
continue; {
if (x + iv->pos.x > RIGHT_BORDER) setOffScreenPixel(offScreen, x + iv->pos.x, y + iv->pos.y,
continue; iv->map[x][y]);
if (x + iv->pos.x < 0) }
continue;
setPixel (x + iv->pos.x, y + iv->pos.y, iv->map[x][y]);
} }
} }
/*---GUARDS---*/ /*---GUARDS---*/
for (x = BORG_WIDTH; x--;) for (x = NUM_COLS; x--;)
{ {
if (guards[x] != 0) setOffScreenPixel(offScreen, x, GUARD_LINE, guards[x]);
{
setPixel (x, GUARD_LINE, guards[x]);
}
} }
/*---SHOTS--*/ /*---SHOTS--*/
unsigned char i; unsigned char i;
for (i = MAX_SHOTS; i--;) for (i = MAX_SHOTS; i--;)
{ {
if (st[i].x < BORG_WIDTH && st[i].y < BORG_HEIGHT) if (st[i].x < NUM_COLS && st[i].y < NUM_ROWS)
{ {
setPixel (st[i].x, st[i].y, 3); setOffScreenPixel(offScreen, st[i].x, st[i].y, 3);
} }
} }
/*draw player shot */ /*draw player shot*/
if (!(cn->ready)) if (!(cn->ready))
{ {
setPixel (shot->x, shot->y, 3); setOffScreenPixel(offScreen, shot->x, shot->y, 3);
} }
/*-- CANNON --*/ /*-- CANNON --*/
if (cn->pos >= LEFT_BORDER + 1) if (cn->pos > 0) /* right border */
{ {
setPixel (cn->pos - 1, 15, pl->lives); setOffScreenPixel(offScreen, cn->pos - 1, NUM_ROWS - 1, pl->lives);
} }
if (cn->pos < BORG_WIDTH) if (cn->pos < NUM_COLS)
{ {
setPixel (cn->pos, 15, pl->lives); setOffScreenPixel(offScreen, cn->pos, NUM_ROWS - 2, pl->lives);
setPixel (cn->pos, 14, pl->lives); setOffScreenPixel(offScreen, cn->pos, NUM_ROWS - 1, pl->lives);
} }
if (cn->pos < RIGHT_BORDER) if (cn->pos < (NUM_COLS - 1)) /* left border */
{ {
setPixel (cn->pos + 1, 15, pl->lives); setOffScreenPixel(offScreen, cn->pos + 1, NUM_ROWS - 1, pl->lives);
} }
flushOffScreenBuffer(offScreen);
} }

View File

@ -11,12 +11,25 @@ uint16_t const hans[7] PROGMEM =
void initGuards(unsigned char *guards) void initGuards(unsigned char *guards)
{ {
memset(guards, 0, BORG_WIDTH); memset(guards, 0, NUM_COLS);
guards[3] = 3; #if NUM_COLS == 16
guards[6] = 3; guards[2] = 3;
guards[5] = 3;
guards[10] = 3; guards[10] = 3;
guards[13] = 3; guards[13] = 3;
#else
unsigned const guard_min_distance = 3;
unsigned char pos;
for (pos = 0; pos <= ((NUM_COLS - (guard_min_distance - 1)) / 2); ++pos)
{
if (((pos % guard_min_distance) == 0) && (pos != 0))
{
guards[pos - 1] = 3;
guards[NUM_COLS - pos] = 3;
}
}
#endif
} }
void initInvaders(Invaders * iv, unsigned char lv) void initInvaders(Invaders * iv, unsigned char lv)
@ -32,16 +45,16 @@ void initInvaders(Invaders * iv, unsigned char lv)
switch (lv) switch (lv)
{ {
case 0: case 0:
default:
for (x = 0; x < 8; ++x) for (x = 0; x < 8; ++x)
{ {
iv->map[x][0] = 2; iv->map[x][0] = 2;
iv->map[x][1] = 2; iv->map[x][1] = 2;
iv->map[x][2] = 2; iv->map[x][2] = 2;
iv->map[x][3] = 1; iv->map[x][3] = 1;
// iv->map[x][4] = 1;
} }
iv->pos.x = 4; iv->pos.x = (NUM_COLS - 8) / 2;
iv->pos.y = SPACESHIP_LINE + 1; iv->pos.y = SPACESHIP_LINE + 1;
iv->speed = MIN_SPEED - 3; iv->speed = MIN_SPEED - 3;
iv->direction = 1; iv->direction = 1;
@ -50,16 +63,13 @@ void initInvaders(Invaders * iv, unsigned char lv)
case 1: case 1:
for (x = 0; x < 8; ++x) for (x = 0; x < 8; ++x)
{ {
//for(y = 0; y < MAX_INVADER_HEIGHT; ++y) {
iv->map[x][0] = 3; iv->map[x][0] = 3;
iv->map[x][1] = 3; iv->map[x][1] = 3;
iv->map[x][2] = 2; iv->map[x][2] = 2;
iv->map[x][3] = 2; iv->map[x][3] = 2;
// iv->map[x][4] = 1;
//}
} }
iv->pos.x = 4; iv->pos.x = (NUM_COLS - 8) / 2;
iv->pos.y = SPACESHIP_LINE + 1; iv->pos.y = SPACESHIP_LINE + 1;
iv->speed = MIN_SPEED - 2; iv->speed = MIN_SPEED - 2;
@ -69,16 +79,14 @@ void initInvaders(Invaders * iv, unsigned char lv)
case 2: case 2:
for (x = 0; x < 8; ++x) for (x = 0; x < 8; ++x)
{ {
//for(y = 0; y < MAX_INVADER_HEIGHT; ++y) {
iv->map[x][0] = 3; iv->map[x][0] = 3;
iv->map[x][1] = 3; iv->map[x][1] = 3;
iv->map[x][2] = 2; iv->map[x][2] = 2;
iv->map[x][3] = 2; iv->map[x][3] = 2;
iv->map[x][4] = 1; iv->map[x][4] = 1;
//}
} }
iv->pos.x = 4; iv->pos.x = (NUM_COLS - 8) / 2;
iv->pos.y = SPACESHIP_LINE + 1; iv->pos.y = SPACESHIP_LINE + 1;
iv->speed = MIN_SPEED - 1; iv->speed = MIN_SPEED - 1;
@ -97,12 +105,11 @@ void initInvaders(Invaders * iv, unsigned char lv)
} }
} }
iv->pos.x = 3; iv->pos.x = (NUM_COLS - 11) / 2;
iv->pos.y = SPACESHIP_LINE + 1; iv->pos.y = SPACESHIP_LINE + 1;
iv->speed = MIN_SPEED + 2; iv->speed = MIN_SPEED + 2;
iv->direction = 1; iv->direction = 1;
break; break;
case 4: case 4:
@ -120,13 +127,11 @@ void initInvaders(Invaders * iv, unsigned char lv)
} }
} }
iv->pos.x = 3; iv->pos.x = (NUM_COLS - 11) / 2;
iv->pos.y = SPACESHIP_LINE + 1; iv->pos.y = SPACESHIP_LINE + 1;
iv->speed = MIN_SPEED + 3; iv->speed = MIN_SPEED + 3;
iv->direction = 1; iv->direction = 1;
break; break;
} }
} }

View File

@ -4,43 +4,36 @@
#include "../../random/prng.h" #include "../../random/prng.h"
#include "invaders2.h" #include "invaders2.h"
void procCannon(Cannon * cn, uPixel * shot) void procCannon(Cannon * cn, pixel * shot)
{ {
static unsigned char mv = 0; static unsigned char mv = 0;
if (mv >= CANNON_SPEED) if (mv++ >= CANNON_SPEED)
{ {
mv = 0; mv = 0;
if (JOYISLEFT) if (JOYISLEFT)
{ {
if (cn->pos != RIGHT_BORDER) if (cn->pos < (NUM_COLS - 1))
{ {
cn->pos++; cn->pos++;
} }
} }
else if (JOYISRIGHT) else if (JOYISRIGHT)
{ {
if (cn->pos != LEFT_BORDER) if (cn->pos > 0)
{ {
cn->pos--; --cn->pos;
} }
} }
else if (JOYISFIRE) else if (JOYISFIRE)
{ {
if (cn->ready) if (cn->ready)
{ {
shot->x = cn->pos; shot->x = cn->pos;
shot->y = 14; shot->y = NUM_ROWS - 3;
cn->ready = 0; cn->ready = 0;
} }
} }
} }
else
{
mv++;
}
} }
static unsigned char areAtBorder(Invaders * iv) static unsigned char areAtBorder(Invaders * iv)
@ -48,21 +41,19 @@ static unsigned char areAtBorder(Invaders * iv)
unsigned char y; unsigned char y;
for (y = SPACESHIP_LINE + 1; y <= GUARD_LINE; ++y) for (y = SPACESHIP_LINE + 1; y <= GUARD_LINE; ++y)
{ {
if (getInvaderPixel(iv, LEFT_BORDER, y) || getInvaderPixel(iv, if (getInvaderPixel(iv, NUM_COLS - 1, y) || getInvaderPixel(iv, 0, y))
RIGHT_BORDER, y))
{ {
return 1; return 1;
} }
} }
return 0; return 0;
} }
void procInvaders(Invaders * iv, uPixel *st) void procInvaders(Invaders * iv, pixel *st)
{ {
static unsigned char mv = 0; static unsigned char mv = 0;
if (mv >= iv->speed) if (mv++ >= iv->speed)
{ {
mv = 0; mv = 0;
if (areAtBorder(iv) && !(iv->isEdged)) if (areAtBorder(iv) && !(iv->isEdged))
@ -76,29 +67,21 @@ void procInvaders(Invaders * iv, uPixel *st)
iv->pos.x += iv->direction; iv->pos.x += iv->direction;
iv->isEdged = 0; iv->isEdged = 0;
} }
} }
mv++;
unsigned char i, y; unsigned char i, y;
unsigned char spos = random8() % 16; unsigned char spos = random8() % UNUM_COLS;
if (spos >= BORG_WIDTH)
return;
unsigned char shoot = random8(); if (random8() < SHOOTING_RATE)
if (shoot < SHOOTING_RATE)
{ {
for (i = 0; i < MAX_SHOTS; ++i) for (i = 0; i < MAX_SHOTS; ++i)
{ {
if (st[i].x > BORG_WIDTH || st[i].y > BORG_HEIGHT) if (st[i].y >= NUM_ROWS)
{ {
for (y = GUARD_LINE; y > SPACESHIP_LINE; --y) for (y = GUARD_LINE; y > SPACESHIP_LINE; --y)
{ {
if (getInvaderPixel(iv, spos, y) != 0) if (getInvaderPixel(iv, spos, y) != 0)
{ {
st[i].x = spos; st[i].x = spos;
st[i].y = y + 1; st[i].y = y + 1;
return; return;
@ -107,27 +90,27 @@ void procInvaders(Invaders * iv, uPixel *st)
} }
} //for SHOTS } //for SHOTS
} }
} }
void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc, void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
unsigned char *guards, uPixel *st, uPixel * shot) unsigned char *guards, pixel *st, pixel * shot)
{ {
unsigned char i; unsigned char i;
static unsigned char cmv = 0, imv = 0; static unsigned char cmv = 0, imv = 0;
// shuß mit einen struct mit dem shuß!!
if (cmv >= CANNON_SHOOTING_SPEED) if (cmv >= CANNON_SHOOTING_SPEED)
{ {
cmv = 0; cmv = 0;
if (!(cn->ready)) if (!(cn->ready))
{ {
shot->y--; if (shot->y > 0)
} {
if (shot->y > BORG_HEIGHT) shot->y--;
{ }
cn->ready = 1; else
{
cn->ready = 1;
}
} }
} }
@ -137,7 +120,7 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
for (i = MAX_SHOTS; i--;) for (i = MAX_SHOTS; i--;)
{ {
if ( /*st[i].x < BORG_WIDTH && */st[i].y < BORG_HEIGHT) if (st[i].y < NUM_ROWS)
{ {
st[i].y++; st[i].y++;
} }
@ -152,7 +135,6 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
/****************************************************************/ /****************************************************************/
// USER CANNON // USER CANNON
unsigned char tmp; unsigned char tmp;
if (!(cn->ready)) if (!(cn->ready))
{ {
@ -163,6 +145,7 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
st[i].x = 255; st[i].x = 255;
st[i].y = 255; st[i].y = 255;
cn->ready = 1; cn->ready = 1;
goto invader_shots;
} }
} }
@ -200,21 +183,20 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
} }
//SPACESHIP //SPACESHIP
if (shot->y == SPACESHIP_LINE) if (shot->y == SPACESHIP_LINE)
{ {
if (shot->x == sc->pos || shot->x == sc->pos + 1) if ((shot->x <= sc->pos) && (shot->x >= sc->pos - 1))
{ {
sc->pos = 255; sc->pos = NO_SPACESHIP;
pl->points += POINTS_FOR_SPACESHIP; pl->points += POINTS_FOR_SPACESHIP;
cn->ready = 1; cn->ready = 1;
goto invader_shots; goto invader_shots;
} }
} }
} // !(cn->ready) }
invader_shots:
invader_shots: for (i = 0; i < MAX_SHOTS; ++i) for (i = 0; i < MAX_SHOTS; ++i)
{ {
if ((tmp = getGuardPixel(guards, st[i].x, st[i].y))) if ((tmp = getGuardPixel(guards, st[i].x, st[i].y)))
{ {
@ -224,7 +206,7 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
st[i].y = 255; st[i].y = 255;
} }
if (st[i].y == BORG_HEIGHT - 1) if (st[i].y == (NUM_ROWS - 2))
{ {
if (st[i].x == cn->pos) if (st[i].x == cn->pos)
{ {
@ -240,66 +222,61 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
void procSpaceship(Spaceship * sc) void procSpaceship(Spaceship * sc)
{ {
unsigned char rnd1 = random8(); unsigned char const rnd1 = random8();
unsigned char rnd2 = random8(); unsigned char const rnd2 = random8();
static unsigned char sct = 0; static unsigned char sct = 0;
if (sc->pos > RIGHT_BORDER) if (sc->pos == NO_SPACESHIP)
{ {
if ((rnd1 == 73) && (rnd2 >= 200))
if (rnd1 == 73)
{ {
if (rnd2 >= 200) sc->pos = NUM_COLS;
{ sct = 0;
sc->pos = RIGHT_BORDER;
sct = 0;
}
} }
} }
else else
{ {
if (sct == SPACESHIP_SPEED) if (sct++ == SPACESHIP_SPEED)
{ {
sct = 0; sct = 0;
if (sc->pos == 0) if (sc->pos > 0)
{
sc->pos = 255;
}
else
{ {
sc->pos--; sc->pos--;
} }
else
{
sc->pos = NO_SPACESHIP;
}
} }
} }
sct++;
} }
unsigned char getStatus(Invaders * iv) unsigned char getStatus(Invaders * iv)
{ {
unsigned char x, y;
//count Invader! // did invaders reached earth?
unsigned char x, y, inv = 0; for (x = NUM_COLS; x--;)
{
if (getInvaderPixel(iv, x, GUARD_LINE + 1))
{
return 2;
}
}
// any invaders left?
for (x = MAX_INVADER_WIDTH; x--;) for (x = MAX_INVADER_WIDTH; x--;)
{ {
for (y = MAX_INVADER_HEIGHT; y--;) for (y = MAX_INVADER_HEIGHT; y--;)
{ {
if (iv->map[x][y] != 0) if (iv->map[x][y])
inv++; {
return 0; // yes
}
} }
} }
//LEVEL BEREINIGT // if we reach here, level was cleared \o/
if (inv == 0) return 1;
return 1;
//INVADERS REACHED EARTH
for (x = BORG_WIDTH; x--;)
{
if (getInvaderPixel(iv, x, GUARD_LINE + 1))
return 2;
}
return 0;
} }

View File

@ -6,8 +6,6 @@
#include "../../scrolltext/scrolltext.h" #include "../../scrolltext/scrolltext.h"
#include "invaders2.h" #include "invaders2.h"
//#include <stdio.h>
void borg_invaders(); void borg_invaders();
#ifdef MENU_SUPPORT #ifdef MENU_SUPPORT
@ -23,45 +21,36 @@ game_descriptor_t invaders_game_descriptor __attribute__((section(".game_descrip
void borg_invaders() void borg_invaders()
{ {
// waitForFire = 0;
/****************************************************************/ /****************************************************************/
/* INITIALIZE */ /* INITIALIZE */
/****************************************************************/ /****************************************************************/
offScreen_t offScreen = {{{0}}};
Invaders iv; Invaders iv;
Cannon cn; Cannon cn;
Player pl; Player pl;
Spaceship sc; Spaceship sc;
unsigned char guards[BORG_WIDTH]; unsigned char guards[NUM_COLS];
unsigned char level = 0; unsigned char level = 0;
unsigned char ivStatus = 0; unsigned char ivStatus = 0;
uPixel st[MAX_SHOTS] = pixel st[MAX_SHOTS] =
{ {
{ 255, 255}, {255, 255},
{ 255, 255}, {255, 255},
{ 255, 255}, {255, 255},
{ 255, 255}, {255, 255},
{ 255, 255} {255, 255},
{255, 255},
{255, 255}
}; };
uPixel shot; pixel shot;
// = {0,0};
pl.points = 0; pl.points = 0;
pl.lives = 3; pl.lives = 3;
//--------Init Cannon-------//
//cn.pos = 4;
//cn.ready = 1;
/****************************************************************/
/* INTRO */
/****************************************************************/
//drawIntroScreen(2000);
/****************************************************************/ /****************************************************************/
/* GAME LOOP */ /* GAME LOOP */
@ -74,77 +63,50 @@ void borg_invaders()
//Spaceship //Spaceship
sc.lives = 1; sc.lives = 1;
sc.pos = 255; sc.pos = NO_SPACESHIP;
//Cannon //Cannon
cn.pos = 7; cn.pos = (NUM_COLS - 3) / 2;
cn.ready = 1; cn.ready = 1;
draw(&iv, &sc, &pl, &cn, guards, st, &shot); while (pl.lives != 0)
while (1)
{ {
procInvaders(&iv, st); procInvaders(&iv, st);
procSpaceship(&sc); procSpaceship(&sc);
procShots(&iv, &pl, &cn, &sc, guards, st, &shot); procShots(&iv, &pl, &cn, &sc, guards, st, &shot);
procCannon(&cn, &shot); procCannon(&cn, &shot);
draw(&iv, &sc, &pl, &cn, guards, st, &shot); draw(offScreen, &iv, &sc, &pl, &cn, guards, st, &shot);
ivStatus = getStatus(&iv); ivStatus = getStatus(&iv);
if (ivStatus == 2) //LOST if (ivStatus == 2) //LOST
{ {
//pl.lives--;
pl.lives = 0; pl.lives = 0;
break; break;
} }
else if (ivStatus == 1) //WON else if (ivStatus == 1) //WON
{ {
unsigned int bonus = POINTS_FOR_LEVEL * (level + 1u) * unsigned int bonus = POINTS_FOR_LEVEL * (level + 1u) *
(unsigned int)(12 - iv.pos.y); (unsigned int)(NUM_ROWS - 4 - iv.pos.y);
pl.points += bonus; pl.points += bonus;
//printf("cleared l: %d , y: %d bonus: %d \n",
// level, iv.pos.y, bonus);
if (level == MAX_LEVEL - 1)
{
level = 0;
}
else
{
level = (level + 1);
}
break;
}
if (pl.lives <= 0) level = (level + 1) % MAX_LEVEL;
{
//scrolltext("GAME OVER",0,80);
break; break;
} }
wait (WAIT_MS); wait (WAIT_MS);
} //IN LEVEL LOOP } //IN LEVEL LOOP
} while (pl.lives > 0); //GAME LOOP } while (pl.lives != 0); //GAME LOOP
clearScreen ();
//wait(5000);
#ifdef SCROLLTEXT_SUPPORT
char text[64];
snprintf(text, 64, "</#points: %u", pl.points);
scrolltext(text);
#endif
//printf("scores: %d\n", pl.points);
/****************************************************************/ /****************************************************************/
/* PLAYER STAT */ /* PLAYER STAT */
/* HIGH SCORES */ /* HIGH SCORES */
/****************************************************************/ /****************************************************************/
#ifdef SCROLLTEXT_SUPPORT
// waitForFire = 1; char text[64];
snprintf(text, 64, "</#points: %u", pl.points);
scrolltext(text);
#endif
} }

View File

@ -9,44 +9,11 @@
#ifndef INVADERS2_H #ifndef INVADERS2_H
#define INVADERS2_H #define INVADERS2_H
/* TEST PARTS NEW API */
#include <stdint.h> #include <stdint.h>
#include "../../config.h" #include "../../config.h"
#include "../../pixel.h" #include "../../pixel.h"
typedef struct
{
signed char x;
signed char y;
} sPixel;
typedef struct
{
unsigned char x;
unsigned char y;
} uPixel;
#define USE_ORIGINAL_PIXEL_API
//for compatibility to pixel.h api!
#ifdef USE_ORIGINAL_PIXEL_API
//typedef uPixel pixel;
#define uPixel pixel
//#define getPixel(_X, _Y) get_pixel( (pixel){_X, _Y})
#define clearScreen() clear_screen(0)
//#define
//#ifdef SIMULATOR
#define setPixel(_X, _Y, _V) setpixel( (pixel){_X, _Y}, _V)
//#else //if defined (AVR)
//#define setPixel(_X, _Y, _V) reverseSetPixel( (pixel){_X, _Y}, _V)
//#endif
#endif
/****************************************************************/ /****************************************************************/
/* GLOBALE VAR */ /* GLOBALE VAR */
/****************************************************************/ /****************************************************************/
@ -57,56 +24,47 @@ extern uint16_t const hans[7];
/****************************************************************/ /****************************************************************/
/* DEFINES */ /* DEFINES */
/****************************************************************/ /****************************************************************/
#define START_LIVES 3 #define START_LIVES 3
#define SPACESHIP_LINE 1 #define SPACESHIP_LINE 1
//#define SPACESHIP_TRIGGER_POINTS 250
//#define SPACESHIP_TRIGGER_RATE 333
#define GUARD_LINE (NUM_ROWS - 3)
#define GUARD_LINE 13 #define MAX_INVADER_HEIGHT 8
#define MAX_INVADER_WIDTH 12
#define MAX_INVADER_LIVES 3
#define BORG_WIDTH 16 #define POINTS_FOR_HIT 5
#define BORG_HEIGHT 16 #define POINTS_FOR_KILL 25
#define POINTS_FOR_SPACESHIP 75
#define POINTS_FOR_LEVEL 100
#ifdef SWITCHED_SIDE #define MAX_SHOTS 7
#define RIGHT_BORDER 0 #define MIN_SPEED 70
#define LEFT_BORDER (BORG_WIDTH -1 ) #define SPEED_INC_RATE 2
#else #define SPEED_INC_VALUE 3
#define RIGHT_BORDER (BORG_WIDTH -1 ) #define MAX_LEVEL 5
#define LEFT_BORDER 0
#endif
#define MAX_INVADER_HEIGHT 8 #define SHOOTING_RATE 6
#define MAX_INVADER_WIDTH 12
#define MAX_INVADER_LIVES 3
#define POINTS_FOR_HIT 5
#define POINTS_FOR_KILL 25
#define POINTS_FOR_SPACESHIP 75
#define POINTS_FOR_LEVEL 100
#define MAX_SHOTS 7
#define MIN_SPEED 70
#define SPEED_INC_RATE 2
#define SPEED_INC_VALUE 3
#define MAX_LEVEL 5
#define SHOOTING_RATE 6
#define INVADER_SHOOTING_SPEED 10 #define INVADER_SHOOTING_SPEED 10
#define CANNON_SHOOTING_SPEED 4 #define CANNON_SHOOTING_SPEED 4
#define SPACESHIP_SPEED 30 #define SPACESHIP_SPEED 30
#define NO_SPACESHIP 255
#define CANNON_SPEED 2 #define CANNON_SPEED 2
#define WAIT_MS 15 #define WAIT_MS 15
//#define WAIT_MS 20
typedef struct
{
signed char x;
signed char y;
} spixel;
typedef struct typedef struct
{ {
unsigned char map[MAX_INVADER_WIDTH][MAX_INVADER_HEIGHT]; unsigned char map[MAX_INVADER_WIDTH][MAX_INVADER_HEIGHT];
sPixel pos; spixel pos;
unsigned char speed; unsigned char speed;
unsigned char speedinc; unsigned char speedinc;
@ -114,21 +72,20 @@ typedef struct
unsigned char isEdged; unsigned char isEdged;
} Invaders; } Invaders;
typedef struct typedef struct
{ {
unsigned char pos; unsigned char pos;
unsigned char lives; unsigned char lives;
} Spaceship; } Spaceship;
typedef struct typedef struct
{ {
unsigned char pos; unsigned char pos;
unsigned char ready; unsigned char ready;
} Cannon; } Cannon;
//typedef struct {
// unsigned char guards[numGards];
//}
typedef struct typedef struct
{ {
@ -136,6 +93,9 @@ typedef struct
unsigned int points; unsigned int points;
} Player; } Player;
typedef unsigned char offScreen_t[NUMPLANE + 1][NUM_ROWS][LINEBYTES];
/****************************************************************/ /****************************************************************/
/* FUNCTIONS */ /* FUNCTIONS */
/****************************************************************/ /****************************************************************/
@ -144,19 +104,18 @@ void borg_invaders();
/*----------------------main_level_funcs-------------------------*/ /*----------------------main_level_funcs-------------------------*/
void procSpaceship(Spaceship * sp); void procSpaceship(Spaceship * sp);
void procCannon(Cannon * cn, uPixel * shot); void procCannon(Cannon * cn, pixel * shot);
void procInvaders(Invaders * iv, uPixel st[MAX_SHOTS]); void procInvaders(Invaders * iv, pixel st[MAX_SHOTS]);
void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc, void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
unsigned char guards[BORG_WIDTH], uPixel st[MAX_SHOTS], uPixel * shot); unsigned char guards[NUM_COLS], pixel st[MAX_SHOTS], pixel * shot);
unsigned char getStatus(Invaders * iv); unsigned char getStatus(Invaders * iv);
/*----------------------Initialization---------------------------*/ /*----------------------Initialization---------------------------*/
void initGuards(unsigned char guards[BORG_WIDTH]);
void initGuards(unsigned char guards[NUM_COLS]);
void initInvaders(Invaders * iv, unsigned char lv); void initInvaders(Invaders * iv, unsigned char lv);
//void initSpaceship(Spaceship* sc);
//void initPlayer(Player* pl);
/*----------------------getter/setter----------------------------*/ /*----------------------getter/setter----------------------------*/
@ -171,15 +130,14 @@ void setGuardPixel(unsigned char *guards, unsigned char x,
inline static unsigned char getGuardPixel(unsigned char *guards, inline static unsigned char getGuardPixel(unsigned char *guards,
unsigned char x, unsigned char y) unsigned char x, unsigned char y)
{ {
if (x < BORG_WIDTH && y == GUARD_LINE) if (x < NUM_COLS && y == GUARD_LINE)
return guards[x]; return guards[x];
return 0; return 0;
} }
/*----------------------drawing Method---------------------------*/ /*----------------------drawing Method---------------------------*/
void draw(Invaders * iv, Spaceship * sc, Player * pl, Cannon * cn, void draw(offScreen_t offscreen, Invaders * iv, Spaceship * sc, Player * pl,
unsigned char *guards, uPixel *ishots, Cannon * cn, unsigned char *guards, pixel *st, pixel * shot);
uPixel * shot);
#endif #endif /* INVADERS2_H */