GLine游戏(Win32GUI实现,CodeBlocks+GCC编译)

Posted maxuewei2

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GLine游戏(Win32GUI实现,CodeBlocks+GCC编译)相关的知识,希望对你有一定的参考价值。

//main.cpp
1
#if defined(UNICODE) && !defined(_UNICODE) 2 #define _UNICODE 3 #elif defined(_UNICODE) && !defined(UNICODE) 4 #define UNICODE 5 #endif 6 7 #include <tchar.h> 8 #include <windows.h> 9 #include "matrix.h" 10 #include "matrix.cpp" 11 #include <stdio.h> 12 #include <pthread.h> 13 #define BKCOLOR RGB(200,200,200) 14 int score=0; 15 int WIDTH=400; 16 int HEIGHT=440; 17 Matrix ma; 18 int width=40;//WIDTH/ma.getN(); 19 int height=40;//HEIGHT/ma.getN(); 20 PAINTSTRUCT ps ; 21 static HBRUSH red_brush =CreateSolidBrush (RGB(255,0,0)); 22 static HBRUSH green_brush =CreateSolidBrush (RGB(0,255,0)); 23 static HBRUSH blue_brush =CreateSolidBrush (RGB(0,0,255)); 24 static HBRUSH yellow_brush =CreateSolidBrush (RGB(255,255,0)); 25 static HBRUSH purple_brush =CreateSolidBrush (RGB(255,0,255)); 26 static HBRUSH white_brush =CreateSolidBrush (BKCOLOR); 27 Point * s_lattice=new Point(0,0),*d_lattice=new Point(0,0); 28 bool paint_path=false; 29 int path_color=0; 30 Point* sou=new Point(); 31 Point* dest=new Point(); 32 Point* mouse=new Point(0,0); 33 Point *position=new Point(0,0); 34 Link<Point *>* path; 35 HBRUSH hBrush ; 36 HDC hdc; 37 POINT pc; 38 RECT client_rect; 39 int score_height; 40 41 /* Declare Windows procedure */ 42 LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); 43 44 /* Make the class name into a global variable */ 45 TCHAR szClassName[ ] = _T("Game"); 46 47 int WINAPI WinMain (HINSTANCE hThisInstance, 48 HINSTANCE hPrevInstance, 49 LPSTR lpszArgument, 50 int nCmdShow) { 51 HWND hwnd; /* This is the handle for our window */ 52 MSG messages; /* Here messages to the application are saved */ 53 WNDCLASSEX wincl; /* Data structure for the windowclass */ 54 55 /* The Window structure */ 56 wincl.hInstance = hThisInstance; 57 wincl.lpszClassName = szClassName; 58 wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ 59 wincl.style = CS_DBLCLKS; /* Catch double-clicks */ 60 wincl.cbSize = sizeof (WNDCLASSEX); 61 62 /* Use default icon and mouse-pointer */ 63 wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); 64 wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); 65 wincl.hCursor = LoadCursor (NULL, IDC_ARROW); 66 wincl.lpszMenuName = NULL; /* No menu */ 67 wincl.cbClsExtra = 0; /* No extra bytes after the window class */ 68 wincl.cbWndExtra = 0; /* structure or the window instance */ 69 /* Use Windows‘s default colour as the background of the window */ 70 wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; 71 72 /* Register the window class, and if it fails quit the program */ 73 if (!RegisterClassEx (&wincl)) 74 return 0; 75 76 /* The class is registered, let‘s create the program*/ 77 hwnd = CreateWindowEx ( 78 0, /* Extended possibilites for variation */ 79 szClassName, /* Classname */ 80 _T("GLine"), /* Title Text */ 81 WS_OVERLAPPEDWINDOW, /* default window */ 82 CW_USEDEFAULT, /* Windows decides the position */ 83 CW_USEDEFAULT, /* where the window ends up on the screen */ 84 WIDTH, /* The programs width */ 85 HEIGHT, /* and height in pixels */ 86 HWND_DESKTOP, /* The window is a child-window to desktop */ 87 NULL, /* No menu */ 88 hThisInstance, /* Program Instance handler */ 89 NULL /* No Window Creation data */ 90 ); 91 92 /* Make the window visible on the screen */ 93 ShowWindow (hwnd, nCmdShow); 94 95 /* Run the message loop. It will run until GetMessage() returns 0 */ 96 while (GetMessage (&messages, NULL, 0, 0)) { 97 /* Translate virtual-key messages into character messages */ 98 TranslateMessage(&messages); 99 /* Send message to WindowProcedure */ 100 DispatchMessage(&messages); 101 } 102 103 /* The program return-value is 0 - The value that PostQuitMessage() gave */ 104 return messages.wParam; 105 } 106 void select_brush(int color,HDC hdc) { 107 108 switch(color) { 109 case RED: 110 SelectObject (hdc, red_brush); 111 break; 112 case GREEN: 113 SelectObject (hdc, green_brush); 114 break; 115 case BLUE: 116 SelectObject (hdc, blue_brush); 117 break; 118 case YELLOW: 119 SelectObject (hdc, yellow_brush); 120 break; 121 case PURPLE: 122 SelectObject (hdc, purple_brush); 123 break; 124 default: 125 SelectObject (hdc, white_brush); 126 } 127 } 128 void paint_lattice(Point * p,HDC hdc,int color) { //-1 original color 129 int left=p->y*width; 130 int right=(p->y+1)*width; 131 int top=p->x*height+score_height; 132 int bottom=(p->x+1)*height+score_height; 133 if(color!=-1) { 134 select_brush(color,hdc); 135 } else { 136 select_brush(ma.get_color(p),hdc); 137 } 138 Ellipse (hdc,left,top,right,bottom) ; 139 } 140 void mouse_to_position() { 141 position->x=(mouse->y-score_height)/height; 142 position->y=mouse->x/width; 143 } 144 char *get_score_string(char* content){ 145 char *message=new char[50]; 146 sprintf (message, "%s %d .",content,score); 147 return message; 148 } 149 int show_score(HWND hwnd,char* content,char* title) { 150 MessageBox(hwnd,get_score_string(content),title,NULL); 151 } 152 void after_show_path(HWND hwnd) { 153 ma.set_flag(dest,ma.get_color(sou)); 154 ma.set_blank(sou); 155 ma.random_set(2); 156 ma.eliminate(score); 157 InvalidateRect (hwnd,NULL, true) ; 158 if(ma.is_all_not_blank()) { 159 show_score(hwnd,"GAME OVER.Your score is","你输了"); 160 } 161 } 162 struct args { 163 HWND hwnd; 164 HDC hdc; 165 }; 166 void* show_path(void* ar) { 167 path=new Link<Point *>(); 168 ma.get_path(sou,dest,path); 169 struct args* arg=(struct args*) ar; 170 HWND hwnd=arg->hwnd; 171 HDC hdc=arg->hdc; 172 if(path->get_first()==NULL) { 173 return 0; 174 } 175 //path->show_link(); 176 path->seek_to_first(); 177 Point* node=path->get_first(); 178 path_color=ma.get_color(node); 179 s_lattice=node; 180 paint_path=true; 181 while((node=path->get_next())!=NULL) { 182 d_lattice=node; 183 InvalidateRect (hwnd,NULL, true) ; 184 Sleep(300); 185 } 186 paint_path=false; 187 after_show_path(hwnd); 188 } 189 190 void init_GUI(HWND hwnd,HDC hdc) { 191 SetBkColor(hdc,BKCOLOR); 192 for(int i=0; i<ma.getN(); i++) { 193 for(int j=0; j<ma.getN(); j++) { 194 Point* p=new Point(i,j); 195 paint_lattice(p,hdc,-1); 196 delete p; 197 } 198 } 199 char * score=get_score_string("You Score: "); 200 TextOut(hdc,0,score_height/4,score,strlen(score)); 201 } 202 203 void sou_dest(HWND hwnd,HDC hdc) { 204 static pthread_t show_path_pth; 205 static int ret; 206 mouse_to_position(); 207 if(sou!=NULL) { 208 if(ma.get_color(position)==BLANK) { 209 *dest=*position; 210 struct args arg; 211 arg.hdc=hdc; 212 arg.hwnd=hwnd; 213 ret= pthread_create( &show_path_pth, NULL, show_path, &arg ); //参数:创建的线程id,线程参数,线程运行函数的起始地址,运行函数的参数 214 if( ret != 0 ) { //创建线程成功返回0 215 printf("pthread_create error:error_code=%d\n",ret ); 216 } 217 } else { 218 *sou=*position; 219 } 220 } else { 221 if(ma.get_color(position)!=BLANK) { 222 *sou=*position; 223 } 224 } 225 } 226 /* This function is called by the Windows function DispatchMessage() */ 227 228 LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { 229 switch (message) { /* handle the messages */ 230 case WM_CREATE: { 231 ma.init(); 232 GetClientRect (hwnd, &client_rect) ; 233 WIDTH=client_rect.right-client_rect.left ; 234 HEIGHT=client_rect.bottom-client_rect.top ; 235 score_height=HEIGHT/10; 236 width=WIDTH/ma.getN(); 237 height=(HEIGHT-score_height)/ma.getN(); 238 InvalidateRect (hwnd,NULL, true) ; 239 } 240 break; 241 case WM_DESTROY: 242 PostQuitMessage (0); /* send a WM_QUIT to the message queue */ 243 break; 244 case WM_LBUTTONUP: { 245 GetCursorPos(&pc); 246 ScreenToClient(hwnd,&pc); 247 mouse->x=pc.x; 248 mouse->y=pc.y; 249 sou_dest(hwnd,hdc); 250 } 251 break; 252 case WM_RBUTTONUP: { 253 show_score(hwnd,"","得分"); 254 } 255 break; 256 case WM_PAINT: { 257 hdc = BeginPaint (hwnd, &ps) ; 258 init_GUI(hwnd,hdc); 259 if(paint_path) { 260 paint_lattice(s_lattice,hdc,BLANK); 261 paint_lattice(d_lattice,hdc,path_color); 262 } 263 EndPaint (hwnd, &ps) ; 264 } 265 break; 266 default: /* for messages that we don‘t deal with */ 267 return DefWindowProc (hwnd, message, wParam, lParam); 268 } 269 return 0; 270 }

 

 

//matrix.h
1
#ifndef MATRIX_H_INCLUDED 2 #define MATRIX_H_INCLUDED 3 #pragma once 4 5 #define BLANK 0 6 #define RED 1 7 #define GREEN 2 8 #define BLUE 3 9 #define YELLOW 4 10 #define PURPLE 5 11 12 class Point { 13 private : 14 15 public: 16 int x; 17 int y; 18 Point(int xx,int yy); 19 Point(); 20 bool operator ==(Point p); 21 Point& operator =(Point p); 22 void show(); 23 }; 24 class node { 25 public : 26 int x; 27 int y; 28 node* father; 29 node(); 30 node(int xx,int yy,node* f); 31 }; 32 template<class T> 33 class LinkNode { 34 private: 35 36 public: 37 T po; 38 LinkNode* next; 39 LinkNode(T pp); 40 LinkNode& operator=(LinkNode nn); 41 void show_node(); 42 }; 43 template <class T> 44 class Link { 45 private: 46 LinkNode<T>* head=NULL; 47 LinkNode<T>* p=NULL; 48 LinkNode<T>* tail=NULL; 49 public: 50 friend class Matrix; 51 void seek_to_first(); 52 T get_first(); 53 T remove_first(); 54 T get_next(); 55 void add_to_first(T t); 56 void add_to_last(T t); 57 void show_link(); 58 }; 59 class Matrix { 60 private: 61 int N=10; 62 int COLOR_NUM=5; 63 int** matrix; 64 int max_random_set_num=5; 65 int get_random_num(int x); 66 void print_matrix() ; 67 bool random_set_one() ; 68 bool is_out(int x,int y) ; 69 public: 70 int getN() ; 71 Matrix() ; 72 void init(); 73 void set_flag(Point* p,int color) ; 74 void set_blank(Point* p); 75 bool is_in_que(int** in,int x,int y) ; 76 void get_path(Point *s,Point* dest,Link<Point *> *&l) ; 77 void random_set(int n) ; 78 int get_color(Point *p) ; 79 int get_color(int x,int y); 80 int sget_color(int dir,int x,int y,int n,int color); 81 void test_line(int x,int y,int dir,int &score); 82 int eliminate(int & score) ; 83 bool is_all_not_blank(); 84 }; 85 #endif // MATRIX_H_INCLUDED

 

 

