[C ++]需要帮助设置课程

时间:2015-04-01 04:49:53

标签: c++ class

我是C ++的新手,我正在尝试创建一个Mario游戏。 我正在进行三个课程:舞台,图像和元素。第一个有自己的.h和.cpp,另外两个有.h和.cpp; 问题是:在类Stage中,我有一些Element类型的变量,我无法对世界上任何事情进行声明而没有任何问题。 我的代码是: Elements.h

#ifndef ELEMENTS_H
#define ELEMENTS_H

#include <stdio.h>
#include <conio.h>
#include <windows.h>

#include "Stage.h"

HDC * mywindow;
Stage * stage2;

class Image{
public:
    bool ontop;
    FILE* entry;
    int lenght, height;
    COLORREF matrix[32][32];
    void load();
    void print(int x, int y, int orientation, int ratiox, int ratioy);
    void erase(int x, int y, int orientation, int ratiox, int ratioy);
private:
};
**strong text**

class Element{
public:
    Element();
    int num, sizex, sizey, orientation, timeofdeath;
    float vely;
    int top, left, spent;
    void print();
    void erase();
    int bottom();
    int right();
    int middlex();
    int middley();
    void InitializeElements(HDC * windowPtr, Stage * stageptr);
};

#endif

Stage.h

#ifndef STAGE_H
#define STAGE_H
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <wingdi.h>
#include <iostream>
#using <mscorlib.dll>

#include "Elements.h"

class Stage{
    //0 = mario, 1 = luigi, 2 = mushroom. 3 = block, 4 = coin, 5 = coinbox, 6 = spentcoinbox, 7 = brick, 8 = bomb
public:
    bool map[1184][600];
    COLORREF colormap[1184][600];
    Element blocks[600], coins[600], mushrooms[600], coinboxes[600], bricks[600], bombs[600], specialcoins[600];
    int blockslen, coinslen, mushroomslen, coinboxeslen, brickslen, bombslen;
    int level;
    Stage();
    void print();
};
#endif

Elements.cpp

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <string.h>
#include <windows.h>
#include <wingdi.h>
#include <iostream>
#include <math.h>
#using <mscorlib.dll>

#include "Elements.h"


#define RED RGB(255, 0, 0)
#define BLUE  RGB(0, 0, 255)
#define BLACK RGB(0, 0, 0)
#define WHITE RGB(255, 255, 255)
#define NONE RGB(1, 2, 3)
#define BROWN RGB(43, 17, 0)
#define SKIN RGB(255, 179, 128)
#define YELLOW RGB(255, 255, 0)
#define SHOE RGB(80, 45, 22)
#define SKY RGB(101, 156, 239)
#define COLOR1 RGB(203, 79, 15)
#define COLOR2 RGB(255, 191, 179)
#define COLOR3 RGB(248, 216, 30)
#define COLOR4 RGB(248, 248, 0)
#define COLOR5 RGB(216, 158, 54)
#define COLOR6 RGB(255, 210, 67)
#define COLOR7 RGB(247, 150, 70)
#define COLOR8 RGB(255, 97, 29)
#define COLOR9 RGB(0,85,170)
#define COLOR0 RGB(131,91,9)
#define COLORZ RGB(246, 182, 4)
#define GREEN RGB(40, 170, 85)

Image images[9];

