Robin Milward Cooney 200849894
Dependencies: N5110 SDFileSystem gameCharacters mbed
Diff: main.cpp
- Revision:
- 0:28392431dbad
- Child:
- 1:046e66f1ca76
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Apr 26 18:44:09 2016 +0000 @@ -0,0 +1,1502 @@ +/* ELEC2645 Game - Shooty shooty bang bang (priliminary name) + +Week 19 - initial version, basic testing of sensor and display + +(c) Robni Milward Cooney Uniersity of Leeds, 06/03/16 + +*/ + +#include "mbed.h" +#include "N5110.h" +#include "math.h" +#include "stdint.h" +#include "tone.h" +#include "gameCharacters.h" + +#define joystickTolerance 0.05f + +N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3); //declare LCD +BusOut led(PTC8,PTC9,PTC0,PTC7,PTC5); //declare Bus output for LED bar +DigitalIn swJoy(PTB18); //define potentiomiter switch +AnalogIn xPot(PTB2); //define x axis analoge in (this is the pot that corresponds to horizontal movement of the joystick) +AnalogIn yPot(PTB3); //define y axis analoge in (this is the pot that corresponds to vertical movement of the joystick) +DigitalIn b_A(PTE24); //define additional first button +DigitalIn b_B(PTE25); //define additional second button +PwmOut PWM(PTC11); +InterruptIn press_b_A(PTE24); //declare the push button A as an interrupt +InterruptIn press_b_B(PTE25); //declare the push button B as an interrupt +Ticker Ticker_Menu; +Ticker Ticker_Game; + +int i; //loop counter +int loop=0; +int g_jump=36; +int jumpUp; +int accel=0; +int bullet=9; +int bullet_height; +int menu_select; +int score; +int kill_score; +int h_movement=0; +int ammo=24; +int bear_movement=100; +int bird_hMovement=95; +int bird_vMovement=20; +int cactus_movement=110; +int t_rex_movement=120; +int fire_ball_hMovement=t_rex_movement-6; +int fire_ball_vMovement=27; +int heart_movement=90; +int ammo_movement=100; +int menuState=0; //initial state for the menu fsm +int jump_flag=0; +int shoot_flag=0; +int print_bear_flag=0; +int print_bird_flag=0; +int print_heart_flag=0; +int print_ammo_flag=0; +int print_cactus_flag=0; +int print_t_rex_flag=0; +int print_fire_ball_flag=0; +int lose_lives_delay_flag=0; +int lives_delay_loop=0; //number of times the lose_lives_delay_flag if statement has iterated +int fire_on_screen=0; +double lives=4; +int bear_lives=0; +int t_rex_lives=0; +int random_num; +volatile int g_Ticker_Menu_flag=0; +volatile int g_Ticker_Game_flag=0; +volatile int g_press_b_A_flag=0; +volatile int g_press_b_B_flag=0; + +void initialize_values() +{ + i=0; //loop counter + loop=0; + g_jump=36; + accel=0; + bullet=9; + h_movement=0; + ammo=24; + bear_movement=100; + bird_hMovement=95; + bird_vMovement=20; + cactus_movement=110; + t_rex_movement=120; + fire_ball_hMovement=t_rex_movement-6; + fire_ball_vMovement=27; + heart_movement=90; + ammo_movement=100; + menuState=0; //initial state for the menu fsm + jump_flag=0; + shoot_flag=0; + print_bear_flag=0; + print_bird_flag=0; + print_heart_flag=0; + print_ammo_flag=0; + print_cactus_flag=0; + print_t_rex_flag=0; + print_fire_ball_flag=0; + lose_lives_delay_flag=0; + lives_delay_loop=0; + lives=4; + bear_lives=0; + score=0; + kill_score=0; + fire_on_screen=0; +} + +void generate_random_number() +{ + time_t seconds = time(NULL); + srand(seconds); + random_num=rand()%10000; +} +void led_bar() //code to test led bar +{ + led=pow(2,lives)-1; + if (lives>5) { + lives=5; + } +} + + +void ground() //funtion to print the ground +{ + for (int x = 0; x<=84 ; x++) { + lcd.setPixel(x,47); + } +} + +void print_heart() +{ + for(int c=0; c<=4; c++) { //4 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=4; r++) { + if (g_heart[r][c]==0) { + if (lcd.getPixel(c+heart_movement,r+35)!=0) { + lcd.setPixel(c+heart_movement,r+35); + } else { + lcd.clearPixel(c+heart_movement,r+35); + } + } else if (g_heart[r][c]==1) { + lcd.setPixel(c+heart_movement,r+35); + } + } + } +} + +void print_ammo_pickUp() +{ + for(int c=0; c<=6; c++) { //6 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=7; r++) { + if (g_ammo_pickUp[r][c]==0) { + if (lcd.getPixel(c+ammo_movement,r+35)!=0) { + lcd.setPixel(c+ammo_movement,r+35); + } else { + lcd.clearPixel(c+ammo_movement,r+35); + } + } else if (g_ammo_pickUp[r][c]==1) { + lcd.setPixel(c+ammo_movement,r+35); + } + } + } +} + +void print_recks_still_gun() +{ + + for(int c=0; c<=9; c++) { //9 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_recks_still_gun[r][c]==0) { + if (lcd.getPixel(c+3,r+37)!=0) { + lcd.setPixel(c+3,r+37); + } else { + lcd.clearPixel(c+3,r+37); + } + } else if (g_recks_still_gun[r][c]==1) { + lcd.setPixel(c+3,r+37); + } + } + } +} + +void print_recks_moving_gun() +{ + + for(int c=0; c<=9; c++) { //9 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_recks_moving_gun[r][c]==0) { + if (lcd.getPixel(c+3,r+37)!=0) { + lcd.setPixel(c+3,r+37); + } else { + lcd.clearPixel(c+3,r+37); + } + } else if (g_recks_moving_gun[r][c]==1) { + lcd.setPixel(c+3,r+37); + } + } + } +} + +void get_recks_still_gun() +{ + +} + +void print_recks_crouch_gun() +{ + for(int c=0; c<=9; c++) { //9 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_recks_crouch_gun[r][c]==0) { + if (lcd.getPixel(c+3,r+37)!=0) { + lcd.setPixel(c+3,r+37); + } else { + lcd.clearPixel(c+3,r+37); + } + } else if (g_recks_crouch_gun[r][c]==1) { + lcd.setPixel(c+3,r+37); + } + } + } +} + + +void print_recks_jump_gun() +{ + for(int c=0; c<=9; c++) { //9 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_recks_jump_gun[r][c]==0) { + if (lcd.getPixel(c+3,r+g_jump)!=0) { + lcd.setPixel(c+3,r+g_jump); + } else { + lcd.clearPixel(c+3,r+g_jump); + } + } else if (g_recks_jump_gun[r][c]==1) { + lcd.setPixel(c+3,r+g_jump); + } + } + } +} + +void print_mob_bear_p1() //funtion to print the still bear on the LCD +{ + for(int c=0; c<=18; c++) { //18 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_mob_bear_p1[r][c]==0) { + if (lcd.getPixel(c+bear_movement,r+37)!=0) { + lcd.setPixel(c+bear_movement,r+37); + } else { + lcd.clearPixel(c+bear_movement,r+37); + } + } else if (g_mob_bear_p1[r][c]==1) { + lcd.setPixel(c+bear_movement,r+37); + } + } + } +} + +void print_mob_bear_p2() //funtion to print the moving bear on the LCD +{ + for(int c=0; c<=18; c++) { //18 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_mob_bear_p2[r][c]==0) { + if (lcd.getPixel(c+bear_movement,r+37)!=0) { + lcd.setPixel(c+bear_movement,r+37); + } else { + lcd.clearPixel(c+bear_movement,r+37); + } + } else if (g_mob_bear_p2[r][c]==1) { + lcd.setPixel(c+bear_movement,r+37); + } + } + } +} + +void print_mob_bear_dead() //funtion to print the moving bear on the LCD +{ + for(int c=0; c<=18; c++) { //18 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_mob_bear_dead[r][c]==0) { + if (lcd.getPixel(c+bear_movement,r+37)!=0) { + lcd.setPixel(c+bear_movement,r+37); + } else { + lcd.clearPixel(c+bear_movement,r+37); + } + } else if (g_mob_bear_dead[r][c]==1) { + lcd.setPixel(c+bear_movement,r+37); + } + } + } +} + +void print_mob_bird_p1() //funtion to print the moving bird on the LCD +{ + for(int c=0; c<=9; c++) { //9 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_mob_bird_p1[r][c]==0) { + if (lcd.getPixel(c+bird_hMovement,r+bird_vMovement)!=0) { + lcd.setPixel(c+bird_hMovement,r+bird_vMovement); + } else { + lcd.clearPixel(c+bird_hMovement,r+bird_vMovement); + } + } else if (g_mob_bird_p1[r][c]==1) { + lcd.setPixel(c+bird_hMovement,r+bird_vMovement); + } + } + } +} + +void print_mob_bird_p2() //funtion to print the moving bird on the LCD +{ + for(int c=0; c<=9; c++) { //9 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_mob_bird_p2[r][c]==0) { + if (lcd.getPixel(c+bird_hMovement,r+bird_vMovement)!=0) { + lcd.setPixel(c+bird_hMovement,r+bird_vMovement); + } else { + lcd.clearPixel(c+bird_hMovement,r+bird_vMovement); + } + } else if (g_mob_bird_p2[r][c]==1) { + lcd.setPixel(c+bird_hMovement,r+bird_vMovement); + } + } + } +} + +void print_mob_bird_dead() //funtion to print the moving bird on the LCD +{ + for(int c=0; c<=9; c++) { //9 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=9; r++) { + if (g_mob_bird_dead[r][c]==0) { + lcd.clearPixel(c+bird_hMovement,r+bird_vMovement); + } else if (g_mob_bird_dead[r][c]==1) { + lcd.setPixel(c+bird_hMovement,r+bird_vMovement); + } + } + } +} + +void print_cactus() //funtion to print the moving bird on the LCD +{ + for(int c=0; c<=4; c++) { //4 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=11; r++) { + if (cactus[r][c]==0) { + if (lcd.getPixel(c+cactus_movement,r+35)!=0) { + lcd.setPixel(c+cactus_movement,r+35); + } else { + lcd.clearPixel(c+cactus_movement,r+35); + } + } else if (cactus[r][c]==1) { + lcd.setPixel(c+cactus_movement,r+35); + } + } + } +} + +void print_t_rex() +{ + for(int c=0; c<=24; c++) { //24 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=25; r++) { + if (g_t_rex[r][c]==0) { + if (lcd.getPixel(c+t_rex_movement,r+21)!=0) { + lcd.setPixel(c+t_rex_movement,r+21); + } else { + lcd.clearPixel(c+t_rex_movement,r+21); + } + } else if (g_t_rex[r][c]==1) { + lcd.setPixel(c+t_rex_movement,r+21); + } + } + } +} + +void print_t_rex_moving() +{ + for(int c=0; c<=24; c++) { //24 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=25; r++) { + if (g_t_rex_moving[r][c]==0) { + if (lcd.getPixel(c+t_rex_movement,r+21)!=0) { + lcd.setPixel(c+t_rex_movement,r+21); + } else { + lcd.clearPixel(c+t_rex_movement,r+21); + } + } else if (g_t_rex_moving[r][c]==1) { + lcd.setPixel(c+t_rex_movement,r+21); + } + } + } +} + +void print_t_rex_attack() +{ + for(int c=0; c<=24; c++) { //24 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=25; r++) { + if (g_t_rex_attack[r][c]==0) { + if (lcd.getPixel(c+t_rex_movement,r+21)!=0) { + lcd.setPixel(c+t_rex_movement,r+21); + } else { + lcd.clearPixel(c+t_rex_movement,r+21); + } + } else if (g_t_rex_attack[r][c]==1) { + lcd.setPixel(c+t_rex_movement,r+21); + } + } + } +} + +void print_fire_ball_p1() +{ + for(int c=0; c<=8; c++) { //24 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=8; r++) { + if (g_fire_ball_p1[r][c]==0) { + if (lcd.getPixel(c+fire_ball_hMovement,r+fire_ball_vMovement)!=0) { + lcd.setPixel(c+fire_ball_hMovement,r+fire_ball_vMovement); + } else { + lcd.clearPixel(c+fire_ball_hMovement,r+fire_ball_vMovement); + } + } else if (g_fire_ball_p1[r][c]==1) { + lcd.setPixel(c+fire_ball_hMovement,r+fire_ball_vMovement); + } + } + } +} + +void print_fire_ball_p2() +{ + for(int c=0; c<=8; c++) { //24 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=8; r++) { + if (g_fire_ball_p2[r][c]==0) { + if (lcd.getPixel(c+fire_ball_hMovement,r+fire_ball_vMovement)!=0) { + lcd.setPixel(c+fire_ball_hMovement,r+fire_ball_vMovement); + } else { + lcd.clearPixel(c+fire_ball_hMovement,r+fire_ball_vMovement); + } + } else if (g_fire_ball_p2[r][c]==1) { + lcd.setPixel(c+fire_ball_hMovement,r+fire_ball_vMovement); + } + } + } +} + + +void print_clouds() +{ + uint8_t clouds_movement=h_movement; //8 bit unsigned interger + for(int c=clouds_movement; c<=clouds_movement+83; c++) { + for(int r=0; r<=2; r++) { + if (g_clouds[r][c]==0) { + lcd.clearPixel(c-clouds_movement,r+6); + } else if (g_clouds[r][c]==1) { + lcd.setPixel(c-clouds_movement,r+6); + } + } + } +} + +void clearCells() // function to clear all pixels on screen +{ + for (int i=0; i<85; i++) { //go through every pixel on the x axis + for (int j=0; j<49; j++) { // go through every pixel on the y axis + lcd.clearPixel(i,j); // clear the cell + } + } + lcd.refresh(); //refresh the lcd +} + +void shoot() +{ + lcd.setPixel(bullet,g_jump+4); + lcd.setPixel(bullet+1,g_jump+4); +} + +void print_score(); +void print_ammo(); + +void intro() +{ + +} + +void Ticker_Menu_isr() +{ + g_Ticker_Menu_flag=1; +} + +void Ticker_Game_isr() +{ + g_Ticker_Game_flag=1; +} + +void press_b_A_isr() +{ + g_press_b_A_flag=1; +} + +enum joystickDirection { //emunm type for every neccessary direction of the joystick + CENTRE, //when the joystick isn't moved + DOWN, + UP, + LEFT, + RIGHT, +}; + +typedef struct JoyStick Joystick; //struct for Joystick +struct JoyStick { + float x; // current x value + float x0; // 'centred' x value + float y; // current y value + float y0; // 'centred' y value + int swJoy; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) + joystickDirection direction; // current direction +}; +// create struct variable +Joystick joystick; + +// read default positions of the joystick to calibrate later readings +void calibrateJoystick() +{ + swJoy.mode(PullDown); + // must not move during calibration + joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) + joystick.y0 = yPot; +} +void updateJoystick() +{ + // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) + joystick.x = xPot - joystick.x0; + joystick.y = yPot - joystick.y0; + // read button state + joystick.swJoy = swJoy; + + // calculate direction depending on x,y values + // tolerance allows a little lee-way in case joystick not exactly in the stated direction + if ( fabs(joystick.y) < joystickTolerance && fabs(joystick.x) < joystickTolerance) { + joystick.direction = CENTRE; + } else if ( joystick.y > joystickTolerance && fabs(joystick.x) < joystickTolerance) { + joystick.direction = UP; + } else if ( joystick.y < joystickTolerance && fabs(joystick.x) < joystickTolerance) { + joystick.direction = DOWN; + } else if ( joystick.x > joystickTolerance && fabs(joystick.y) < joystickTolerance) { + joystick.direction = LEFT; + } else if ( joystick.x < joystickTolerance && fabs(joystick.y) < joystickTolerance) { + joystick.direction = RIGHT; + } +} + +struct motion { //defines the motion of the charecters + int x; + int v_x; + int acc_x; + int y; + int v_y; + int acc_y; +}; + +struct menuState { + int menu_select; // output value + int nextState[5]; // array of next states 6 to account for all of the joystick states +}; +typedef const struct menuState STyp; + +STyp fsm_main_menu[5] = { + {0,{0,1,4,0,0}}, + {8,{1,2,0,1,1}}, + {16,{2,3,1,2,2}}, + {24,{3,4,2,3,3}}, + {32,{4,0,3,4,4}} +}; + +STyp fsm_settings_menu[5] = { + {0,{0,1,1,0,0}}, + {8,{1,0,0,1,1}}, + {16,{2,2,2,2,2}}, + {24,{3,3,3,3,3}}, + {32,{4,4,4,4,4}} +}; + +int main() //main function, currently used to test different functions +{ + b_A.mode(PullDown); + b_B.mode(PullDown); + swJoy.mode(PullDown); + initialize_values(); + lcd.init(); + Ticker_Menu.attach(&Ticker_Menu_isr,0.2); + Ticker_Game.attach(&Ticker_Game_isr,0.1); + //press_b_A.fall(&press_b_A_isr); + //press_b_A.mode(PullDown); + calibrateJoystick(); + int run=0; + +Menu: //declares a "goto" location for the goto functions + while(1) { + + if (g_Ticker_Menu_flag) { + g_Ticker_Menu_flag=0; + updateJoystick(); + generate_random_number(); + menu_select = fsm_main_menu[menuState].menu_select; // set ouput depending on current state + menuState = fsm_main_menu[menuState].nextState[joystick.direction]; // when joystick.direction has a vaule of CENTRE/RIGHT/LEFT the state machine input is 0 when it has a value of DOWN it is 0b01 when it has a value of UP is is 0b10 + clearCells(); + lcd.printString("Story mode",13,0); + lcd.printString("Minigame",18,1); + lcd.printString("Settings",18,2); + lcd.printString("Leaderboard",9,3); + lcd.printString("Credits",21,4); + for (int i=0; i<85; i++) { //go through every pixel on the x axis + for (int j=menu_select; j<(menu_select+8); j++) { // go through relevant pixels on the y axis + if (lcd.getPixel(i,j)== 0) { //if the pixel is on trun it off + lcd.setPixel(i,j); + } else { + lcd.clearPixel(i,j); //if the pixel is off turn it on + } + } + } + lcd.refresh(); + if (b_A & menu_select==0) { + goto Story_Mode; + } else if (b_A & menu_select==8) { + goto Minigame; + } else if (b_A & menu_select==16) { + goto Settings; + } else if (b_A & menu_select==24) { + goto Leaderboard; + } else if (b_A & menu_select==32) { + goto Credits; + } + } + sleep(); + } + +Story_Mode: +Minigame: +Settings: + menuState=0; + while (1) { + if (g_Ticker_Menu_flag) { + g_Ticker_Menu_flag=0; + updateJoystick(); + menu_select = fsm_settings_menu[menuState].menu_select; // set ouput depending on current state + menuState = fsm_settings_menu[menuState].nextState[joystick.direction]; // when joystick.direction has a vaule of CENTRE/RIGHT/LEFT the state machine input is 0 when it has a value of DOWN it is 0b01 when it has a value of UP is is 0b10 + clearCells(); + lcd.printString("Brightness",13,0); + lcd.printString("Music",18,1); + for (int i=0; i<85; i++) { //go through every pixel on the x axis + for (int j=menu_select; j<(menu_select+8); j++) { // go through relevant pixels on the y axis + if (lcd.getPixel(i,j)== 0) { //if the pixel is on trun it off + lcd.setPixel(i,j); + } else { + lcd.clearPixel(i,j); //if the pixel is off turn it on + } + } + } + lcd.refresh(); + if (b_A & menu_select==0) { + goto Brightness; + } else if (b_A & menu_select==8) { + goto Music; + } + } + } +Brightness: +Music: +Leaderboard: +Credits: +Continue: + wait(0.5); + while (1) { + clearCells(); + updateJoystick(); + led_bar(); + print_score(); + generate_random_number(); + if (g_Ticker_Game_flag) { + g_Ticker_Game_flag=0; + if (lives==0) { + goto GameOver; + } + if (lose_lives_delay_flag==1) { + lives_delay_loop++; + if (lives_delay_loop>=10) { //means that the delay between one life beeing lost and another one beeing lost is at least 1s + lives_delay_loop=0; + lose_lives_delay_flag=0; + } + + } + if (ammo<=5) { + if (i%8>=4) { + print_ammo(); + } + } else { + print_ammo(); + } + if (b_B) { + jump_flag=1; + } + if (swJoy==1) { + goto Pause; + } + if (shoot_flag==0) { + bullet=9; + if (joystick.direction==DOWN) { + bullet_height=40; + } else { + bullet_height=g_jump+4; + } + } else if (shoot_flag==1&loop==0) { + loop++; + ammo--; + if (joystick.direction==DOWN&jump_flag!=1) { + bullet_height=42; + } else { + bullet_height=g_jump+4; + } + } + if (b_A) { + if (ammo<=0) { + shoot_flag=0; + } else { + shoot_flag=1; + } + } + if (random_num<1000) { + print_bear_flag=1; + } else if (random_num>=1000&random_num<3000) { + print_bird_flag=1; + } else if (random_num>=3000&random_num<3010) { + print_heart_flag=1; + } else if (random_num>=3010&random_num<3050) { + print_ammo_flag=1; + } else if (random_num>=3050&random_num<6000) { + print_cactus_flag=1; + } else if (random_num>=6000&random_num<7000) { + print_ammo_flag=1; + print_t_rex_flag=1; + } + if (print_t_rex_flag==1&random_num%10==0&fire_on_screen==0) { + fire_on_screen=1; + print_fire_ball_flag=1; + fire_ball_hMovement=t_rex_movement-6; + fire_ball_vMovement=27; + } + if (joystick.direction==CENTRE&jump_flag!=1) { + lcd.setPixel(bullet,bullet_height); + lcd.setPixel(bullet+1,bullet_height); + print_recks_still_gun(); + if (shoot_flag==1) { + bullet+=20; + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + } + } else if (joystick.direction==LEFT&jump_flag!=1) { + if (run%2==0) { + print_recks_still_gun(); + } else if (run%2==1) { + print_recks_moving_gun(); + } + lcd.setPixel(bullet,bullet_height); + lcd.setPixel(bullet+1,bullet_height); + if (shoot_flag==1) { + bullet+=21; + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + } + h_movement--; + run++; + } else if (joystick.direction==RIGHT&jump_flag!=1) { + if (run%2==0) { + print_recks_still_gun(); + } else if (run%2==1) { + print_recks_moving_gun(); + } + lcd.setPixel(bullet,bullet_height); + lcd.setPixel(bullet+1,bullet_height); + if (shoot_flag==1) { + bullet+=19; + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + } + h_movement++; + run++; + } else if (joystick.direction==DOWN&jump_flag!=1) { + lcd.setPixel(bullet,bullet_height); + lcd.setPixel(bullet+1,bullet_height); + print_recks_crouch_gun(); + bullet_height=42; + if (shoot_flag==1) { + bullet+=20; + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + } + } else if (jump_flag==1) { + if (joystick.direction==LEFT) { + if (shoot_flag==1) { + bullet+=21; + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + } + h_movement--; + } else if (joystick.direction==RIGHT) { + if (shoot_flag==1) { + bullet+=19; + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + } + h_movement++; + } else if (joystick.direction==CENTRE) { + if (shoot_flag==1) { + bullet+=20; + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + } + } + if (g_jump<=36&jumpUp==0&g_jump!=15) { + lcd.setPixel(bullet,bullet_height); + lcd.setPixel(bullet+1,bullet_height); + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + print_recks_jump_gun(); + g_jump-=6-accel; + accel++; + } else if (g_jump>=15&g_jump!=36) { + lcd.setPixel(bullet,bullet_height); + lcd.setPixel(bullet+1,bullet_height); + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + print_recks_jump_gun(); + g_jump+=6-accel; + accel--; + jumpUp=1; + } else if (g_jump==36&jumpUp==1) { + lcd.setPixel(bullet,bullet_height); + lcd.setPixel(bullet+1,bullet_height); + if (bullet>=84) { + shoot_flag=0; + loop=0; + } + print_recks_still_gun(); + jump_flag=0; + jumpUp=0; + g_jump=36; + accel=0; + } + } + } + if (print_bear_flag==1) { + if (i%4>=2) { + print_mob_bear_p1(); + } else if (i%4<2) { + print_mob_bear_p2(); + } + if (joystick.direction==LEFT) { + bear_movement++; + } else if (joystick.direction==RIGHT) { + bear_movement--; + } + bear_movement-=2; + if (bear_movement<=-15) { + bear_movement=100; + print_bear_flag=0; + } + if (bear_movement<=8&bear_movement>=-5&g_jump>28&lose_lives_delay_flag==0) { //a life is lost if recks has an vertical and horizontal position equal to one of the pixels corresponding to the bears position + lives--; + lose_lives_delay_flag=1; + } + if (shoot_flag==1&bullet_height<72) { + bear_lives++; + if (bear_lives==12-3*g_g1-3*g_g2) { + print_mob_bear_dead(); + print_bear_flag=0; + bear_movement=100; + kill_score+=7; + bear_lives=0; + } + } + } + if (print_bird_flag==1) { + if (i%8>=4) { + print_mob_bird_p1(); + } else if (i%8<4) { + print_mob_bird_p2(); + } + bird_hMovement-=2; + if (joystick.direction==LEFT) { + bird_hMovement++; + } else if (joystick.direction==RIGHT) { + bird_hMovement--; + } + if (random_num%6==0) { + bird_vMovement--; + } else if (random_num%6>=4) { + bird_vMovement++; + } + if (bird_vMovement>=37) { + bird_vMovement=37; + } else if (bird_vMovement<=10) { + bird_vMovement=10; + } + if (bird_hMovement<=-10) { + print_bird_flag=0; + bird_hMovement=100; + bird_vMovement=20; + bear_lives=0; + } + if ((bird_hMovement>=0&bird_hMovement<=10)&(bird_vMovement+5>=g_jump&bird_vMovement+5<=g_jump+10&lose_lives_delay_flag==0)) { + lives--; + lose_lives_delay_flag=1; + } + if (shoot_flag==1&(bullet_height==bird_vMovement+5|bullet_height==bird_vMovement+4)) { + print_mob_bird_dead(); + print_bird_flag=0; + bird_hMovement=100; + bird_vMovement=20; + kill_score+=5; + } + } + if (print_cactus_flag==1) { + print_cactus(); + if (joystick.direction==LEFT) { + cactus_movement++; + } else if (joystick.direction==RIGHT) { + cactus_movement--; + } + if (cactus_movement<=-10) { + cactus_movement=110; + print_cactus_flag=0; + } + if (cactus_movement<=10&cactus_movement>=2&g_jump>32&lose_lives_delay_flag==0) { + lives--; + lose_lives_delay_flag=1; + } + } + + if (print_t_rex_flag==1) { + if (joystick.direction==LEFT) { + t_rex_movement++; + } else if (joystick.direction==RIGHT) { + t_rex_movement--; + } + if (random_num%4==0&print_fire_ball_flag!=1) { + t_rex_movement+=2; + print_t_rex_moving(); + } else if (random_num%4==1&print_fire_ball_flag!=1) { + t_rex_movement-=2; + print_t_rex_moving(); + } else if (random_num%4>1&print_fire_ball_flag!=1) { + print_t_rex(); + } + if (t_rex_movement<=-100) { + t_rex_movement=120; + print_t_rex_flag=0; + } + if (t_rex_movement<=6&t_rex_movement>=2&lose_lives_delay_flag==0) { + lives--; + lose_lives_delay_flag=1; + } + } + if (print_fire_ball_flag==1) { + print_t_rex_attack(); + fire_ball_hMovement-=2; + if (joystick.direction==LEFT) { + fire_ball_hMovement++; + } else if (joystick.direction==RIGHT) { + fire_ball_hMovement--; + } + if (i%4>=2) { + print_fire_ball_p1(); + } else { + print_fire_ball_p2(); + } + if (fire_ball_vMovement>=37) { + fire_ball_vMovement=37; + } + if (random_num%3==0) { + fire_ball_vMovement++; + } else if (random_num%3==1) { + fire_ball_vMovement+=2; + } + if (fire_ball_hMovement<=-10) { + fire_ball_vMovement=27; + fire_ball_hMovement=t_rex_movement-6; + print_fire_ball_flag=0; + fire_on_screen=0; + } + if (fire_ball_hMovement>=3&fire_ball_hMovement<=8&fire_ball_vMovement>g_jump-5&fire_ball_vMovement<g_jump+10&lose_lives_delay_flag==0) { + lives--; + lose_lives_delay_flag=1; + } + if (shoot_flag==1) { + t_rex_lives++; + if (t_rex_lives>=24-3*g_g1-3*g_g2) { + print_t_rex_flag=0; + t_rex_movement=120; + kill_score+=20; + t_rex_lives=0; + } + } + } + if (print_heart_flag==1) { + print_heart(); + if (joystick.direction==LEFT) { + heart_movement++; + } else if (joystick.direction==RIGHT) { + heart_movement--; + } + if (heart_movement<=5&g_jump>26) { + heart_movement=90; + print_heart_flag=0; + lives++; + } + } + if (print_ammo_flag==1) { + print_ammo_pickUp(); + if (joystick.direction==LEFT) { + ammo_movement++; + } else if (joystick.direction==RIGHT) { + ammo_movement--; + } + if (ammo_movement<=5&g_jump>26) { + ammo_movement=90; + print_ammo_flag=0; + ammo+=5; + ammo+=rand()%7; + } + } + print_clouds(); + ground(); + i++; + lcd.refresh(); + sleep(); + } + +Pause: + while (1) { + clearCells(); + wait(0.2); + if (swJoy==1) { + goto Continue; + } + } +GameOver: + clearCells(); + lcd.printString("GAME",25,1); + lcd.printString("OVER",25,2); + lcd.printString("A Retry",40,4); + lcd.printString("B Back to Menu",0,5); + while (1) { + if (b_A==1) { + wait(0.2); + initialize_values(); + goto Continue; + } else if (b_B==1) { + wait(0.2); + initialize_values(); + goto Menu; + } + } +} + + + + + +// ----------------------------------------------------------------------------------------------------------------------------------------- + +void game() { + + }; + +void print_ammo() +{ + for(int c=0; c<=2; c++) { // 2 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=4; r++) { + for (int n=70; n<=74; n+=4) { + if (n==74&ammo%10==0) { + if (zero[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==74&ammo%10==1) { + if (one[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==74&ammo%10==2) { + if (two[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==74&ammo%10==3) { + if (three[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==74&ammo%10==4) { + if (four[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==74&ammo%10==5) { + if (five[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==74&ammo%10==6) { + if (six[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==74&ammo%10==7) { + if (seven[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==74&ammo%10==8) { + if (eight[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==74&ammo%10==9) { + if (nine[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } + + if (n==70&(ammo%100>=0&ammo%100<10)) { + if (zero[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==70&(ammo%100>=10&ammo%100<20)) { + if (one[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==70&(ammo%100>=20&ammo%100<30)) { + if (two[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==70&(ammo%100>=30&ammo%100<40)) { + if (three[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==70&(ammo%100>=40&ammo%100<50)) { + if (four[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==70&(ammo%100>=50&ammo%100<60)) { + if (five[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==70&(ammo%100>=60&ammo%100<70)) { + if (six[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==70&(ammo%100>=70&ammo%100<80)) { + if (seven[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==70&(ammo%100>=80&ammo%100<90)) { + if (eight[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==70&(ammo%100>=90&ammo%100<100)) { + if (nine[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } + } + } + } + for(int c=0; c<=14; c++) { //14 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=4; r++) { + if (g_ammo[r][c]==0) { + lcd.clearPixel(c+50,r); + } else if (g_ammo[r][c]==1) { + lcd.setPixel(c+50,r); + } + } + } + if (ammo>=99) { + ammo=99; + } + lcd.refresh(); +} + +void print_score() +{ + score=h_movement/10+kill_score; + if (score<=0) { + score=0; + } + for(int c=0; c<=2; c++) { //2 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=4; r++) { + for (int n=24; n<=36; n+=4) { + if (n==36&score%10==0) { + if (zero[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==36&score%10==1) { + if (one[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==36&score%10==2) { + if (two[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==36&score%10==3) { + if (three[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==36&score%10==4) { + if (four[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==36&score%10==5) { + if (five[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==36&score%10==6) { + if (six[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==36&score%10==7) { + if (seven[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==36&score%10==8) { + if (eight[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==36&score%10==9) { + if (nine[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } + + if (n==32&(score%100>=0&score%100<10)) { + if (zero[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==32&(score%100>=10&score%100<20)) { + if (one[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==32&(score%100>=20&score%100<30)) { + if (two[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==32&(score%100>=30&score%100<40)) { + if (three[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==32&(score%100>=40&score%100<50)) { + if (four[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==32&(score%100>=50&score%100<60)) { + if (five[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==32&(score%100>=60&score%100<70)) { + if (six[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==32&(score%100>=70&score%100<80)) { + if (seven[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==32&(score%100>=80&score%100<90)) { + if (eight[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==32&(score%100>=90&score%100<100)) { + if (nine[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } + if (n==28&(score%1000>=0&score%1000<100)) { + if (zero[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==28&(score%1000>=100&score%1000<200)) { + if (one[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==28&(score%1000>=200&score%1000<300)) { + if (two[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==28&(score%1000>=300&score%1000<400)) { + if (three[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==28&(score%1000>=400&score%1000<500)) { + if (four[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==28&(score%1000>=500&score%1000<600)) { + if (five[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==28&(score%1000>=600&score%1000<700)) { + if (six[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==28&(score%1000>=700&score%1000<800)) { + if (seven[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==28&(score%1000>=800&score%1000<900)) { + if (eight[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==28&(score%1000>=900&score%1000<1000)) { + if (nine[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } + if (n==24&(score%10000>=0&score%10000<1000)) { + if (zero[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==24&(score%10000>=1000&score%10000<2000)) { + if (one[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==24&(score%10000>=2000&score%10000<3000)) { + if (two[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==24&(score%10000>=3000&score%10000<4000)) { + if (three[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==24&(score%10000>=4000&score%10000<5000)) { + if (four[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==24&(score%10000>=5000&score%10000<6000)) { + if (five[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==24&(score%10000>=6000&score%10000<7000)) { + if (six[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==24&(score%10000>=7000&score%10000<8000)) { + if (seven[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==24&(score%10000>=8000&score%10000<9000)) { + if (eight[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } else if (n==24&(score%10000>=9000&score%10000<10000)) { + if (nine[r][c]==1) { + lcd.setPixel(c+n,r); + } else { + lcd.clearPixel(c+n,r); + } + } + } + } + } + for(int c=0; c<=18; c++) { //18 beacause the the loop stats from 0 but the array size from 1 + for(int r=0; r<=4; r++) { + if (g_score[r][c]==0) { + lcd.clearPixel(c,r); + } else if (g_score[r][c]==1) { + lcd.setPixel(c,r); + } + } + } + lcd.refresh(); +} \ No newline at end of file