Hey I am currently trying to make a simple game using the microsoft visual c++ compiler.
The problem I have is with level two. I currently have two circles that i want to move up and down out of holes for the stick man to jump over, I have got them moving but its interfering with the getch so the man cannot move. Any help rectifying this would be very helpful. Thanks in advance
header files (levels.h)
Code:
#ifndef __levels_h__
#define __levels_h__
void level_one(void);
void level_two(void);
void level_three(void);
void level_four(void);
#endif
source files levels
Code:
#include "graphics_lib.h"
#include <stdio.h>
#include <math.h>
/*
* The level function - starts executing here
*/
void level_one(void)
{
int x_head, y_head;
int wall_colour;
// TARGET VARIABLLES
char label [3];
int x_line;
int y_line;
int x_label;
int y_label;
int scores[5] = {50,20,10,20,50};
int line1;
wall_colour=BROWN;
// Initialise stick man position
x_head = 150;
y_head = 180;
//draw wall one
setcolor(wall_colour);
rectangle(470,220,520,324);
setfillstyle(SOLID_FILL,wall_colour);
floodfill(490,270,wall_colour);
// Ground level
setcolor(GREEN);
line(0,325,1200,325);
line(0,325,0,480);
line(0,480,1200,480);
line(1200,325,1200,480);
setfillstyle(SOLID_FILL,GREEN);
floodfill(1,326,GREEN);
//draw wall one
setcolor(wall_colour);
rectangle(470,220,520,324);
setfillstyle(SOLID_FILL,wall_colour);
floodfill(490,270,wall_colour);
//draw wall two
setcolor(wall_colour);
rectangle(970,120,1020,324);
setfillstyle(SOLID_FILL,wall_colour);
floodfill(990, 210,wall_colour);
// TARGET
for (line1 = 0; line1 < 5; line1++)
{
/* Calculate position of line based on the value of the line variable */
y_line = 300;
x_line = 550 + 84*line1;
/* Draw the line in the right place */
setcolor(BLUE);
line(x_line, y_line, x_line, y_line + 25);
/* Calculate position of lable based on the value of the line variable */
x_label = 550 + 84*line1;
y_label = 300;
/* Create a string with lable in it */
sprintf(label, "%d", scores[line1]);
/* Use outtextxy to display the text */
outtextxy (x_label, y_label, label);
}
}
void level_two(int stick_man_colour)
{
int x_head, y_head;
int x_moon1;
int y_moon1;
int x_moon2;
int y_moon2;
int direction = 0;
char key_press;
int move_value = 0;
int level=1;
//int stick_man_colour;
int move = 0;
// Prototypes
int jump_stickman(int x_head, int y_head, int stick_man_colour);
int jump_stickman2(int x_head, int y_head, int stick_man_colour);
// Initialise stick man position
x_head = 150;
y_head = 180;
//Initialise moon_1 position
x_moon1 = 495;
y_moon1 = 390;
//Initialise moon_2 position
x_moon2 = 795;
y_moon2 = 390;
// Ground level
setcolor(BLACK);
line(0,325,1200,325);
setcolor(GREEN);
line(0,325,0,480);
line(0,480,1200,480);
line(1200,325,1200,480);
setfillstyle(SOLID_FILL,GREEN);
floodfill(1,326,GREEN);
//draw hole one
setcolor(BLACK);
rectangle(470,325,520,410);
setfillstyle(SOLID_FILL,BLACK);
floodfill(500,350,BLACK);
//draw hole two
setcolor(BLACK);
rectangle(770,325,820,410);
setfillstyle(SOLID_FILL,BLACK);
floodfill(800,350,BLACK);
//cover wall one
setcolor(BLACK);
rectangle(470,220,520,410);
setfillstyle(SOLID_FILL,BLACK);
floodfill(490,270,BLACK);
//cover wall two
setcolor(BLACK);
rectangle(970,120,1020,324);
setfillstyle(SOLID_FILL,BLACK);
floodfill(990,210,BLACK);
//draw moon_1
setcolor(WHITE);
circle(x_moon1, y_moon1,20);
setfillstyle(SOLID_FILL, WHITE);
fillellipse(x_moon1,y_moon1, 20, 20);
//draw moon_2
setcolor(WHITE);
circle(x_moon2, y_moon2,20);
setfillstyle(SOLID_FILL, WHITE);
fillellipse(x_moon2,y_moon2,20,20);
while(1)
{
if(kbhit == 0)
{
key_press = getch();
if (key_press == 75 && x_head >= 60 && level !=3)
{
move_value = -58;
}
else if (key_press == 77 && x_head <= 412 && level !=2 && level !=3)
{ move_value = 58;
}
else if (key_press == 77 && x_head <=890 && level==2)
{move_value = 58;
}
else if (key_press ==75 && x_head <=870 && level==3)
{ move_value = 58;
}
else if (key_press == 77 && x_head >=60 && level==3)
{ move_value = -58;
}
else if (key_press == 72 && level==2 && x_head<=780)
{
x_head = jump_stickman(x_head,y_head, stick_man_colour);
}
else if (key_press == 80 && level==2 && x_head>=180)
{
x_head = jump_stickman2(x_head,y_head, stick_man_colour);
}
else if (key_press == 80 && level==3 && x_head<=780)
{
x_head = jump_stickman(x_head,y_head, stick_man_colour);
}
else if (key_press == 72 && level==3 && x_head>=180)
{
x_head = jump_stickman2(x_head,y_head, stick_man_colour);
}
} // end of kbhit
printf("testing");
//erase moon_1
setcolor(BLACK);
circle(x_moon1, y_moon1,20);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(x_moon1,y_moon1, 20, 20);
if( direction == 0)
{
y_moon1 -=5;
if( y_moon1 < 80)
direction = 1;
}
else if( direction == 1)
{
y_moon1 +=5;
if( y_moon1 > 380)
direction = 0;
}
//erase moon_2
setcolor(BLACK);
circle(x_moon2, y_moon2,20);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(x_moon2,y_moon2,20,20);
if( direction ==0)
{
y_moon2 -=5;
if( y_moon1 < 80)
direction = 1;
}
else if( direction == 1)
{
y_moon2 +=5;
if( y_moon1 > 380)
direction = 0;
}
/* if (y_moon1 <= 390 && y_moon1 >= 150)
{
y_moon1 -= 5 ;
if(y_moon1 > 150 )
y_moon1 -= 5;
*/
//draw moon1
setcolor(WHITE);
circle(x_moon1, y_moon1,20);
setfillstyle(SOLID_FILL, WHITE);
fillellipse(x_moon1,y_moon1, 20, 20);
delay(5);
//draw moon_2
setcolor(WHITE);
circle(x_moon2, y_moon2,20);
setfillstyle(SOLID_FILL, WHITE);
fillellipse(x_moon2,y_moon2,20,20);
delay(1);
}
if (move == 32 && level==1)
{
move_value = 0;
}
}
void level_three(void)
{
int x_head, y_head;
int x_moon1;
int y_moon1;
int x_moon2;
int y_moon2;
int x_moon3;
int y_moon3;
int x_cresent1;
int y_cresent1;
int x_cresent2;
int y_cresent2;
int x_cresent3;
int y_cresent3;
// Initialise stick man position
x_head = 150;
y_head = 180;
//Initialise moon_1 position
x_moon1 = 495;
y_moon1 = 390;
//Initialise cresent_1 position
x_cresent1 =490;
y_cresent1 =387;
//Initialise moon_2 position
x_moon2 = 795;
y_moon2 = 390;
//Initialise cresent_2 position
x_cresent2 =790;
y_cresent2 =387;
//Initialise moon_3 position
x_moon3 =150;
y_moon3 =290;
//Initialise cresent_3 position
x_cresent3 =142;
y_cresent3 =287;
// Ground level
setcolor(GREEN);
line(0,325,1200,325);
line(0,325,0,480);
line(0,480,1200,480);
line(1200,325,1200,480);
setfillstyle(SOLID_FILL,GREEN);
floodfill(1,326,GREEN);
//cover hole one
setcolor(GREEN);
rectangle(470,325,520,410);
setfillstyle(SOLID_FILL,GREEN);
floodfill(500,350,GREEN);
//cover hole two
setcolor(GREEN);
rectangle(770,325,820,410);
setfillstyle(SOLID_FILL,GREEN);
floodfill(800,350,GREEN);
//cover wall one
setcolor(BLACK);
rectangle(470,220,520,324);
setfillstyle(SOLID_FILL,BLACK);
floodfill(490,270,BLACK);
//cover wall two
setcolor(BLACK);
rectangle(970,120,1020,324);
setfillstyle(SOLID_FILL,BLACK);
floodfill(990, 210,BLACK);
//draw moon_3
setcolor(WHITE);
circle(x_moon3, y_moon3,30);
setfillstyle(SOLID_FILL, WHITE);
fillellipse(x_moon3,y_moon3, 30, 30);
//draw cresent_3
setcolor(BLACK);
circle(x_cresent3, y_cresent3,24);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(x_cresent3,y_cresent3, 24, 24);
}
void level_four(void)
{
int x_head, y_head;
int wall_colour;
int x_moon3;
int y_moon3;
int x_cresent3;
int y_cresent3;
wall_colour=BROWN;
// Initialise stick man position
x_head = 150;
y_head = 180;
//Initialise moon_3 position
x_moon3 =150;
y_moon3 =290;
//Initialise cresent_3 position
x_cresent3 =142;
y_cresent3 =287;
// Ground level
setcolor(GREEN);
line(0,325,1200,325);
line(0,325,0,480);
line(0,480,1200,480);
line(1200,325,1200,480);
setfillstyle(SOLID_FILL,GREEN);
floodfill(1,y_head+226,GREEN);
//draw wall one
setcolor(wall_colour);
rectangle(470,220,520,324);
setfillstyle(SOLID_FILL,wall_colour);
floodfill(480,270,wall_colour);
//draw wall two
setcolor(wall_colour);
rectangle(970,120,1020,324);
setfillstyle(SOLID_FILL,wall_colour);
floodfill(990, 210,wall_colour);
//cover moon_3
setcolor(BLACK);
circle(x_moon3, y_moon3,30);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(x_moon3,y_moon3,30,30);
}
main source file
Code:
/*
* A program to demonstrate simple graphical operations
* C Programming laboratory 1
*/
/* This line allows the compiler to understand the
* graphics functions
*/
#include "graphics_lib.h"
#include <stdio.h>
#include <math.h>
#define GRAVITY 9.81
#define PI 3.14159265
#include "levels.h"
/*
* The main function - the program starts executing here
*/
// Prototypes
int jump_stickman(int x_head, int y_head, int stick_man_colour);
int jump_stickman2(int x_head, int y_head, int stick_man_colour);
int main(void)
{
/* window variables */
int x_window=1200, y_window=480;
/*movement keypress*/
int move = 0;
/*distance to move x axis*/
int move_value = 0;
int exit = 0;
/* stick_man variables */
int stick_man_colour;
int x_head, y_head;
double time;
int initial_pos_x;
int initial_pos_y;
double vel_x;
double vel_y;
int pos_x;
int pos_y;
int degreeAngle;
double radAngle;
int velocity;
int level=1;
int shotstaken=0;
int x_moon1;
int y_moon1;
int x_moon2;
int y_moon2;
int x_moon3;
int y_moon3;
int x_cresent1;
int y_cresent1;
int x_cresent2;
int y_cresent2;
int x_cresent3;
int y_cresent3;
// Allow color choice
printf("\nChoose an integer representing the colour of your stick-man\n");
printf("colour from the following options:\n\n");
printf("1\tBLUE\t\t\t9\tLIGHTBLUE\n");
printf("2\tGREEN\t\t\t10\tLIGHTGREEN\n");
printf("3\tCYAN\t\t\t11\tLIGHTCYAN\n");
printf("4\tRED\t\t\t12\tLIGHTRED\n");
printf("5\tMAGENTA\t\t\t13\tLIGHTMAGENTA\n");
printf("6\tBROWN\t\t\t14\tYELLOW\n");
printf("7\tLIGHTGRAY\t\t15\tWHITE\n");
printf("8\tDARKGRAY\n");
printf("\nWhat coulour would you like your stick-man to be?\n");
scanf("%d",&stick_man_colour);
//Allow velocity choice
printf("\nChoose your projectiles velocity\n");
scanf("%d", &velocity);
printf("\nWhat angle would you like this speed to be at?\n");
printf("\nNote: Angle is measured in degrees from the ground.");
printf("\nValues between 89 and -89 are accepted.\n");
printf("\nTip for inexperienced players: Angle should be more than 0 degrees");
printf("\nYou will loose a life if it is not.\n");
scanf("%d", °reeAngle);
radAngle= ((degreeAngle*PI)/180);
vel_y=velocity*sin(radAngle);
vel_x=velocity*cos(radAngle);
// Initialise stick man position
x_head = 150;
y_head = 180;
initwindow(x_window, y_window);
//Initialise moon_1 position
x_moon1 = 495;
y_moon1 = 390;
//Initialise cresent_1 position
x_cresent1 =490;
y_cresent1 =387;
//Initialise moon_2 position
x_moon2 = 795;
y_moon2 = 390;
//Initialise cresent_2 position
x_cresent2 =790;
y_cresent2 =387;
//Initialise moon_3 position
x_moon3 =150;
y_moon3 =290;
//Initialise cresent_3 position
x_cresent3 =142;
y_cresent3 =287;
do
{
// ********* DRAW STICK MAN ********
setcolor(stick_man_colour);
// draw head
setfillstyle(SOLID_FILL,stick_man_colour);
circle(x_head, y_head, 40);
fillellipse(x_head,y_head, 40,40);
// draw body
line(x_head,y_head+30,x_head,y_head+85);
// draw arms
line(x_head-30,y_head+60,x_head+30,y_head+60);
// draw legs
//Left leg
line(x_head,y_head+85,x_head - 20,y_head+144);
// Right Leg
line(x_head,y_head+85,x_head +20,y_head+144);
/********* END OF STICK MAN ********/
if (level == 1)
{
level_one();
}
if (level == 2)
{
level_two();
}
if (level == 3)
{
level_three();
}
if (level == 4)
{
level_four();
}
move = getch();
if (move == 75 && x_head >= 60 && level !=3)
move_value = -58;
else if (move == 77 && x_head <= 412 && level !=2 && level !=3)
move_value = 58;
else if (move == 77 && x_head <=890 && level==2)
move_value = 58;
else if (move ==75 && x_head <=870 && level==3)
move_value = 58;
else if (move == 77 && x_head >=60 && level==3)
move_value = -58;
else if (move == 72 && level==2 && x_head<=780)
{
x_head = jump_stickman(x_head,y_head, stick_man_colour);
}
else if (move == 80 && level==2 && x_head>=180)
{
x_head = jump_stickman2(x_head,y_head, stick_man_colour);
}
else if (move == 80 && level==3 && x_head<=780)
{
x_head = jump_stickman(x_head,y_head, stick_man_colour);
}
else if (move == 72 && level==3 && x_head>=180)
{
x_head = jump_stickman2(x_head,y_head, stick_man_colour);
}
else if (move == 32 && level==1)
{
move_value = 0;
// Draw projectile
/* setcolor(stick_man_colour);
initial_pos_y = y_head;
initial_pos_x = x_head;
moveto(initial_pos_x, initial_pos_y);
pos_x = initial_pos_x;
do
{
time = (pos_x - initial_pos_x) / vel_x;
// time taken to move to pos_x
pos_y = (int) (initial_pos_y - (vel_y * time) + (GRAVITY * time * time)/2);
if (pos_y >= 325)
break;
if (pos_x >=470 && pos_x <=520 && pos_y>=220)
break;
if (pos_x>=970 && pos_x <=1020 && pos_y>=120)
break;
if (shotstaken>=3)
break;
else
lineto(pos_x,pos_y);
pos_x++;
setcolor(stick_man_colour);
// draw head
setfillstyle(SOLID_FILL,stick_man_colour);
circle(pos_x, pos_y, 40);
fillellipse(pos_x,pos_y, 40,40);
// draw body
line(pos_x,pos_y+30,pos_x,pos_y+85);
// draw arms
line(pos_x-30,pos_y+60,pos_x+30,pos_y+60);
// draw legs
//Left leg
line(pos_x,pos_y+85,pos_x - 20,pos_y+144);
// Right Leg
line(pos_x,pos_y+85,pos_x +20,pos_y+144);
Sleep(1);
setcolor(BLACK);
// draw head
setfillstyle(SOLID_FILL,BLACK);
circle(pos_x, pos_y, 40);
fillellipse(pos_x,pos_y, 40,40);
// draw body
line(pos_x,pos_y+30,pos_x,pos_y+85);
// draw arms
line(pos_x-30,pos_y+60,pos_x+30,pos_y+60);
// draw legs
//Left leg
line(pos_x,pos_y+85,pos_x - 20,pos_y+144);
// Right Leg
line(pos_x,pos_y+85,pos_x +20,pos_y+144);
} while (pos_x <1200);
initial_pos_x =pos_x;
initial_pos_y =pos_y;
pos_x = x_head;
*/
// Draw projectile
setcolor(stick_man_colour);
initial_pos_y = y_head + 60;
initial_pos_x = x_head + 30;
moveto(initial_pos_x, initial_pos_y);
pos_x = initial_pos_x;
do
{
time = (pos_x - initial_pos_x) / vel_x;
// time taken to move to pos_x
pos_y = (int) (initial_pos_y - (vel_y * time) + (GRAVITY * time * time)/2);
if (pos_y >= 325)
break;
if (pos_x >=470 && pos_x <=520 && pos_y>=220)
break;
if (pos_x>=970 && pos_x <=1020 && pos_y>=120)
break;
if (shotstaken>=3)
break;
else
lineto(pos_x,pos_y);
pos_x++;
} while (pos_x <1200);
shotstaken = shotstaken+1;
delay(1000);
setcolor(BLACK);
initial_pos_y = y_head + 60;
initial_pos_x = x_head + 30;
moveto(initial_pos_x, initial_pos_y);
pos_x = initial_pos_x;
do
{
time = (pos_x - initial_pos_x) / vel_x; // time taken to move to pos_x
pos_y = (int) (initial_pos_y - (vel_y * time) + (GRAVITY * time * time)/2);
if (pos_y >= 325)
break;
if (pos_x >=470 && pos_x <=520 && pos_y >=220)
break;
if (pos_x>=970 && pos_x <=1020 && pos_y>=120)
break;
if (level>=2)
break;
else
lineto(pos_x,pos_y);
pos_x++;
} while (pos_x <1200);
}
else
move_value = 0;
// ********* COVER STICK MAN ********
setcolor(BLACK);
// draw head
setfillstyle(SOLID_FILL,BLACK);
circle(x_head, y_head, 40);
fillellipse(x_head,y_head, 40, 40);
// draw body
line(x_head,y_head+30,x_head,y_head+85);
// draw arms
line(x_head-30,y_head+60,x_head+30,y_head+60);
// draw legs
//Left leg
line(x_head,y_head+85,x_head - 20,y_head+144);
// Right Leg
line(x_head,y_head+85,x_head +20,y_head+144);
/********* END OF STICK MAN ********/
x_head = x_head + move_value;
if (level >= 2)
shotstaken = 0;
if (shotstaken==3){
shotstaken = 0;
level++;
}
printf("\n%d %d\n",shotstaken,level);
if (x_head>=890 && level==2)
level++;
if (x_head<=210 && level==3)
level++;
if (y_head <=110 && level==1 && move ==80)
y_head = 180;
} while (exit == 0);
return 0;
}
int jump_stickman(int x_head, int y_head, int stick_man_colour)
{
int initial_pos_x;
int initial_pos_y;
double time;
int pos_x;
int pos_y;
double vel_x;
double vel_y;
vel_x = 25;
vel_y = 30;
setcolor(stick_man_colour);
initial_pos_y = y_head;
initial_pos_x = x_head;
moveto(initial_pos_x, initial_pos_y);
pos_x = initial_pos_x;
do
{
time = (pos_x - initial_pos_x) / vel_x;
// time taken to move to pos_x
pos_y = (int) (initial_pos_y - (vel_y * time) + (GRAVITY * time * time)/2);
if (pos_y > y_head)
break;
else
{
setcolor(stick_man_colour);
// draw head
setfillstyle(SOLID_FILL,stick_man_colour);
circle(pos_x, pos_y, 40);
fillellipse(pos_x,pos_y, 40,40);
// draw body
line(pos_x,pos_y+30,pos_x,pos_y+85);
// draw arms
line(pos_x-30,pos_y+60,pos_x+30,pos_y+60);
// draw legs
//Left leg
line(pos_x,pos_y+85,pos_x - 20,pos_y+144);
// Right Leg
line(pos_x,pos_y+85,pos_x +20,pos_y+144);
Sleep(1);
setcolor(BLACK);
// draw head
setfillstyle(SOLID_FILL,BLACK);
circle(pos_x, pos_y, 40);
fillellipse(pos_x,pos_y, 40,40);
// draw body
line(pos_x,pos_y+30,pos_x,pos_y+85);
// draw arms
line(pos_x-30,pos_y+60,pos_x+30,pos_y+60);
// draw legs
//Left leg
line(pos_x,pos_y+85,pos_x - 20,pos_y+144);
// Right Leg
line(pos_x,pos_y+85,pos_x +20,pos_y+144);
}
pos_x++;
} while (pos_x <1200);
/* setcolor(stick_man_colour);
// draw head
setfillstyle(SOLID_FILL,stick_man_colour);
circle(pos_x, pos_y, 40);
fillellipse(pos_x,pos_y, 40,40);
// draw body
line(pos_x,pos_y+30,pos_x,pos_y+85);
// draw arms
line(pos_x-30,pos_y+60,pos_x+30,pos_y+60);
// draw legs
//Left leg
line(pos_x,pos_y+85,pos_x - 20,pos_y+144);
// Right Leg
line(pos_x,pos_y+85,pos_x +20,pos_y+144);
*/
// initial_pos_x =pos_x;
// initial_pos_y =pos_y;
// pos_x = x_head;
return pos_x;
}
int jump_stickman2(int x_head, int y_head, int stick_man_colour)
{
int initial_pos_x;
int initial_pos_y;
double time;
int pos_x;
int pos_y;
double vel_x;
double vel_y;
vel_x = -25;
vel_y = 30;
setcolor(stick_man_colour);
initial_pos_y = y_head;
initial_pos_x = x_head;
moveto(initial_pos_x, initial_pos_y);
pos_x = initial_pos_x;
do
{
time = (pos_x - initial_pos_x) / vel_x;
// time taken to move to pos_x
pos_y = (int) (initial_pos_y - (vel_y * time) + (GRAVITY * time * time)/2);
if (pos_y > y_head)
break;
else
{
setcolor(stick_man_colour);
// draw head
setfillstyle(SOLID_FILL,stick_man_colour);
circle(pos_x, pos_y, 40);
fillellipse(pos_x,pos_y, 40,40);
// draw body
line(pos_x,pos_y+30,pos_x,pos_y+85);
// draw arms
line(pos_x-30,pos_y+60,pos_x+30,pos_y+60);
// draw legs
//Left leg
line(pos_x,pos_y+85,pos_x - 20,pos_y+144);
// Right Leg
line(pos_x,pos_y+85,pos_x +20,pos_y+144);
Sleep(1);
setcolor(BLACK);
// draw head
setfillstyle(SOLID_FILL,BLACK);
circle(pos_x, pos_y, 40);
fillellipse(pos_x,pos_y, 40,40);
// draw body
line(pos_x,pos_y+30,pos_x,pos_y+85);
// draw arms
line(pos_x-30,pos_y+60,pos_x+30,pos_y+60);
// draw legs
//Left leg
line(pos_x,pos_y+85,pos_x - 20,pos_y+144);
// Right Leg
line(pos_x,pos_y+85,pos_x +20,pos_y+144);
}
pos_x--;
} while (pos_x <1200);
return pos_x;
}