void win() { fillPalette(startScreen_palette, 0, STARTSCREEN_PALETTE_SIZE); drawImage4(0, 0, 240, 160, start); drawString4(90, 130, "YOU WIN!", PALETTE[2]); drawString4(60, 145, "PRESS <START> TO RESTART", PALETTE[2]); state = 2; }
void lose() { fillPalette(gameOverScreen_palette, 0, GAMEOVERSCREEN_PALETTE_SIZE); drawImage4(0, 0, 240, 160, gameOver); drawString4(90, 130, "TRY AGAIN?", PALETTE[2]); drawString4(60, 145, "PRESS <START> TO RESTART", PALETTE[2]); state = 2; }
static void drawCars() { for (int i = 0; i < numOfCars; i++) { if (cars[i].x + 30 <= 10) { cars[i].x = 220; } cars[i].x -= cars[i].dx; drawImage4(cars[i].x, cars[i].y, 16, 10, cars[i].img); } }
static void drawTrucks() { for (int i = 0; i < numOfTrucks; i++) { if (trucks[i].x >= 230) { trucks[i].x = 0; } trucks[i].x += trucks[i].dx; drawImage4(trucks[i].x, trucks[i].y, 128, 16, trucks[i].img); } }
static void drawWoods() { for (int i = 0; i < numOfWoods; i++) { if (woods[i].x + 30 <= 10) { woods[i].x = 220; } woods[i].x -= woods[i].dx; drawImage4(woods[i].x, woods[i].y, 42, 12, woods[i].img); } }
static void drawCrocodiles() { for (int i = 0; i < numOfCrocodiles; i++) { if (crocodiles[i].x >= 230) { crocodiles[i].x = 0; } crocodiles[i].x += crocodiles[i].dx; drawImage4(crocodiles[i].x, crocodiles[i].y, 46, 16, crocodiles[i].img); } }
int main() { REG_DISPCTL = MODE4 | BG2_ENABLE | BUFFER1FLAG; loadPalette(pics_palette, PICS_PALETTE_SIZE, 0); int state = BEGIN; while(1) { switch(state) { case BEGIN: // loadPalette(pics_palette, PICS_PALETTE_SIZE, 0); drawImage4(0, 0, 240, 160, (u16*)screen); waitForVblank(); FlipPage(); while(!KEY_DOWN_NOW(BUTTON_START)); state = GAME; break; case GAME: state = play(); break; case WIN: loadPalette(pics_palette, PICS_PALETTE_SIZE, 0); drawImage4(0, 0, 240, 160, (u16*)win); drawString4(5, 8, "Press Select", PALETTE[0]); waitForVblank(); FlipPage(); while(!KEY_DOWN_NOW(BUTTON_SELECT)); state = BEGIN; break; case LOSE: // loadPalette(pics_palette, PICS_PALETTE_SIZE, 0); drawImage4(0, 0, 240, 160, (u16*)die); waitForVblank(); FlipPage(); while(!KEY_DOWN_NOW(BUTTON_SELECT)); state = BEGIN; break; default: break; } } return 0; }
int main() { REG_DISPCNT = BG2_EN | MODE_4; int state = SPLASH; //CURSOR STUFF user.row = 80; user.col = 120; user.oldRow = user.row; user.oldCol = user.col; user.oldoldRow = user.row; user.oldoldCol = user.col; user.pixelState = deadYes; user.oldPixelState = deadNo; user.oldoldPixelState = deadNo; while(1) { switch(state) { case SPLASH: FlipPage(); setPalette(titleScreen_palette); drawImage4(0,0,240,160,titleScreen); FlipPage(); PALETTE[0] = deadNo; drawRect4(0,0,160,240,0); while(!KEY_DOWN_NOW(BUTTON_START)); while(KEY_DOWN_NOW(BUTTON_START)); FlipPage(); PALETTE[0] = deadNo; drawRect4(0,0,160,240,0); state = EDIT; break; case EDIT: editableBy(user); state = SIMULATE; break; case SIMULATE: simulating(); state = EDIT; break; } } return 0; }
int main() { //create obj //X megaman megamanX; megamanX.r = 62; megamanX.c = 30; megamanX.mapX = megamanX.c; megamanX.mapY = megamanX.r; megamanX.inAir = 0; megamanX.lives = 20; //blast Blast blast;; blast.r = 0; blast.c = megamanX.c; blast.fired = 0; blast.speed = 8; //drlight Drlight drlight; drlight.r = 0; drlight.c = 0; //minion Minion minion; minion.r = 0; minion.c = 0; minion.lives = 5; //turret Turret turret; turret.r = 0; turret.c = 0; turret.lives = 10; //bullet Bullet bullet; bullet.r = 0; bullet.c = 0; //worldMap ground set up WorldMap worldmap[1688]; for(int x=0; x<535; x++) { worldmap[x].y = 94; } for(int x=545; x<905; x++) { worldmap[x].y = 74; } for(int x=921; x<1688; x++) { worldmap[x].y = 84; } // 1. Set up REG_DISPCNT // Enable objects (sprites) (by setting bit 10) and set the sprite dimension type (given to you by nin10kit) // 2. Copy the palette given to into the sprite palette (SPRITEPAL) // 3 Copy the sprite tile graphics to character block 5 // 4. For each of the 128 sprites set its attribute 0 to hide them (ATTR0_HIDE) // 5. When you want to use a sprite pick a OamEntry. // a. Modify its attribute 0 with its x and or this with the palette type (from nin10kit) and shape (nin10kit) // b. Modify its attribute 1 with its y and size (nin10kit) // c. Modify its attribute 2 with the sprite id (nin10kit) REG_DISPCTL = MODE4 | BG2_ENABLE | OBJ_ENABLE | X_DIMENSION_TYPE; DMA[3].src = X_palette; DMA[3].dst = SPRITEPAL; DMA[3].cnt = DMA_ON | X_PALETTE_SIZE; DMA[3].src = X; DMA[3].dst = SPRITEDATA; DMA[3].cnt = DMA_ON | X_SIZE; //copy OamEntry shadow[128]; //make all sprites transparent for(int i=0; i<128; i++) { shadow[i].attr0 = ATTR0_HIDE; } //helper //((SPRITEMEM[0].attr0 &~ 0x00FF)|(20)) //((SPRITEMEM[1].attr0 &~ 0x01FF)|(0)) int state=0; int hold=0; while(1) { if(gameState==0) { DMA[3].src = title_palette; DMA[3].dst = PALETTE; DMA[3].cnt = DMA_ON | TITLE_PALETTE_SIZE; stage_stop(); megaman_play(); } while(gameState==0) { drawImage4(0,0,240,160,title); if(KEY_DOWN_NOW(BUTTON_A) || KEY_DOWN_NOW(BUTTON_START)) { gameState=1; megaman_stop(); break; } waitForVblank(); flipPage(); } if(gameState==1) { //setup stage palette DMA[3].src = stage_palette; DMA[3].dst = PALETTE; DMA[3].cnt = DMA_ON | STAGE_PALETTE_SIZE; //music stage_play(); } while(gameState==1) { if(KEY_DOWN_NOW(BUTTON_SELECT)) { for(int i=0; i<128; i++) { shadow[i].attr0 = ATTR0_HIDE; } hurting=0; minionSpawn=0; turretSpawn=0; turretFired=0; megamanX.r = 62; megamanX.c = 30; megamanX.mapX = megamanX.c; megamanX.mapY = megamanX.r; megamanX.inAir = 0; megamanX.lives = 20; started=0; telCount=0; gameState=0; } //conversation if(started==0) { drawStage(30,STAGE_WIDTH,STAGE_HEIGHT,stage); switch (state) { case 0: drawHollowRect4(100,0,59,239,0); drawRect4(102,2,56,236,140); char buffer[50]; sprintf(buffer, "X:"); drawString4(105,5,buffer,0); sprintf(buffer, "What happened here?"); drawString4(120,5,buffer,0); if(KEY_DOWN_NOW(BUTTON_A) && hold==0) { state=1; hold=1; } break; case 1: drawHollowRect4(100,0,59,239,0); drawRect4(102,2,56,236,140); char buffer2[50]; sprintf(buffer2, "X:"); drawString4(105,5,buffer2,0); sprintf(buffer2, "The highway is destroyed!"); drawString4(120,5,buffer2,0); if(KEY_DOWN_NOW(BUTTON_A) && hold==0) { state=2; hold=1; } break; case 2: drawHollowRect4(100,0,59,239,0); drawRect4(102,2,56,236,140); char buffer3[50]; sprintf(buffer3, "X:"); drawString4(105,5,buffer3,0); sprintf(buffer3, "I need to turn in my CS2110 homework!"); drawString4(120,5,buffer3,0); if(KEY_DOWN_NOW(BUTTON_A) && hold==0) { state=3; hold=1; } break; case 3: drawHollowRect4(100,0,59,239,0); drawRect4(102,2,56,236,140); char buffer4[50]; sprintf(buffer4, "Press Right to walk, Up to jump,"); drawString4(105,5,buffer4,0); char buffer5[50]; sprintf(buffer5, "B to dash, A to fire."); drawString4(120,5,buffer5,0); if(KEY_DOWN_NOW(BUTTON_A) && hold==0) { started=1; state=0; hold=1; } break; } if(!KEY_DOWN_NOW(BUTTON_A)) { hold=0; } } //teleports if(started==1) { drawStage(30,STAGE_WIDTH,STAGE_HEIGHT,stage); if(telCount<80) { drawRect4(telCount,megamanX.c-1+14,30,6,149); drawRect4(telCount+1,megamanX.c+14,26,2,0); telCount+=8; } else if(telCount>=80 && telCount<=120) { shadow[2].attr0 = megamanX.r | X_PALETTE_TYPE | X_SPRITE_SHAPE; shadow[2].attr1 = megamanX.c | X_SPRITE_SIZE; shadow[2].attr2 = CHARGES_ID; telCount+=8; } else { shadow[2].attr0 = ATTR0_HIDE; //draw megaman shadow[0].attr0 = megamanX.r | X_PALETTE_TYPE | X_SPRITE_SHAPE; shadow[0].attr1 = megamanX.c | X_SPRITE_SIZE; shadow[0].attr2 = X_ID; started=2; telCount=0; } } //in Game if(started==2) { //update background moveScreen(megamanX.mapX); //status check if(!borderCheck(megamanX.r, megamanX.c)) { megamanX.lives-=1; hurting=1; } if(!checkHealth(&megamanX)) { //need delay here // for(int i=0; i<160;i++) { // hurt(&megamanX, shadow); // } for(int i=0; i<128; i++) { shadow[i].attr0 = ATTR0_HIDE; } refreshBlast(&megamanX, &blast, shadow, 1); hurting=0; minionSpawn=0; turretSpawn=0; turretFired=0; started=0; gameState=3; } //buttons if(KEY_DOWN_NOW(BUTTON_A) && hurting==0) { if(charge<102) charge++; if(charge>20) { drawCharges(&megamanX, shadow); } } if(!KEY_DOWN_NOW(BUTTON_A) && charge<=20 && charge>0 && hurting==0) { blast.fired = 1; blast.r = megamanX.r; charge = 0; blastType = 0; } else if(!KEY_DOWN_NOW(BUTTON_A) && charge<=100 && charge>20 && hurting==0) { blast.fired = 1; blast.r = megamanX.r; charge = 0; blastType = 1; shadow[2].attr0 = ATTR0_HIDE; chargesDelay = 0; } else if(!KEY_DOWN_NOW(BUTTON_A) && charge<=102 && charge>100 && hurting==0) { blast.fired = 1; blast.r = megamanX.r; charge = 0; blastType = 2; shadow[2].attr0 = ATTR0_HIDE; chargesDelay = 0; } if((KEY_DOWN_NOW(BUTTON_B) || (KEY_DOWN_NOW(BUTTON_B) && KEY_DOWN_NOW(BUTTON_RIGHT))) && stopMove==0 && hurting==0) { dash(&megamanX, shadow); moving = 2; } if(KEY_DOWN_NOW(BUTTON_RIGHT) && !KEY_DOWN_NOW(BUTTON_UP) && !KEY_DOWN_NOW(BUTTON_LEFT) && !KEY_DOWN_NOW(BUTTON_B) && megamanX.inAir==0 && hurting==0) { stopMove = 0; dashCounter = 0; megamanX.mapX++; walk(&megamanX, shadow); moving = 1; } if(KEY_DOWN_NOW(BUTTON_LEFT) && !KEY_DOWN_NOW(BUTTON_UP) && !KEY_DOWN_NOW(BUTTON_RIGHT) && !KEY_DOWN_NOW(BUTTON_B) && megamanX.inAir==0 && hurting==0) { stopMove = 0; dashCounter = 0; if(megamanX.mapX-25>0) megamanX.mapX--; walk(&megamanX, shadow); moving = -1; } if(KEY_DOWN_NOW(BUTTON_RIGHT) && KEY_DOWN_NOW(BUTTON_UP) && megamanX.inAir==0 && hurting==0) { megamanX.inAir = 1; jumpForward = 1; moving = 1; } if(KEY_DOWN_NOW(BUTTON_UP) && megamanX.inAir==0 && hurting==0) { megamanX.inAir = 1; moving = 0; } //default position if(!KEY_DOWN_NOW(BUTTON_START) && !KEY_DOWN_NOW(BUTTON_SELECT) && !KEY_DOWN_NOW(BUTTON_B) && !KEY_DOWN_NOW(BUTTON_R) && !KEY_DOWN_NOW(BUTTON_L) && !KEY_DOWN_NOW(BUTTON_LEFT) && !KEY_DOWN_NOW(BUTTON_RIGHT) && !KEY_DOWN_NOW(BUTTON_DOWN) && !KEY_DOWN_NOW(BUTTON_UP) && megamanX.inAir==0 && hurting==0) { stopMove = 0; jumpCounter = 0; dashCounter = 0; shadow[0].attr2 = X_ID; moving = 0; } //draw blast drawBlast(&blast, &megamanX, shadow); //gap check when walking if(((megamanX.mapX>=535 && megamanX.mapX<=545) || (megamanX.mapX>=905 && megamanX.mapX<=921))) { if(megamanX.inAir==0) { megamanX.inAir=1; falling=1; jumpCounter=4; } } if(megamanX.inAir==1) { jump(&megamanX, shadow); } if(falling==1) { groundCheck(&megamanX, worldmap); } geoCheck(&megamanX); drawLives(&megamanX); //restore if(hurtTime<=0) { restore(shadow); } blastCollision(&megamanX, &blast, &minion, &turret,shadow); if(explosionDelay==0) { shadow[2].attr0 = ATTR0_HIDE; explosionDelay=12; } else { explosionDelay--; } //player collision if(hurting==0) { playerCollision(&megamanX, &minion, &turret, &bullet, shadow); } //check hurt is second priority if(hurting==1) { hurt(&megamanX, shadow); } // for testing // char string[50]; // char string2[50]; // sprintf(string, "world x: %d, y: %d", megamanX.mapX, worldmap[megamanX.mapX].y-32); // sprintf(string2, "I am x: %d, y: %d", megamanX.mapX, megamanX.r); // drawString4(10,0,string,5); // drawString4(20,0,string2,5); //Enemy if(((megamanX.mapX>=50 && megamanX.mapX<=250) || (megamanX.mapX>=922 && megamanX.mapX<=1122) || (megamanX.mapX>=1200 && megamanX.mapX<=1300)) && minionSpawn==0) { //more condition here to spawn in area minionSpawn = 1; minion.lives=5; minion.c = megamanX.mapX%240+240; if(megamanX.mapX>=50 && megamanX.mapX<=250) { minion.r = 67; //it's short } else { minion.r = 57; } } if(minionSpawn==1) { drawMinion(&minion, shadow); if(!borderCheck(minion.r,minion.c)) { minionSpawn=0; shadow[3].attr0 = ATTR0_HIDE; } if(moving!=0 && hurting==0) { minion.c-=moving; } } //turret if(((megamanX.mapX>=300 && megamanX.mapX<=650) || (megamanX.mapX>=950 && megamanX.mapX<=1050)) && turretSpawn==0) { turretSpawn = 1; turret.lives = 10; turret.c = megamanX.mapX%240+240; if(megamanX.mapX>=300 && megamanX.mapX<=650) { turret.r = 36; //it's short } else { turret.r = 46; } } if(turretSpawn==1) { drawTurret(&turret, shadow); if(!borderCheck(turret.r,turret.c)) { turretSpawn=0; shadow[4].attr0 = ATTR0_HIDE; } else if(turretFired==0) { bullet.c = turret.c-10; if(megamanX.mapX>=300 && megamanX.mapX<=650) { bullet.r = 45; //it's short } else { bullet.r = 55; } } if(moving!=0 && hurting==0) { bullet.c-=moving; } } //fire if(turretFired==1) { bulletAnimate(&bullet, shadow); } if(!borderCheck(bullet.r,bullet.c)) { turretFired=0; shadow[5].attr0 = ATTR0_HIDE; } else if(turretSpawn==1) { if(bulletDelay<=0) { turretFired=1; bulletDelay=100; } else { bulletDelay--; } } if(moving!=0 && hurting==0) { turret.c-=moving; } //spawn drlight when reach end if(megamanX.mapX>=STAGE_WIDTH-270) { //more condition here to spawn in area started=3; } } if(started==3) { for(int i=0; i<128; i++) { shadow[i].attr0 = ATTR0_HIDE; } drawStage(STAGE_WIDTH-270,STAGE_WIDTH,STAGE_HEIGHT,stage); shadow[0].attr0 = 52 | X_PALETTE_TYPE | X_SPRITE_SHAPE; shadow[0].attr1 = 30 | X_SPRITE_SIZE; shadow[0].attr2 = X_ID; drlight.c = megamanX.c+160; drlight.r = megamanX.r; shadow[6].attr0 = drlight.r | X_PALETTE_TYPE | X_SPRITE_SHAPE; shadow[6].attr1 = drlight.c | X_SPRITE_SIZE; shadow[6].attr2 = DRLIGHT_ID; switch (state) { case 0: drawHollowRect4(100,0,59,239,0); drawRect4(102,2,56,236,140); char buffer[50]; sprintf(buffer, "X:"); drawString4(105,5,buffer,0); sprintf(buffer, "Professor!"); drawString4(120,5,buffer,0); if(KEY_DOWN_NOW(BUTTON_A) && hold==0) { state=1; hold=1; } break; case 1: drawHollowRect4(100,0,59,239,0); drawRect4(102,2,56,236,140); char buffer2[50]; sprintf(buffer2, "Professor Light:"); drawString4(105,5,buffer2,0); sprintf(buffer2, "Finally, you have come."); drawString4(120,5,buffer2,0); if(KEY_DOWN_NOW(BUTTON_A) && hold==0) { state=2; hold=1; } break; case 2: drawHollowRect4(100,0,59,239,0); drawRect4(102,2,56,236,140); char buffer3[50]; sprintf(buffer3, "X:"); drawString4(105,5,buffer3,0); sprintf(buffer3, "Here's my assignment"); drawString4(120,5,buffer3,0); if(KEY_DOWN_NOW(BUTTON_A) && hold==0) { state=3; hold=1; } break; case 3: drawHollowRect4(100,0,59,239,0); drawRect4(102,2,56,236,140); char buffer4[50]; sprintf(buffer4, "Professor Light:"); drawString4(105,5,buffer4,0); char buffer5[50]; sprintf(buffer5, "Goodjob X."); drawString4(120,5,buffer5,0); if(KEY_DOWN_NOW(BUTTON_A) && hold==0) { started=4; state=0; hold=1; break; } break; } if(!KEY_DOWN_NOW(BUTTON_A)) { hold=0; } } if(started==4) { drawStage(STAGE_WIDTH-270,STAGE_WIDTH,STAGE_HEIGHT,stage); if(telCount<40) { //hide megaman shadow[0].attr0 = ATTR0_HIDE; shadow[2].attr0 = megamanX.r | X_PALETTE_TYPE | X_SPRITE_SHAPE; shadow[2].attr1 = megamanX.c | X_SPRITE_SIZE; shadow[2].attr2 = CHARGES_ID; telCount+=8; } else if(telCount>=40 && telCount<=120) { shadow[2].attr0 = ATTR0_HIDE; drawRect4(121-telCount,megamanX.c-1+14,30,6,149); drawRect4(121-telCount+1,megamanX.c+14,26,2,0); telCount+=8; } else { for(int i=0; i<128; i++) { shadow[i].attr0 = ATTR0_HIDE; } megamanX.r = 62; megamanX.c = 30; megamanX.mapX = megamanX.c; megamanX.mapY = megamanX.r; megamanX.inAir = 0; megamanX.lives = 20; started=0; telCount=0; gameState=2; } } waitForVblank(); flipPage(); //update spritememory DMA[3].src = shadow; DMA[3].dst = SPRITEMEM; DMA[3].cnt = 128 * 4 | DMA_ON; } if(gameState==2) { stage_stop(); DMA[3].src = win_palette; DMA[3].dst = PALETTE; DMA[3].cnt = DMA_ON | WIN_PALETTE_SIZE; } while(gameState==2) { drawImage4(0,0,240,160,win); if(KEY_DOWN_NOW(BUTTON_A) || KEY_DOWN_NOW(BUTTON_START) || KEY_DOWN_NOW(BUTTON_SELECT)) { gameState=0; break; } waitForVblank(); flipPage(); } if(gameState==3) { stage_stop(); DMA[3].src = lost_palette; DMA[3].dst = PALETTE; DMA[3].cnt = DMA_ON | LOST_PALETTE_SIZE; } while(gameState==3) { drawImage4(0,0,240,160,lost); if(KEY_DOWN_NOW(BUTTON_A) || KEY_DOWN_NOW(BUTTON_START)) { hurting=0; megamanX.r = 62; megamanX.c = 30; megamanX.mapX = megamanX.c; megamanX.mapY = megamanX.r; megamanX.inAir = 0; megamanX.lives = 20; started=0; telCount=0; gameState=1; break; } if(KEY_DOWN_NOW(BUTTON_SELECT)) { megamanX.r = 62; megamanX.c = 30; megamanX.mapX = megamanX.c; megamanX.mapY = megamanX.r; megamanX.inAir = 0; megamanX.lives = 20; started=0; telCount=0; gameState=0; break; } waitForVblank(); flipPage(); } } }
static void drawFrog() { drawImage4(frog.x, frog.y, 14, 14, prevImg); if (KEY_DOWN_NOW(KEY_LEFT)) { frog.x -= frog.dx; drawImage4(frog.x, frog.y, 14, 14, frog.imgL1); drawImage4(frog.x, frog.y, 14, 14, frog.imgL2); drawImage4(frog.x, frog.y, 14, 14, frog.imgL3); prevImg = frog.imgL1; } else if (KEY_DOWN_NOW(KEY_RIGHT)) { frog.x += frog.dx; drawImage4(frog.x, frog.y, 14, 14, frog.imgR1); drawImage4(frog.x, frog.y, 14, 14, frog.imgR2); drawImage4(frog.x, frog.y, 14, 14, frog.imgR3); prevImg = frog.imgR1; } else if (KEY_DOWN_NOW(KEY_UP)) { frog.y -= frog.dy; drawImage4(frog.x, frog.y, 14, 14, frog.imgU1); drawImage4(frog.x, frog.y, 14, 14, frog.imgU2); drawImage4(frog.x, frog.y, 14, 14, frog.imgU3); prevImg = frog.imgU1; } else if (KEY_DOWN_NOW(KEY_DOWN)) { frog.y += frog.dy; drawImage4(frog.x, frog.y, 14, 14, frog.imgD1); drawImage4(frog.x, frog.y, 14, 14, frog.imgD2); drawImage4(frog.x, frog.y, 14, 14, frog.imgD3); prevImg = frog.imgD1; } }
int play() { int score = 0; int speed = 1; int life = 1; int timer = 10; int pT = 0; char lifes[15]; char scores[15]; BIRD bird; bird.r = 70; bird.c = BIRD_C; // loadPalette(pics_palette, PICS_PALETTE_SIZE, 0); drawImage4(0, 0, 240, 160, (u16*)background); FlipPage(); drawImage4(0, 0, 240, 160, (u16*)background); PIPE pp[4]; for(int i = 0; i < 4; i++) { pp[i].r = 10; pp[i].c = 219 - i*60; pp[i].hole = 25 + rand() / (RAND_MAX / (145 - HOLE_L - 25 + 1) + 1); pp[i].on = 0; if(i == 0) { pp[i].on = 1; } } int frame = 0; while(1) { if(KEY_DOWN_NOW(BUTTON_SELECT)) { return 0; } if(KEY_DOWN_NOW(BUTTON_A)) { speed = -5; } if(timer == 0) { bird.r += speed; speed++; timer = 3; } timer--; if(pT == 0) { pp[0].on = 1; } else { pT--; } if(bird.r < 10) { bird.r = 10; } if(bird.r > 149) { life--; if (life < 0) { return LOSE; } bird.r = 70; speed = 1; timer = 2; } for(int i = 1; i < 4; i++) { if(pp[0].c < 220 - i*60 && !pp[i].on) { pp[i].c = 219; pp[i].on = 1; } } for(int i = 0; i < 4; i++) { if(pp[i].on) { pp[i].c -= 1; if(bird.c == (pp[i].c + PIPE_W / 2) && pp[i].on) { score++; if(score == 5) { return WIN; } } if(pp[i].c < 0) { pp[i].c = 219; pp[i].hole = 25 + rand() / (RAND_MAX / (145 - HOLE_L - 25 + 1) + 1); pT = 20; pp[i].on = 0; } //collision if(bird.c + 14 > pp[i].c && bird.c < pp[i].c + 20 && ((bird.r < pp[i].hole) | (bird.r + 10 > pp[i].hole + HOLE_L)) && pp[i].on) { life--; if (life < 0) { return LOSE; } bird.r = pp[i].hole + 2; speed = 1; timer = 2; } } } drawImage4(0, 0, 240, 160, (u16*)background); delay(3); //draw pipe for(int i = 0; i < 4; i++) { if(pp[i].on) { drawPipe(pp[i].r, pp[i].c, pp[i].hole, (u16*)pipes); } } //draw bird if(frame % 9 == 0) { drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy0); } else if(frame % 9 == 1) { drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy1); } else if(frame % 9 == 2) { drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy2); } else if(frame % 9 == 3) { drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy3); } else if(frame % 9 == 4) { drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy4); } else if(frame % 9 == 5) { drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy5); } else if(frame % 9 == 6) { drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy6); } else if(frame % 9 == 7) { drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy7); } else { drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy8); } // drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy0); // drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy1); // drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy2); // drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy3); // drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy4); // drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy5); // drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy6); // drawImage4(bird.r, bird.c, BIRD_W, BIRD_H, (u16*)flappy7); sprintf(lifes, "Life: %d", life); drawString4(0, 10, lifes, PALETTE[0]); sprintf(scores, "Score: %d", score); drawString4(0, 170, scores, PALETTE[0]); waitForVblank(); frame++; FlipPage(); } return 1; }
static void drawBG() { fillScreen4(200); drawImage4(10, 10, 220, 140, frogBG); }
int main(void) { for (int i = 0; i < SCREEN_PALETTE_SIZE; i++) { PALETTE[i] = screen_palette[i]; } for (int j = 129; j < PLAYSCREEN_PALETTE_SIZE; j++) { PALETTE[j] = playscreen_palette[j]; } REG_DISPCTL = MODE4 | BG2_ENABLE ; int state = START; while(1) { switch(state) { case START: setPalette(screen_palette, SCREEN_PALETTE_SIZE); drawImage4(0, 0, HOMESCREEN_WIDTH, HOMESCREEN_HEIGHT, homescreen); // drawString4(90, 100, "HELLO!", PALETTE[100]); life = 3; frog.x = 146; frog.y = 120; redcar1.x = 127; redcar1.y = 190; yellowcar1.x = 108; yellowcar1.y = 60; bluecar1.x = 90; bluecar1.y = 140; flower1.x = 60; flower1.y = 0; tree1.x = 46; tree1.y = 150; flower2.x = 32; flower2.y = 60; tree2.x = 18; tree2.y = 210; if(KEY_DOWN_NOW(BUTTON_START)) { state = PLAY; // for (int i = 0; i < PLAYSCREEN_PALETTE_SIZE; i++) { // PALETTE[i] = playscreen_palette[i]; // } setPalette(playscreen_palette, PLAYSCREEN_PALETTE_SIZE); drawImage4(0, 0, BACKGROUND_WIDTH, BACKGROUND_HEIGHT, background); //drawImage4(100, 100, FROG1_WIDTH, FROG1_HEIGHT, frog1); //drawImage4(30, 30, FLOWER_WIDTH, FLOWER_HEIGHT, flower); flipPage(); } break; case PLAY: state = play(); break; case GAMEWIN: setPalette(screen_palette,SCREEN_PALETTE_SIZE); drawImage4(0, 0, WINSCREEN_WIDTH, WINSCREEN_HEIGHT, winscreen); if(KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } break; case GAMELOSE: setPalette(screen_palette,SCREEN_PALETTE_SIZE); drawImage4(0, 0, GAMEOVER_WIDTH, GAMEOVER_HEIGHT, gameover); if(KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } break; } waitForVblank(); flipPage(); } return(0); }
void startGame() { fillPalette(startScreen_palette, 0, STARTSCREEN_PALETTE_SIZE); drawImage4(0, 0, 240, 160, start); drawString4(30, 140, "PRESS <START> TO PLAY", PALETTE[3]); }
void menuLogic(){ int stillHere=1; usr temp[1]; temp->r=mouse->r; temp->c=mouse->c; mouse->r=140; mouse->c=20; drawBoard(); while(stillHere){ if(KEY_DOWN_NOW(BUTTON_SELECT)){ while(KEY_DOWN_NOW(BUTTON_SELECT)); toReset=1; } if(KEY_DOWN_NOW(BUTTON_UP)){ delay(10); //while(KEY_DOWN_NOW(BUTTON_UP)) if(temp->r>50){ mouse->r=temp->r; mouse->c=temp->c; } else{ mouse->r=(UpperLeftRow+MAXR*4-5); mouse->c=UpperLeftCol-2; } drawBoard(); stillHere=0; continue; } if(KEY_DOWN_NOW(BUTTON_DOWN)){ delay(10); //while(KEY_DOWN_NOW(BUTTON_DOWN)) if(temp->r<50){ mouse->r=temp->r; mouse->c=temp->c; } else{ mouse->r=UpperLeftRow-1; mouse->c=UpperLeftCol-2; } drawBoard(); stillHere=0; continue; } if(KEY_DOWN_NOW(BUTTON_RIGHT)){ delay(10); //while(KEY_DOWN_NOW(BUTTON_RIGHT)); if (mouse->c>=170){ ; } else{mouse->c+=30;} drawBoard(); } if(KEY_DOWN_NOW(BUTTON_LEFT)){ delay(10); //while(KEY_DOWN_NOW(BUTTON_LEFT)); if (mouse->c<39){ ; } else{mouse->c-=30;} drawBoard(); } if(KEY_DOWN_NOW(BUTTON_A)){ while(KEY_DOWN_NOW(BUTTON_A)); if (mouse->c==20){ int check=1; playing=1; while(check){ delay(20); nextArray(); drawBoard(); if(KEY_DOWN_NOW(BUTTON_A)){ while(KEY_DOWN_NOW(BUTTON_A)); check=0; } } playing=0; } if (mouse->c==50){ clear(); drawBoard(); } if (mouse->c==80){ clear(); a[10][15]=1; a[10][25]=1; a[17][12]=1; a[18][13]=1; a[19][14]=1; a[20][15]=1; a[21][16]=1; a[22][17]=1; a[22][18]=1; a[22][19]=1; a[22][20]=1; a[22][21]=1; a[22][22]=1; a[22][23]=1; a[21][24]=1; a[20][25]=1; a[19][26]=1; a[18][27]=1; a[17][28]=1; drawBoard(); } if (mouse->c==110){ clear(); a[15][20]=1; a[12][20]=1; a[13][20]=1; a[13][19]=1; a[13][21]=1; a[17][20]=1; a[17][19]=1; a[17][21]=1; a[18][20]=1; drawBoard(); } if (mouse->c==140){ nextArray(); drawBoard(); } if (mouse->c==170){ if (mode==NORMAL){ clear(); int m; int n; for(m=0;m<MAXR;m++){ for(n=0;n<MAXC;n++){ prevA[m][n]=0; } } mode=NEXT; drawImage4(2,5,50,12,nextModeButton); drawImage4(140,177,50,12,normalModeButton); } else { mode=NORMAL; drawImage4(2,5,50,12,normalModeButton); drawImage4(140,177,50,12,nextModeButton); } } } drawBoard(); } }
void drawTitle() { setPalette(title_palette, TITLE_PALETTE_SIZE, 0); drawImage4(0, 0, TITLE_WIDTH, TITLE_HEIGHT, title); while (!KEY_DOWN_NOW(BUTTON_START)); drawRect4(0, 0, SCREENWIDTH, SCREENHEIGHT, BLACKINDEX); }
void drawGhost(int x, int y) { drawImage4(x, y, GHOST_WIDTH, GHOST_HEIGHT, ghost); }
void drawPacman(int x, int y) { drawImage4(x, y, PACMAN_WIDTH, PACMAN_HEIGHT, Pacman); }
static int gotHome(int isSafe) { if (homeFlags[0] == 0 && frog.y >= 10 && frog.y <= 30 && frog.x >= 10 && frog.x <= 25) { drawString4(95, 75, "YOU ARE SAVE NOW!", PALETTE[1]); flipPage(); homeFlags[0] = 1; loadAvatar(); isSafe = 1; delay(20); } if (homeFlags[0]) { drawImage4(18, 15, 14, 14, frog.imgU1); } if (homeFlags[1] == 0 && frog.y >= 10 && frog.y <= 30 && frog.x >= 60 && frog.x <= 70) { drawString4(95, 75, "YOU ARE SAVE NOW!", PALETTE[1]); flipPage(); homeFlags[1] = 1; loadAvatar(); isSafe = 1; delay(20); } if (homeFlags[1]) { drawImage4(66, 15, 14, 14, frog.imgU1); } if (homeFlags[2] == 0 && frog.y >= 10 && frog.y <= 30 && frog.x >= 108 && frog.x <= 118) { drawString4(95, 75, "YOU ARE SAVE NOW!", PALETTE[1]); flipPage(); homeFlags[2] = 1; loadAvatar(); isSafe = 1; delay(20); } if (homeFlags[2]) { drawImage4(114, 15, 14, 14, frog.imgU1); } if (homeFlags[3] == 0 && frog.y >= 10 && frog.y <= 30 && frog.x >= 155 && frog.x <= 165) { drawString4(95, 75, "YOU ARE SAVE NOW!", PALETTE[1]); flipPage(); homeFlags[3] = 1; loadAvatar(); isSafe = 1; delay(20); } if (homeFlags[3]) { drawImage4(160, 15, 14, 14, frog.imgU1); } if (homeFlags[4] == 0 && frog.y >= 10 && frog.y <= 30 && frog.x >= 195 && frog.x <= 210) { drawString4(95, 75, "YOU ARE SAVE NOW!", PALETTE[1]); flipPage(); homeFlags[4] = 1; loadAvatar(); isSafe = 1; delay(20); } if (homeFlags[4]) { drawImage4(208, 15, 14, 14, frog.imgU1); } if (homeFlags[0] && homeFlags[1] && homeFlags[2] && homeFlags[3] && homeFlags[4]) { win(); } return isSafe; }
void drawEnd() { setPalette(end_palette, END_PALETTE_SIZE, 0); drawImage4(0, 0, END_WIDTH, END_HEIGHT, end); while (!KEY_DOWN_NOW(BUTTON_START)); main(); }
int play() { drawFrog(); while(1) { drawImage4(0, 0, BACKGROUND_WIDTH, BACKGROUND_HEIGHT, background); // if(life < 61 && life >= 45) { // lifeCount = 3; // } else if (life < 45 && life >= 30){ // lifeCount = 2; // } else if (life < 30 && life > 10) { // lifeCount = 1; // } char str[15]; drawString4 (4, 8, "Your life: ", PALETTE[11]); sprintf (str, "%d", life); drawString4 (4, 70, str, PALETTE[11]); if(KEY_DOWN_NOW(BUTTON_RIGHT)) { frog.y = frog.y + 1; drawFrog(); flipPage(); } else if (KEY_DOWN_NOW(BUTTON_LEFT)) { frog.y = frog.y - 1; drawFrog(); flipPage(); } else if (KEY_DOWN_NOW(BUTTON_UP)) { frog.x = frog.x - 1; // drawImage4(frog.x, frog.y, FROG1_WIDTH, FROG1_HEIGHT, frog1); // drawImage4(frog.x, frog.y, FROG1_WIDTH, FROG1_HEIGHT, frog2); // drawImage4(frog.x, frog.y, FROG1_WIDTH, FROG1_HEIGHT, frog3); // drawImage4(frog.x, frog.y, FROG1_WIDTH, FROG1_HEIGHT, frog4); // drawImage4(frog.x, frog.y, FROG1_WIDTH, FROG1_HEIGHT, frog1); drawFrog(); flipPage(); } else if (KEY_DOWN_NOW(BUTTON_DOWN)) { frog.x = frog.x + 1; drawFrog(); flipPage(); } if (KEY_DOWN_NOW(BUTTON_SELECT)) { return START; } waitForVblank(); if ((frog.x == 0)) { return GAMEWIN; } drawOther(); redcar1.x %= 160; yellowcar1.x %= 160; bluecar1.x %= 160; flower1.x %= 160; tree1.x %= 160; flower2.x %= 160; tree2.x %= 160; redcar1.y %= 240; yellowcar1.y %= 240; bluecar1.y %= 240; flower1.y %= 240; tree1.y %= 240; flower2.y %= 240; tree2.y %= 240;; if(((((frog.x >= redcar1.x - 7)&&(frog.x <= redcar1.x +7)) && ((frog.y >= redcar1.y-7) && (frog.y <= redcar1.y + 7)))) || ((((frog.x >= yellowcar1.x - 7)&&(frog.x <= yellowcar1.x +7)) && ((frog.y >= yellowcar1.y-7) && (frog.y <= yellowcar1.y + 7)))) || ((((frog.x >= bluecar1.x - 7)&&(frog.x <= bluecar1.x +7)) && ((frog.y >= bluecar1.y-7) && (frog.y <= bluecar1.y + 7)))) || ((((frog.x >= flower1.x - 7)&&(frog.x <= flower1.x +7)) && ((frog.y >= flower1.y-7) && (frog.y <= flower1.y + 7)))) || ((((frog.x >= tree1.x - 7)&&(frog.x <= tree1.x +7)) && ((frog.y >= tree1.y-7) && (frog.y <= tree1.y + 7)))) || ((((frog.x >= flower2.x - 7)&&(frog.x <= flower2.x +7)) && ((frog.y >= flower2.y-7) && (frog.y <= flower2.y + 7)))) || ((((frog.x >= tree2.x - 7)&&(frog.x <= tree2.x +7)) && ((frog.y >= tree2.y-7) && (frog.y <= tree2.y + 7))))) { frog.x = 146; frog.y = 120; drawFrog(); life -= 1; if (life == 0) { return GAMELOSE; } if (frog.x == 0) { return GAMEWIN; } } } return 0; }
void drawBoard(){ if(toReset){ int i; mode=NORMAL; playing=0; for (i=0;i<2;i++){ PALETTE[i]=LifeGame_palette[i]; } drawImage4(0,0,240,160,LifeGame); FlipPage(); while(!(KEY_DOWN_NOW(BUTTON_START))){ } FlipPage(); u16 colors[]={BLACK,LTGREY,RED,GREEN,BLUE,WHITE,DKGREY,YELLOW}; int numcolors = sizeof(colors)/sizeof(colors[0]); for (i=0;i<numcolors;i++){ PALETTE[i]=colors[i]; } clear();//Clear array to start game mouse->r=UpperLeftRow-1; mouse->c=UpperLeftCol-2; toReset=0; } else{ drawRect4(0,0,240,160,LTGREYINDEX); drawImage4(140,58,20,12,clearButton); drawImage4(140,80,20,12,One); drawImage4(140,110,20,12,Two); drawImage4(140,145,20,12,next); if(playing)drawImage4(140,30,12,12,pause); else drawImage4(140,30,12,12,play); if (mode==NORMAL){ drawImage4(2,5,50,12,normalModeButton); drawImage4(140,177,50,12,nextModeButton); } else{ drawImage4(2,5,50,12,nextModeButton); drawImage4(140,177,50,12,normalModeButton); } drawMouse(mouse->r,mouse->c,5,5,BLUEINDEX); int i; int j; if (mode==NORMAL){ for (i=0;i<MAXR;i++){ for (j=0;j<MAXC;j++){ if (a[i][j])drawRect4(i*4+UpperLeftRow,j*4+UpperLeftCol,3,3,GREENINDEX); else drawRect4(i*4+UpperLeftRow,j*4+UpperLeftCol,3,3,BLACKINDEX); } } } else{ int n; int m; for(n=0;n<MAXR;n++){ for(m=0;m<MAXC;m++){ oldA[n][m]=prevA[n][m]; } } nextArray(); for (i=0;i<MAXR;i++){ for (j=0;j<MAXC;j++){ if (a[i][j]==prevA[i][j]){ if(a[i][j]) drawRect4(i*4+UpperLeftRow,j*4+UpperLeftCol,3,3,GREENINDEX); else drawRect4(i*4+UpperLeftRow,j*4+UpperLeftCol,3,3,BLACKINDEX); } else{ if(a[i][j])drawRect4(i*4+UpperLeftRow,j*4+UpperLeftCol,3,3,YELLOWINDEX); else drawRect4(i*4+UpperLeftRow,j*4+UpperLeftCol,3,3,REDINDEX); } } } for(n=0;n<MAXR;n++){ for(m=0;m<MAXC;m++){ a[n][m]=prevA[n][m]; } } for(n=0;n<MAXR;n++){ for(m=0;m<MAXC;m++){ prevA[n][m]=oldA[n][m]; } } } } FlipPage(); waitForVblank(); }
int main() { REG_DISPCTL = MODE4 | BG2_ENABLE; // Splash screen here setPalette(mainScreen_palette); drawImage4(0, 0, 240, 160, mainScreen); waitForVBlank(); flipPage(); int screen = 1; int currentMode = EDITMODE; const u16 colors[] = {WHITE, BLACK, RED, GREEN}; // alive = black = 1 // dead = white = 0 // alive + cursor = green = 3 // dead + cursor = red = 2 while (1) { if (screen) { setPalette(mainScreen_palette); drawImage4(0, 0, 240, 160, mainScreen); waitForVBlank(); flipPage(); if (KEY_DOWN_NOW(BUTTON_SELECT)) { screen = 0; setPalette(colors); fillScreen4(DEAD); flipPage(); copy(); while (KEY_DOWN_NOW(BUTTON_SELECT)) { } } } else { int draw = 0; // Select (backspace) = toggle between edit and simmode if (KEY_DOWN_NOW(BUTTON_SELECT)) { if (currentMode == EDITMODE) { currentMode = SIMMODE; eraseCursor(); } else { currentMode = EDITMODE; } while (KEY_DOWN_NOW(BUTTON_SELECT)) { // Won't do anything while select button is down } } // SIMMODE = 1 // EDITMODE = 0 // ***************SIM MODE******************* if (currentMode) { int paused = 0; int sim = 1; while (sim) { while (~paused & currentMode) { currentMode = simTick(); if (KEY_DOWN_NOW(BUTTON_SELECT)) { currentMode = EDITMODE; sim = 0; while (KEY_DOWN_NOW(BUTTON_SELECT)) { } } if (KEY_DOWN_NOW(BUTTON_A)) { paused = 1; while (KEY_DOWN_NOW(BUTTON_A)) { } } } //******************PAUSED**************** while (paused) { // Button B is X // This toggles next frame if (KEY_DOWN_NOW(BUTTON_B)) { simTick(); } if (KEY_DOWN_NOW(BUTTON_SELECT)) { currentMode = EDITMODE; while (KEY_DOWN_NOW(BUTTON_SELECT)) { } sim = 0; paused = 0; } if (KEY_DOWN_NOW(BUTTON_A)) { paused = 0; while (KEY_DOWN_NOW(BUTTON_A)) { } } } // end of paused } // end of sim } // end of currentMode if // ************EDIT MODE************** if (~currentMode) { copy(); drawCursor(); draw = cursorMoves(); if (KEY_DOWN_NOW(BUTTON_L)) { fillScreen4(DEAD); flipPage(); copy(); while (KEY_DOWN_NOW(BUTTON_L)) { } } } drawCursor(); if (draw) { waitForVBlank(); flipPage(); draw = 0; } } // end of else } }