随机生成的关卡未显示。邪恶的猴子教程

发布于 10-22 11:26 字数 6595 浏览 5 评论 0原文

您好,我用 3D Buzz 教程 制作了一个关卡生成器,名为 Evil Monkeys。 我生成了一个关卡,但无法将其绘制在屏幕上。

我的代码:

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

Hi I made a level generator with a 3D Buzz tutorial called Evil Monkeys.
I generated a level but I can't get it to draw on the screen.

My code:

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, '

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

I've also got Sprite.cpp, Sprite.h, Character.h,Character.cpp and main.cpp if you need them

); 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

drawEngine.cpp

drawEngine.h

I've also got Sprite.cpp, Sprite.h, Character.h,Character.cpp and main.cpp if you need them

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好久不见√2024-10-29 11:26:31

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

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

此外,在循环中使用 xy 成员在drawBackground中。您应该分别使用 screenWidthscreenWidth。顺便说一句,我不知道 drawEngine 中的 x 和 y 应该是什么

更新: gotoxy 调用中的 x 和 y 坐标>drawBackground 混合在一起,因此您将所有内容都绘制在同一行上。顺便说一句:index有什么用?

Ok, I made it through the code and found one issue. The Runner class encapsulates a drawEngine object. At the constructor of Runner, the default c'tor of drawEngine is called, which doesn't set values for sceenWidth and screenHeight (or any other member). Luckily in debug mode, they are defaulted to 0xcccccccc which is negative so you're drawBackground returns immediately (Visual Studio 2010).
You should change that c'tor (or even remove it) and corretly initialize the engine in runner's constructor, e.g.:

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

Further, the x and y members are used in the loops in drawBackground. You should use screenWidth and screenWidth, resp. BTW, I don't know what x and y should be in drawEngine

UPDATE: The x and y coordinates at the gotoxy call in drawBackground are mixed up, so you draw everything on the same line. BTW: what is index used for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文