Game question.

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

}
 
B

benben

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.

What exactly is wrong? Can you describe the problem even further (e.g. text
isn't displayed, incorrectly displayed, etc?) Did you forget to paint the
background before you repaint all the characters?

Even better, post to microsoft.public.win32.programmer.gdi, I'm sure there
are all those professional Windows GDI experts there.

Regards,
Ben
 
D

David White

enki said:
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.

How do you know? If something isn't displayed correctly in a window then it
very likely is a win32 problem, which of course is off-topic here.
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;

I have a suggestion about your design. You are mixing two major but
unrelated responsibilities - maze logic and user interface - in a single
class, so you could consider separating the maze layout, movement and logic
from your GUI code. If you have a simple maze class that just concerns
itself with the maze data and operations and knows nothing of HWNDs or DCs
or windows text display or message boxes you could use it anywhere and it
would be much easier to understand and maintain your code.
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);
};

[snip]

DW
 
E

enki

I think that is a good point. I am an untrained programmer, I taught
myself. I have often read books to understand the language. I have
often heard that I should just write programs to figure the stuff out.
I do have this program written in a console. I am trying to add an
interfact to it. I am trying to figure out how to make this work.

I realy have no idea how to seperate the two concepts. I will think
about it and try to figue somthingf out.
 
E

enki

I am not realy sure how I can create class to handle windows objects.

Here is some more of the code:



#include "Dungeon.h"

void paint(HDC hdc){
RECT rect;
HBRUSH white = CreateSolidBrush(RGB(0xfc,0xfc,0xfc));

SetRect(&rect,0,0,300,355);
FillRect(hdc, &rect, white);
DeleteObject(white);

}

void create(HWND hwnd, maze& mz){

HWND qbutton1,
qbutton2,
qbutton3,
qbutton4,
qbutton5;

HINSTANCE hin;

qbutton1 = CreateWindow("BUTTON", "Up",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
450, 320, 120, 20, hwnd, (HMENU) 1,
hin, NULL);

qbutton2 = CreateWindow("BUTTON", "Left",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
450, 340, 60, 20, hwnd, (HMENU) 2,
hin, NULL);

qbutton3 = CreateWindow("BUTTON", "Right",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
510, 340, 60, 20, hwnd, (HMENU) 3,
hin, NULL);

qbutton4 = CreateWindow("BUTTON", "Down",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
450, 360, 120, 20, hwnd, (HMENU) 4,
hin, NULL);

qbutton5 = CreateWindow("BUTTON", "Quit",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
480 , 400, 60, 20, hwnd, (HMENU) 5,
hin, NULL);

}

void commands(WPARAM wParam, LPARAM lParam, HWND hwnd, maze& m){

if(LOWORD(wParam)== 1){m.move(hwnd, n);}
if(LOWORD(wParam)== 2){m.move(hwnd, e);}
if(LOWORD(wParam)== 3){m.move(hwnd, w);}
if(LOWORD(wParam)== 4){m.move(hwnd, s);}
if(LOWORD(wParam)== 5){
SendMessage(GetParent((HWND) lParam), WM_DESTROY, 0, 0);
}
}

int dice(int roll, int dice){
int lp = 0;
int total = 0;

while(lp != roll){
total = total + dice;
}
return total;
}
 
K

Karl Heinz Buchegger

enki said:
I am not realy sure how I can create class to handle windows objects.

You are now on your way to Windows programming.
As has been already suggested, this is not really topical in this
group. You need to find a newsgroup which discusses Windows programming.

Having said that: Windows programming is complicated (If you don't use
a framework). You will need some literature for it. Walk to the next book
store and order some. Petzold's 'Programming Windows' is often recommended.

You can also try to find some online tutorial on programming Windows on the web.
You will figure out, that it takes a lot of code to bring up a single window
and handle all messages sent by it. Far to much for anybody to talk you through
in a newsgroup.
 
E

enki

Thanks, I wasn't realy asking about my windows programming. I was
wondering if my logic was ok. I am trying to become a better
programmer and the only way to do is to program. Books only give bare
basics of anything.
 
J

Jonathan Mcdougall

enki said:
I am writing a game and I am having trouble with moving the character
on the map.

Ok, here we go.
Here is what I have right now. It involves win32 programming but that
not my problem.

