我是c++新手,所以我知道的不多。这是一个获取用户输入并改变玩家位置变量(简称pp)的程序。.
然后它会根据玩家位置变量改变屏幕并打印出来。
到目前为止:
"
#include <iostream>
using namespace std;
// main
int main() {
// variables
string scr = ". ";
string grd = "0 ";
string plr = "1 ";
int pp = 0;
string p11 = scr;
string p12 = scr;
string p13 = scr;
string p14 = scr;
string p15 = scr;
string p21 = scr;
string p22 = scr;
string p23 = scr;
string p24 = scr;
string p25 = scr;
string p31 = scr;
string p32 = scr;
string p33 = scr;
string p34 = scr;
string p35 = scr;
string p41 = scr;
string p42 = scr;
string p43 = scr;
string p44 = scr;
string p45 = scr;
string p51 = scr;
string p52 = scr;
string p53 = scr;
string p54 = scr;
string p55 = scr;
while(true){
// "clearing" the console
cout << "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnn" << endl;
// printing screen variables to the console
cout << p11 << p12 << p13 << p14 << p15 << endl;
cout << p21 << p22 << p23 << p24 << p25 << endl;
cout << p31 << p32 << p33 << p34 << p35 << endl;
cout << p41 << p42 << p43 << p44 << p45 << endl;
cout << p51 << p52 << p53 << p54 << p55 << endl;
// check for user input
string input;
cin >> input;
if(input == "a"){
pp = pp - 1;
}
if(input == "d"){
pp = pp + 1;
}
// checking if the number is too large
if(pp < -2){
pp == -2;
}
if(pp > 2){
pp == 2;
}
// moving the player
if(pp == -2){
p51 == plr;
}else{
p51 == scr;
}
if(pp == -1){
p52 == plr;
}else{
p52 == scr;
}
if(pp == 0){
p53 == plr;
}else{
p53 == scr;
}
if(pp == 1){
p54 == plr;
}else{
p54 == scr;
}
if(pp == 2){
p55 == plr;
}else{
p55 == scr;
}
}
return 0;
}
"然而当我输入所需的字符串时,屏幕似乎没有改变。我对c++了解不多,我只是把我的python游戏转换成c++。
也许我真的很笨,但如果有人知道怎么做,那将是一个很大的帮助。
在你的第三个if语句周围,你有相等运算符而不是赋值!
// checking if the number is too large
if(pp < -2){
pp == -2; // <-- HERE: This expression returns a Boolean!
// Doesn't do anything to 'pp'
}
应该是:
// checking if the number is too large
if(pp < -2){
pp = -2;
}