2011-03-16 50 views
0

嗨我做了一个3D Buzz tutorial称为邪恶猴子的水平发电机。 我生成了一个级别,但我无法让它在屏幕上绘制。随机生成的级别没有显示。邪恶的猴子教程

我的代码:

Level.cpp

#include "Level.h" 
#include <stdlib.h> 
#include "Sprite.h" 
Level::Level(drawEngine *de, int w, int h) 
{ 
    drawArea = de; 

    width = w; 
    height = h; 

    gamer = 0; 

    //create memory for our level 
    level = new char *[width]; 

    for(int x = 0; x < width; x++) 
    { 
     level[x] = new char[height]; 
    } 

    //create the level 
    createLevel(); 

    drawArea->setMap(level); 
} 

Level::~Level() 
{ 
    for(x = 0; x < width; x++) 
     delete []level[x]; 

    delete [] level; 
} 

void Level::createLevel(void) 
{ 
    for(int x = 0; x < width; x++) 
    { 
     for(int y = 0; y < height; y++) 
     { 
      int random = rand() % 100; 

      if (y == 0 || y == height - 1 || x == 0 || x == width - 1) 
      { 
       level[x][y] = TILE_WALL; 
      } 
      else 
      { 
       if (random < 90 || (x < 3 && y < 3)) 
        level[x][y] = TILE_EMPTY; 
       else 
        level[x][y] = TILE_WALL; 
      } 
     } 
    } 
} 


void Level::draw(void) 
{ 
    // level to be drawn here 
    drawArea->drawBackground(); 
} 


Level.h 

    #ifndef LEVEL_H 
#define LEVEL_H 

enum 
{ 
    TILE_EMPTY, 
    TILE_WALL 
}; 
#include "drawEngine.h" 
class Character; 

class Level 
{ 
public: 
    Level(drawEngine *de, int width = 30, int height = 20); 
    ~Level(); 
    int x; 
    int y; 

    void addPlayer(Character *p); 
    void update(void); 
    void draw(void); 
    bool keyPress(char c); 

protected: 
    void createLevel(void); 

private: 
    int width; 
    int height; 
    char **level; 

    Character *gamer; 
    drawEngine *drawArea; 

}; 

#endif 

Game.cpp

#include "Game.h" 
#include <conio.h> 
#include <iostream> 
#include "drawEngine.h" 
#include "Character.h" 
#include <windows.h> 
using namespace std; 
//this will give ME 32 fps 
#define GAME_SPEED 25.33 
bool Runner::run() 
{ 
    level = new Level(&drawArea, 30, 20); 

    drawArea.createBackgroundTile(TILE_EMPTY, ' '); 
    drawArea.createBackgroundTile(TILE_WALL, '+'); 

    drawArea.createSprite(0, '$'); 
    gamer = new Character(&drawArea, 0); 

    level->draw(); 




    char key = ' '; 

    startTime = timeGetTime(); 

    frameCount = 0; 
    lastTime = 0; 

    posX = 0; 

    while (key != 'q') 
    { 
     while(!getInput(&key)) 
     { 
      timerUpdate(); 
     } 

     //gamer->keyPress(key); 
     //cout << "Here's what you pressed: " << key << endl; 
    } 

    delete gamer; 
    cout << frameCount/((timeGetTime() - startTime)/100) << " fps " << endl; 
    cout << "Game Over" << endl; 

    return true; 
} 

bool Runner::getInput(char *c) 
{ 
    if (kbhit()) 
    { 
     *c = getch(); 
     return true; 
    } 
} 

void Runner::timerUpdate() 
{ 
    double currentTime = timeGetTime() - lastTime; 

    if (currentTime < GAME_SPEED) 
     return; 


    frameCount++; 

    lastTime = timeGetTime(); 
} 

game.h

#ifndef GAME_H 
#define GAME_H 
#include "drawEngine.h" 
#include "Sprite.h" 
#include "Character.h" 
#include "level.h" 


class Runner 
{ 
public: 
    bool run(); 



    Runner(){}; 
protected: 
    bool getInput(char *c); 

    void timerUpdate(); 
private: 
    Level *level; 
    Character* gamer; 
    double frameCount; 
    double startTime; 
    double lastTime; 

    int posX; 



    drawEngine drawArea; 
}; 

#endif 

drawEngine.cpp

#include <iostream> 
#include "drawEngine.h" 
#include <windows.h> 




using namespace std; 
drawEngine::drawEngine(int index, int xSize, int ySize, int x, int y) 
{ 
    screenWidth = x; 
    screenHeight = y; 

    map = 0; 

    //set cursor visibility to false 
    //cursorVisibility(false); 


} 

drawEngine::~drawEngine() 
{ 
    cursorVisibility(true); 
    //set cursor visibility to true 
} 

