0

我正在检查以查看并理解其他人的代码...该代码在他的计算机上运行,​​但是当我尝试在我的计算机中对其进行编辑时,它似乎不起作用。lib文件都已正确连接...

#include "play.h"
#include "Lib110ct/Lib110ct.h"
#include <ctime>


int main(int argc, char** argv)
{
Win110ct win;
Turtle * t = win.getTurtle();
win.hideTurtle();
const int NumOfBook = 7;
Player You(512,384);
Book B[NumOfBook];
int pts = 0;
char in;
time_t ti;
while (in != 'x')
{
    for (int i = 0; i<NumOfBook; i++)
    {
        if (A[i].isReadBy(You))
        {
            B[i].setPosition(rand() % 1024 + 1,rand() % 768 + 1);
            pts++;
        }
        B[i].move();
        B[i].draw(t);
    }
    You.draw(t);
    win << "Points: " << pts << "\n";
    win.render();
    win.setPosition(-1,-1);
    in = win.getchar();
    win.clearBack();
    win.clear();
    win.setPosition(0,0);
    if (in == 'w')
        You.moveRelative(0,-50);
    else if (in == 's')
        You.moveRelative(0,50);
    else if (in == 'a')
        You.moveRelative(-50,0);
    else if (in == 'd')
        You.moveRelative(50,0);
}
return 0;
}

但是我遇到了错误......“书”没有在这个范围内声明。

头文件play.h文件的代码如下..

#include "Lib110ct/Lib110ct.h"
#include <cstdlib>

//Player class, used to create the controllable character.
class Player
{
protected:
double xpos, ypos;
public:
Player(){};
//constructor, user input variables stored as xpos and ypos, then player is drawn
Player(double x, double y)
{
    xpos = x;
    ypos = y;
}

//draws the new position of the turtle
void draw(Turtle* t)
{
    t->setPosition(xpos,ypos);
    t->penDown();
    for (int i = 0; i < 180; i++ )
    {
        t->turn(2);
        t->moveForward(1);
    }
}


double getX(){ return xpos; } //returns the x position of the player
double getY(){ return ypos; } //returns the y position of the player

//moves the player by x increment and y increment (leave x or y as 0 to not move along the respective axis)
//relative to current position
void moveRelative(double xadd, double yadd)
{
    if (xpos+xadd > 0 && xpos+xadd < 1024 && ypos+yadd > 0 && ypos+yadd < 768)
    {
        xpos += xadd;
        ypos += yadd;
    }
}
};

//Food class. Not controlled by player
class Food
{
protected:
double xpos, ypos, xdir, ydir;
public:
//constructor, random x and y co-ordinates stored as xpos and ypos. food is drawn in       those co-ordinates
//random xdir and ydir, food initially moves in this direction
Food()
{
    xpos = rand() % 1024 + 1;
    ypos = rand() % 768 + 1;
    xdir = rand() % 41 - 20;
    ydir = rand() % 41 - 20;
}
//draws the food
void draw(Turtle* t)
{
    t->setPosition(xpos,ypos);
    t->penDown();
    for (int i = 0; i < 4; i++ )
    {
        t->turn(90);
        t->moveForward(20);
    }
}
double getX(){ return xpos; } //returns the x position of the player
double getY(){ return ypos; } //returns the y position of the player

//moves the food to a specific location, not relative to current position
void setPosition(double x,double y)
{
    xpos = x;
    ypos = y;
}

//moves the food in the specified directions, given by the variables xdir and ydir
void move()
{
    if (!(xpos+xdir>0 && xpos+xdir<1024 && ypos+ydir>0 && ypos+ydir<768))
    {
        xdir = -xdir;
        ydir = -ydir;
    }
    xpos += xdir;
    ypos += ydir;
}

//returns TRUE if the player is in a close proximity to the food, otherwise false
bool isEatenBy(Player Play)
{
    double pX = Play.getX();
    double pY = Play.getY();
    double fX = getX();
    double fY = getY();
    return pX+60>fX && pX-20<fX & pY+40>fY && pY-40<fY;
}
};
4

1 回答 1

1

您正在尝试使用“Book”类型的数组,这可能是您打算定义的类:

Book B[NumOfBook];

但是,您尚未在上面发布的代码或您放入 Dropbox 的代码中定义这一点。

要么将 Book 类的定义添加到 play.h,要么创建一个新的头文件,可能称为 book.h,其中包含定义,然后在你的 cpp 文件中#include "book.h"。您的 Book 类将需要设置位置、移动和绘制的方法。

于 2013-05-03T01:25:42.760 回答