如何在Visual C ++中创建8个益智游戏. [英] How to create an 8 puzzle game in visual c++.

查看:55
本文介绍了如何在Visual C ++中创建8个益智游戏.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个8拼图游戏(在Windows应用程序中).我创建了界面,但是我不知道如何移动按钮.
我还需要知道如何结束游戏(即如何让程序知道难题已解决).
OK

I need to create an 8 puzzle game (in windows application). I created the interface, but i don''t know how to move the buttons.
I also need to know how to end the game (ie.. how to let the program know that the puzzle is solved ).
OK

推荐答案

Google是您的朋友:很好,经常拜访他.与在这里发布问题相比,他可以更快地回答问题.

快速的Google使用"8益智游戏c ++"获得了6,000,000次点击.
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A quick Google using "8 puzzle game c++" gave 6,000,000 hits.


解决了什么问题?如果您实际尝试编写代码,但现在遇到问题,请向我们显示使您遇到问题的代码.
Solving what problem? If you''ve actually tried to write the code, and now have a problem, show us the code that''s giving you the problem.


#include<iostream>
#include <windows.h>


#define MY_WIN_CLASS        "MyWinClass"

/* Command identifiers */
#define CMD_GAME_OVER   101
#define CMD_WHAT 102
#define ID_LIFE 103
#define ID_ITEM_b     301
#define CMD_level 1098
#define CMD_level_1 1034
#define CMD_level_2 1094
#define CMD_level_3 1095



int Init(HINSTANCE hInstance);
HWND CreateMainWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void PUZZLE(int x, int y);

HWND hMainWin=NULL , hButton=NULL;    /* main window handle */
 int x=0,y=0,bx=100,by=170,w=340,h=70,wX=600,wY=450;  

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                          LPSTR lpszCmdLine, int nCmdShow )
{
    MSG msg;
    
    if ( Init(hInstance) == 0 ) {
        MessageBox(NULL,"Failed to initialize","Error",MB_OK);
        return 0;
    } else {
        while (GetMessage(&msg,NULL,0,0)) {         
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        return msg.wParam;
    }   
}



int Init(HINSTANCE hInstance)
{
    WNDCLASS WinClass;
    int Ok;
    
    /* registers a window class */
    WinClass.style = NULL;
    WinClass.lpfnWndProc = WndProc;
    WinClass.cbClsExtra = 0;
    WinClass.cbWndExtra = 0;
    WinClass.hInstance = hInstance;
    WinClass.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
    WinClass.hCursor = LoadCursor(NULL,IDC_ARROW);
    WinClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WinClass.lpszMenuName = (LPCSTR)NULL;   
    WinClass.lpszClassName = (LPCSTR)MY_WIN_CLASS;
    
    
    if ( Ok = RegisterClass(&WinClass) ) {
        /* Create the main window */
        hMainWin = CreateMainWindow(hInstance);
        if ( hMainWin == NULL ) Ok = 0;
    }
    return Ok;
}



 
HWND CreateMainWindow(HINSTANCE hInstance)
{
    HWND hWin;
    HMENU hMainMenu, hSubMenu;
   
     hMainMenu = CreateMenu();
     
     
    hSubMenu = CreateMenu();
    
    // GAME menu
    AppendMenu(hSubMenu,MF_STRING,CMD_GAME_OVER,"&Over");
    AppendMenu(hMainMenu,MF_STRING|MF_POPUP,(UINT)hSubMenu,"&Game");
     
    
    // GAME menu
    AppendMenu(hSubMenu,MF_STRING,CMD_level,"&Easy");
     AppendMenu(hSubMenu,MF_STRING,CMD_level,"&Medium");
      AppendMenu(hSubMenu,MF_STRING,CMD_level,"&Hard");
    AppendMenu(hMainMenu,MF_STRING|MF_POPUP,(UINT)hSubMenu,"&Level");
    
    hSubMenu = CreateMenu();  //ABOUT menu
    AppendMenu(hMainMenu,MF_STRING,CMD_WHAT,"&About");
       

        if (hWin = CreateWindow((LPCSTR)MY_WIN_CLASS,
        " Midterm Test",
        (DWORD)WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL|WS_CLIPSIBLINGS ,
        60, 30,600 , 450,NULL, hMainMenu, hInstance, (LPVOID)NULL)) {  
          hButton=CreateWindow("BUTTON","  1  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            100,5,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  2  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            211,5,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  3  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY,
            322,5,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);  
            
       hButton=CreateWindow("BUTTON","  4  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            100,116,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  5  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            211,116,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  6  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY,
            322,116,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
            
        hButton=CreateWindow("BUTTON","  7  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            100,227,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  8  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY,
            322,227,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL); 
        ShowWindow(hWin, SW_SHOWNORMAL);
        SetActiveWindow(hWin);        
        //System.Windows.Forms.TextBox;      
    }
    return hWin;
}





LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{   
    switch (message) {      
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_CTLCOLORSTATIC:         
            return (LRESULT)GetStockObject(WHITE_BRUSH);    
        case WM_MOUSEMOVE:
        		x = LOWORD(lParam);
        		y = HIWORD(lParam);
                PUZZLE(x,y); 
                return 0;      
        case WM_COMMAND:
            switch (LOWORD(wParam)) {   
                case CMD_WHAT:
                     MessageBox(NULL," PROGRAMMED BY:: OUSMAN KASSAMA ","User Interface Prototype",MB_OK);
                    break;
               case CMD_GAME_OVER:
                    DestroyWindow(hMainWin);
                    break;      
                       
            }
            return 0;
    }
    
    return (DefWindowProc(hWnd, message, wParam, lParam));
}

void PUZZLE(int x, int y)  
{
     int x1=x,y1=y;
     int x2=x1,y2=y1;
     int x3=x1,y3=y1;
     int posX=0,posY=0;
if((x1 >= bx-20) && (x1 <= 420) && (y1 >= by-20) && (y1 <= (by-20)+h)){
      if(y1<= 225){posX=600-x1;posY=450-y1; }
 else{posX=(300)-x1;posY=(225)-y1;}
if (posX <= 0)posX = 0;
if (posY <= 0)posY = 0;
if (posX >= wX-w)posX = 260;
if (posY >= wY-h)posY = 360;

  
      SetWindowPos(hButton,NULL,posX,posY,0,0,SWP_NOSIZE|SWP_NOZORDER);
      bx=posX;
      by=posY;
}

}</windows.h></iostream>


这篇关于如何在Visual C ++中创建8个益智游戏.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