void runInstructions(){ buttonsIns = BUTTONS; if(BUTTON_PRESSED_INS(BUTTON_SELECT)){ setupSplash(); stateFunction = runSplash; }else if(BUTTON_PRESSED_INS(BUTTON_START)){ waitForVblank(); nbr = (nbr +1) %6; switch(nbr){ case 0: drawImage3(ins1Bitmap,0,0,160,240); drawString3(140,20,"To return press SELECT", WHITE); drawString3(150,20,"To continue press START", WHITE); break; case 1: drawImage3(ins2Bitmap,0,0,160,240); break; case 2: drawImage3(ins3Bitmap,0,0,160,240); break; case 3: drawImage3(ins4Bitmap,0,0,160,240); break; case 4: drawImage3(ins5Bitmap,0,0,160,240); break; case 5: drawRect3(0,0,160,240, BLACK); drawString3(40,20,"To PAUSE at any time press SELECT",WHITE); drawString3(50,20,"To return from PAUSE press START",WHITE); drawString3(60,20,"To turn cheat ON/OFF press R",WHITE); break; } } oldButtonsIns = buttonsIns; }
void barControl(){ ballControl(); if(KEY_DOWN_NOW(BUTTON_RIGHT)) { cur_bar->CL = cur_bar->CL + cur_bar->CD; } if(KEY_DOWN_NOW(BUTTON_LEFT)) { cur_bar->CL = cur_bar->CL - (cur_bar->CD); } cur_bar = &bar; oldcur_bar = &bar_old; if(cur_bar->CL < 0) { cur_bar->CL = 0; } if(cur_bar->CL > 239-BAR_Width+1) { cur_bar->CL = 239-BAR_Width+1; } waitForVblank(); oldcur_bar = &bar_old; drawImage3(oldcur_bar->RW, oldcur_bar->CL, BAR_Width, BAR_Height, 0x0000); bar_old = bar; cur_bar = &bar; drawImage3(cur_bar->RW, cur_bar->CL, BAR_Width, BAR_Height, BAR); }
void drawEnemy(int x, int y, int oldx, int oldy, int invert) {// draws phantos drawRect(oldx, oldy, PHANTOS_WIDTH, PHANTOS_HEIGHT, 0); //clearRect(oldy, oldx, PHANTOS_WIDTH, PHANTOS_HEIGHT); (invert == 0) ? drawImage3(x, y, PHANTOS_WIDTH, PHANTOS_HEIGHT, phantos) : drawImage3(x, y, PHANTOS2_WIDTH, PHANTOS2_HEIGHT, phantos2); }
void switchTile(Move_t move) { REG_DISPCTL = MODE3 | BG2_ENABLE; // get blank tiles current co-ordinates int x_blank = game_grid[0].x_cord; int y_blank = game_grid[0].y_cord; // calcualte co-ordinates for the tile that you need to switch with the blank tile int x_tile = 0; int y_tile = 0; int tile_to_move = 0; if (move == UP) { x_tile = x_blank; y_tile = y_blank - 1; // draw black tiles over the 2 tiles you need to move } else if (move == DOWN) { x_tile = x_blank; y_tile = y_blank + 1; } else if (move == RIGHT) { x_tile = x_blank + 1; y_tile = y_blank; } else //(move == LEFT) { x_tile = x_blank - 1; y_tile = y_blank; } tile_to_move = game_state[x_tile][y_tile]; // draw black tiles over the 2 tiles you need to move WaitForVblank(); drawRect3(y_cords[y_tile], x_cords[x_tile], TILE1_WIDTH, TILE1_HEIGHT, BLACK); drawRect3(y_cords[y_blank], x_cords[x_blank], TILE1_WIDTH, TILE1_HEIGHT, BLACK); // draw the two tiles WaitForVblank(); drawImage3(y_cords[y_tile], x_cords[x_tile], TILE1_WIDTH, TILE1_HEIGHT, game_grid[0].image_data); drawImage3(y_cords[y_blank], x_cords[x_blank], TILE1_WIDTH, TILE1_HEIGHT, game_grid[tile_to_move].image_data); // update game_grid game_grid[tile_to_move].x_cord = x_blank; game_grid[tile_to_move].y_cord = y_blank; game_grid[0].x_cord = x_tile; game_grid[0].y_cord = y_tile; // update game_state game_state[x_blank][y_blank] = tile_to_move; game_state[x_tile][y_tile] = 9; }
void reset() { state = TITLE; currentScore = 0; lives = 3; playerSpeed = 1; drawImage3(0, 0, START_SCREEN_WIDTH, START_SCREEN_HEIGHT, start_screen); }
// Start menu. void start(){ REG_DISPCTL = MODE3 | BG2_ENABLE; drawImage3(0,0,240,160, menu); while(!KEY_DOWN_NOW(BUTTON_START)); while(KEY_DOWN_NOW(BUTTON_START)); }
void drawBullet(Bullet a) { if(a.image == 0) return; drawImage3(a.r, a.c, BULLET_WIDTH, BULLET_HEIGHT, a.image); }
void updateShip(SHIP* ship) { ship->OldX = ship->x; ship->OldY = ship->y; if (KEY_DOWN_NOW(BUTTON_LEFT)) { ship->x -= 3; } else if (KEY_DOWN_NOW(BUTTON_RIGHT)) { ship->x += 3; } else if (KEY_DOWN_NOW(BUTTON_UP)) { ship->y -= 3; } else if (KEY_DOWN_NOW(BUTTON_DOWN)) { ship->y += 3; } if (ship->y < 0) { ship->y = 0; } if (ship->x < 0) { ship->x = 0; } if (ship->y + ship->width > 150) { ship->y = 150 - ship->width; } if (ship->x + ship->height > 240) { ship->x = 240 - ship->height; } eraseImage3(ship->OldY, ship->OldX, ship->width, ship->height); drawImage3(ship->y, ship->x, ship->width, ship->height, Player); }
void setupInstructions(){ REG_DISPCTL = MODE3 | BG2_ENABLE; nbr = 0; drawImage3(ins1Bitmap,0,0,160,240); drawString3(140,20,"To return press SELECT", WHITE); drawString3(150,20,"To continue press START", WHITE); }
//Brings mario back to home when wall is it void home() { drawRect3(currentPlayer.x, currentPlayer.y, MARIO_WIDTH, MARIO_HEIGHT, CYAN); currentPlayer.x = SCREEN_HEIGHT - MARIO_HEIGHT - BOTTOM_BUFFER - 5; currentPlayer.y = SCREEN_WIDTH/2 - 20; drawImage3(currentPlayer.x, currentPlayer.y, MARIO_WIDTH, MARIO_HEIGHT, mario); playerSpeed=1; }
void drawHealth(int x, int y, int health) {// depending on the input, will change the enemy health bar to current health drawRect(x, y, ENHEALTH5_WIDTH, ENHEALTH5_HEIGHT, 0); switch( health ) { case 5: drawImage3(x, y, ENHEALTH5_WIDTH, ENHEALTH5_HEIGHT, enhealth5); break; case 4: drawImage3(x, y, ENHEALTH4_WIDTH, ENHEALTH4_HEIGHT, enhealth4); break; case 3: drawImage3(x, y, ENHEALTH3_WIDTH, ENHEALTH3_HEIGHT, enhealth3); break; case 2: drawImage3(x, y, ENHEALTH2_WIDTH, ENHEALTH2_HEIGHT, enhealth2); break; case 1: drawImage3(x, y, ENHEALTH1_WIDTH, ENHEALTH1_HEIGHT, enhealth1); break; case 0: drawImage3(x, y, ENHEALTH0_WIDTH, ENHEALTH0_HEIGHT, enhealth0); break; default: drawImage3(x, y, ENHEALTH0_WIDTH, ENHEALTH0_HEIGHT, enhealth0); break; } }
/* ================================== * TITLE SCREEN * ================================== */ void titleScreen() { drawImage3(0,0,240,160,title); while(1) { key_poll(); if (key_hit(KEY_START)) break; } }
/* ================================= * GAME OVER SCREEN * ================================= */ void gameOverScreen() { drawImage3(0,0,240,160,gameoverscreen); while(1) { key_poll(); if (key_hit(KEY_START)) break; } }
void startGame() { // draw grid WaitForVblank(); drawImage3(0,0,160,240, grid); // draw tiles WaitForVblank(); for (int i = 0; i < HEIGHT; ++i) { for (int j = 0; j < WIDTH; ++j) { // 8, 7, 6, // 5, 4, 3, // 2, 1, 0 drawImage3( y_cords[i], x_cords[j], TILE1_WIDTH, TILE1_HEIGHT, game_grid[((WIDTH * HEIGHT) - 1) - (WIDTH * i) - j].image_data); } } }
void draw_entity( EntityClass* entity, const u16* image ) { drawImage3( entity->row, entity->col, entity->width, entity->height, image ); }
//prepares the game for the start void initiate() { state = TITLE; drawImage3(0, 0, 240, 160, startscreen); goingUp = 0; acceleration = 1; distance = 0; upCount = 0; for (int i = 0; i < NUM_RECS; i++) { topWall[i].color = RED; topWall[i].width = RECWIDTH; topWall[i].height = RECHEIGHT; topWall[i].xPos = RECWIDTH * i; topWall[i].yPos = 0; } for (int i = 0; i < NUM_RECS; i++) { bottomWall[i].color = RED; bottomWall[i].width = RECWIDTH; bottomWall[i].height = RECHEIGHT; bottomWall[i].xPos = RECWIDTH * i; bottomWall[i].yPos = 140; } middleWall[0].color = RED; middleWall[0].width = RECWIDTH; middleWall[0].height = RECHEIGHT; middleWall[0].xPos = 20; middleWall[0].yPos = randomMidHeight(); middleWall[1].color = RED; middleWall[1].width = RECWIDTH; middleWall[1].height = RECHEIGHT; middleWall[1].xPos = 120; middleWall[1].yPos = randomMidHeight(); middleWall[2].color = RED; middleWall[2].width = RECWIDTH; middleWall[2].height = RECHEIGHT; middleWall[2].xPos = 200; middleWall[2].yPos = randomMidHeight(); gary.xPos = 50; gary.yPos = 60; gary.height = 21; gary.width = 15; gary.image = ghost; }
void displayLives(){ // drawImage3(120, 30, 48, 20, livesText); int i = 130; int j = 110; int s = 10; int l = 0; while( l < lives ){ setPixel(i, j + (l*s), RED); l ++; } }
void drawSnake(LIST* snake, int ateFood, int oldRow, int oldCol, int seed){ //draw the new location of the head of the snake drawImage3(snake->head->row, snake->head->col, snake->head->width, snake->head->height, WHITE); //only draw over the tail if it didn't eat food if(ateFood == 0){ drawImage3(oldRow, oldCol, snake->tail->width, snake->tail->height, BLACK); } else{ //snake ate the food, find a new place for the food srand(seed); int row = 0; int col = 0; do{ row = rand() % 16; col = rand() % 24; } while(contains(snake, row, col)); //make a new food piece NODE* newFood = create_node(row * snake->head->height, col * snake->head->width, snake->head->height, snake->head->width); DEBUG_PRINTF("New food place at (%d,%d)\n",newFood->row,newFood->col); snake->food = newFood; drawImage3(snake->food->row, snake->food->col, snake->food->width, snake->food->height, WHITE); } }
//Draws bombs, coins, mushrooms void draw() { for(int i=0; i<=10; i++) { ITEM* a = &spawned[i]; if(!a->alive) { if(!a->type) { drawRect3(a->x, a->y, BOMB_WIDTH, BOMB_HEIGHT, CYAN); }else if(a->type) { drawRect3(a->x, a->y, COIN_WIDTH, COIN_HEIGHT, CYAN); } else { drawRect3(a->x, a->y, MUSHROOM_WIDTH, MUSHROOM_HEIGHT, CYAN); } } else { if(!a->type) { drawImage3(a->x, a->y, BOMB_WIDTH, BOMB_HEIGHT, bomb); }else if(a->type==1) { drawImage3(a->x, a->y, COIN_WIDTH, COIN_HEIGHT, coin); } else { drawImage3(a->x, a->y, MUSHROOM_WIDTH, MUSHROOM_HEIGHT, mushroom); } } } drawImage3(currentPlayer.x, currentPlayer.y, MARIO_WIDTH, MARIO_HEIGHT, mario); drawImage3(currentPlayer.x, currentPlayer.y, MARIO_WIDTH, MARIO_HEIGHT, mario); }
void intro4() { REG_DISPCTL = MODE3 | BG2_ENABLE; WaitForVblank(); drawImage3(0,0,160,240,instruction4); while(!KEY_DOWN_NOW(BUTTON_A) && !KEY_DOWN_NOW(BUTTON_SELECT)); if (KEY_DOWN_NOW(BUTTON_A)) { state = PLAYING; } else { state = WELCOME; } while(KEY_DOWN_NOW(BUTTON_A)|| KEY_DOWN_NOW(BUTTON_SELECT)); }
void intro0() { REG_DISPCTL = MODE3 | BG2_ENABLE; WaitForVblank(); drawImage3(0,0,160,240,welcome); while(!KEY_DOWN_NOW(BUTTON_A) && !KEY_DOWN_NOW(BUTTON_SELECT)); if (KEY_DOWN_NOW(BUTTON_A)) { state = INTRO1; } else { state = WELCOME; } while(KEY_DOWN_NOW(BUTTON_A)|| KEY_DOWN_NOW(BUTTON_SELECT)); }
//Clears entities on screen and shows end screen void endGame() { state = GAMEOVER; for(int i=0; i<=10; i++) { ITEM* a = &spawned[i]; a->x=SCREEN_WIDTH*2; a->y=SCREEN_HEIGHT*2; a->alive = 0; } currentPlayer.x = SCREEN_WIDTH*2; currentPlayer.y = SCREEN_HEIGHT*2; draw(); drawRect3(0,0, SCREEN_WIDTH, SCREEN_HEIGHT, BLACK); drawImage3(0, 0, GAME_OVER_WIDTH, GAME_OVER_HEIGHT, game_over); sprintf(scoreString, "Final Score: %d", currentScore); drawString(SCREEN_HEIGHT/2 - 15, SCREEN_WIDTH/2 - 45, scoreString, WHITE); sprintf(playAgain, "Press 'select' to play again"); drawString(SCREEN_HEIGHT/2, SCREEN_WIDTH/2 - 85, playAgain, WHITE); }
void splash() { REG_DISPCTL = MODE3 | BG2_ENABLE; drawImage3(nerdbgBitmap, 0, 0, 160, 240); int rand_seed = 42; while (state == SPLASH) { rand_seed++; key_poll(); if (key_hit(BUTTON_A)) state = INSTRUCTIONS; else if (key_hit(BUTTON_B)) state = GAME; } srand(rand_seed); }
//Initializes score strings, entities void startGame() { state = NORMAL; currentPlayer.x = SCREEN_HEIGHT - MARIO_HEIGHT - BOTTOM_BUFFER - 5; currentPlayer.y = SCREEN_WIDTH/2 - 20; drawRect3(0, 0, 240, 160, CYAN); drawRect3(SCREEN_HEIGHT - BOTTOM_BUFFER, 0 , SCREEN_WIDTH, BOTTOM_BUFFER, BLACK); drawImage3(currentPlayer.x, currentPlayer.y, MARIO_WIDTH, MARIO_HEIGHT, mario); sprintf(scoreString, "Score: %d", currentScore); drawString(SCREEN_HEIGHT - BOTTOM_BUFFER + BOTTOM_BUFFER/5, 5, scoreString, GREEN); sprintf(livesString, "Lives: %d", lives); drawString(SCREEN_HEIGHT - BOTTOM_BUFFER + BOTTOM_BUFFER/5, SCREEN_WIDTH - 60, livesString, GREEN); sprintf(speedString, "Speed: %d", playerSpeed); drawString(SCREEN_HEIGHT - BOTTOM_BUFFER + BOTTOM_BUFFER/5, SCREEN_WIDTH/3 + 20, speedString, GREEN); srand(frame); frame = 0; initSpawned(); spawnCoin(); spawnMushroom(); }
void titleScreen() { drawImage3(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH, (const u16*) img_title); int keysPressed = 0; //title screen loop while (1) { //test button release for all buttons for (int i = 0; i < 10; i++) { if (KEY_DOWN_NOW(1 << i)) { keysPressed |= (1 << i); } if ((keysPressed & (1 << i)) && !KEY_DOWN_NOW(1 << i)) { playGame(); } } } }
//10 cols, 5 rows void instantiateBrickArray() { int colorChoice = 0; for (int i = 0; i < NUM_OF_BRICKS; i++) { brickArray[i].x = (i % 10) * BRICK_WIDTH; brickArray[i].y = (i / 10) * BRICK_HEIGHT; brickArray[i].health = 1; } for (int i = 0; i < NUM_OF_BRICKS; i++) { colorChoice = i % 6; if (brickArray[i].health) { switch(colorChoice) { case 0: drawImage3(brickArray[i].x, brickArray[i].y, BRICK_WIDTH, BRICK_HEIGHT, redbrick); break; case 1: drawImage3(brickArray[i].x, brickArray[i].y, BRICK_WIDTH, BRICK_HEIGHT, bluebrick); break; case 2: drawImage3(brickArray[i].x, brickArray[i].y, BRICK_WIDTH, BRICK_HEIGHT, greenbrick); break; case 3: drawImage3(brickArray[i].x, brickArray[i].y, BRICK_WIDTH, BRICK_HEIGHT, yellowbrick); break; case 4: drawImage3(brickArray[i].x, brickArray[i].y, BRICK_WIDTH, BRICK_HEIGHT, purplebrick); break; case 5: drawImage3(brickArray[i].x, brickArray[i].y, BRICK_WIDTH, BRICK_HEIGHT, whitebrick); break; } } } }
void gameOverScreen() { drawImage3(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH, (const u16*) img_game_over); sprintf(strBuffer, "Score: %d", score); drawStringCentered(SCREEN_HEIGHT / 2 + 14, SCREEN_WIDTH / 2, strBuffer, WHITE); int keysPressed = 0; //game over screen loop while (1) { //test button release for all buttons for (int i = 0; i < 10; i++) { if (KEY_DOWN_NOW(1 << i)) { keysPressed |= (1 << i); } if ((keysPressed & (1 << i)) && !KEY_DOWN_NOW(1 << i)) { titleScreen(); } } } }
int main() { // 1. TODO Set REG_DISPCNT appropriately for drawing in mode 3. // Use the #define fields near the top of this file to accomplish this. // 2. TODO Call your drawing function using parameters from the header. // You can use whatever you want for X and Y. If you use a high X and the width // goes beyond the edge of the screen, it will wrap around onto the next line. // This is normal. // You may test your own images with calls to drawImage3 here using the // appropriate fields from your header, but when you submit this file it // must contain ONLY a valid call using the diddy.h parameters, or else // our grader will not work and you will not receive credit for this file. // 3. (already done) wait forever after drawing. REG_DISPCNT = MODE_3 | BG2_EN; drawImage3(0,0,TITLE_WIDTH,TITLE_HEIGHT,title_data); while(1); return 0; }
void gameLoop() { // Set our game data shipRow = 55; shipCol = 0; enemyCol = 225; enemy_num = 0; projectile_num = 0; time_diff = 0; kills = 0; REG_DISPCNT = MODE3 | BG2_ENABLE; EntityClass player_ship; initialize_entity( &player_ship, SHIP_WIDTH, SHIP_HEIGHT, shipRow, shipCol ); EntityClass enemy; initialize_entity( &enemy, 0, 0, 0, 0 ); enemies[max_enemies+1] = enemy; while (lives != 0) { // Check if player won the level. if( kills == 10 ) { state = WIN; break; } // Draw background drawImage3( 0, 0, BACKGROUND_WIDTH, BACKGROUND_HEIGHT, background ); // Draw ship draw_entity( &player_ship, ship ); // Generate enemy if( time_diff <= 0 && enemy_num < 10 ) { time_diff = 75; int rand_row = ( rand() % (120+1-20) ) + 20; generate_enemy( rand_row, enemyCol ); } // Draw Enemy update_enemies(); // Draw each projectile update_projectiles(); // Check if player is dead. if( is_player_dead( &player_ship ) == 1 ) { lives--; gameLoop(); } check_collisions(); remove_old_projectiles(); remove_old_enemies(); // Draw lives for( int i = 0; i < lives; i++ ) { drawImage3( 140, 170 + ( i * 25 ), SHIP_WIDTH, SHIP_HEIGHT, ship ); } waitForVblank(); // If player hits 'select' return to titleScreen and start game over. if( KEY_DOWN_NOW( BUTTON_SELECT ) ) { state = TITLE; break; } // Player Actions if( KEY_DOWN_NOW( BUTTON_DOWN ) ) { if( get_row( &player_ship ) < 121 ) move_entity( &player_ship, 2, 0 ); } if( KEY_DOWN_NOW( BUTTON_UP ) ) { if( get_row( &player_ship ) > 20 ) move_entity( &player_ship, -2, 0 ); } if( KEY_DOWN_NOW( BUTTON_A ) ) { while( KEY_DOWN_NOW( BUTTON_A ) ) { ; } fire_weapon( &player_ship ); } time_diff--; } if( lives == 0 ) { state = GAME_OVER; } else if( state == WIN ) return; }
int main() { REG_DISPCTL = MODE3 | BG2_ENABLE; while(1) { waitForVblank(); //handle different states switch(state) { //Enter Title Screen case START: drawImage3(0, 0, 240, 160, title); state = LEVEL1; //state = LEVEL4; break; //Enter Level 1 case LEVEL1: if (KEY_DOWN_NOW(BUTTON_START)) { drawLevel(state); state = play(state); } if (KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } break; //Enter Pause Screen 1 case PAUSE1: drawImage3(0, 0, 240, 160, dkvsmario); if (KEY_DOWN_NOW(BUTTON_START)) { state = LEVEL2; drawLevel(state); state = play(state); } if (KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } break; //Enter Level 2 case LEVEL2: drawLevel(state); state = play(state); if (KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } break; //Enter Pause Screen 2 case PAUSE2: drawImage3(0, 0, 240, 160, dkvsmario); if (KEY_DOWN_NOW(BUTTON_START)) { state = LEVEL3; drawLevel(state); state = play(state); } if (KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } break; //Enter Level 3 case LEVEL3: drawLevel(state); state = play(state); if (KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } break; // //Enter Pause Screen 3 // case PAUSE3: // drawImage3(0, 0, 240, 160, dkvsmario); // if (KEY_DOWN_NOW(BUTTON_START)) { // state = LEVEL4; // drawLevel(state); // state = play(state); // } // if (KEY_DOWN_NOW(BUTTON_SELECT)) { // state = START; // } // break; // //Enter Level 4 // case LEVEL4: // drawLevel(state); // state = play(state); // if (KEY_DOWN_NOW(BUTTON_SELECT)) { // state = START; // } // break; // //Enter Pause Screen 4 // case PAUSE4: // drawImage3(0, 0, 240, 160, dkvsmario); // if (KEY_DOWN_NOW(BUTTON_START)) { // state = LEVEL4; // drawLevel(state); // state = play(state); // } // if (KEY_DOWN_NOW(BUTTON_SELECT)) { // state = START; // } // break; //You lost... gameover. case GAMEOVER: drawImage3(0, 0, 240, 160, gameover); if (KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } break; //You actually won... case WIN: drawImage3(0, 0, 240, 160, win); if (KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } break; } } return 0; }