#include "gbacode.cpp" int button = 0; int score = 100; int play = 1; int highScore = 0; int level = 1; int gunx = 0,guny = 0; int power = 25; int x = 0,y = 0; float fpower; void drawTerrain() { int segment, run; int oldx = 0, oldy = 0; int newx=0, newy=0; int incx=0, incy=0; //starting y location on left edge oldy = 100 + rand() % 50; //keep drawing segments until we reach right edge of screen while (newx < 240) { //segments are a run of pixels at a certain slope segment = rand() % 4 + 1; //get random x,y increments incx = rand() % 2 + 1; incy = rand() % 4 - 2; //draw some lines along this run for (run = 1; run < segment; run++) { newx = oldx + incx; newy = oldy + incy; //keep from going off the screen if (newy < 70) newy = 70; if (newy > 150) newy = 150; DrawLine3(oldx, oldy, newx, newy, BROWN); DrawLine3(oldx, oldy+1, newx, newy+1, BROWN); oldx = newx; oldy = newy; } } } void placeGun() { gunx = 20; for (guny=10; guny<160; guny++) { if (getPixel(gunx,guny) != BLACK) { drawbox(gunx-6,guny-4,gunx+6,guny+4,GREEN); break; } } DrawLine3(gunx,guny-4,gunx+6,guny-10,GREEN); } void placeTarget() { x = 215; y = 0; for (y=10; y<160; y++) { if (getPixel(x,y) != BLACK || getPixel(x+1,y) != BLACK) { drawbox(x-5,y-4,x+5,y+4,RED); break; } } } void recalcPower() { fpower = (float)(100 - power) / 1000.0f; } void printScore() { char s[7]; sprintf(s,"%i",score); print(200,150,s,WHITE); } void printLevel() { char s[7]; sprintf(s,"%i",level); print(10,1,s,WHITE); } void eraseLevel() { drawbox(1,1,30,10,BLACK); } void printHighScore() { char s[9]; sprintf(s,"%i",highScore); print(76,10,s,WHITE); } void playAgain() { clearScreen(); print (1,1,"PLAY AGAIN? (UP=YES DOWN=NO)",WHITE); print(1,10,"HIGHSCORE",WHITE); printHighScore(); while(button != 1) { if(buttonPressed(BUTTON_UP)) { play = 0; button = 1; clearScreen(); eraseLevel(); } else if(buttonPressed(BUTTON_DOWN)) { while(1) { clearScreen(); } } } } int main() { float shotx = 0,shoty = 0; int ishotx,ishoty; float velx; float vely; int firing; char s[40]; velx = 1.0f; vely = -1.8f; firing = 0; srand(time(NULL)); setMode(VIDEO_MODE_3); drawTerrain(); placeGun(); placeTarget(); recalcPower(); eraseLevel(); while(play == 1) { button = 0; waitRetrace(); //erase bottom text line drawbox(1,150,239,159,BLACK); //erase previous pixel DrawPixel3((int)shotx,(int)shoty,BLACK); //FIRE!! if (buttonPressed(BUTTON_A)) { if (!firing) { firing++; shotx = gunx + 8; shoty = guny - 12; vely = -1.8f; button = 1; } } if(button == 1) { playSound(s_boom); } //D-pad left decreases power if (buttonPressed(BUTTON_LEFT)) { if (!firing) { if (power > 1) { power -= 1; recalcPower(); } } } //D-pad right increases power if (buttonPressed(BUTTON_RIGHT)) { if (!firing) { if (power < 100) { power += 1; recalcPower(); } } } //start button resets game if (buttonPressed(BUTTON_START)) { clearScreen(); drawTerrain(); placeGun(); placeTarget(); } //update shot if (firing) { playSound(s_shot); vely += fpower; shotx += velx; ishotx = (int)shotx; shoty += vely; ishoty = (int)shoty; if (ishotx > 240 || ishoty > 160 || getPixel(ishotx,ishoty) != BLACK) { button = 0; firing = 0; } if(ishotx > 240 || ishoty > 160 || getPixel(ishotx,ishoty) == BROWN) { button = 0; score = score - 10; } if(getPixel(ishotx,ishoty) == BROWN) { button = 0; playSound(s_boom); } if(getPixel(ishotx,ishoty) == RED) { button = 0; playSound(s_boom); clearScreen(); drawTerrain(); placeGun(); placeTarget(); level = level + 1; eraseLevel(); } if(level == 7 || score == 0) { if(score > highScore) { highScore = score; } score = 100; playAgain(); } DrawPixel3(ishotx,ishoty,RED); } sprintf(s, "POW %i VELY %0.2f", power, vely); print(1,150,s,DKGRAY); print(158,150,"SCORE",WHITE); print(1,1,"L",BLUE); printLevel(); printScore(); } }