问题描述
我正在写游戏,我在地图上移动角色
时遇到了麻烦。
这就是我现在所拥有的。它涉及win32编程但是
不是我的问题。我想要一些建议如何让我的代码更好。
更好。如果您需要更多代码,我会发布它。我在早期
开发游戏。
map.h:
#include< map。 h>
#include< fstream.h>
#include< windows.h>
#include" structlib.h"
#include" space.h"
#ifndef maze_h
#define maze_h
类迷宫{
const static int numx = 38;
const static int numy = 30;
space grid [numy] [numx ];
map< char,dir>钥匙;
HWND hwnd;
char gchr;
coord pcoord;
dir n; / *方向* /
dir s;
dir e;
dir w;
void fill();
ifstream& cfill(ifstream&,char&); / *从文件中读取地图* /
int convx(int x){return x * 12;}
int convy(int y){return y * 8;}
public:
迷宫();
迷宫(const迷宫&);
~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;
键[''n''] = n;
键[ ''s''] = s;
键[''e''] = e;
键[''w''] = w;
fill();
pcoord.x = 1;
pcoord.y = 1;
player pl;
player * play =& pl;
grid [pcoord.x] [pcoord.y] .SetPlayer(play);
}
迷宫::迷宫(const maze& mz){}
迷宫::〜ma ze(){
删除[]网格;
}
ifstream&迷宫:: cfill(ifstream& in,char& spc){
in>> spc;
返回;
}
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){
玩家*玩;
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,不能通过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);
}
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);
}
推荐答案
到底出了什么问题?你能进一步描述这个问题吗(例如文字
没有显示,显示不正确等等?)在重新绘制所有内容之前,你是否忘了画出
背景?字符?
更好的是,发布到microsoft.public.win32.programmer.gdi,我很有信心
都是那些专业的Windows GDI专家在那里。
问候,
Ben
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
[snip]
DW
[snip]
DW
这篇关于游戏问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!