I've been following a few tutorials for making a somewhat Snake Game , just not a full-tailed-one , It's going alright and i think i got it almost right but I've got two problems
#include <bits/stdc++.h> #include <conio.h> #include <unistd.h> using namespace std; int side,x,y; int fruitx,fruity,score,flag; bool isOver; void randomize(); void frame(int); void userinp(); void game(); int main(){ do{ cout << "Enter the side of your frame!(not less than 20)\n"; cin >> side; }while(side < 20); randomize(); while(!isOver){ frame(side); userinp(); game(); } return 0; } void randomize(){ x = side/2; y = side/2; label1: fruitx = rand()%side; if (fruitx == 0 || fruitx == side) goto label1; label2: fruity = rand()%side; if (fruity == 0 || fruity == side) goto label2; score = 0; } void frame(int s){ system("cls"); for(int i=1;i<=s;i++){ for(int j=1;j<=s;j++){ if(i == 1 || i == s || j == 1 || j == s) cout << "*"; else{ if(i == x && j == y){ cout << "-"; } else if(i == fruitx && j == fruity){ cout << "x"; } else cout << " "; } } cout << "\n"; } cout << "Your score: " << score << endl; } void userinp(){ if(kbhit()){ switch (getch()){ case 'a': flag = 1; break; case 's': flag = 2; break; case 'd': flag = 3; break; case 'w': flag = 4; break; default: isOver = true; } } } void game(){ sleep(0.899); switch (flag){ case 1: y--; break; case 2: x++; break; case 3: y++; break; case 4: x--; break; } if (x < 0 || x > side || y < 0 || y > side){ x = side/2; y = side/2; } if (x == fruitx && y == fruity) { label3: fruitx = rand()%(side); if (fruitx == 0) goto label3; label4: fruity = rand()%(side); if (fruity == 0) goto label4; score++; } } the first problem:
using system"(cls") helped with the screen not going down way many times to avoid the screen scrolling down i tried it to make the screen clear every time the sleep() is called but i cannot get how to slow down the process a bit using the sleep() function , It's either too slow or too fast The screen , Is there a different way to approach the whole thing or is the function itself not proper to use here ?
second problem: whenever i input the side with 20 , i cannot find the fruit itself at all but when it is any number more than this it does show me the random fruits just fine so is there a way to fix why at just side = 20 it would not work ?
PS: If there is anything that could be improved aside from these 2 problems i'd appreciate it .. Thanks in advance!
sleep(0.899);looks like sleep is in seconds and is an integer value: https://man7.org/linux/man-pages/man3/sleep.3.html I think you want: https://man7.org/linux/man-pages/man3/usleep.3.html but then there is this question about using that on MS Windows: https://stackoverflow.com/questions/5801813/c-usleep-is-obsolete-workarounds-for-windows-mingw/17283549