Ive been learning c for the past few weeks, I made a simple turn-based game to show off what I know. Is there anything I can do to improve the code or make the game run better?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <time.h>
#include <stdbool.h>
int main(){
srand(time(NULL));
bool play = true;
while(play){
int choose = -2;
int P_h = 25;
int E_h = 30;
int SP_u = 3;
puts("Do you wanna play this game. [Press 0 to exit or press any other number to play]");
scanf("%d", &choose);
if(choose == 0){
play = false;
puts("Goodbye");
break;
}
else{
puts("Okay");
Sleep(300);
while(E_h > 0){
int P_at = (rand()%8);
int E_at = (rand()%10);
int SP = 100;
int at_p = -5;
if(P_h <= 0){
puts("You have died");
break;
}
if(E_h <= 0){
puts("you win!");
break;
}
printf("You have %d uses left of the super attack \n", SP_u);
puts("Would you like to attack or do a super attack [Press 0 to do a super attack, Press 1 to attack]");
scanf("%d", &at_p);
if(at_p == 0){
if(SP_u <= 0){
puts("Sorry, you have 0 uses left, you cannot use this \n");
}
else{
E_h -= SP;
SP_u--;
printf("\nThe enemy took a damage of %d! It now has a health of %d!", SP, E_h);
P_h -= E_at;
printf("\nThe enemy attacks! Your health went down by %d it is now %d \n \n", E_at, P_h);
}
}
else if(at_p == 1){
E_h -= P_at;
printf("\nThe enemy took a damage of %d! It now has a health of %d!", P_at, E_h);
P_h -= E_at;
printf("\nThe enemy attacks! Your health went down by %d it is now %d \n \n", E_at, P_h);
}
else{
puts("Thats not an option, idiot \n");
}
}
}
if(E_h <= 0){
puts("You killed the enemy!");
puts("Would you like to play again? [0 to exit, Press anything else to play again]");
scanf("%d", &choose);
if(choose == 0){
play = false;
break;
}
else{
play = true;
}
}
else if(P_h <= 0){
puts("You died. Would you like to play again to try to defeat the enemy. [0 to exit, anything else to play again]");
scanf("%d", &choose);
if(choose == 0){
play = false;
break;
}
else{
play = true;
}
}
}
return 0;
}
I feel like there is a better way to program the decisions because right now they are just nested if statements.
I think