简体   繁体   English

贪吃蛇游戏太快控制台 c++

[英]Snake Game going too fast console c++

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?使用system"(cls")帮助屏幕多次不向下滚动以避免屏幕向下滚动我尝试在每次调用sleep()时使屏幕清晰但我不知道如何减慢进程位使用sleep() function,屏幕太慢或太快,是否有不同的方法来处理整个事情,或者 function 本身不适合在这里使用?

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?第二个问题:每当我输入带有 20 的边时,我根本找不到水果本身,但是当它比这更多时,它确实向我显示随机水果就好了,所以有没有办法解决为什么只是side = 20不行吗?

PS: If there is anything that could be improved aside from these 2 problems i'd appreciate it.. Thanks in advance! PS:如果除了这两个问题之外还有什么可以改进的,我会很感激..提前谢谢!

First sleep is a bit rough.第一次睡觉有点难。 See answers here for better solutions: Sleep for milliseconds在此处查看答案以获得更好的解决方案: 睡眠毫秒

Second you don't account for your own overhead.其次,您不考虑自己的开销。 Your program might run faster/slower on different setups.您的程序可能在不同的设置上运行得更快/更慢。 For games in general you want to use a somewhat precise but fast time function to get an idea of how much to move everything (granted not really needed for snake).对于一般游戏,您希望使用稍微精确但快速的时间 function 来了解移动所有东西的量(当然蛇并不真正需要)。 For more suggestions on this topic see this quesiton: How to Make a Basic FPS Counter?有关此主题的更多建议,请参阅此问题: 如何制作基本 FPS 计数器?

Last the console is not really good for this sort of application.最后,控制台对于这类应用程序并不是很好。 I would suggest switching to a simple GUI, just so you don't have to deal with the console craziness.我建议切换到一个简单的 GUI,这样您就不必处理疯狂的控制台。 If your goal is to have some sort of demo to learn something and move on it might be ok, and live with the strange behavior.如果您的目标是进行某种演示来学习某些东西并继续前进,那可能没问题,并且可以忍受这种奇怪的行为。

For the issue with the fruit:对于水果的问题:

  • NEVER use goto.永远不要使用 goto。 It's very hard to reason about it and for the standard tools to deal with it.很难推理它以及处理它的标准工具。 Use functions/loops that are easier reason about.使用更容易推理的函数/循环。 In your case a do{ friutx =...; } while (friuitx==0)在你的情况下do{ friutx =...; } while (friuitx==0) do{ friutx =...; } while (friuitx==0) would do. do{ friutx =...; } while (friuitx==0)会做。
  • The two places where you setup the fruit use different code.设置水果的两个地方使用不同的代码。 Likely even if you fix one, the other might still be broken.很可能即使您修复了一个,另一个可能仍然会损坏。 Create a function and call it from both places.创建一个 function 并从两个地方调用它。
  • % will do a reminder ( 0 to side-1 , inclusive). % 将做一个提醒( 0side-1 ,包括)。 Looks like your target is 2 to side-1, so maybe do rand()%(side-1) + 1 or better yet, use std::uniform_int_distribution .看起来你的目标是 2 到 side-1,所以也许做rand()%(side-1) + 1或更好,使用std::uniform_int_distribution
  • It's not clear to me why it's not seen correctly.我不清楚为什么没有正确看到它。 Use a debugger to check when you are rendering a frame what are the coordinates of the fruit.在渲染帧时使用调试器检查水果的坐标是什么。 They might overlap the edges and be replaced with a * .它们可能与边缘重叠并被替换为*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM