invader fixed

This commit is contained in:
robelix 2019-01-27 17:22:44 +01:00
parent c257e09232
commit 6ce412b8d5

View file

@ -45,6 +45,8 @@ byte ledsData[8];
byte currentled = 0;
int stepcount = 0;
// low level functions
@ -83,25 +85,25 @@ void pushout() {
//byte invaderA0[11] = {112,24,125,182,188,60,188,182,125,24,112};
//byte invaderA1[11] = {14,24,189,118,60,60,60,118,189,24,14};
static byte invaderA0[8] = {152,92,182,95,95,182,92,152};
static byte invaderA1[8] = {88,188,22,63,63,22,188,88};
static byte invader_A0[8] = {152,92,182,95,95,182,92,152};
static byte invader_A1[8] = {88,188,22,63,63,22,188,88};
bool invaderread(int x, int y, bool invaderstate) {
if (invaderstate) {
return bitRead(invaderA0[x],y);
bool invader_read(int x, int y, bool invader_state) {
if (invader_state) {
return bitRead(invader_A0[x],y);
} else {
return bitRead(invaderA1[x],y);
return bitRead(invader_A1[x],y);
}
}
void drawinvader(int xposition, int yposition, bool invaderstate) {
void invader_draw(int xposition, int yposition, bool invaderstate) {
for (int x=0; x<8; x++) {
int screenx = x+xposition;
if (screenx >=0 && screenx < 8) {
for (int y=0; y<8; y++) {
int screeny = y+yposition;
if (screeny >=0 && screeny < 8) {
if ( invaderread(x,y,invaderstate) ) {
if ( invader_read(x,y,invaderstate) ) {
writexy(screenx,screeny);
}
}
@ -111,15 +113,40 @@ void drawinvader(int xposition, int yposition, bool invaderstate) {
}
byte currentcol=1;
bool direction = true;
byte invader_currentcol=1;
bool invader_direction = true;
int invaderstart = -12;
int invaderend = 8;
int invaderposition = invaderstart;
bool invaderstate = true;
int invader_start = -12;
int invader_end = 8;
int invader_position = invader_start;
bool invader_state = true;
int statecount = 0;
int invader_statecount = 0;
void invader_setup() {
invader_state = true;
stepcount = 0;
invader_statecount = 0;
}
void invader_step() {
invader_draw(0,0, invader_state);
pushout();
invader_state = !invader_state;
stepcount++;
invader_statecount++;
if (invader_statecount >=4) {
invader_position++;
if (invader_position >=invader_end) {
invader_position = invader_start;
}
invader_statecount = 0;
}
delay(600);
}
// Game of life
@ -285,10 +312,76 @@ void gol_step() {
delay(200);
}
// pattern 1
int pattern_state = 0;
bool pattern_direction = true;
void pattern1_setup() {
stepcount = 0;
pattern_state = 0;
pattern_direction = true;
}
void pattern1_step() {
// x-line
for(byte i=0; i<8; i++) {
writexy(i,pattern_state);
}
// y line
for(byte i=0; i<8; i++) {
writexy(pattern_state,i);
}
if(pattern_direction) {
pattern_state++;
} else {
pattern_state--;
}
if (pattern_state >=8) {
pattern_state = 6;
pattern_direction = false;
}
if (pattern_state <0) {
pattern_state=1;
pattern_direction = true;
}
pushout();
stepcount++;
delay(120);
}
// pattern 2
void pattern2_setup() {
stepcount = 0;
pattern_state = 0;
pattern_direction = true;
}
void pattern2_step() {
for (int i=pattern_state; i < 8-pattern_state; i++) {
writexy(pattern_state,i);
writexy(i,pattern_state);
writexy(7-pattern_state,i);
writexy(i,7-pattern_state);
}
pattern_state++;
if(pattern_state>=4) {
pattern_state= 0;
}
pushout();
stepcount++;
delay(800);
}
// main loop
int mode = 0;
#define NRMODES 3
#define NRMODES 6
void switchmode( ){
mode = random(NRMODES);
@ -306,11 +399,22 @@ void switchmode( ){
gol_stepcount = 0;
gol_setup_spaceship();
break;
case 3: // spaceinvader
stepcount = 0;
invader_setup();
break;
case 4:
stepcount = 0;
pattern1_setup();
break;
case 5:
stepcount = 0;
pattern2_setup();
break;
}
}
void loop() {
clear();
@ -320,13 +424,28 @@ void loop() {
case 0: // game of life
case 1:
case 2:
if (gol_stepcount >100) {
if (gol_stepcount >150) {
switchmode();
}
if (gol_deadcount > 12) {
switchmode();
}
break;
case 3: // spaceinvader
if (stepcount > 50) {
switchmode();
}
break;
case 4:
if (stepcount > 100) {
switchmode();
}
break;
case 5:
if (stepcount > 40) {
switchmode();
}
break;
}
switch(mode) {
@ -335,55 +454,15 @@ void loop() {
case 2:
gol_step();
break;
case 3: // spaceinvader
invader_step();
break;
case 4:
pattern1_step();
break;
case 5:
pattern2_step();
break;
}
/*
// x-line
for(byte i=0; i<8; i++) {
writexy(i,currentcol-1);
}
// y line
for(byte i=0; i<8; i++) {
writexy(currentcol-1,i);
}
*/
/* invaders
drawinvader(0,0, invaderstate);
pushout();
invaderstate = !invaderstate;
statecount++;
if (statecount >=4) {
invaderposition++;
if (invaderposition >=invaderend) {
invaderposition = invaderstart;
}
statecount = 0;
}
invaders end */
/*
if(direction) {
currentcol++;
} else {
currentcol--;
}
if (currentcol >8) {
currentcol = 7;
direction = false;
}
if (currentcol <=0) {
currentcol=2;
direction = true;
}
*/
}