Issues With Translating To C++

Hi there,
I'm currently writing code for a simple game for a project and have stumbled across some issues.
The game has an avatar, holding an object, moving across the screen that will drop said object a certain distance when the spacebar is pressed. The object is to get the object that is dropped into a bucket (made up of simply a rectangle) which will score user a point, and the game will end if user misses the bucket.

I already have the function to release the object when the spacebar is pressed, however my issues have come when trying to implement a graphical score counter to appear when the object enters the bucket.

Basically I want my code to read that when the x and y coordinate of the circle (object dropped) is between the x and y coordinates of the bucket (rectangle, another circle will appear somewhere else on the screen, acting as a score counter, and for when the circle lands outwith these coordinates, the game will end. Has anyone got any suggestions as to how I should translate this into C++??
Start by telling us what language + windowing/event framework you're currently using, and what windowing/event framework you wish to use in C++. If you haven't decided yet, I suggest using SFML, personally.
https://www.sfml-dev.org/
most graphical libraries provide a way to write text on the screen at a location.

you can also mix and match in windows gui games; for example you can draw in a rectangle and off to the side put the score in a gui text box.
I'm using C++ in Microsoft Visual Studio Community 2015 and using BGI library
If it helps this is the code I have so far:



void main(void)
{

int gd = DETECT, gm = 0; // Initialise graphic window
initgraph(&gd, &gm, "");
printf("Hello, world\n");

int x = 0;
int c = 32; // for input from keyboard that wil move the object being held by avatar when implemented into the
//kbhit function
int y = 0;

do {
clearviewport();

rectangle(250, 300, 350, 400); //draw goal


circle(x, 100, 20); //draw avatar
circle(x - 48, 100, 30);
setcolor(BROWN);


circle(x, y + 135, 10); //draw object


x += 2; //move avatar and object horizontally


if (_kbhit()) //object moves vertically when spacebar is pressed {
c = _getch();
if (c == 32);
{
y += 30;
}

printf("Key pressed: %d\n", c);

}
delay(20);
}
while (c != KEY_ESCAPE);


closegraph();
It's not clear what you want to do.
You could change your printf to cout.
Also your main function should return int, not be void.
To output text you could use outtext or outtextxy
What I’m trying to achieve is that when the circle that is dropped ends up within the coordinates of the rectangle, another circle will appear somewhere else on the screen, acting as a graphical score counter. However when the circle lands out with the rectangles coordinates, the game will end
Your task would be easier if you can use classes or at least structs.
Have you learned them ?
Also it will be easier if you create some functions for the normal game functions.
As far as I can see the circle doesn't move downwards so you need to fix this first.
Next step will be to check if the circle is inside the box.
Some pseudo code:
1
2
3
4
5
6
draw welcome screen
while game is running
   process the input
   update all game objects - i.e. move them, check for collision...
   draw the game or draw game over screen
end-while

Difficult to give more detailed advice without writing the game for you.
Last edited on
OK, obviously you writing the game for me would be very handy (and appreciated ;) ) I still want to do it myself. Would you be able to give me the code for something simpler which I could then implement into the game as it is this one part that has stumped me.
Say I have:
int x = 0;
int y = 0;

int a = 0;
int b = 0;

int c = 32;

do {

clearviewport();

//DRAW GRAPHICS
circle(x, y + 20, 20);
setcolor(GREEN);
line(a + 100, b, a + 100, b + 100);
setcolor(RED);


//MOVE CIRCLE WHEN SPACE BAR IS PRESSED
if (_kbhit())
{
c = _getch();
if (c == 32);
{
x += 20;
}
}

in my main. How could I then write the code to say that when a>x (when the x coordinate of the circle is greater than that of the line) that a circle should appear somewhere else. I have tried to do this however when I debug my solution, the circle which I would like to appear when a>x is already in my graphic.
How did you get mixed up with BGI? Google research is saying that this is a discontinued graphics library that won't even work on most compilers. So I can't get it to test any features...
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define ESC     27
#define SPACE   32

int main()
{
  int gd = DETECT, gm;
  int ballX = 20, ballY = 20, radius = 20, lineX = 100, lineTop = 10, 
      lineBottom = 100;

  initgraph(&gd, &gm, "");
  while(true)
  {
    if (kbhit())
    {
      clearviewport();
      int key = getch();
      switch (key)
      {
        case ESC: 
          break;
        case SPACE:
          ballX += 10;
          if (ballX + radius >= lineX) // ball touched line
          { 
            // do sth. 
            printf("Ball touched line");
          }
          break;
      }
    }
    circle(ballX, ballY, radius);
    line(lineX, lineTop, lineX, lineBottom);    
  }
  closegraph();
  getch();
  return 0;
}


@Manga,
I compile it with WinBGI: https://github.com/Duthomhas/WinBGIm-fixed-sort-of-
Topic archived. No new replies allowed.