簡體   English   中英

如何更新.h文件中的布爾值?

[英]How can I update a bool in a .h file?

好的,所以我正在為一個課堂項目制作游戲,並且我有兩個文件: game.cppgame.h 我聲明了所有私有變量和公共函數,目前正在.cpp文件中定義它們。

在菜單功能中,我有以下開關:

switch (option) {
case 1:

    while (this->playing) {
        //clean screen
        h.screen();

        //deletescroll bar
        h.nosc();

        //draw canvas
        h.draw();


    } 
}

這是我的draw函數:

void Game::draw() {
    HWND window= GetConsoleWindow();

    MoveWindow(window, 400, 100, 750, 550, TRUE);
    Game h;



    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    this->columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    this->rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;

    h.nosc();

    CONSOLE_SCREEN_BUFFER_INFO csbi;

    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    this->columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    this->rows= csbi.srWindow.Bottom - csbi.srWindow.Top + 1;*/

    gotoxy(15, 0);
    cout << "Lives: " << lives;

    gotoxy(45, 0);
    cout << "Level: " << this->level;

    gotoxy(75, 0);
    cout << "Score: " << this->score;

    //up - down
    for (int i = 2; i < this->columns - 1; i++) {

        gotoxy(i, 1);
        printf("%c", 205);
        //down
        gotoxy(i, this->rows - 1);
        printf("%c", 205);
    }

    //left - right
    for (int j = 2; j < this->rows - 1; j++) {
        gotoxy(2, j);
        printf("%c", 186);
        gotoxy(this->columns - 1, j);
        printf("%c", 186);
    }


    gotoxy(2, 1); 
    printf("%c", 201);
    gotoxy(2, this->rows - 1); 
    printf("%c", 200);
    gotoxy(this->columns - 1, 1);
    printf("%c", 187);
    gotoxy(columns - 1, rows - 1);
    printf("%c", 188);

    this->playing = false;
}

它應該返回到開關,並且由於現在playing錯誤,因此不應再次運行,對嗎? 但是它仍在繼續,我不確定該怎么辦。 這是我的標頭(.h)文件:

#pragma once

#include<Windows.h>
#include <iostream>
#include <conio.h>

using namespace std;

class Game {

private:
    int xAxis= 25;
    int yAxis = 25;
    int direction = 0;
    int columns = 0;
    int rows = 0;
    int lives = 3;
    int level = 1;
    int score = 0;
    int scoreArr[9];    
    bool playing;

private:

我知道這段代碼看起來有些混亂,但我仍在學習中。 非常感謝您的幫助。

目前尚不清楚“ h”對象到底是什么,但是this->playing = false; h.draw內部將訪問菜單函數中的while (this->playing)進行playing的其他變量或實例。 如果您需要的是在完成h.draw之后立即結束循環,為什么還要使用while循環呢?

假設您有一個名為h的對象,則可以像下面這樣修改您的代碼。 您不應該在此檢查播放變量。

while (h.playing) {
    //clean screen
    h.screen();

    //deletescroll bar
    h.nosc();

    //draw canvas
    h.draw();
} 

PS:如果您根據“ 最小”,“完整”和“可驗證”來更新您的問題,我也會更新我的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM