i'm trying to make a geme with c++ but i can finish it because i have trouble with the code :( .....this is the code of the game that i want to make....

#include<iostream.h>
#include<conio.h>
#include<graphics.h>

//using namespace std;

int main(void)
{
    
    double pos_x,i_pos_x,pos_y,i_pos_y,vel_x,vel_y;
    double time,gravity ; //t=time,g=gravity
    int x_position,y_position;
    
    initwindow(640,480);
    
    //stick person
    setcolor(CYAN);
    circle(50,200,30);   
    setfillstyle(1,3);
    floodfill(50,200,3);
    
    setcolor(CYAN);
    line(50,230,50,300);//badan
    line(20,250,80,250);//tangan
    line(50,300,90,340);//kaki kanan
    line(50,300,10,340);//kaki kiri
    
   
    
    
    //stand
    setcolor(3);
    line(0,340,110,340);//2
    line(110,340,110,450);//3
    line(0,450,110,450);//4
    line(0,340,0,450);//1
    setfillstyle(1,3);
    floodfill(100,440,3);
    

    line(0,450,640,450);//platform
    
    //box
    line(400,380,400,450);//1
    line(400,450,500,450);//2
    line(500,380,500,450);//3
    
    
     //ball
    setcolor(RED);
    circle(90,250,10);
    setfillstyle(1,4);
    floodfill(90,250,4);
    
    pos_x=90;
    i_pos_x=90;
    pos_y=250;
    i_pos_y=250;
    vel_x=60;
    vel_y=60;
    gravity=9.81;
    time=6.2;
    
    moveto((int)pos_x,(int)pos_y);
    for(pos_x=i_pos_x;pos_x<450;pos_x++)
    {
                                    time=(pos_x-i_pos_x)/vel_x;
                                    pos_y=i_pos_y-(vel_y*time)+(9.81*time*time);
                                    lineto((int)pos_x,(int)pos_y);
                                    delay(7);
                                    }
    
    
    
    getch ();
    closegraph();
    return 0;
}

i'm trying to make the ball move but there is only the line that move not the ball at the stick person hand:-/ ...can somebody help me to solve this problem...:?:

Dani AI

Generated

Short version: the line moved because the program was drawing a continuous path instead of repainting a discrete, filled sprite every frame. The reliable pattern is: compute the ball position from elapsed time (use consistent units), restore the background for that frame, draw a filled circle at the new position, then present the frame. made a good start by isolating the scene drawing into a function; that helps keep static art separate from the moving sprite.

Use a time-stepped simulation (dt) rather than incrementing X by 1 and deriving time from that. Keep velocities in pixels/second and advance a timer by the frame interval so motion is framerate-independent. The vertical motion in a top-left-origin graphics system (y increases downward) is:
y = y0 - vyt + 0.5gtt
(0.5 is important; sign depends on whether vy is defined as upward). Example loop structure:

double dt = frameMs / 1000.0;   // seconds per frame
double t = 0.0;
for (;;) {
  int x = (int)(x0 + vx * t);
  int y = (int)(y0 - vy * t + 0.5 * g * t * t);
  // restore background, draw ball at (x,y), present frame
  delay(frameMs);
  t += dt;
  if (y >= groundY) break;
}

Rendering tips to avoid flicker: draw the ball with a fill primitive (for BGI use setfillstyle(...) + fillellipse(...)) instead of circle+floodfill (floodfill depends on a closed border color and often misbehaves). To prevent flicker, either restore a saved background with putimage before drawing the ball, or use page flipping if available (setactivepage / setvisualpage) so the scene is composed off-screen and swapped to the display.

Quick troubleshooting checklist: cast positions to int for drawing calls, ensure gravity/velocities are in the same units (pixels vs meters), pick a dt that matches your delay, and test with exaggerated gravity to verify motion visually.

Recommended Answers

All 8 Replies

int main()

to begin... more to follow...

Does your compiler fuss at you for not having header files or anything?


Ahh I see what the problem is.. the arch is stopping or getting too slow. Let me see if I can track it down.

and the int main(void) still works fine anyhow

Add Code Tags Please !!!

And there is no #include<iostream.h> its #include<iostream>

Alright so I got the line to go further with this here:

for(pos_x=i_pos_x;pos_x<555;pos_x++)

my suggestion is something along the line of instead of having pos_x < XXX have it say something like pos_y > 0 (If its the ground you're trying to strike)


ROFL.. I just re-read your post. I'm not even looking at it right. I'll try something with the ball now

My fault

for(pos_x=i_pos_x;pos_y>0;pos_x++)
{
    time=(pos_x-i_pos_x)/vel_x;
    pos_y=i_pos_y-(vel_y*time)+(9.81*time*time);
    // lineto((int)pos_x,(int)pos_y);
    circle(pos_x,pos_y,10);
    setfillstyle(1,4);
    floodfill(90,250,4);
    delay(1);
}

Is where I'm at now. All we need is something to destroy the previous circle and we're golden.

#include <iostream>
#include <conio.h>
#include <graphics.h>

// using namespace std;


void drawSetting(); // Function to redraw everything

int main(void)
{

double pos_x,i_pos_x,pos_y,i_pos_y,vel_x,vel_y;
double time,gravity ; //t=time,g=gravity
int x_position,y_position;

initwindow(640,480);




//ball
//setcolor(RED);
//circle(90,250,10);
//setfillstyle(1,4);
//floodfill(90,250,4);




pos_x=90;
i_pos_x=90;
pos_y=250;
i_pos_y=250;
vel_x=60;
vel_y=60;
gravity=9.81;
time=6.2;

moveto((int)pos_x,(int)pos_y);
for(pos_x=i_pos_x;pos_y>0;pos_x++)
{
    time=(pos_x-i_pos_x)/vel_x;
    pos_y=i_pos_y-(vel_y*time)+(9.81*time*time);
    //lineto((int)pos_x,(int)pos_y);
    cleardevice();
    drawSetting();
    setcolor(RED);
    circle(pos_x,pos_y,10);
    setfillstyle(1,4);
    floodfill(pos_x,pos_y,4);
    delay(7);
}



getch ();
closegraph();
return 0;
}

void drawSetting()
{
    //stick person
    setcolor(CYAN);
    circle(50,200,30);
    setfillstyle(1,3);
    floodfill(50,200,3);

    setcolor(CYAN);
    line(50,230,50,300);//badan
    line(20,250,80,250);//tangan
    line(50,300,90,340);//kaki kanan
    line(50,300,10,340);//kaki kiri




    //stand
    setcolor(3);
    line(0,340,110,340);//2
    line(110,340,110,450);//3
    line(0,450,110,450);//4
    line(0,340,0,450);//1
    setfillstyle(1,3);
    floodfill(100,440,3);


    line(0,450,640,450);//platform

    //box
    line(400,380,400,450);//1
    line(400,450,500,450);//2
    line(500,380,500,450);//3
}

This works somewhat... I do NOT like the way it looks. :<

#include <iostream>
#include <conio.h>
#include <graphics.h>

// using namespace std;


void drawSetting(); // Function to redraw everything

int main(void)
{

double pos_x,i_pos_x,pos_y,i_pos_y,vel_x,vel_y;
double time,gravity ; //t=time,g=gravity
int x_position,y_position;

initwindow(640,480);




//ball
//setcolor(RED);
//circle(90,250,10);
//setfillstyle(1,4);
//floodfill(90,250,4);




pos_x=90;
i_pos_x=90;
pos_y=250;
i_pos_y=250;
vel_x=60;
vel_y=60;
gravity=9.81;
time=6.2;

moveto((int)pos_x,(int)pos_y);
for(pos_x=i_pos_x;pos_y>0;pos_x++)
{
    time=(pos_x-i_pos_x)/vel_x;
    pos_y=i_pos_y-(vel_y*time)+(9.81*time*time);
    //lineto((int)pos_x,(int)pos_y);
    cleardevice();
    drawSetting();
    setcolor(RED);
    circle(pos_x,pos_y,10);
    setfillstyle(1,4);
    floodfill(pos_x,pos_y,4);
    delay(7);
}



getch ();
closegraph();
return 0;
}

void drawSetting()
{
    //stick person
    setcolor(CYAN);
    circle(50,200,30);
    setfillstyle(1,3);
    floodfill(50,200,3);

    setcolor(CYAN);
    line(50,230,50,300);//badan
    line(20,250,80,250);//tangan
    line(50,300,90,340);//kaki kanan
    line(50,300,10,340);//kaki kiri




    //stand
    setcolor(3);
    line(0,340,110,340);//2
    line(110,340,110,450);//3
    line(0,450,110,450);//4
    line(0,340,0,450);//1
    setfillstyle(1,3);
    floodfill(100,440,3);


    line(0,450,640,450);//platform

    //box
    line(400,380,400,450);//1
    line(400,450,500,450);//2
    line(500,380,500,450);//3
}

This works somewhat... I do NOT like the way it looks. :<

Thanks...for the code...but can you explain a little bit more about the code that you have used to make the ball move...i want to understand more about the code because i still new in this language....

Well, what I ended up doing is making a function to draw the whole setting (drawSetting) - the stick person, the box, the platform, etc etc.

The cleardevice() function clears the whole screen (no setting, no circle). Then the setting is redrawn, and the circle is redrawn in the new position (pos_x, pos_y)

Basically this is the only way I could figure out how to do it. It would look a lot smoother if we could erase or delete the circle we've drawn, but I do not know a way to do that other than clearing the whole canvas.

Is it clearer now?

ok...tq...that's really helpful....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.