[英]How can I update a bool in a .h file?
好的,所以我正在為一個課堂項目制作游戲,並且我有兩個文件: game.cpp
和game.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.