好,所以我正在为一个课堂项目做游戏,我有两个文件: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:
我知道这段代码对于某些人来说似乎是一团糟,但我仍在学习。非常感谢您的帮助。
答案 0 :(得分:0)
目前尚不清楚'h'对象到底是什么,但是public async Task RouteAsync(RouteContext context)
{
var requestPath = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
{
// Trim the leading slash
requestPath = requestPath.Substring(1);
}
// Get the page that matches.
var page = GetPageList()
.Where(x => x.VirtualPath.Equals(requestPath))
.FirstOrDefault();
// If we got back a null value set, that means the URI did not match
if (page == null)
{
return;
}
//Invoke MVC controller/action
var oldRouteData = context.RouteData;
var newRouteData = new RouteData(oldRouteData);
newRouteData.Routers.Add(this.target);
// TODO: You might want to use the page object (from the database) to
// get both the controller and action, and possibly even an area.
// Alternatively, you could create a route for each table and hard-code
// this information.
if (context.RouteData.Values["action"] == "fakeAction")
newRouteData.Values["controller"] = "realController";
newRouteData.Values["action"] = "realAction";
// This will be the primary key of the database row.
// It might be an integer or a GUID.
newRouteData.Values["id"] = page.Id;
try
{
context.RouteData = newRouteData;
await this.target.RouteAsync(context);
}
finally
{
// Restore the original values to prevent polluting the route data.
if (!context.IsHandled)
{
context.RouteData = oldRouteData;
}
}
}
app.UseMvc(routes =>
{
routes.Routes.Add(
new CustomRoute(routes.ServiceProvider.GetRequiredService<IMemoryCache>(),
routes.DefaultHandler));
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
中的行this->playing = false;
将访问h.draw
内的playing
的不同变量或不同实例菜单功能。如果您需要的是在完成while (this->playing)
之后立即结束循环,那为什么还需要while循环呢?
答案 1 :(得分:0)
假设您有一个名为h
的对象,则可以像下面这样修改代码。您不应该在此检查播放变量。
while (h.playing) {
//clean screen
h.screen();
//deletescroll bar
h.nosc();
//draw canvas
h.draw();
}
PS:如果您根据Minimal, Complete, and Verifiable更新问题,我也会更新我的答案。