Code:
#define nlines 10
#define brickcount 7
#define rotation 4
#define bricksize 4
#define blue 3
#define white 0
//Globale Variablen
signed char shift,bricknr,rot,pos;
unsigned long line[nlines];
unsigned long brick[nlines];
unsigned char bricks[brickcount][rotation][bricksize]= {
{ {0b011,0b010,0b010,0b0},{0b0,0b111,0b001,0},{0b010,0b010,0b110,0b0},{0b100,0b111,0b000,0b0} },
{ {0b100,0b100,0b100,0b100},{0,0,0b1111,0},{0b100,0b100,0b100,0b100},{0,0,0b1111,0} },
{ {0b010,0b010,0b011,0b0},{0b0,0b111,0b100,0},{0b110,0b010,0b010,0b0},{0b001,0b111,0b0,0b0} },
{ {0b010,0b011,0b010,0b0},{0b0,0b111,0b010,0},{0b010,0b110,0b010,0b0},{0b010,0b111,0b000,0b0} },
{ {0b011,0b011,0b0,0b0},{0b011,0b011,0b0,0b0},{0b011,0b011,0b0,0b0},{0b011,0b011,0b0,0b0} },
{ {0b001,0b011,0b010,0b0},{0b110,0b011,0b0,0b0},{0b001,0b011,0b010,0b0},{0b110,0b011,0b0,0b0} },
{ {0b010,0b011,0b001,0b0},{0b0,0b011,0b110,0b0},{0b010,0b011,0b001,0b0},{0b0,0b011,0b110,0b0} },
};
#define visible 32
void draw_lines(long s1,long s2,char color){
char colors[6]={238,14,224,170,10,160};
for(char c=0; c<visible; c++){
if((s1&(1L<<c))&&(s2&(1L<<c))){
lcd_writedata(0);
lcd_writedata(238);
lcd_writedata(colors[color]);
lcd_writedata(238);}
else if(s1&(1L<<c)){
lcd_writedata(0);
lcd_writedata(14);
lcd_writedata(colors[color+1]);
lcd_writedata(14);}
else if(s2&(1L<<c)){
lcd_writedata(0);
lcd_writedata(224);
lcd_writedata(colors[color+2]);
lcd_writedata(224);}
else{
lcd_writedata(0);
lcd_writedata(0);
lcd_writedata(0);
lcd_writedata(0);}
}
lcd_newline(8,0);
}
#define random_() rand()%brickcount
#define key_() key()
#define t_delay 100
#define keyA 4
#define keyB 6
#define keyup 2
#define keydown 7
#define keyright 2
#define keyleft 8
void refresh(void){
for(char c=0; c<nlines; c++) brick[c]=0;
for(char c=0; c<4; c++) if((c+pos<nlines)&&(c+pos>=0)) brick[c+pos]=(long)bricks[bricknr][rot][c]<<shift;
}
int main(void) {
delay_ms(50);
init_font;
lcd_led_on;
uart_init(77);//9600BAUD
lcd_init();
char buf[5];
spiel:
{
pos=nlines/2-2;
shift=0;
bricknr=random_();
rot=0;
while(1){
for(char c=0; c<4; c++){
for(char c=0; c<nlines; c++) brick[c]=0;
for(char c=0; c<4; c++) if((c+pos<nlines)&&(c+pos>=0)) brick[c+pos]=(long)bricks[bricknr][rot][c]<<shift;
lcd_gotoxy(0,0);
for(char c=0; c<nlines; c+=2) draw_lines(brick[c]|line[c],brick[c+1]|line[c+1],white);
for(char c=0; c<nlines; c++){
if(brick[c]<<1L&line[c]) { for(char c=0; c<nlines; c++) line[c]|=brick[c]; goto spiel; }//anderen stein geschnitten?
if(brick[c]>=(1L<<31)){ for(char c=0; c<nlines; c++) line[c]|=brick[c]; goto spiel; }//unteren rand erreicht ?
}
if(key_()==keyright) pos--;
for(char c=0; c<nlines; c++) brick[c]=0;
for(char c=0; c<4; c++) if((c+pos<nlines)&&(c+pos>=0)) brick[c+pos]=(long)bricks[bricknr][rot][c]<<shift;
for(char c=0; c<nlines; c++) if(brick[c]&line[c]){ lcd_puts("oho"); delay_ms(1000) ;pos++;}
if(key_()==keyleft) pos++;
for(char c=0; c<nlines; c++) brick[c]=0;
for(char c=0; c<4; c++) if((c+pos<nlines)&&(c+pos>=0)) brick[c+pos]=(long)bricks[bricknr][rot][c]<<shift;
for(char c=0; c<nlines; c++) if(brick[c]&line[c]){lcd_puts("oho"); delay_ms(1000) ;pos--;}
for(char c=0; c<nlines; c++) brick[c]=0;
for(char c=0; c<4; c++) if((c+pos<nlines)&&(c+pos>=0)) brick[c+pos]=(long)bricks[bricknr][rot][c]<<shift;
if(pos>nlines-4){//überschreitet brick den spielfeldrand ?
signed char free=0;
for(signed char c=3; c>-1; c--){ if(bricks[bricknr][rot][c]) break;
else free++;}
if((pos-free)>nlines-4) pos--;
}
if(pos<0){//überschreitet brick den spielfeldrand ?
signed char free=0;
for(char c=0; c<4; c++){ if(bricks[bricknr][rot][c]) break;
else free--;}
if(free>pos) pos++;
}
if(key_()==keyB) rot--;
if(rot==-1) rot=3;
for(char c=0; c<nlines; c++) if(brick[c]&line[c]) rot++;
if(key_()==keyA) rot++;
if(rot==4) rot=0;
for(char c=0; c<nlines; c++) if(brick[c]&line[c]) rot--;
if(key_()!=keydown) delay_ms(t_delay);
else break;
}
shift++;
if(key_()==0) goto loose;
for(char c=0; c<nlines; c++) if(line[c]&(1L<<0)) goto loose;
}
}
loose:
lcd_gotoxy(00,47);
lcd_putstring("Verloren",largeFont);
delay_ms(500);
}
mfg martin
Lesezeichen