void Image::load(){
    char read;
    for (int j = 0; j != height; j++){
        for (int i = 0; i != lenght; i++){
            fscanf(entry, "%c", &read);
            if (read == 'R')
                matrix[i][j] = RED;
            else if (read == 'L')
                matrix[i][j] = BLUE;
            else if (read == 'S')
                matrix[i][j] = SKIN;
            else if (read == 'Y')
                matrix[i][j] = YELLOW;
            else if (read == 'N')
                matrix[i][j] = BROWN;
            else if (read == 'W')
                matrix[i][j] = WHITE;
            else if (read == 'Q')
                matrix[i][j] = SHOE;
            else if (read == 'B')
                matrix[i][j] = BLACK;
            else if (read == '1')
                matrix[i][j] = COLOR1;
            else if (read == '2')
                matrix[i][j] = COLOR2;
            else if (read == '3')
                matrix[i][j] = COLOR3;
            else if (read == '4')
                matrix[i][j] = COLOR4;
            else if (read == '5')
                matrix[i][j] = COLOR5;
            else if (read == '6')
                matrix[i][j] = COLOR6;
            else if (read == '7')
                matrix[i][j] = COLOR7;
            else if (read == '8')
                matrix[i][j] = COLOR8;
            else if (read == '9')
                matrix[i][j] = COLOR9;
            else if (read == '0')
                matrix[i][j] = COLOR0;
            else if (read == 'Z')
                matrix[i][j] = COLORZ;
            else if (read == 'G')
                matrix[i][j] = GREEN;
            else
                matrix[i][j] = NONE;
        }
        fscanf(entry, "%c", &read);
    }
    fclose(entry);
}
void Image::print(int x, int y, int orientation, int ratiox, int ratioy){
    COLORREF color;
    int i;
    for (int j = 0; j < height; j++){
        for (int k = 0; k < lenght; k++){
            if (matrix[i][j] == NONE)
                continue;
            if (orientation == 0)
                i = k;
            else
                i = 11 - k;
            for (int m = 0; m < ratiox; m++){
                for (int n = 0; n < ratioy; n++){
                    if (ontop == 0)
                        stage2.colormap[i][j] = color;
                    SetPixelV((*mywindow) , x + ratiox*k + m, y + ratioy*j + n, matrix[i][j]);
                }
            }
        }
    }
}
void Image::erase(int x, int y, int orientation, int ratiox, int ratioy){
    COLORREF color;
    int i;
    for (int j = 0; j < height; j++){
        for (int k = 0; k < lenght; k++){
            if (matrix[i][j] == NONE)
                continue;
            if (orientation == 0)
                i = k;
            else
                i = 11 - k;
            for (int m = 0; m < ratiox; m++){
                for (int n = 0; n < ratioy; n++){
                    if (ontop == 1){
                        SetPixelV((*mywindow), x + ratiox*k + m, y + ratioy*j + n, stage2.colormap[i][j]);
                    }
                    else{
                        SetPixelV((*mywindow), x + ratiox*k + m, y + ratioy*j + n, SKY);
                        stage2.colormap[i][j] = SKY;
                    }
                }
            }
        }
    }
}


Element::Element(){
    vely = 0;
    sizex = 2;
    sizey = 2;
    orientation = 0;
    spent = 0;
    timeofdeath = 0;

}
void Element::print(){
    images[i].print(left, top, orientation, sizex, sizey);
}
void Element::erase(){
    images[num].print(left, top, orientation, sizex, sizey);
}
int Element::bottom(){
    return (top + images[i].height - 1);
}
int Element::right(){
    return (left + images[i].length - 1);
}
int Element::middlex(){
    return (left + right())/2;
}
int Element::middley(){
    return (top + bottom()) / 2;
}

void Element::InitializeElements(HDC * windowPtr, Stage * stageptr){
    mywindow = windowPtr;
    stage2 = stageptr;
    //It's a me, Mario!
    images[0].entry = fopen("C:\\Mario\\Mario12x16.txt", "r");
    images[0].height = 16;
    images[0].lenght = 12;
    images[0].ontop = 1;
    images[0].load();
    //It's a me, Aluisio!
    images[1].entry = fopen("C:\\Mario\\Luigi12x16.txt", "r");
    images[1].height = 16;
    images[1].lenght = 12;
    images[1].ontop = 1;
    images[1].load();
    //Mario's hungry. Let's make a mushroom!
    images[2].entry = fopen("C:\\Mario\\Mushroom16x16.txt", "r");
    images[2].height = 16;
    images[2].lenght = 16;
    images[2].ontop = 0;
    images[2].load();
    //I need something to walk on!
    images[3].entry = fopen("C:\\Mario\\Block32x32.txt", "r");
    images[3].height = 32;
    images[3].lenght = 32;
    images[3].ontop = 0;
    images[3].load();
    //And money to buy Peach a nice dildo...
    images[4].entry = fopen("C:\\Mario\\Coin12x16.txt", "r");
    images[4].height = 16;
    images[4].lenght = 12;
    images[4].ontop = 0;
    images[4].load();
    //
    images[5].entry = fopen("C:\\Mario\\Coinbox16x16.txt", "r");
    images[5].height = 16;
    images[5].lenght = 16;
    images[5].ontop = 0;
    images[5].load();
    //
    images[6].entry = fopen("C:\\Mario\\SpentCoinbox16x16.txt", "r");
    images[6].height = 16;
    images[6].lenght = 16;
    images[6].ontop = 0;
    images[6].load();
    //
    images[7].entry = fopen("C:\\Mario\\Brick16x16.txt", "r");
    images[7].height = 16;
    images[7].lenght = 16;
    images[7].ontop = 0;
    images[7].load();
    //
    images[8].entry = fopen("C:\\Mario\\Bomb29x29.txt", "r");
    images[8].height = 29;
    images[8].lenght = 29;
    images[8].ontop = 0;
    images[8].load();
    //
}

