Space Invaders is both flicker free and resolution aware, now.
This commit is contained in:
parent
29c532b8b2
commit
77545ffb94
|
@ -1,10 +1,45 @@
|
|||
#include <assert.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----------------------------*/
|
||||
|
||||
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
|
||||
- iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0))
|
||||
if (((x - iv->pos.x) < MAX_INVADER_WIDTH) && ((x - iv->pos.x) >= 0) &&
|
||||
((y - iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0))
|
||||
{
|
||||
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,
|
||||
unsigned char val)
|
||||
{
|
||||
if (((x - iv->pos.x) < MAX_INVADER_WIDTH) && ((x - iv->pos.x) >= 0) && ((y
|
||||
- iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0))
|
||||
if (((x - iv->pos.x) < MAX_INVADER_WIDTH) && ((x - iv->pos.x) >= 0) &&
|
||||
((y - iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0))
|
||||
{
|
||||
iv->map[x - iv->pos.x][y - iv->pos.y] = val;
|
||||
}
|
||||
}
|
||||
|
||||
void setGuardPixel(unsigned char *guards, unsigned char x,
|
||||
unsigned char y, unsigned char val)
|
||||
void setGuardPixel(unsigned char *guards, unsigned char x, unsigned char y,
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------drawing Method---------------------------*/
|
||||
|
||||
void draw(Invaders * iv, Spaceship * sc, Player * pl, Cannon * cn,
|
||||
unsigned char *guards, uPixel *st, uPixel * shot)
|
||||
void draw(offScreen_t offScreen, Invaders * iv, Spaceship * sc, Player * pl,
|
||||
Cannon * cn, unsigned char *guards, pixel *st, pixel * shot)
|
||||
{
|
||||
clearScreen ();
|
||||
|
||||
unsigned char x, y;
|
||||
|
||||
/*---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--*/
|
||||
// for (y = 0; 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--;)
|
||||
{
|
||||
//mal in oder statement umwandeln ;-)
|
||||
if (iv->map[x][y] == 0)
|
||||
continue;
|
||||
if (x + iv->pos.x > RIGHT_BORDER)
|
||||
continue;
|
||||
if (x + iv->pos.x < 0)
|
||||
continue;
|
||||
|
||||
setPixel (x + iv->pos.x, y + iv->pos.y, iv->map[x][y]);
|
||||
if ((x + iv->pos.x >= 0) && (x + iv->pos.x < NUM_COLS) &&
|
||||
(y + iv->pos.y < NUM_ROWS))
|
||||
{
|
||||
setOffScreenPixel(offScreen, x + iv->pos.x, y + iv->pos.y,
|
||||
iv->map[x][y]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*---GUARDS---*/
|
||||
for (x = BORG_WIDTH; x--;)
|
||||
for (x = NUM_COLS; x--;)
|
||||
{
|
||||
if (guards[x] != 0)
|
||||
{
|
||||
setPixel (x, GUARD_LINE, guards[x]);
|
||||
}
|
||||
setOffScreenPixel(offScreen, x, GUARD_LINE, guards[x]);
|
||||
}
|
||||
|
||||
/*---SHOTS--*/
|
||||
unsigned char 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))
|
||||
{
|
||||
setPixel (shot->x, shot->y, 3);
|
||||
setOffScreenPixel(offScreen, shot->x, shot->y, 3);
|
||||
}
|
||||
|
||||
/*-- 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);
|
||||
setPixel (cn->pos, 14, pl->lives);
|
||||
setOffScreenPixel(offScreen, cn->pos, NUM_ROWS - 2, 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);
|
||||
}
|
||||
|
|
|
@ -11,12 +11,25 @@ uint16_t const hans[7] PROGMEM =
|
|||
|
||||
void initGuards(unsigned char *guards)
|
||||
{
|
||||
memset(guards, 0, BORG_WIDTH);
|
||||
memset(guards, 0, NUM_COLS);
|
||||
|
||||
guards[3] = 3;
|
||||
guards[6] = 3;
|
||||
#if NUM_COLS == 16
|
||||
guards[2] = 3;
|
||||
guards[5] = 3;
|
||||
guards[10] = 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)
|
||||
|
@ -32,16 +45,16 @@ void initInvaders(Invaders * iv, unsigned char lv)
|
|||
switch (lv)
|
||||
{
|
||||
case 0:
|
||||
default:
|
||||
for (x = 0; x < 8; ++x)
|
||||
{
|
||||
iv->map[x][0] = 2;
|
||||
iv->map[x][1] = 2;
|
||||
iv->map[x][2] = 2;
|
||||
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->speed = MIN_SPEED - 3;
|
||||
iv->direction = 1;
|
||||
|
@ -50,16 +63,13 @@ void initInvaders(Invaders * iv, unsigned char lv)
|
|||
case 1:
|
||||
for (x = 0; x < 8; ++x)
|
||||
{
|
||||
//for(y = 0; y < MAX_INVADER_HEIGHT; ++y) {
|
||||
iv->map[x][0] = 3;
|
||||
iv->map[x][1] = 3;
|
||||
iv->map[x][2] = 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->speed = MIN_SPEED - 2;
|
||||
|
||||
|
@ -69,16 +79,14 @@ void initInvaders(Invaders * iv, unsigned char lv)
|
|||
case 2:
|
||||
for (x = 0; x < 8; ++x)
|
||||
{
|
||||
//for(y = 0; y < MAX_INVADER_HEIGHT; ++y) {
|
||||
iv->map[x][0] = 3;
|
||||
iv->map[x][1] = 3;
|
||||
iv->map[x][2] = 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->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->speed = MIN_SPEED + 2;
|
||||
iv->direction = 1;
|
||||
|
||||
break;
|
||||
|
||||
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->speed = MIN_SPEED + 3;
|
||||
iv->direction = 1;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,43 +4,36 @@
|
|||
#include "../../random/prng.h"
|
||||
#include "invaders2.h"
|
||||
|
||||
void procCannon(Cannon * cn, uPixel * shot)
|
||||
void procCannon(Cannon * cn, pixel * shot)
|
||||
{
|
||||
static unsigned char mv = 0;
|
||||
if (mv >= CANNON_SPEED)
|
||||
if (mv++ >= CANNON_SPEED)
|
||||
{
|
||||
mv = 0;
|
||||
if (JOYISLEFT)
|
||||
{
|
||||
if (cn->pos != RIGHT_BORDER)
|
||||
if (cn->pos < (NUM_COLS - 1))
|
||||
{
|
||||
cn->pos++;
|
||||
}
|
||||
}
|
||||
else if (JOYISRIGHT)
|
||||
{
|
||||
if (cn->pos != LEFT_BORDER)
|
||||
if (cn->pos > 0)
|
||||
{
|
||||
cn->pos--;
|
||||
--cn->pos;
|
||||
}
|
||||
}
|
||||
else if (JOYISFIRE)
|
||||
{
|
||||
|
||||
if (cn->ready)
|
||||
{
|
||||
shot->x = cn->pos;
|
||||
shot->y = 14;
|
||||
shot->y = NUM_ROWS - 3;
|
||||
cn->ready = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
mv++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static unsigned char areAtBorder(Invaders * iv)
|
||||
|
@ -48,21 +41,19 @@ static unsigned char areAtBorder(Invaders * iv)
|
|||
unsigned char y;
|
||||
for (y = SPACESHIP_LINE + 1; y <= GUARD_LINE; ++y)
|
||||
{
|
||||
if (getInvaderPixel(iv, LEFT_BORDER, y) || getInvaderPixel(iv,
|
||||
RIGHT_BORDER, y))
|
||||
if (getInvaderPixel(iv, NUM_COLS - 1, y) || getInvaderPixel(iv, 0, y))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
void procInvaders(Invaders * iv, uPixel *st)
|
||||
void procInvaders(Invaders * iv, pixel *st)
|
||||
{
|
||||
static unsigned char mv = 0;
|
||||
|
||||
if (mv >= iv->speed)
|
||||
if (mv++ >= iv->speed)
|
||||
{
|
||||
mv = 0;
|
||||
if (areAtBorder(iv) && !(iv->isEdged))
|
||||
|
@ -76,29 +67,21 @@ void procInvaders(Invaders * iv, uPixel *st)
|
|||
iv->pos.x += iv->direction;
|
||||
iv->isEdged = 0;
|
||||
}
|
||||
|
||||
}
|
||||
mv++;
|
||||
|
||||
unsigned char i, y;
|
||||
unsigned char spos = random8() % 16;
|
||||
if (spos >= BORG_WIDTH)
|
||||
return;
|
||||
unsigned char spos = random8() % UNUM_COLS;
|
||||
|
||||
unsigned char shoot = random8();
|
||||
|
||||
if (shoot < SHOOTING_RATE)
|
||||
if (random8() < SHOOTING_RATE)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (getInvaderPixel(iv, spos, y) != 0)
|
||||
{
|
||||
|
||||
st[i].x = spos;
|
||||
st[i].y = y + 1;
|
||||
return;
|
||||
|
@ -107,27 +90,27 @@ void procInvaders(Invaders * iv, uPixel *st)
|
|||
}
|
||||
} //for SHOTS
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
static unsigned char cmv = 0, imv = 0;
|
||||
|
||||
// shuß mit einen struct mit dem shuß!!
|
||||
|
||||
if (cmv >= CANNON_SHOOTING_SPEED)
|
||||
{
|
||||
cmv = 0;
|
||||
if (!(cn->ready))
|
||||
{
|
||||
shot->y--;
|
||||
}
|
||||
if (shot->y > BORG_HEIGHT)
|
||||
{
|
||||
cn->ready = 1;
|
||||
if (shot->y > 0)
|
||||
{
|
||||
shot->y--;
|
||||
}
|
||||
else
|
||||
{
|
||||
cn->ready = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,7 +120,7 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
|
|||
|
||||
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++;
|
||||
}
|
||||
|
@ -152,7 +135,6 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
|
|||
/****************************************************************/
|
||||
|
||||
// USER CANNON
|
||||
|
||||
unsigned char tmp;
|
||||
if (!(cn->ready))
|
||||
{
|
||||
|
@ -163,6 +145,7 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
|
|||
st[i].x = 255;
|
||||
st[i].y = 255;
|
||||
cn->ready = 1;
|
||||
goto invader_shots;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -200,21 +183,20 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
|
|||
}
|
||||
|
||||
//SPACESHIP
|
||||
|
||||
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;
|
||||
cn->ready = 1;
|
||||
goto invader_shots;
|
||||
}
|
||||
}
|
||||
} // !(cn->ready)
|
||||
}
|
||||
|
||||
|
||||
invader_shots: for (i = 0; i < MAX_SHOTS; ++i)
|
||||
invader_shots:
|
||||
for (i = 0; i < MAX_SHOTS; ++i)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
if (st[i].y == BORG_HEIGHT - 1)
|
||||
if (st[i].y == (NUM_ROWS - 2))
|
||||
{
|
||||
if (st[i].x == cn->pos)
|
||||
{
|
||||
|
@ -240,66 +222,61 @@ void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
|
|||
|
||||
void procSpaceship(Spaceship * sc)
|
||||
{
|
||||
unsigned char rnd1 = random8();
|
||||
unsigned char rnd2 = random8();
|
||||
unsigned char const rnd1 = random8();
|
||||
unsigned char const rnd2 = random8();
|
||||
|
||||
static unsigned char sct = 0;
|
||||
|
||||
if (sc->pos > RIGHT_BORDER)
|
||||
if (sc->pos == NO_SPACESHIP)
|
||||
{
|
||||
|
||||
if (rnd1 == 73)
|
||||
if ((rnd1 == 73) && (rnd2 >= 200))
|
||||
{
|
||||
if (rnd2 >= 200)
|
||||
{
|
||||
sc->pos = RIGHT_BORDER;
|
||||
sct = 0;
|
||||
}
|
||||
sc->pos = NUM_COLS;
|
||||
sct = 0;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sct == SPACESHIP_SPEED)
|
||||
if (sct++ == SPACESHIP_SPEED)
|
||||
{
|
||||
sct = 0;
|
||||
if (sc->pos == 0)
|
||||
{
|
||||
sc->pos = 255;
|
||||
}
|
||||
else
|
||||
if (sc->pos > 0)
|
||||
{
|
||||
sc->pos--;
|
||||
}
|
||||
else
|
||||
{
|
||||
sc->pos = NO_SPACESHIP;
|
||||
}
|
||||
}
|
||||
}
|
||||
sct++;
|
||||
}
|
||||
|
||||
unsigned char getStatus(Invaders * iv)
|
||||
{
|
||||
unsigned char x, y;
|
||||
|
||||
//count Invader!
|
||||
unsigned char x, y, inv = 0;
|
||||
// did invaders reached earth?
|
||||
for (x = NUM_COLS; x--;)
|
||||
{
|
||||
if (getInvaderPixel(iv, x, GUARD_LINE + 1))
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
// any invaders left?
|
||||
for (x = MAX_INVADER_WIDTH; x--;)
|
||||
{
|
||||
for (y = MAX_INVADER_HEIGHT; y--;)
|
||||
{
|
||||
if (iv->map[x][y] != 0)
|
||||
inv++;
|
||||
if (iv->map[x][y])
|
||||
{
|
||||
return 0; // yes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//LEVEL BEREINIGT
|
||||
if (inv == 0)
|
||||
return 1;
|
||||
|
||||
//INVADERS REACHED EARTH
|
||||
for (x = BORG_WIDTH; x--;)
|
||||
{
|
||||
if (getInvaderPixel(iv, x, GUARD_LINE + 1))
|
||||
return 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
// if we reach here, level was cleared \o/
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -6,8 +6,6 @@
|
|||
#include "../../scrolltext/scrolltext.h"
|
||||
#include "invaders2.h"
|
||||
|
||||
//#include <stdio.h>
|
||||
|
||||
void borg_invaders();
|
||||
|
||||
#ifdef MENU_SUPPORT
|
||||
|
@ -23,45 +21,36 @@ game_descriptor_t invaders_game_descriptor __attribute__((section(".game_descrip
|
|||
|
||||
void borg_invaders()
|
||||
{
|
||||
// waitForFire = 0;
|
||||
/****************************************************************/
|
||||
/* INITIALIZE */
|
||||
/****************************************************************/
|
||||
offScreen_t offScreen = {{{0}}};
|
||||
|
||||
Invaders iv;
|
||||
Cannon cn;
|
||||
Player pl;
|
||||
Spaceship sc;
|
||||
|
||||
unsigned char guards[BORG_WIDTH];
|
||||
|
||||
unsigned char guards[NUM_COLS];
|
||||
unsigned char level = 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;
|
||||
// = {0,0};
|
||||
pixel shot;
|
||||
|
||||
pl.points = 0;
|
||||
pl.lives = 3;
|
||||
|
||||
//--------Init Cannon-------//
|
||||
//cn.pos = 4;
|
||||
//cn.ready = 1;
|
||||
|
||||
/****************************************************************/
|
||||
/* INTRO */
|
||||
/****************************************************************/
|
||||
//drawIntroScreen(2000);
|
||||
|
||||
|
||||
/****************************************************************/
|
||||
/* GAME LOOP */
|
||||
|
@ -74,77 +63,50 @@ void borg_invaders()
|
|||
|
||||
//Spaceship
|
||||
sc.lives = 1;
|
||||
sc.pos = 255;
|
||||
sc.pos = NO_SPACESHIP;
|
||||
|
||||
//Cannon
|
||||
cn.pos = 7;
|
||||
cn.pos = (NUM_COLS - 3) / 2;
|
||||
cn.ready = 1;
|
||||
|
||||
draw(&iv, &sc, &pl, &cn, guards, st, &shot);
|
||||
|
||||
while (1)
|
||||
while (pl.lives != 0)
|
||||
{
|
||||
procInvaders(&iv, st);
|
||||
procSpaceship(&sc);
|
||||
|
||||
procShots(&iv, &pl, &cn, &sc, guards, st, &shot);
|
||||
procCannon(&cn, &shot);
|
||||
|
||||
draw(&iv, &sc, &pl, &cn, guards, st, &shot);
|
||||
draw(offScreen, &iv, &sc, &pl, &cn, guards, st, &shot);
|
||||
|
||||
ivStatus = getStatus(&iv);
|
||||
|
||||
if (ivStatus == 2) //LOST
|
||||
{
|
||||
//pl.lives--;
|
||||
pl.lives = 0;
|
||||
break;
|
||||
|
||||
}
|
||||
else if (ivStatus == 1) //WON
|
||||
{
|
||||
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;
|
||||
//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)
|
||||
{
|
||||
//scrolltext("GAME OVER",0,80);
|
||||
level = (level + 1) % MAX_LEVEL;
|
||||
break;
|
||||
}
|
||||
|
||||
wait (WAIT_MS);
|
||||
} //IN LEVEL 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);
|
||||
|
||||
} while (pl.lives != 0); //GAME LOOP
|
||||
|
||||
/****************************************************************/
|
||||
/* PLAYER STAT */
|
||||
/* HIGH SCORES */
|
||||
/****************************************************************/
|
||||
|
||||
// waitForFire = 1;
|
||||
|
||||
#ifdef SCROLLTEXT_SUPPORT
|
||||
char text[64];
|
||||
snprintf(text, 64, "</#points: %u", pl.points);
|
||||
scrolltext(text);
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -9,44 +9,11 @@
|
|||
|
||||
#ifndef INVADERS2_H
|
||||
#define INVADERS2_H
|
||||
/* TEST PARTS NEW API */
|
||||
|
||||
#include <stdint.h>
|
||||
#include "../../config.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 */
|
||||
/****************************************************************/
|
||||
|
@ -57,56 +24,47 @@ extern uint16_t const hans[7];
|
|||
/****************************************************************/
|
||||
/* DEFINES */
|
||||
/****************************************************************/
|
||||
#define START_LIVES 3
|
||||
#define START_LIVES 3
|
||||
|
||||
#define SPACESHIP_LINE 1
|
||||
//#define SPACESHIP_TRIGGER_POINTS 250
|
||||
//#define SPACESHIP_TRIGGER_RATE 333
|
||||
#define SPACESHIP_LINE 1
|
||||
|
||||
#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 BORG_HEIGHT 16
|
||||
#define POINTS_FOR_HIT 5
|
||||
#define POINTS_FOR_KILL 25
|
||||
#define POINTS_FOR_SPACESHIP 75
|
||||
#define POINTS_FOR_LEVEL 100
|
||||
|
||||
#ifdef SWITCHED_SIDE
|
||||
#define RIGHT_BORDER 0
|
||||
#define LEFT_BORDER (BORG_WIDTH -1 )
|
||||
#else
|
||||
#define RIGHT_BORDER (BORG_WIDTH -1 )
|
||||
#define LEFT_BORDER 0
|
||||
#endif
|
||||
#define MAX_SHOTS 7
|
||||
#define MIN_SPEED 70
|
||||
#define SPEED_INC_RATE 2
|
||||
#define SPEED_INC_VALUE 3
|
||||
#define MAX_LEVEL 5
|
||||
|
||||
#define MAX_INVADER_HEIGHT 8
|
||||
#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 SHOOTING_RATE 6
|
||||
#define INVADER_SHOOTING_SPEED 10
|
||||
#define CANNON_SHOOTING_SPEED 4
|
||||
#define SPACESHIP_SPEED 30
|
||||
#define CANNON_SHOOTING_SPEED 4
|
||||
#define SPACESHIP_SPEED 30
|
||||
#define NO_SPACESHIP 255
|
||||
|
||||
#define CANNON_SPEED 2
|
||||
|
||||
#define WAIT_MS 15
|
||||
//#define WAIT_MS 20
|
||||
#define WAIT_MS 15
|
||||
|
||||
typedef struct
|
||||
{
|
||||
signed char x;
|
||||
signed char y;
|
||||
} spixel;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char map[MAX_INVADER_WIDTH][MAX_INVADER_HEIGHT];
|
||||
sPixel pos;
|
||||
spixel pos;
|
||||
|
||||
unsigned char speed;
|
||||
unsigned char speedinc;
|
||||
|
@ -114,21 +72,20 @@ typedef struct
|
|||
unsigned char isEdged;
|
||||
} Invaders;
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char pos;
|
||||
unsigned char lives;
|
||||
} Spaceship;
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char pos;
|
||||
unsigned char ready;
|
||||
} Cannon;
|
||||
|
||||
//typedef struct {
|
||||
// unsigned char guards[numGards];
|
||||
//}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
@ -136,6 +93,9 @@ typedef struct
|
|||
unsigned int points;
|
||||
} Player;
|
||||
|
||||
typedef unsigned char offScreen_t[NUMPLANE + 1][NUM_ROWS][LINEBYTES];
|
||||
|
||||
|
||||
/****************************************************************/
|
||||
/* FUNCTIONS */
|
||||
/****************************************************************/
|
||||
|
@ -144,19 +104,18 @@ void borg_invaders();
|
|||
/*----------------------main_level_funcs-------------------------*/
|
||||
|
||||
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,
|
||||
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);
|
||||
|
||||
/*----------------------Initialization---------------------------*/
|
||||
void initGuards(unsigned char guards[BORG_WIDTH]);
|
||||
|
||||
void initGuards(unsigned char guards[NUM_COLS]);
|
||||
void initInvaders(Invaders * iv, unsigned char lv);
|
||||
//void initSpaceship(Spaceship* sc);
|
||||
//void initPlayer(Player* pl);
|
||||
|
||||
/*----------------------getter/setter----------------------------*/
|
||||
|
||||
|
@ -171,15 +130,14 @@ void setGuardPixel(unsigned char *guards, unsigned char x,
|
|||
inline static unsigned char getGuardPixel(unsigned char *guards,
|
||||
unsigned char x, unsigned char y)
|
||||
{
|
||||
if (x < BORG_WIDTH && y == GUARD_LINE)
|
||||
if (x < NUM_COLS && y == GUARD_LINE)
|
||||
return guards[x];
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------drawing Method---------------------------*/
|
||||
|
||||
void draw(Invaders * iv, Spaceship * sc, Player * pl, Cannon * cn,
|
||||
unsigned char *guards, uPixel *ishots,
|
||||
uPixel * shot);
|
||||
void draw(offScreen_t offscreen, Invaders * iv, Spaceship * sc, Player * pl,
|
||||
Cannon * cn, unsigned char *guards, pixel *st, pixel * shot);
|
||||
|
||||
#endif
|
||||
#endif /* INVADERS2_H */
|
||||
|
|
Loading…
Reference in New Issue