Next time, strip non-standard code (that includes windows things). It
will make it easier for everybody and will avoid some flaming.
I would like some suggestions how I can make my code
better.

I understand. The problem is that you will be better served with a
specific question (line 3 fails to compile) than with a "design"
question. Suggestions about making "code better" will be hard to get.
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>

These headers are non standard. For the first two, you can have

# include <map>
# include said:
From now on, all occurences of "map" and "ifstream" or "ofstream" will
have to be replaced by "std::map", "std::ifstream" and "std::eek:stream".
Get a good book for all the explanations.
#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);
};

Take the habit of writing the parameter name even in declaration. It
helps most of the times, and would be evn better since you have no
comments in your code.

Is that another file? Specify it next time.

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

Noooo! You didn't allocate grid with a "new", don't delete it! 'grid'
will be destroyed automatically. I'm suprised it hasn't crashed.
}

ifstream& maze::cfill(ifstream& in, char& spc){
in>>spc;
return in;
}

Well that's a bit unnecessary. You could have done it directly in the
code.

<snip>

It is hard to comment on a code you know nothing about. Here are
several general suggestions:

1) comment your code, not too much, but just enough.
2) one function, one responsability. it goes the same for classes.
functions should not have more than 5-10 lines, in general.
3) seperate between functionality and user interface.

In general, it looks ok.


Jonathan
 
E

enki

Thanks to all I will stop bothering here until I have a specific
question. I am writing code a few levels above my ability. I want to
be a good programmer. Do I need to go to school to learn this stuff?

I have the desire to create programs. I want to take the ideas I have
and put them into code. All I have is the bloodshed compiler and a few
books. I was going to study computers but I am realy bad at advanced
math. Yes I am fustrated. I try to studt books but they all have the
same basic stuff.

What kind of programs can I write to make me a better programmer? I
have ideas and I want to put them into code. I don't care about art
work. I am trying to do better than console applications. I have
spent time trying to understant perl/tk but C/win32 programming makes
more sence.
 
K

Karl Heinz Buchegger

enki said:
Thanks to all I will stop bothering here until I have a specific
question. I am writing code a few levels above my ability. I want to
be a good programmer. Do I need to go to school to learn this stuff?

No. Not at all. What you really need is practice, practice, practice.
Oh and yes, start at the beginnings.
I have the desire to create programs. I want to take the ideas I have
and put them into code. All I have is the bloodshed compiler and a few
books. I was going to study computers but I am realy bad at advanced
math.

Doesn't matter.
Unless you create programs in the technical computation section you rarely
ever need more then addition, subtraction, multiplication, division and probably
a small skills in computing percentages. If you are able to solve linear
equations you are well on your way to write most programs.
Yes I am fustrated. I try to studt books but they all have the
same basic stuff.

But important stuff.
Programming is like playing chess: Each of the individual moves of each
character is simple by itself. It is the combination of those moves (the
right sequence) that makes the game complicated.
Same with programming: The basic stuff is simple. But compibing those
basic stuff with other basic stuff drives the complexity up very fast.
What kind of programs can I write to make me a better programmer? I
have ideas and I want to put them into code. I don't care about art
work. I am trying to do better than console applications.

I don't know your skills. But if you are heading for GUI programming, then
the basic stuff should be of no real problem to you. GUI adds another layer
of complexity to a program. Sometimes it requires a different way of thinking
(eg. It took me a way to get fluent with the event driven paradigma one typically
finds in GUI programming).

So stick with console programs until you don't have big problems in, lets
say, creating a simple, menu driven, console phonebook. Complete with input,
output, sorting, saerching, saving to file, reading from file. If you can do that
in, lets say, 2 days you are ready to put a GUI around it. Otherwise you will
fight 2 beasts at once. And that is never a good idea if you have the coice.

Same with your game. Write it as a console program. This way, debugging is also
easier. Once it works, porting that to a GUI has one advantage: you don't have
to worry about the game itself, as you know it already works and can concentrate
on the GUI part itself. You then also will see how good your design has been. In
a good design you won't have to touch much from the inner game engine code.
 
E

enki

Thanks that is realy intersting. I have some work posted at
www.planetsource.com
pennies and prizes is my best program.

I can do pretty good console programs. I guess I am having trouble
understanding the GUI thing. I don't find too many good refrences. So
the GUI and the program should be separate files. I will try to create
the programs you suggest. I have references to most of that in
Accelerated C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top