I
I
Ilya2018-12-26 18:11:25
C++ / C#
Ilya, 2018-12-26 18:11:25

How to end the loop so that the game ends with graphics.h?

Guys, I honestly tried to find the tag code, but apparently I'm an idiot. This question is a simple game where balls fall from above, on the hero, if it hits it should end, but this does not happen, why I have no idea (

do {

    setactivepage(o);
    cleardevice();
    int am = 40;
    int k = 0, xm = 0, ym = 0;
    int maxx = getmaxx();
    int maxy = getmaxy();
    line(0, 500, maxx, 500);
    keyb_init();
    keyb_redo();
    setcolor(Red);

    for (int i = 0; i < 30; i++)
    {
      if ((met[i][2] == 0) && ((float)rand() / RAND_MAX < 0.01)) { // Цикл задания координат, и вероятности появления шариков
        met[i][2] = 1;
        met[i][0] = rand() % 1000;
        met[i][1] = 0;
      }
    }
    for (int i = 0; i < 30; i++) // цикл отрисовки шариков
    {
      if (met[i][2] == 1) {
        met[i][1] += 3;
        circle(met[i][0], met[i][1], 15);
      }

    }
    for (int i = 0; i < 30; i++)
    {
      if (met[i][1] > 800) 
      { 
        met[i][1] = 0; 
        met[i][2] = 0; 

      
      }
      
    }
      
      
    // движение героя
      
    if (keyb_isPressed(VK_UP) || p == 3) {
      setcolor(Black);
      line(0, 500, maxx, 500);
      if (x == 1200)
        x = -100;
      else if (x == -100)
        x = 1200;
      
      if (keyb_isPressed(VK_UP)&&(y!=0)) { p = 3;  y = y - dy; }
      else y = y + dy;
      if (y == 800)
        live--;
      if (y == 500 && dy < 0) { dy = -dy; p = 0; }
      if (keyb_isPressed(VK_RIGHT))
      {
        x = x + 5;
        for (int j = 0; j < 52; j++)
          for (int i = 0; i < 68; i++)
            if (A.now[3][i][j] != 11)
              putpixel(x + j, y + 100 - i, A.now[3][i][j]);

      }
      else if (keyb_isPressed(VK_LEFT)) {
        x = x - 5;
        for (int j = 0; j < 52; j++)
          for (int i = 0; i < 68; i++)
            if (A.now[3][i][j] != 11)
              putpixel(x - j, y + 100 - i, A.now[3][i][j]);
      }
      else {
        for (int j = 0; j < 52; j++) for (int i = 0; i < 68; i++)
          if (A.now[3][i][j] != 11)putpixel(x + j, y + 100 - i, A.now[3][i][j]);
      }
    }
    else
      if (keyb_isPressed(VK_RIGHT))
      {
        x = x + 20; p++;
        for (int j = 0; j < 52; j++)
          for (int i = 0; i < 68; i++)
            if (A.now[p][i][j] != 11)
              putpixel(x + j, y + 100 - i, A.now[p][i][j]);
        if (p == 2) p = 0;
        if (x == 1200)
          x = -100;
      }
      else if (keyb_isPressed(VK_LEFT)) {
        x = x - 20; p++;
        for (int j = 0; j < 52; j++)
          for (int i = 0; i < 68; i++)
            if (A.now[p][i][j] != 11)
              putpixel(x - j, y + 100 - i, A.now[p][i][j]);
        if (p == 2) p = 0;
        if (x == -50)
          x = 1200;
      }
      else {
        p = 0;  for (int j = 0; j < 52; j++) for (int i = 0; i < 68; i++)
          if (A.now[p][i][j] != 11)putpixel(x + j, y + 100 - i, A.now[p][i][j]);
      }
    if (keyb_isPressed(VK_UP))
    {
      y = y - 5; p = 3;

    }

        counter(k);


    //

    for (int i = 0; i < maxx / 3; i++)
    {

      {

        x1 = rand() % 1199;
        y2 = rand() % 800;
        c = rand() % 15;
        if (getpixel(x1, y2) != 0)
          putpixel(x1, y2, 0);
        else
          putpixel(x1, y2, Color(c));
        if (getpixel(x1, y2 != 0))
        {
          putpixel(x1, y2, 0);
        }
      }
    }

    for ( int i = 0; i < 30; i++)
    {
      if((x+68==met[i][0])&&(y+52==met[i][1])) // проверка столковения
        live = 0; 

    }
    
    ///////////////////////
    setvisualpage(o);
    o++;
    if (o == 2) o = 0;
    
  } while (!keyb_isPressed(VK_ESCAPE)||(live==0));
  
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dedalqq, 2018-12-27
@dedalqq

I can assume that you are looking for a "pixel to pixel" collision, try expanding the collision zone by replacing "a == x" with "a > xd && a < x + d" in your condition

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question