SHOW:
|
|
- or go back to the newest paste.
| 1 | // this code is written by Aidan Dodds 7/may/2013 | |
| 2 | // do not reuse this code in anything commercial without my permission | |
| 3 | ||
| 4 | // | |
| 5 | int invaderSeed = 0; | |
| 6 | ||
| 7 | // | |
| 8 | char invader[] = | |
| 9 | {
| |
| 10 | 0, 1, 1, 1, 0, | |
| 11 | 1, 0, 1, 0, 1, | |
| 12 | 1, 0, 1, 0, 1, | |
| 13 | 1, 1, 1, 1, 1, | |
| 14 | 1, 0, 1, 0, 1, | |
| 15 | }; | |
| 16 | ||
| 17 | // | |
| 18 | uint32 g_noise = 0xDEADBABE; | |
| 19 | ||
| 20 | // | |
| 21 | static | |
| 22 | inline int myRand( void ) | |
| 23 | {
| |
| 24 | const uint32 taps = 0x80306031; | |
| 25 | sint32 l = g_noise; | |
| 26 | if (l & 0x1) l = (1 << 31) | ((l ^ taps) >> 1); | |
| 27 | else l = (l >> 1); | |
| 28 | g_noise = l; | |
| 29 | return l; | |
| 30 | } | |
| 31 | ||
| 32 | // | |
| 33 | void generateInvader( void ) | |
| 34 | {
| |
| 35 | // | |
| 36 | invaderSeed = g_noise; | |
| 37 | // | |
| 38 | for ( int i=0; i<5*5; i++ ) | |
| 39 | invader[i] = 0; | |
| 40 | // | |
| 41 | for ( int y=0; y<5; y++ ) | |
| 42 | {
| |
| 43 | for ( int x=0; x<5; x++ ) | |
| 44 | {
| |
| 45 | if ( x < 3 ) | |
| 46 | {
| |
| 47 | // left side randomness | |
| 48 | invader[ x + y*5 ] = myRand( ) & 0x1; | |
| 49 | } | |
| 50 | else | |
| 51 | {
| |
| 52 | // right side symmetry | |
| 53 | invader[ x + y*5 ] = invader[ (4-x) + y*5 ]; | |
| 54 | } | |
| 55 | } | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 59 | // | |
| 60 | void drawInvader( int x, int y ) | |
| 61 | {
| |
| 62 | // | |
| 63 | draw_set_colour( invaderSeed ); | |
| 64 | // | |
| 65 | for ( int i=0; i<5*5; i++ ) | |
| 66 | {
| |
| 67 | if ( invader[i] != 0 ) | |
| 68 | {
| |
| 69 | int tx = x + i % 5; | |
| 70 | int ty = y + i / 5; | |
| 71 | // plot a pixel | |
| 72 | draw_plot( tx, ty ); | |
| 73 | } | |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | // | |
| 78 | void make_all_invaders( void ) | |
| 79 | {
| |
| 80 | draw_clear_screen( ); | |
| 81 | // | |
| 82 | int x = 16; | |
| 83 | int y = 16; | |
| 84 | // | |
| 85 | while ( true ) | |
| 86 | {
| |
| 87 | generateInvader( ); | |
| 88 | drawInvader( x, y ); | |
| 89 | // | |
| 90 | if ( x + 7 > target->w - 16 ) | |
| 91 | {
| |
| 92 | x = 16; | |
| 93 | y += 7; | |
| 94 | // | |
| 95 | if ( y + 7 > target->h - 16 ) | |
| 96 | break; | |
| 97 | } | |
| 98 | else | |
| 99 | // | |
| 100 | x += 7; | |
| 101 | } | |
| 102 | } |