int drawEngine::createSprite(int index, char c) 
{ 
    if (index >= 0 && index < 16) 
    { 
     spriteImage[index] = c; 
    return index; 
    } 

    return -1; 
} 

void drawEngine::deleteSprite(int index) 
{ 
    // in this implemantation we don't need it 
} 

void drawEngine::drawSprite(int index, int posX, int posY) 
{ 
    //go to the correct location 
    gotoxy (index, posX, posY); 
    // draw the sprite 

    cout << spriteImage[index]; 

    cursorVisibility(false); 
} 


void drawEngine::eraseSprite(int index, int posX, int posY) 
{ 
    gotoxy (index, posX, posY); 
    cout << ' '; 
} 

void drawEngine::setMap(char **data) 
{ 
    map = data; 
} 

void drawEngine::createBackgroundTile(int index, char c) 
{ 
    if (index >= 0 && index < 16) 
    { 
     tileImage[index] = c; 
    } 

} 

void drawEngine::drawBackground(void) 
{ 
    if(map) 
    { 
     for(int y = 0; y < screenHeight; y++) 
     { 
      gotoxy(0,y, 0); 

      for(int x = 0; x < screenWidth; x++) 
       cout << tileImage[map[x][y]]; 

     } 
    } 
} 

void drawEngine::gotoxy(int index, int x, int y) 
{ 
    HANDLE output_handle; 
    COORD pos; 

    pos.X = x; 
    pos.Y = y; 

    output_handle = GetStdHandle(STD_OUTPUT_HANDLE); 

    SetConsoleCursorPosition(output_handle, pos); 
} 

void drawEngine::cursorVisibility(bool visibility) 
{ 
    HANDLE output_handle; 
    CONSOLE_CURSOR_INFO cciInfo; 

    cciInfo.dwSize = 1; 
    cciInfo.bVisible = visibility; 

    output_handle = GetStdHandle(STD_OUTPUT_HANDLE); 

    SetConsoleCursorInfo(output_handle, &cciInfo); 
} 

drawEngine.h

#ifndef DRAWENGINE_H 
#define DRAWENGINE_H 
class drawEngine 
{ 
public: 
    drawEngine(int index, int xSize = 30, int ySize = 20, int x = 0, int y = 0); 
    ~drawEngine(); 
    drawEngine(){}; 

    int createSprite(int index, char c); 

    void deleteSprite(int index); 
    void eraseSprite(int index, int posX, int posY); 

    void createBackgroundTile(int index, char c); 

    void drawSprite(int index, int posX, int posY); 
    void drawBackground(void); 

    void setMap(char **); 
protected: 
    char **map; 
    int screenWidth, screenHeight; 
    char spriteImage[16]; 
    char tileImage[16]; 
private: 


    void gotoxy(int index, int x, int y); 
    void cursorVisibility(bool visibility); 

}; 

#endif 

我也得到了Sprite.cpp,Sprite.h,Character.h,Character.cpp和main.cpp中,如果你需要他们

+3

GAH!我们如何读取它!? – quasiverse 2011-03-16 09:12:01

+0

具体问题有哪些?是否调用drawBackground方法? 另请阅读编辑指南(http://stackoverflow.com/editing-help),并在发布代码 – 2011-03-16 09:16:56

回答

0

好,我通过代码完成了它,发现了一个问题。 Runner类封装了一个drawEngine对象。在Runner的构造函数中,调用默认c'tor drawEngine,该值不会为sceenWidthscreenHeight(或任何其他成员)设置值。幸运的是,在调试模式下,它们默认为0xcccccccc,这是负值,因此您立即返回drawBackground(Visual Studio 2010)。
你应该改变c'tor(甚至删除),并corretly初始化引擎在亚军的构造函数,例如:

class Runner { 
public: 
    Runner() : drawArea(0, width, height, ?, ?){}; 
    [...] 
}; 

此外,xy成员在循环使用drawBackground。您应该使用screenWidthscreenWidth,顺便说一句,我不知道什么是X和Y应在drawEngine

UPDATE: x和y在drawBackgroundgotoxy呼叫坐标混合起来,让你画在同一行的一切。顺便说一下:index用于什么?

+0

之前使用预览框,但我实际上并不知道使用了什么索引,但他们在教程中使用它。 – Blackelfwolf 2011-03-17 04:41:07

+0

你是什么意思c'tor和[...] ?? – Blackelfwolf 2011-03-17 04:51:05

+0

c'tor =构造函数。 ''[...]'只是意味着我忽略了其余的类定义,并且只包含了必须更改的行。对于'?'我不知道要设置什么值,因为它们从未在代码中使用过。您可以简单地删除它们 – 2011-03-17 12:01:28