Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <graphics_lib.h>
- #include <time.h>
- #include <stdlib.h>
- #include <math.h>
- #include <unistd.h>
- int initial_x_position= 100;
- int initial_y_position = 350;
- int total_Score = 0;
- int velocity;
- int angle = 45;
- int shoot = 0;
- int initial_projectile_x = 150;
- int initial_projectile_y = 295;
- int Target_X;
- int bin_x = 20000;
- int tree_x = 20000;
- //void main_menu();
- int get_velocity(void)
- {
- int velocity;
- printf("Enter a velocity: \n");
- scanf("%d", &velocity );
- return velocity;
- }
- int get_angle(void)
- { int angle;
- printf("Enter an angle: \n");
- scanf("%d", &angle );
- angle = angle * 0.0174533;
- return angle;
- }
- void draw_target(Target_X, initial_projectile_x)
- {
- int distance = (-initial_projectile_x + Target_X);
- GFX_DrawLine(Target_X, 410, Target_X, 470, 2);
- GFX_InitFont();
- draw_Well(Target_X - 15, 365);
- char Target_Text[20];
- GFX_SetColour(BLACK);
- sprintf(Target_Text, "Distance = %d", distance);
- GFX_DrawText(Target_X - 125, 450, Target_Text);
- }
- void display_score(score)
- {
- GFX_DrawFilledRectangle(50,50, 350, 80, LIGHTBLUE);
- GFX_InitFont();
- char score_text[20];
- sprintf(score_text, "Total Score: = %d", score);
- GFX_DrawText(50, 50, score_text);
- }
- int draw_Well(x,y)
- {
- GFX_InitBitmap();
- BITMAP well = GFX_LoadBitmap("well.png");
- GFX_DrawBitmap(well, x, y);
- GFX_UpdateDisplay();
- }
- int draw_Man(x,y)
- {
- GFX_InitBitmap();
- BITMAP man = GFX_LoadBitmap("man3.png");
- GFX_DrawBitmap(man, x, y);
- GFX_UpdateDisplay();
- }
- int draw_tree(x,y)
- {
- GFX_InitBitmap();
- BITMAP tree = GFX_LoadBitmap("tree.png");
- GFX_DrawBitmap(tree, x, y);
- GFX_UpdateDisplay();
- }
- int draw_bin(x,y)
- {
- GFX_InitBitmap();
- BITMAP bin = GFX_LoadBitmap("bin.png"); //bin is 70x72px
- GFX_DrawBitmap(bin, x, y);
- GFX_UpdateDisplay();
- }
- int draw_title(x,y)
- {
- GFX_InitBitmap();
- BITMAP title = GFX_LoadBitmap("title.png");
- GFX_DrawBitmap(title, x, y);
- GFX_UpdateDisplay();
- }
- int draw_Background(x,y)
- {
- GFX_DrawFilledRectangle(0,0,800,500,LIGHTBLUE);
- GFX_InitBitmap();
- BITMAP background = GFX_LoadBitmap("Background.png");
- GFX_DrawBitmap(background, x, y);
- GFX_UpdateDisplay();
- }
- int calculate_Score(int Target_X, int final_X)
- {
- int score;
- int temp;
- temp = abs(abs(Target_X)- abs(final_X));
- score = 1000-temp;
- return score;
- }
- int draw_stickman(int x, int y, int colour)
- {
- GFX_SetColour(colour);
- GFX_DrawLine(x, y, x + 40, y + 60, 2);
- GFX_DrawLine(x, y, x - 40, y + 60, 2);
- GFX_DrawLine(x, y, x, y + 5, 2);
- GFX_DrawLine(x, y, x, y - 70, 2);
- GFX_DrawCircle (x, y-90, 20, 2);
- GFX_DrawLine(x - 50 , y - 55, x + 50, y - 55, 2);
- void;
- }
- int draw_ground(x)
- { draw_Background(350,250);
- GFX_DrawFilledRectangle(0, x+6, 800, x+400, BROWN);
- GFX_SetColour(GREEN);
- GFX_DrawLine(0, x, 800, x, 5);
- GFX_SetColour(BLACK);
- GFX_DrawLine(0, x + 7, 800, x + 7, 10);
- void;
- }
- int draw_projectile(int initial_x, int initial_y, int vel_x, int vel_y)
- {
- float gravity = 9.81;
- float time;
- float x_position;
- float y_position;
- int hit;
- y_position = initial_y;
- x_position = initial_x;
- GFX_SetColour(BLACK);
- while(y_position < 410)
- {
- time = (x_position - initial_x)/ vel_x;
- y_position = initial_y - (vel_y*time) + (gravity*time*time)/2;
- GFX_DrawLineTo((int)x_position, (int)y_position, 3);
- x_position += 1;
- if(((int)y_position > 350) && ((bin_x -25) < (int)x_position))
- {
- if (((int)x_position)< (bin_x + 25))
- {
- hit = 0;
- break;
- }
- }
- else
- {
- hit = 1;
- }
- if(((int)y_position > 250) && ((tree_x -35) < (int)x_position))
- {
- if (((int)x_position)< (tree_x + 25))
- {
- hit = 0;
- break;
- }
- }
- else
- {
- hit = 1;
- }
- GFX_UpdateDisplay();
- }
- if (hit != 0)
- {
- draw_playagain_menu();
- }
- else
- {
- gameover();
- }
- return x_position;
- }
- int check_target(int final_x, int target_x)
- {
- if (final_x > (target_x-50) && final_x < (target_x+50))
- {
- printf("Well done\n");
- }
- else
- {
- printf("Better luck next time\n");
- }
- }
- int gameover()
- {
- int count = 0;
- GFX_DrawFilledRectangle(0, 65, 800, 120, LIGHTBLUE);
- GFX_DrawText(370, 68, "Game Over!");
- GFX_UpdateDisplay();
- sleep(3);
- main_menu();
- }
- int initialise_window(x,y)
- {
- //GFX_SetColour(RED);
- //draw_stickman(x, y, 5);
- draw_ground(x+310);
- draw_Man(x+9,y-22);
- void;
- }
- void draw_vel_controls(velocity)
- {
- GFX_DrawFilledTriangle(40,100, 40, 80, 30, 90, BLUE);
- GFX_DrawFilledTriangle(170,100, 170, 80, 180, 90, BLUE);
- GFX_InitFont();
- char vel_text[20];
- sprintf(vel_text, "Velocity: %d", velocity);
- GFX_DrawText(50, 80, vel_text);
- GFX_UpdateDisplay();
- }
- void draw_ang_controls(angle)
- {
- GFX_DrawFilledTriangle(220,100, 220, 80, 210, 90, BLUE);
- GFX_DrawFilledTriangle(320,100, 320, 80, 330, 90, BLUE);
- GFX_InitFont();
- char vel_text[20];
- sprintf(vel_text, "Angle: %d", angle);
- GFX_DrawText(230, 80, vel_text);
- GFX_UpdateDisplay();
- }
- void draw_playagain_menu()
- {
- GFX_DrawText(500, 70, "Next Level");
- GFX_DrawText(500, 100, "Main Menu");
- GFX_UpdateDisplay();
- }
- void get_controls()
- {
- int mouseX, mouseY;
- int mouseX2, mouseY2;
- int control_check = 1;
- GFX_CreateEventQueue();
- GFX_InitMouse();
- GFX_RegisterMouseEvents();
- while(control_check == 1)
- {
- GFX_WaitForEvent();
- if (GFX_IsEventMouseButton())
- {
- GFX_GetMouseCoordinates(&mouseX, &mouseY);
- if ((30 < mouseX && mouseX < 40) && (80 < mouseY && mouseY < 100))
- {
- if (velocity >=5)
- {
- velocity = velocity - 5;
- GFX_DrawFilledRectangle(50,80,150,100,LIGHTBLUE);
- draw_vel_controls(velocity);
- GFX_UpdateDisplay();
- }
- }
- else if ((170 < mouseX && mouseX < 180) && (80 < mouseY && mouseY < 100))
- {
- velocity = velocity + 5;
- GFX_DrawFilledRectangle(50,80,179,100,LIGHTBLUE);
- draw_vel_controls(velocity);
- GFX_UpdateDisplay();
- }
- else if ((350 < mouseX && mouseX < 420) && (70 < mouseY && mouseY < 110))
- { int i;
- i = fire(velocity, angle);
- if (i != 0)
- {
- control_check = 0;
- }
- int done=1;
- while(done)
- {
- GFX_WaitForEvent();
- if (GFX_IsEventMouseButton())
- {
- GFX_GetMouseCoordinates(&mouseX2, &mouseY2);
- if ((500 <mouseX2 && mouseX2 < 650) && (70 < mouseY2 && mouseY2< 85))
- {
- GFX_ClearWindow();
- initialise_window(initial_x_position, initial_y_position);
- game();
- GFX_UpdateDisplay();
- done=0;
- }
- else if ((500 <mouseX2 && mouseX2 < 650) && (100 < mouseY2 && mouseY2< 120 ))
- { GFX_ClearWindow();
- main_menu();
- total_Score = 0;
- GFX_UpdateDisplay();
- }
- }
- }
- }
- else if ((210 < mouseX && mouseX < 220) && (80 < mouseY && mouseY < 100))
- {
- if (angle >= 5)
- {
- angle = angle - 5;
- GFX_DrawFilledRectangle(221,80,319,100,LIGHTBLUE);
- draw_ang_controls(angle);
- GFX_WaitForEvent();
- }
- }
- else if ((320 < mouseX && mouseX < 330) && (80 < mouseY && mouseY < 100))
- {
- if (angle <= 80)
- {
- angle = angle + 5;
- GFX_DrawFilledRectangle(221,80,319,100,LIGHTBLUE);
- draw_ang_controls(angle);
- GFX_UpdateDisplay();
- }
- }
- }
- }
- }
- int fire(int velocity, int angle)
- {
- int vel_x;
- int vel_y;
- int final_x;
- int score;
- int distance;
- GFX_UpdateDisplay();
- vel_x = velocity * cos(angle*0.0174533);
- vel_y = velocity * sin(angle*0.0174533);
- GFX_MoveTo(initial_projectile_x, initial_projectile_y);
- final_x = draw_projectile(initial_projectile_x, initial_projectile_y,vel_x, vel_y);
- score = calculate_Score(final_x, Target_X);
- total_Score= total_Score + score;
- display_score(total_Score);
- GFX_UpdateDisplay();
- shoot = 0;
- distance = -(initial_projectile_x - final_x);
- printf("You threw %d metres\n", distance);
- printf("You scored %d\n", score);
- check_target(final_x, Target_X);
- GFX_UpdateDisplay();
- return final_x;
- }
- void clear_screen()
- { draw_target();
- GFX_UpdateDisplay();
- }
- void display_shoot()
- {
- GFX_DrawRectangle(350, 70, 420, 110, 2);
- GFX_DrawText(360, 80, "Shoot!");
- GFX_UpdateDisplay();
- }
- int get_highscores(x,y)
- {
- int i=0,totalNums;
- double Scores[100];
- char line[100]; /* declare a char array */
- FILE *file; /* declare a FILE pointer */
- file = fopen("highscores.txt", "r"); /* open a text file for reading */
- while(fgets(line, sizeof line, file)!=NULL) { /* keep looping until NULL pointer... */
- // printf("Lines of numbers.txt file are: %s", line);
- Scores[i]=atoi(line); /* convert string to double float*/
- i++;
- }
- char highscore1[20];
- sprintf(highscore1, "1. %d", (int)Scores[0]);
- GFX_DrawText(x, y + 20 , highscore1);
- char highscore2[20];
- sprintf(highscore2, "2. %d", (int)Scores[1]);
- GFX_DrawText(x,y + 40 , highscore2);
- fclose(file);
- char highscore3[20];
- sprintf(highscore3, "3. %d", (int)Scores[2]);
- GFX_DrawText(x, y+ 60 , highscore3);
- char highscore4[20];
- sprintf(highscore4, "4. %d", (int)Scores[3]);
- GFX_DrawText(x, y+ 80 , highscore4);
- }
- int game(void)
- {
- velocity = 50;
- srand(time(NULL));
- if( total_Score < 1500)
- {
- Target_X = GFX_RandNumber(250, 750);
- draw_target(Target_X);
- }
- else if ((total_Score >=1501) && (total_Score <= 5000))
- {
- Target_X = GFX_RandNumber(500, 750);
- if (Target_X < 500)
- {
- bin_x = GFX_RandNumber(Target_X + 50, Target_X + 200);
- }
- else if (Target_X > 500)
- {
- bin_x = GFX_RandNumber(250, Target_X - 200);
- }
- draw_target(Target_X, initial_projectile_x);
- draw_bin(bin_x, 387);
- }
- else if (total_Score > 5000)
- {
- Target_X = GFX_RandNumber(400, 750);
- tree_x = GFX_RandNumber(300, Target_X - 100);
- if (Target_X <500)
- {
- bin_x = GFX_RandNumber(Target_X + 50, 750);
- }
- bin_x = GFX_RandNumber(Target_X + 50, Target_X + 200);
- draw_tree(tree_x, 322);
- draw_target(Target_X, initial_projectile_x);
- draw_bin(bin_x, 387);
- }
- display_score(total_Score);
- display_shoot();
- draw_vel_controls(velocity);
- draw_ang_controls(angle);
- get_controls();
- GFX_UpdateDisplay();
- }
- void main_menu()
- {
- int mouseX, mouseY;
- int check = 1;
- GFX_UpdateDisplay();
- draw_Background(350,250);
- draw_ground(initial_x_position+310);
- draw_title(395, 30);
- GFX_DrawRectangle(200, 120, 600, 170, 2);
- GFX_InitFont();
- GFX_DrawText(380, 138, "Play");
- GFX_DrawRectangle(200,190,600,240,2);
- GFX_DrawText(380,208, "Exit");
- GFX_DrawText(200, 65, "Welcome! Adjust the angle and velocity to throw ");
- GFX_DrawText(200, 77, "the coin as close to the well as possible. Try");
- GFX_DrawText(200, 89, "and avoid any obstacles. Good luck! ");
- GFX_DrawText(357, 250, "HIGHSCORES:");
- get_highscores(360, 260);
- GFX_UpdateDisplay();
- GFX_CreateEventQueue();
- GFX_InitMouse();
- GFX_RegisterMouseEvents();
- while(check == 1)
- {
- GFX_WaitForEvent();
- if (GFX_IsEventMouseButton())
- {
- GFX_GetMouseCoordinates(&mouseX, &mouseY);
- if ((200 < mouseX && mouseX < 600) && (120 < mouseY && mouseY < 170))
- {
- check = 0;
- GFX_ClearWindow();
- initialise_window(initial_x_position, initial_y_position);
- game();
- }
- else if ((200 < mouseX && mouseX < 600) && (190 < mouseY && mouseY < 240))
- {
- check = 0;
- GFX_CloseWindow();
- }
- }
- }
- }
- int main(void)
- {
- GFX_InitWindow(800, 480);
- main_menu();
- getchar();
- GFX_CloseWindow();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement