473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<fstrea m.h>
#include<window s.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(cons t maze& mz){}

maze::~maze(){
delete [] grid;
}

ifstream& maze::cfill(ifs tream& 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(H WND 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),conv x(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),conv x(lp1),&ch,1);
}
}
ReleaseDC(hwnd, hdc);

}

Jul 23 '05 #1
11 2537
> 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.publi c.win32.program mer.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<fstrea m.h>
#include<window s.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 responsibilitie s - 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 = CreateSolidBrus h(RGB(0xfc,0xfc ,0xfc));

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

}

void create(HWND hwnd, maze& mz){

HWND qbutton1,
qbutton2,
qbutton3,
qbutton4,
qbutton5;

HINSTANCE hin;

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

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

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

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

qbutton5 = CreateWindow("B UTTON", "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(wPara m)== 1){m.move(hwnd, n);}
if(LOWORD(wPara m)== 2){m.move(hwnd, e);}
if(LOWORD(wPara m)== 3){m.move(hwnd, w);}
if(LOWORD(wPara m)== 4){m.move(hwnd, s);}
if(LOWORD(wPara m)== 5){
SendMessage(Get Parent((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<fstrea m.h>
#include<window s.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::ifstr eam" and "std::ostre am".
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(cons t 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(ifs tream& 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

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

Similar topics

4
2707
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 I have found, and the only thing that makes me think it might be possible is this: http://asbahr.com/python.html I would be interested to hear...
138
6416
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 yeah,-that's-non-obvious dept. theodp writes "As if there weren't enough dodgy patents, here's an excerpt from one granted to Microsoft Tuesday for a 'Method and...
9
3869
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" http://www.gamedev.net/reference/design/features/makegames/. the article suggests first programming a tetris clone, then moving on to "breakout", "pacman", and a super...
1
2662
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 message broadcasting via the server. I'd now like to move the game logic into the server.
5
8917
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 data into an array of structures, with the animal and question in the same variable(question) and the bool isAnimal tells me whether its an animal or...
1
2318
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: player gets a choice to go first or second player picks first player can choose either 1 2 3 player picks 1 // computer now has 2 choices 2,3...
0
1673
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 A,B,C,D letters that the correct answer is assigned too. The code that does this is highlighted in bold and the code that assigns it to a variable is...
5
4569
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 statements, and os file operator. My code needs some adjustments, most of all at the end. Please give some tips!!! import os while True: ...
7
2364
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 i've realized is that, apparently, most of the games out there are likely to be coded in C++, is this because the language offers features which are...
0
7467
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7736
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7982
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6066
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3514
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3494
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1079
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.