Stage.cpp

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <string.h>
#include <windows.h>
#include <wingdi.h>
#include <iostream>
#include <math.h>
#using <mscorlib.dll>

#include "Stage.h"
//#include "Elements.h"
//#include "Elements.cpp"

Stage::Stage(){
    blockslen = 0;
    coinslen = 0;
    mushroomslen = 0;
    coinboxeslen = 0;
    brickslen = 0;
    bombslen = 0;
    for (int j = 0; j < 600; j++){
        for (int i = 0; i < 1184; i++){
            map[i][j] == 0;
        }
    }
    char read;
    FILE *entry;
    if (level == 1)
        entry = fopen("C:\\Mario\\stage1.txt", "r");
    for (int j = 0; j < 18; j++){
        for (int i = 0; i < 37; i++){
            fscanf(entry, "%c", &read);
            if (read == '0'){
                blocks[blockslen].num = 3;
                blocks[blockslen].sizex = 1;
                blocks[blockslen].sizey = 1;
                blocks[blockslen].left = 32 * i;
                blocks[blockslen++].top = 32 * j + 24;
                for (int m = 0; m < 32; m++){
                    for (int n = 0; n < 32; n++){
                        map[32 * i + m][32 * j + n + 24] = 0;
                    }
                }
            }
            if (read == 'C'){
                coins[coinslen].num = 4;
                coins[coinslen].left = 32 * i + 4;
                coins[coinslen++].top = 32 * j + 24;
            }
            if (read == 'D'){
                coinboxes[coinboxeslen].num = 5;
                coinboxes[coinboxeslen].left = 32 * i;
                coinboxes[coinboxeslen++].top = 32 * j + 24;
                for (int m = 0; m < 32; m++){
                    for (int n = 0; n < 32; n++){
                        map[32 * i + m][32 * j + n + 24] = 1;
                    }
                }
            }
            if (read == 'E'){
                coinboxes[coinboxeslen].num = 6;
                coinboxes[coinboxeslen].spent = true;
                coinboxes[coinboxeslen].left = 32 * i;
                coinboxes[coinboxeslen++].top = 32 * j + 24;
                for (int m = 0; m < 32; m++){
                    for (int n = 0; n < 32; n++){
                        map[32 * i + m][32 * j + n + 24] = 1;
                    }
                }
            }
            if (read == 'F'){
                bricks[brickslen].num = 7;
                bricks[brickslen].left = 32 * i;
                bricks[brickslen++].top = 32 * j + 24;
                for (int m = 0; m < 32; m++){
                    for (int n = 0; n < 32; n++){
                        map[32 * i + m][32 * j + n + 24] = 1;
                    }
                }
            }
            if (read == 'M'){
                mushrooms[mushroomslen].num = 2;
                mushrooms[mushroomslen].left = 32 * i;
                mushrooms[mushroomslen++].top = 32 * j + 24;
            }
        }
        fscanf(entry, "%c", &read);
    }
}

void Stage::print(){
    for (int i = 0; i < 1184; i++)
        for (int j = 0; j < 600; j++)
            stage.colormap[i][j] = SKY;
    for (int i = 0; i < blockslen; i++){
        blocks[i].print();
    }
    for (int i = 0; i < coinslen; i++){
        coins[i].print();
    }
    for (int i = 0; i < mushroomslen; i++){
        mushrooms[i].print();
    }
    for (int i = 0; i < coinboxeslen; i++){
        coinboxes[i].print();
    }
    for (int i = 0; i < brickslen; i++){
        bricks[i].print();
    }
    for (int i = 0; i < 1184; i++)
        for (int j = 0; j < 600; j++)
            if (stage.colormap[i][j] == SKY)
                SetPixelV(mywindow, i, j, SKY);
}

现在出现错误:image

谢谢大家

3 个答案:

答案 0 :(得分:0)

Stage *stage2;移出您的Elements.h,或者转发class Stage Elements.h#include "Stage.h"

您目前所做的事情始终是错误的。

答案 1 :(得分:0)

您需要在Elements.h中替换以下行

Stage * stage2;

class Stage;

同样在其他文件中

答案 2 :(得分:-1)

我强烈建议您重温C ++的语法。您遇到的所有错误都是由语法错误引起的。 例如,第一个可以通过替换第一个来轻松处理。在3 =块到a之前, 另外,将顺序更改为blocks = 3等。同时将all定义为ints,并且不要让编译器隐式为您执行此操作。

显示的其余错误位于mario.cpp中,您尚未显示。 希望这有帮助!