Code:
#define ID_MENU 101
#define IDB_KHUNG 102
#define IDB_BITMAP0 110
#define IDB_BITMAP1 111
#define IDB_BITMAP2 112
#define IDB_BITMAP3 113
#define IDB_BITMAP4 114
#define IDB_BITMAP5 115
#define IDB_BITMAP6 116
#define IDB_BITMAP7 117
#define IDB_BITMAP8 118
#define IDB_BITMAP9 119
#define IDB_BITMAP10 120
#define IDB_BITMAP11 121
#define IDB_BITMAP12 122
#define IDB_BITMAP13 123
#define IDB_BITMAP14 124
#define IDB_BITMAP15 125
#define IDB_BITMAP16 126
#define IDB_BITMAP17 127
#define IDB_BITMAP18 128
#define IDB_BITMAP19 129
#define IDB_BITMAP20 130
#define ID_GAME_NEWGAME 40001
#define ID_GAME_EXIT 40002
#define ID_HELP_HOWTOPLAY 40003
#define ID_HELP_ABOUTPUZZLEGAME 40004
#define ID_GAME_START 40005
#include <afxwin.h>
#include <time.h>
class CS : public CFrameWnd
{
public:
int xAnh,yAnh;
int a[20],k,idBitmapLeft,idBitmapRight,idBitmapUp,idBitmapDown;
CS()
{
CreateEx(WS_EX_TOOLWINDOW,0,0,WS_POPUP,0,0,400,520,0,0);
ShowWindow(1);
UpdateWindow();
menuBar();
veAnh(0,0,130);
}
void menuBar()
{
CMenu menu;
menu.LoadMenuA(101);
SetMenu(&menu);
}
void veAnh(int wBitmap,int hBitmap,int idBitmap)
{
CBitmap anh;
anh.LoadBitmap(idBitmap);
CDC cdc;
CClientDC d(this);
cdc.CreateCompatibleDC(&d);
cdc.SelectObject(&anh);
d.BitBlt(wBitmap,hBitmap,400,500,&cdc,0,0,SRCCOPY);
}
int timx(int a[],int n,int x)// co gia tri x trong mang a do dai n hay ko
{
int i;
if(n==0)return 0;
for(i=0;i<n;++i)
if(a[i]==x)return 1;
return 0;
}
void genRandom(int a[], int n)// tao n gia tri ngau nhien luu vao mang a
{
int i,x;
x=rand()%20+110;
for(i=0;i<n;++i)
{
while(timx(a,i,x))
x=rand()%20+110;
a[i]=x;
}
}
void content()
{
k=0;
genRandom(a,20);
srand((unsigned) time(NULL));
for(int i=0;i<400;i+=100)
{
for(int j=0;j<500;j+=100)
{
if(a[k]==129)
{
xAnh=i;
yAnh=j;
idBitmapLeft = k-5;
idBitmapRight = k+5;
idBitmapUp = k-1;
idBitmapDown = k+1;
}
veAnh(i,j,a[k]);
if(k>=0&&k<=20)
{
k++;
}
else
{
break;
}
}
}
}
void itemStart()
{
content();
}
void itemNew()
{
veAnh(0,0,130);
}
void itemExit()
{
DestroyWindow();
}
void itemHowDoPlay()
{
MessageBox("Use the UP,DOWN,LEFT,RIGHT key to move the image","How do play ?",MB_OK);
}
void itemAbout()
{
MessageBox("Puzzle Game version 1.0","About Puzzle Game",MB_OK);
}
void OnKeyUp(UINT c, UINT r,UINT f)
{
if(c==VK_LEFT)
{
veAnh(xAnh,yAnh,a[idBitmapRight]);
veAnh(xAnh+100,yAnh,129);
idBitmapRight=idBitmapRight+5;
xAnh=xAnh+100;
}
if(c==VK_RIGHT)
{
veAnh(xAnh,yAnh,a[idBitmapLeft]);
veAnh(xAnh-100,yAnh,129);
idBitmapLeft=idBitmapLeft-5;
xAnh=xAnh-100;
}
if(c==VK_DOWN)
{
veAnh(xAnh,yAnh,a[idBitmapUp]);
veAnh(xAnh,yAnh-100,129);
idBitmapUp=idBitmapUp-1;
yAnh=yAnh-100;
}
if(c==VK_UP)
{
veAnh(xAnh,yAnh,a[idBitmapDown]);
veAnh(xAnh,yAnh+100,129);
idBitmapDown=idBitmapDown+1;
yAnh=yAnh+100;
}
if(c==VK_F1)
{
itemStart();
}
if(c==VK_F2)
{
veAnh(0,0,130);
}
if(c==VK_ESCAPE)
{
itemExit();
}
}
DECLARE_MESSAGE_MAP()
};
/*****************************/
BEGIN_MESSAGE_MAP(CS, CFrameWnd)
ON_COMMAND(ID_GAME_START, itemStart)
ON_COMMAND(ID_GAME_EXIT, itemExit)
ON_COMMAND(ID_GAME_NEWGAME, itemNew)
ON_COMMAND(ID_HELP_HOWTOPLAY, itemHowDoPlay)
ON_COMMAND(ID_HELP_ABOUTPUZZLEGAME, itemAbout)
ON_WM_KEYUP()
END_MESSAGE_MAP()
/*****************************/
class CT : public CWinApp
{
public:
BOOL InitInstance()
{
m_pMainWnd = new CS;
return 1;
}
}app;
Mình đang mắc phần di chuyển của các ảnh bên cạnh ảnh trắng. Khi thực hiện ấn phím sang trái rồi sang phải thì ảnh vừa di chuyện bị mất. Các bạn giúp mình bài này với nhé.
Bookmarks