473,414 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Game question.

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

}

Jul 23 '05 #1
11 2514
> 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
Jul 23 '05 #2
enki wrote:
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

Jul 23 '05 #3
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.

Jul 23 '05 #4
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;
}

Jul 23 '05 #5
enki wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6
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.

Jul 23 '05 #7
enki wrote:
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 <fstream>
From now on, all occurences of "map" and "ifstream" or "ofstream" will have to be replaced by "std::map", "std::ifstream" and "std::ostream".
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.
#endif
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

Jul 23 '05 #8
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.

Jul 23 '05 #9
enki wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #10
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++.

Jul 25 '05 #11
qubit
1
hey, just thought i would drop a few cents into the mix..
these are some great turorials that got me started on win32 programming.

try each one, some are good for the concepts, some are better with the actuall nitty gritty coding details.


http://www.clipx.net/ng/misc/Generic_Win32_App.php
http://www.xaff.org/CPP/WIN32BAS/basic1.html
http://winprog.net/tutorial/ <<-- my favorate.

and just because this is a GAMING board, these are some good openGL pages.
i'm still trying to get the hang of that myself. :confused:
http://www.ultimategameprogramming.com/index2.php
http://www.opengl.org/resources/faq/...g_started.html
Jul 29 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Moosebumps | last post by:
When I have time, I am planning to evaluate Python for console game development (on Playstation 2, GameCube, and Xbox). Does anyone have any experience with this? Pretty much the only resource...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
9
by: the_philospher | last post by:
I am a beginner programmer learning c++. i have read some useful information about programming games (which is my goal) on gamedev.net, particularly "how do i program games"...
1
by: Jos | last post by:
Hello all. I have a working server, using the asyncore/chat module, which enables logging in, rooms and private messaging. I've used this framework to make some simple games that only required...
5
by: Kraken | last post by:
Hi, i have a bit of a problem here. I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file...
1
by: HighBuddha | last post by:
Hello, i have a question about game trees. I've been given an assignment in class (highschool) and the assignment is to write a program the simulates The Game Of Eight. The game runs like this: ...
0
by: Shawn Minisall | last post by:
For my final project, I'm trying to do a GUI based game similar to are you smarter then a 5th grader. I've been working on it and am stuck with some code someone helped me with to randomize the...
5
by: alesitaam | last post by:
Help!!!! Im new using python, currently writing a program which tests one game, IQ test. When the module is run, the program should ask user to choose the game to start. Also, I'm using Try...Except...
7
by: Benjamin Vigneaux | last post by:
Well, I'm very interested in game development, I'm just starting out though, browsing here and there for tutorials, references, etc.. and learning about the game development industry... What...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.