S
S
SuperDuperPuper2020-08-14 00:55:14
C++ / C#
SuperDuperPuper, 2020-08-14 00:55:14

How to increase the frame rate when outputting strings to the console?

THE CODE
#include <time.h>
#include <conio.h>
#include <string>
#include <iostream>
#include <windows.h>
using namespace std;

class snake {
private:
  char got_key;
  int directX = 1;
  int directY = 0;
public:
  int x;
  int y;
  int coords_body[500][2];
  int snake_len = 2;
  char head;
  int speed;
  snake(int x, int y, char head, int speed) { set_values(x, y, head, speed); }

  void set_values(int X, int Y, char HEAD, int SPEED) {
    head = HEAD;
    speed = SPEED;
    x = X;
    y = Y;
    coords_body[0][0] = x;
    coords_body[0][1] = y;
    coords_body[1][0] = x;
    coords_body[1][1] = y + 1;

  }

  void move(int X, int Y) {
    int preX = coords_body[0][0];
    int preY = coords_body[0][1];
    coords_body[0][0] = x;
    coords_body[0][1] = y;
    for (int i = 1; i <= snake_len; i++) {
      X = coords_body[i][0];
      Y = coords_body[i][1];
      coords_body[i][0] = preX;
      coords_body[i][1] = preY;
      preX = X;
      preY = Y;

    }
    coords_body[snake_len + 1][0] = preX;
    coords_body[snake_len + 1][1] = preY;
  }

  void append() {
    snake_len += 1;
  }
  void die() {
    head = '~';
    directX = 0;
    directY = 0;
  }
  int check_collission(char array[29][120]) {
    char a = array[coords_body[0][1]][coords_body[0][0]];
    if (a == '#') {
      die();
      return 0;
    }
    else if (a == '$') {
      append();
      return 1;
    }
  }
  void get_key() {
    if (head != '~'){
      char key;
      if (_kbhit() != 0) {
        key = _getch();
        switch (key) {
        case 'w':
          directX = 0;
          directY = -1;
          break;
        case 's':
          directX = 0;
          directY = 1;
          break;
        case 'd':
          directX = 1;
          directY = 0;
          break;
        case 'a':
          directX = -1;
          directY = 0;
          break;
        }
      }
      x = x + directX * speed;
      y = y + directY * speed;
      move(x, y);
    }
    
  }


};
void spawn_raspberries(int raspberries[5][2]){
  int x;
  int y;
  for (int i = 0; i < 5;i++) {
    x = rand() % 120;
    y = rand()%29 ;
    raspberries[i][0] = x;
    raspberries[i][1] = y;

    
  }
}
void draw_raspberries(int raspberries[5][2],char array[29][120]) {
  int x;
  int y;
  for (int i = 0; i < 5; i++) {
    x = raspberries[i][0];
    y = raspberries[i][1];
    array[y][x] = '$';
  }
}

void delete_raspberry(int x,int y,int raspberries[5][2]){
  for (int i = 0; i < 5; i++) {
    if (raspberries[i][0]==x && raspberries[i][1] == y){
      raspberries[i][0] = -1;
      raspberries[i][1] = -1;
    }
  }
}

void delay(int ms) {
  int EndTime = clock() + ms;
  while (clock() < EndTime) {}

}
void draw_ground(char array[29][120]) {
  string output = "";
  for (int y = 0; y < 29; y++) {
    for (int x = 0; x < 120; x++) {

      output += array[y][x];
    }
    cout<<output << endl;
    output = "";

  }
}
  void create_ground(char array[29][120]) {
    for (int y = 0; y < 29; y++) {
      array[y][0] = '#';
      array[y][119] = '#';
      }
    for (int x = 0; x < 120; x++) {
      array[0][x] = '#';
      array[28][x] = '#';
    }
    for (int y = 1; y < 28; y++) {
      for (int x = 1; x < 119; x++) {
        array[y][x] = '.';
      }
    }
  }
  void append_snake_to_array(char array[29][120], int coords_body[500][2], int snake_len, char snake_head) {
    char body = ' ';
    int x = coords_body[0][0];
    int y = coords_body[0][1];
    array[y][x] = snake_head;
    if (snake_head != '~') {
      body = '#';
    }
    else {
      body = '~';
    }
    for (int i = 1; i < snake_len; i++) {
      x = coords_body[i][0];
      y = coords_body[i][1];
      array[y][x] = body;
    }
  }

  
  void game() {
    int fps = 0;
    int count = 0;
    int begin = clock() + 1000;

    bool game = true;
    char array[29][120];

    int raspberries[5][2];
    spawn_raspberries(raspberries);
    int raspberries_ammount = 5;

    snake snake_1(10, 10, '@', 1);
    while (game == true) {
      create_ground(array);
      draw_raspberries(raspberries,array);
      append_snake_to_array(array, snake_1.coords_body, snake_1.snake_len, snake_1.head);
      snake_1.get_key();
      draw_ground(array);

      if (snake_1.check_collission(array) == 1) {
        delete_raspberry(snake_1.coords_body[0][0], snake_1.coords_body[0][1],raspberries);
        raspberries_ammount -= 1; 
      }
      if (raspberries_ammount == 0) {
        raspberries_ammount = 5;
        spawn_raspberries(raspberries);
      }

      cout << "fps:" << fps;
      count += 1;
      if (clock() >begin ) {
        begin+= 1000;
        fps = count;
        count = 0;
      }


      system("cls");
      }
  
    }

int main()
{	
  game();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-08-14
@SuperDuperPuper

How to increase the frame rate when outputting strings to the console?

The answer to this question is: don't redraw the whole thing every frame.
The answer to the question "how to understand what the program execution time is spent on": with the help of profiling.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question