2015-04-01 148 views
-2

我是新的C++,我正在尝试创建一个Mario游戏。 我有三个我正在工作的课:舞台,图像和元素。第一个有自己的.h和.cpp,另外两个有另一个.h和.cpp; 问题是:在Stage阶段,我有一些Element类型的变量,我不能为世界上的任何东西声明没有问题。 我的代码是: Elements.h[C++]需要帮助建立一个类

#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

谢谢大家

回答

0

上移Stage *stage2;出你Elements.h的,或转发申报Elements.hclass Stage#include "Stage.h"

你现在做什么,总会出错。

0

您需要

class Stage; 

同样替换下列行Elements.h

Stage * stage2; 

在其他文件中也

+1

你想声明一个结构类? ('HDC') - 请参阅'windef.h' – Blacktempel 2015-04-01 05:19:03

-1

我强烈建议你重温C的语法++。你所有的错误都是由语法错误造成的。例如,第一个可以很容易地通过替换。在之前的3 =块到, 此外,更改顺序为blocks = 3等。同时定义全部为ints,并且不要让编译器隐式为您做。

显示的其余错误在mario.cpp中,您没有显示。 希望这有助于!

+0

我试着在你的答案上多次阅读,我不知道你想说什么。另外,我看到无处不在的初始化变量。 – Blacktempel 2015-04-01 05:26:30