E
enki
I am writing a game and I am having trouble with moving the character
on the map.
Here is what I have right now. It involves win32 programming but that
not my problem. I would like some suggestions how I can make my code
better. If you need more code I will post it. I am in early
devlopment of the game.
map.h:
#include<map.h>
#include<fstream.h>
#include<windows.h>
#include "structlib.h"
#include "space.h"
#ifndef maze_h
#define maze_h
class maze{
const static int numx = 38;
const static int numy = 30;
space grid[numy][numx];
map<char,dir> keys;
HWND hwnd;
char gchr;
coord pcoord;
dir n; /* directions */
dir s;
dir e;
dir w;
void fill();
ifstream& cfill(ifstream&, char&); /*Reads map from file */
int convx(int x){return x*12;}
int convy(int y){return y*8;}
public:
maze();
maze(const maze&);
~maze();
void showall(HWND);
void move(HWND, char);
void show(HWND);
};
#endif
#include "maze.h"
maze::maze(){
n.ud = -1;
n.lr = 0;
s.ud = 1;
s.lr = 0;
e.ud = 0;
e.lr = 1;
w.ud = 0;
w.lr = -1;
keys['n']=n;
keys['s']=s;
keys['e']=e;
keys['w']=w;
fill();
pcoord.x = 1;
pcoord.y = 1;
player pl;
player* play = &pl;
grid[pcoord.x][pcoord.y].SetPlayer(play);
}
maze::maze(const maze& mz){}
maze::~maze(){
delete [] grid;
}
ifstream& maze::cfill(ifstream& in, char& spc){
in>>spc;
return in;
}
void maze::fill(){
int l1 = 0;
int l2 = 0;
ifstream f ("level.txt");
if (! f.is_open()) {
MessageBox(NULL, "Missing File!", "Error!", MB_OK);
exit (1);
}
for(l1=0; l1 != numy; l1++){
for(l2=0; l2 != numx; l2++){
if(cfill(f, gchr)){
grid[l1][l2].readCH(gchr);
}
}
}
l2 = 0;
}
void maze::showall(HWND hwnd){
HDC hdc = GetDC(hwnd);
int lp1 = 0;
int lp2 = 0;
char ch;
for(lp1 = 0; lp1 != numy; lp1++){
for(lp2 = 0; lp2 != numx; lp2++){
ch = grid[lp1][lp2].getCH();
TextOut(hdc, convy(lp2),convx(lp1),&ch,1);
}
}
ReleaseDC(hwnd, hdc);
}
void maze::move(HWND hwnd, char dir){
player* play;
coord tcoord;
show(hwnd);
tcoord.x = pcoord.x + keys[dir].ud;
tcoord.y = pcoord.y + keys[dir].lr;
if (!grid[tcoord.x][tcoord.y].CheckWall()){
MessageBox(NULL, "Can't Go through Wall!", "Ouch", MB_OK);
return;
}
grid[pcoord.x][pcoord.y].ClearPlayer();
pcoord.x = tcoord.x;
pcoord.y = tcoord.y;
grid[pcoord.x][pcoord.y].SetPlayer(play);
show(hwnd);
}
void maze::show(HWND hwnd){
HDC hdc = GetDC(hwnd);
int lp1 = pcoord.x-1;
int lp2 = pcoord.y-1;
char ch;
for(lp1; lp1 != (pcoord.y)+1; lp1++){
for(lp2; lp2 != (pcoord.x)+1; lp2++){
ch = grid[lp1][lp2].getCH();
TextOut(hdc, convy(lp2),convx(lp1),&ch,1);
}
}
ReleaseDC(hwnd, hdc);
}
on the map.
Here is what I have right now. It involves win32 programming but that
not my problem. I would like some suggestions how I can make my code
better. If you need more code I will post it. I am in early
devlopment of the game.
map.h:
#include<map.h>
#include<fstream.h>
#include<windows.h>
#include "structlib.h"
#include "space.h"
#ifndef maze_h
#define maze_h
class maze{
const static int numx = 38;
const static int numy = 30;
space grid[numy][numx];
map<char,dir> keys;
HWND hwnd;
char gchr;
coord pcoord;
dir n; /* directions */
dir s;
dir e;
dir w;
void fill();
ifstream& cfill(ifstream&, char&); /*Reads map from file */
int convx(int x){return x*12;}
int convy(int y){return y*8;}
public:
maze();
maze(const maze&);
~maze();
void showall(HWND);
void move(HWND, char);
void show(HWND);
};
#endif
#include "maze.h"
maze::maze(){
n.ud = -1;
n.lr = 0;
s.ud = 1;
s.lr = 0;
e.ud = 0;
e.lr = 1;
w.ud = 0;
w.lr = -1;
keys['n']=n;
keys['s']=s;
keys['e']=e;
keys['w']=w;
fill();
pcoord.x = 1;
pcoord.y = 1;
player pl;
player* play = &pl;
grid[pcoord.x][pcoord.y].SetPlayer(play);
}
maze::maze(const maze& mz){}
maze::~maze(){
delete [] grid;
}
ifstream& maze::cfill(ifstream& in, char& spc){
in>>spc;
return in;
}
void maze::fill(){
int l1 = 0;
int l2 = 0;
ifstream f ("level.txt");
if (! f.is_open()) {
MessageBox(NULL, "Missing File!", "Error!", MB_OK);
exit (1);
}
for(l1=0; l1 != numy; l1++){
for(l2=0; l2 != numx; l2++){
if(cfill(f, gchr)){
grid[l1][l2].readCH(gchr);
}
}
}
l2 = 0;
}
void maze::showall(HWND hwnd){
HDC hdc = GetDC(hwnd);
int lp1 = 0;
int lp2 = 0;
char ch;
for(lp1 = 0; lp1 != numy; lp1++){
for(lp2 = 0; lp2 != numx; lp2++){
ch = grid[lp1][lp2].getCH();
TextOut(hdc, convy(lp2),convx(lp1),&ch,1);
}
}
ReleaseDC(hwnd, hdc);
}
void maze::move(HWND hwnd, char dir){
player* play;
coord tcoord;
show(hwnd);
tcoord.x = pcoord.x + keys[dir].ud;
tcoord.y = pcoord.y + keys[dir].lr;
if (!grid[tcoord.x][tcoord.y].CheckWall()){
MessageBox(NULL, "Can't Go through Wall!", "Ouch", MB_OK);
return;
}
grid[pcoord.x][pcoord.y].ClearPlayer();
pcoord.x = tcoord.x;
pcoord.y = tcoord.y;
grid[pcoord.x][pcoord.y].SetPlayer(play);
show(hwnd);
}
void maze::show(HWND hwnd){
HDC hdc = GetDC(hwnd);
int lp1 = pcoord.x-1;
int lp2 = pcoord.y-1;
char ch;
for(lp1; lp1 != (pcoord.y)+1; lp1++){
for(lp2; lp2 != (pcoord.x)+1; lp2++){
ch = grid[lp1][lp2].getCH();
TextOut(hdc, convy(lp2),convx(lp1),&ch,1);
}
}
ReleaseDC(hwnd, hdc);
}