我一直在设计我的c ++控制台游戏,我对游戏循环有疑问。每当游戏循环时,我最终会在构造函数中重新初始化我的速度变量,这会将它们设置为" 0"。我有几个关于如何解决这个问题的想法,但它最终破坏了我的游戏基础设施,并在一段时间后让人感到困惑。 有人可以告诉我如何防止我的速度变量重新初始化。 顺便说一下,我将在稍后了解如何更好地对标题进行分组。
FPS.timerMilli(1)
是一个在退出函数前计算1毫秒的函数。 (试图控制FPS)。
rectangle.cpp:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include "rectangle.h"
void rectangle::rectangleDrawPos(int h, int k) {
// Draw top of rectangle
for (int verticle = 1; verticle <= h; verticle += 1) {
std::cout << "\n";
}
for (int horizontal = 1; horizontal <= h; horizontal += 1) {
std::cout << " ";
}
for (int x = 1; x <= width; x += 1) {
std::cout << ".";
}
std::cout << "\n";
//Number of sides
midWidth = width - 2;
for (int y = 1; y <= height; y+=1) {
//Draw sides
for (int x = 1; x <= h; x += 1) {
std::cout << " ";
}
std::cout << ":";
for (int x = 1; x <= midWidth; x+=1) {
std::cout << " ";
}
std::cout << ":\n";
}
//Bottom of rectangle
for (int x = 1; x <= h; x += 1) {
std::cout << " ";
}
for (int x = 1; x <= width; x += 1) {
std::cout << ".";
}
std::cout << "\n";
}
//constructor
rectangle::rectangle(int locHeight, int locWidth) {
width = locWidth;
height = locHeight;
}
rectangle.h:
#ifndef RECTANGLE_H
#define RECTANGLE_H
class rectangle {
//Variables
int height, width, midWidth;
public:
//functions
void rectangleDrawPos(int h, int k);
//constructor
rectangle(int locHeight, int locWidth);
};
#endif
MyGame.cpp:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <ctime>
#include "timer.h"
#include "config.h"
#include "IOMovement.h"
#include "rectangle.h"
int main(){
//object def
timer FPS;
config startupConfig;
IOMovement IO;
//config
startupConfig.Set_Consol_Size(startupConfig.half_screen_Size, GetSystemMetrics(SM_CYSCREEN));
startupConfig.Position_Consol(-6, 0);
//Game Loop
while (1==1) {
FPS.timerMilli(1);
//Game Startup
IO.IOStartup();
//map
//ai
}
//exit
return 0;
}
IOMovement.cpp:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include "IOMovement.h"
#include "rectangle.h"
#define W_KEY 0x57
#define S_KEY 0x53
#define A_KEY 0x41
#define D_KEY 0x44
#define R_KEY 0x52
void IOMovement::IO() {
rectangle player(15, 5);
if (GetAsyncKeyState(W_KEY)) {
system("CLS");
velocityVerticle--;
player.rectangleDrawPos(velocityHorizontal, velocityVerticle);
}
if (GetAsyncKeyState(S_KEY)) {
system("CLS");
velocityVerticle++;
std::cout << "Working\n";
player.rectangleDrawPos(velocityHorizontal, velocityVerticle);
}
if (GetAsyncKeyState(A_KEY)) {
system("CLS");
velocityHorizontal--;
player.rectangleDrawPos(velocityHorizontal, velocityVerticle);
}
if (GetAsyncKeyState(D_KEY)) {
system("CLS");
velocityHorizontal++;
player.rectangleDrawPos(velocityHorizontal, velocityVerticle);
}
}
void IOMovement::IOStartup() {
//Variable decleration
velocityVerticle = 0;
velocityHorizontal = 0;
//Functions
IO();
}
IOMovement.h:
#ifndef IOMOVEMENT_H
#define IOMOVEMENT_H
class IOMovement {
int velocityVerticle, velocityHorizontal;
bool variableInitialization;
void IO();
public:
void IOStartup();
};
#endif
答案 0 :(得分:0)
如果你需要你的变量保留它的值,你可以把它们变成静态变量。