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);
} 11 2462
> 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
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
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.
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;
}
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
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.
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
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.
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
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++. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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"...
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |