主机游戏不能正常工作
Console game not working properly
我主要是为了学习而制作一款简单的游戏,最近我遇到了这个问题。记住,我仍然是一个巨大的初学者。当我从菜单进入游戏并在"命令行"中编写任何内容时,我会立即挨饿和脱水。我已经好几天不能上网了,我把整个程序都看了一遍,但没有发现任何问题。
menu.h
#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <dos.h>
#include <windows.h>
#include <WinBase.h>
//-------------//
#include "tutorial.h"
#include "game.h"
void menu() {
std::cout << "-------MENU------- n";
std::cout << " 1.Play n";
std::cout << " 2.Tutorial n";
std::cout << " 3.Exit n";
std::cout << " n";
std::cout << " n";
std::cout << " n";
std::cout << "Choose Option: ";
int menuOption;
std::cin >> menuOption;
int menuLoop = 0;
while (menuLoop != 1) {
if (menuOption == 1) {
menuLoop = 1;
play();
}
if (menuOption == 2) {
menuLoop = 1;
system("CLS");
tutorial();
}
if (menuOption == 3) {
menuLoop = 1;
std::cout << "Bye!";
Sleep(1000);
}
if (menuOption > 3)
std::cout << """ << menuOption << """ << " is not a valid option.n";
}
}
game.h
#include <iostream>
#include <string>
#include <windows.h>
#include <WinBase.h>
//initiating functions
void step();
void run();
void theme();
void starve();
void die();
void dehydrate();
void b();
//globals
std::string name;
std::string commandLine;
int onRoad = 1; // 1 = True, 0 = False
int steps = 0;
double hunger = 0.0;
double thirst = 0.0;
int energy = 5;
void play() {
system("CLS");
std::cout << "Enter your name: n";
std::cin >> name;
system("CLS");
theme();
Sleep(350);
std::cout << " " << name << "'s Roadtripn";
std::cout << "Type "/help" for helpn";
std::cout << "---------Command Line---------n";
std::cin >> commandLine;
while (onRoad != 0){
//------------------Conditions start------------------
// Hunger Conditions
if (hunger = 0){
if (hunger < 0){
std::cout << "You can't eat that, you're not hungry.n";
b();
}
}
if (hunger > 100){
hunger = 100;
}
if (hunger < 0){
hunger = 0;
}
if (hunger = 100){
starve();
}
else if (hunger > 96){
std::cout << "You're extremely hungry! If you don't eat something quick you're going to die!n";
b();
}
else if (hunger > 90) {
std::cout << "You're very hungry.n";
b();
}
else if (hunger > 80) {
std::cout << "You're hungry.n";
b();
}
// Thirst Conditions
if (thirst = 0){
if (thirst < 0){
std::cout << "You can't drink that, you're not thirsty.n";
}
}
if (thirst < 0){
thirst = 0;
}
if (thirst > 100) {
thirst = 100;
}
if (thirst = 100){
dehydrate();
}
else if (thirst > 90){
std::cout << "You're extremely thirsty! If you don't drink something quick you're going to die!n";
b();
}
else if (thirst > 75) {
std::cout << "You're very thirsty.n";
b();
}
else if (thirst > 50){
std::cout << "You're thirsty.n";
b();
}
//Energy Conditions
if (energy > 10){
energy = 10;
}
if (energy < 0){
energy = 0;
}
//-------------------Conditions end-------------------
if (commandLine == "/commands"){
std::cout << "-Command- -Action-n";
std::cout << " /help Displays this menu.n";
std::cout << " /commands Displays list of commands.n";
std::cout << " /step Take a step and display total amount of steps.n";
std::cout << " /run Take 5 steps and consume 5 energy.n";
std::cout << " Doesn't increase hunger or thirst.n";
std::cout << " /inventory Displays inventory.n";
std::cout << " /info Displays stats.n";
b();
}
if (commandLine == "/step") {
step();
b();
}
if (commandLine == "/info") {
std::cout << name << "'s statsn";
std::cout << "Hunger: " << hunger << std::endl;
std::cout << "Thirst: " << thirst << std::endl;
std::cout << "Energy: " << energy << std::endl;
b();
}
else {
std::cout << commandLine << " is not a valid command. Type /commands to display commands.n";
b();
}
}
}
void step(){
steps += 1;
std::cout << steps;
hunger += 5;
thirst += 5;
}
void run() {
steps += 5;
std::cout << steps;
}
void starve(){
std::cout << "You starved to death!n";
die();
}
void dehydrate(){
std::cout << "You dehydrated!n";
die();
}
void die(){
std::cout << "Steps taken: " << steps << std::endl;
onRoad = 0;
}
void theme(){
Beep(600, 200);
Beep(500, 200);
Beep(800, 400);
}
// b takes you back to the command line
void b(){
std::cin >> commandLine;
}
main.cpp
#include <iostream>
#include "menu.h"
#include <WinBase.h>
#include <windows.h>
int main(){
menu();
system("PAUSE");
return 0;
}
**编辑:** Pic: https://i.stack.imgur.com/UrZee.png(需要10个代表发布图片)这真的很奇怪。我输入/step,它工作,然后我输入/run,它也工作。我不明白……
你的一些if语句使用赋值而不是比较
if (hunger = 100){
starve();
}
您可能需要将=改为==
在编译时启用警告,如果您还没有这样做。
因为
// b takes you back to the command line
void b(){
std::cin >> commandLine;
}
b不会将您带回到命令行,只是等待读取一个字符,然后返回。如果你想回去,你应该沿着你来的路走。例如,退出播放将返回到菜单循环,显然,使用menuLoop = 1,它将退出整个程序,但通过修改,这不是一个糟糕的循环系统。
编辑:我知道你说的"命令行"是什么意思了。就像别人说的,你有很多条件不小心拼成了赋值。
实际上,b()
函数正在吞食后续命令。
也许你应该
- 使用
std::getline()
一次一行读取命令 -
或使用
std::cin.ignore()
在b()
中实际消耗,直到行结束
p。由于使用全局变量,我很难验证游戏循环逻辑。我只知道/step
之后的/step
现在被忽略了,没有效果。将您的输入与循环控件分开,并尝试删除全局变量。
INFO而不是每次都写std::cout
你可以只写using namespace std;
在开头之后你不需要写std::cout
只写cout << "" ;
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程