//matrix.cpp
1
#include "matrix.h" 2 3 #include <stdlib.h> 4 #include <time.h> 5 #include <stdio.h> 6 7 8 Point::Point(int xx,int yy) { 9 x=xx; 10 y=yy; 11 } 12 Point::Point() {} 13 bool Point::operator ==(Point p) { 14 return x==p.x&y==p.y; 15 } 16 Point& Point::operator =(Point p) { 17 x=p.x; 18 y=p.y; 19 } 20 void Point::show() { 21 printf("(%d,%d)->",x,y); 22 } 23 24 node::node() {} 25 node::node(int xx,int yy,node* f) { 26 x=xx; 27 y=yy; 28 father=f; 29 } 30 31 template<class T> 32 LinkNode<T>::LinkNode(T pp) { 33 po=pp; 34 next=NULL; 35 } 36 template<class T> 37 LinkNode<T>& LinkNode<T>::operator=(LinkNode<T> nn) { 38 po=nn.p; 39 next=nn.next; 40 } 41 template<class T> 42 void LinkNode<T>::show_node() { 43 po->show(); 44 } 45 46 template<class T> 47 void Link<T>::seek_to_first() { 48 p=head; 49 } 50 template<class T> 51 T Link<T>::get_first() { 52 return head==NULL?NULL:head->po; 53 } 54 template<class T> 55 T Link<T>::remove_first() { 56 if(head==NULL)return NULL; 57 T *temp=&(head->po); 58 head=head->next; 59 return *temp; 60 } 61 template<class T> 62 T Link<T>::get_next() { 63 if(p==tail)return NULL; 64 if(p==NULL)return NULL; 65 if(p->next==NULL)return NULL; 66 p=p->next; 67 return p->po; 68 } 69 template<class T> 70 void Link<T>::add_to_first(T t) { 71 LinkNode<T>* x=new LinkNode<T>(t); 72 x->next=head; 73 head=x; 74 if(tail=NULL) { 75 tail=head; 76 return ; 77 } 78 } 79 template<class T> 80 void Link<T>::add_to_last(T t) { 81 LinkNode<T>* x=new LinkNode<T>(t); 82 if(head==NULL) { 83 head=x; 84 x->next=NULL; 85 tail=x; 86 return ; 87 } 88 tail->next=x; 89 x->next=NULL; 90 tail=x; 91 } 92 template<class T> 93 void Link<T>::show_link() { 94 printf("show_link:\n"); 95 p=head; 96 while(p!=NULL) { 97 p->show_node(); 98 p=p->next; 99 } 100 printf("\n"); 101 } 102 103 104 int Matrix::get_random_num(int x) { 105 int n=rand()%x; 106 return n; 107 } 108 void Matrix::print_matrix() { 109 printf("matrix:\n"); 110 for(int i=0; i<N; i++) { 111 for(int j=0; j<N; j++) { 112 printf("%d",matrix[i][j]); 113 } 114 printf("\n"); 115 } 116 } 117 118 bool Matrix::random_set_one() { 119 int x=get_random_num(N),y=get_random_num(N); 120 if(matrix[x][y]!=BLANK) { 121 return false; 122 } 123 Point *p=new Point(x,y); 124 set_flag(p,get_random_num(COLOR_NUM)+1); 125 return true; 126 } 127 bool Matrix::is_out(int x,int y) { 128 return x<0|x>=N|y<0|y>=N; 129 } 13

以上是关于GLine游戏(Win32GUI实现,CodeBlocks+GCC编译)的主要内容,如果未能解决你的问题,请参考以下文章

无法加载 pywin32 库 win32gui

python win32gui模块下载

python实战教程之自动扫雷

如何使用“pip”安装“win32gui”?

win32gui:如何获取窗口的状态栏文本?

win32gui.FindWindow 找不到窗口