diff --git a/snake_09_cpp/Makefile.win b/snake_09_cpp/Makefile.win new file mode 100644 index 0000000..72e1eaf --- /dev/null +++ b/snake_09_cpp/Makefile.win @@ -0,0 +1,33 @@ +# Project: ̰ +# Makefile created by Dev-C++ 4.9.9.2 + +CPP = g++.exe -D__DEBUG__ +CC = gcc.exe -D__DEBUG__ +WINDRES = windres.exe +RES = ̰_private.res +OBJ = main.o $(RES) +LINKOBJ = main.o $(RES) +LIBS = -L"C:/Program Files/DEV-CPP/Lib" -mwindows -lgmon -pg -g3 +INCS = -I"C:/Program Files/DEV-CPP/include" +CXXINCS = -I"C:/Program Files/DEV-CPP/lib/gcc/mingw32/3.4.2/include" -I"C:/Program Files/DEV-CPP/include/c++/3.4.2/backward" -I"C:/Program Files/DEV-CPP/include/c++/3.4.2/mingw32" -I"C:/Program Files/DEV-CPP/include/c++/3.4.2" -I"C:/Program Files/DEV-CPP/include" +BIN = ̰.exe +CXXFLAGS = $(CXXINCS) -pg -g3 +CFLAGS = $(INCS) -pg -g3 +RM = rm -f + +.PHONY: all all-before all-after clean clean-custom + +all: all-before ̰.exe all-after + + +clean: clean-custom + ${RM} $(OBJ) $(BIN) + +$(BIN): $(OBJ) + $(CPP) $(LINKOBJ) -o "̰.exe" $(LIBS) + +main.o: main.cpp + $(CPP) -c main.cpp -o main.o $(CXXFLAGS) + +̰_private.res: ̰_private.rc + $(WINDRES) -i ̰_private.rc --input-format=rc -o ̰_private.res -O coff diff --git a/snake_09_cpp/README.md b/snake_09_cpp/README.md new file mode 100644 index 0000000..e61074d --- /dev/null +++ b/snake_09_cpp/README.md @@ -0,0 +1,7 @@ +# snake_09_cpp +该目录下为29大约在2009年(14岁)左右的时候使用Dev C++开发的Windows Only的贪吃蛇游戏,由于年代久远了,Dev C++已经在Win10上跑不起来了,所以现在重新上传至Github的时候也只是根据找到的历史存档直接上传上来的,并没有在如今这个时代重新进行编译和测试。因此对于依然还想尝试build from source的朋友们,我只能说……祝你好运…… + +当然,如果只是想玩一玩的话,csdn和pudn上都有我当年上传的,包含有预编译二进制的压缩包,大家可以到这些地方去下载来直接玩耍: + +- https://download.csdn.net/download/czy_133/1742022 +- http://www.pudn.com/Download/item/id/1064306.html diff --git a/snake_09_cpp/main.cpp b/snake_09_cpp/main.cpp new file mode 100644 index 0000000..80e478b --- /dev/null +++ b/snake_09_cpp/main.cpp @@ -0,0 +1,379 @@ +//̰ + +#include +#include +#include +#include + +#define WIDTH 20 +#define HEIGHT 20 + +using namespace std; + +bool CheckNode (int,int,bool); //ͷβײ +void SnakeMove (void); //ߵƶ + +LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM); //ڹ̺ + +list m_snake; list m_key; //Լ뻺 +int direction = VK_LEFT,score = 0; //ǰ򼰵÷ +const int speed = 250; //ٶ +bool is_quickmove = false; //ĿǰǷڿƶ֮ +RECT rc_node,food,rc; //ʳPĻνṹ +HBRUSH red,green,blue; //õĻˢ + +int WINAPI WinMain (HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPSTR lpCmdLine, + int nCmdShow) + +{ + RECT window; // + DWORD style = WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE; //бб߿йرհťķ + HWND hwnd; //ھ + MSG messages; // + WNDCLASSEX wincl; // + + wincl.hInstance = hInstance; + wincl.lpszClassName = "CZY_Jerry"; + wincl.lpfnWndProc = WndProc; + wincl.style = 0; + wincl.cbSize = sizeof (WNDCLASSEX); + wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); + wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); + wincl.hCursor = LoadCursor (NULL, IDC_ARROW); + wincl.lpszMenuName = NULL; + wincl.cbClsExtra = 0; + wincl.cbWndExtra = 0; + wincl.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); + + if (!RegisterClassEx (&wincl)) + return 0; + + window.left = 0; + window.right = 500; + window.top = 0; + window.bottom = 500; + + AdjustWindowRect (&window,style,false); + + srand ((unsigned) time (NULL)); // + + /**/ + hwnd = CreateWindow ( + "CZY_Jerry", + "̰", + style, + 0, + 0, + window.right - window.left, + window.bottom - window.top, + NULL, + NULL, + hInstance, + NULL + ); + + if (!hwnd) + { + MessageBox (hwnd,"޷ڣ","ERROR",MB_OK | MB_ICONERROR); + return 0; + } + + SetWindowPos (hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE); //ö + ShowWindow (hwnd, nCmdShow); //ʾ + UpdateWindow (hwnd); //´ + + while (1) + { + if (GetMessage (&messages, NULL, 0, 0)) + { + TranslateMessage(&messages); //Ϣ + DispatchMessage(&messages); //ôڹ̺ + } + else break; + } + + return messages.wParam; +} + +LRESULT CALLBACK WndProc (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) +{ + HDC hdc,mdc; //ʵDCDC + char str[30] = ""; // + bool flag = true; + list::iterator i_key; //뻺ĵ + list::iterator i_snake,i_temp; //ĵ + HBITMAP hbmp; // + PAINTSTRUCT ps; // + + switch (message) + { + case WM_CREATE: + m_snake.clear();// + m_key.clear();//ռ뻺 + + /*ʼʳνṹ*/ + rc_node.top = 300; + rc_node.bottom = rc_node.top + HEIGHT; + + for (int i=5;i>=0;i--) + { + rc_node.left = 300 + i * WIDTH; + rc_node.right = rc_node.left + WIDTH; + m_snake.push_front (rc_node); + } + + m_key.push_back (VK_RIGHT); + m_key.push_back (0); + + do + { + food.left = rand() % ((500 - WIDTH) / WIDTH) * WIDTH; + food.top = rand() % ((500 - HEIGHT) / HEIGHT) * HEIGHT; + } while (CheckNode (food.left,food.top,true)); + + food.right = food.left + WIDTH; + food.bottom = food.top + HEIGHT; + + /*ˢ*/ + red = CreateSolidBrush (RGB (255,0,0)); + green = CreateSolidBrush (RGB (0,255,0)); + blue = CreateSolidBrush (RGB (0,0,255)); + + /*ʱ*/ + SetTimer (hwnd,1,speed,NULL); + break; + + case WM_PAINT: //ʳ + hdc = BeginPaint (hwnd,&ps); + mdc = CreateCompatibleDC (hdc); + GetClientRect (hwnd,&rc); + hbmp = CreateCompatibleBitmap (hdc,rc.right - rc.left,rc.bottom - rc.top); + SelectObject (mdc,hbmp); + FillRect (mdc,&rc,(HBRUSH) GetStockObject (WHITE_BRUSH)); + + FillRect (mdc,&food,blue); + + i_snake = m_snake.begin(); + FillRect (mdc,&(*i_snake),red); + i_snake++; + + for (;i_snake != m_snake.end();i_snake++) + FillRect (mdc,&(*i_snake),green); + + BitBlt (hdc,0,0,rc.right - rc.top,rc.bottom - rc.top,mdc,0,0,SRCCOPY); + + EndPaint (hwnd,&ps); + DeleteDC (mdc); + DeleteObject (hbmp); + break; + + case WM_TIMER: + hdc = GetDC (hwnd); + SnakeMove(); //ƶ + + if (m_snake.begin() -> left == food.left && m_snake.begin() -> top == food.top) + { + do + { + food.left = rand() % ((500 - WIDTH) / WIDTH) * WIDTH; + food.top = rand() % ((500 - HEIGHT) / HEIGHT) * HEIGHT; + } while (CheckNode (food.left,food.top,true)); + + food.right = food.left + WIDTH; + food.bottom = food.top + HEIGHT; + score++; + } + else + { + i_temp = m_snake.end(); + i_temp--; + m_snake.erase (i_temp); + } + + m_key.push_back (0); + + /*ײԼ*/ + if (CheckNode (m_snake.begin() -> left,m_snake.begin() -> top,false)) + { + if (is_quickmove) KillTimer (hwnd,2); + else KillTimer (hwnd,1); + + sprintf (str,"Ϸ\nĵ÷֣%d",score); + MessageBox (hwnd,str,"ʾ",MB_OK | MB_ICONASTERISK | MB_TOPMOST); + + DestroyWindow (hwnd); + return 0; + } + else InvalidateRect (hwnd,NULL,false); //ǿˢ¿ͻ + + ReleaseDC (hwnd,hdc); //ͷDC + break; + + case WM_KEYDOWN: + if ((int) wParam == VK_ESCAPE) + { + DestroyWindow (hwnd); + return 0; + } + i_key = m_key.begin(); + + /*ƶ*/ + if (direction == (int) wParam) + { + if (! is_quickmove) + { + is_quickmove = true; + KillTimer (hwnd,1); + SetTimer (hwnd,2,speed/4,NULL); + } + } + /*ƶ*/ + else if (is_quickmove) + { + is_quickmove = false; + KillTimer (hwnd,2); + SetTimer (hwnd,1,speed,NULL); + } + + //жƶһ֮ڣ̰Ƿ෴Ԫس + for (;i_key != m_key.end();i_key++) + { + if ((int) wParam != (*i_key)) flag = true; + else + { + flag = false; + break; + } + } + + if (flag) //û + { + direction = (int) wParam; + //m_key.erase (m_key.end()); + + switch (direction) + { + case VK_UP: + m_key.push_back (VK_DOWN); + break; + case VK_DOWN: + m_key.push_back (VK_UP); + break; + case VK_LEFT: + m_key.push_back (VK_RIGHT); + break; + case VK_RIGHT: + m_key.push_back (VK_LEFT); + } + + m_key.push_back (0); + } + break; + + case WM_KEYUP: + /*ƶ*/ + if (is_quickmove) + { + is_quickmove = false; + KillTimer (hwnd,2); + SetTimer (hwnd,1,speed,NULL); + } + break; + + case WM_CLOSE: + DestroyWindow (hwnd); + return 0; + + case WM_DESTROY: + PostQuitMessage (0); + return 0; + + default: + return DefWindowProc (hwnd,message,wParam,lParam); + } + + return 0; +} + +bool CheckNode (int x,int y,bool flag) //ײ +{ + list::iterator i_s = m_snake.begin(); + if (! flag) + i_s++++++; + + for (;i_s != m_snake.end();i_s++) + if (i_s -> left == x && i_s -> top == y) return true; + + return false; +} + +void SnakeMove () //ƶ +{ + m_key.clear(); + + switch (direction) + { + case VK_UP: + if (rc_node.top <= 0) + { + rc_node.top = 500 - HEIGHT; + rc_node.bottom = 500; + } + else + { + rc_node.top -= HEIGHT; + rc_node.bottom -= HEIGHT; + } + + m_key.push_back (VK_DOWN); + break; + + case VK_DOWN: + if (rc_node.bottom >= 500) + { + rc_node.top = 0; + rc_node.bottom = HEIGHT; + } + else + { + rc_node.top += HEIGHT; + rc_node.bottom += HEIGHT; + } + + m_key.push_back (VK_UP); + break; + + case VK_LEFT: + if (rc_node.left <= 0) + { + rc_node.left = 500 - WIDTH; + rc_node.right = 500; + } + else + { + rc_node.left -= WIDTH; + rc_node.right -= WIDTH; + } + + m_key.push_back (VK_RIGHT); + break; + + case VK_RIGHT: + if (rc_node.right >= 500) + { + rc_node.left = 0; + rc_node.right = WIDTH; + } + else + { + rc_node.left += WIDTH; + rc_node.right += WIDTH; + } + + m_key.push_back (VK_LEFT); + } + + m_snake.push_front (rc_node); +} diff --git "a/snake_09_cpp/\350\264\252\345\220\203\350\233\207.dev" "b/snake_09_cpp/\350\264\252\345\220\203\350\233\207.dev" new file mode 100644 index 0000000..1cb095b --- /dev/null +++ "b/snake_09_cpp/\350\264\252\345\220\203\350\233\207.dev" @@ -0,0 +1,59 @@ +[Project] +FileName=̰.dev +Name=̰ +UnitCount=1 +Type=0 +Ver=1 +ObjFiles= +Includes= +Libs= +PrivateResource=̰_private.rc +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +IsCpp=1 +Icon=̰.ico +ExeOutput= +ObjectOutput= +OverrideOutput=0 +OverrideOutputName=̰.exe +HostApplication= +Folders= +CommandLine= +UseCustomMakefile=0 +CustomMakefile= +IncludeVersionInfo=1 +SupportXPThemes=0 +CompilerSet=0 +CompilerSettings=0000000000000101000000 + +[Unit1] +FileName=main.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[VersionInfo] +Major=1 +Minor=1 +Release=1 +Build=1 +LanguageID=2052 +CharsetID=1252 +CompanyName=CZY_JERRYST_Һæ +FileVersion=V1.1 +FileDescription=̰ +InternalName=̰ +LegalCopyright= +LegalTrademarks= +OriginalFilename=̰ +ProductName=̰ +ProductVersion=V1.1 +AutoIncBuildNr=0 + diff --git "a/snake_09_cpp/\350\264\252\345\220\203\350\233\207.ico" "b/snake_09_cpp/\350\264\252\345\220\203\350\233\207.ico" new file mode 100644 index 0000000..14b81e2 Binary files /dev/null and "b/snake_09_cpp/\350\264\252\345\220\203\350\233\207.ico" differ diff --git "a/snake_09_cpp/\350\264\252\345\220\203\350\233\207_private.h" "b/snake_09_cpp/\350\264\252\345\220\203\350\233\207_private.h" new file mode 100644 index 0000000..1d55bc6 --- /dev/null +++ "b/snake_09_cpp/\350\264\252\345\220\203\350\233\207_private.h" @@ -0,0 +1,23 @@ +/* THIS FILE WILL BE OVERWRITTEN BY DEV-C++ */ +/* DO NOT EDIT ! */ + +#ifndef ̰_PRIVATE_H +#define ̰_PRIVATE_H + +/* VERSION DEFINITIONS */ +#define VER_STRING "1.1.1.1" +#define VER_MAJOR 1 +#define VER_MINOR 1 +#define VER_RELEASE 1 +#define VER_BUILD 1 +#define COMPANY_NAME "CZY_JERRYST_Һæ" +#define FILE_VERSION "V1.1" +#define FILE_DESCRIPTION "̰" +#define INTERNAL_NAME "̰" +#define LEGAL_COPYRIGHT "" +#define LEGAL_TRADEMARKS "" +#define ORIGINAL_FILENAME "̰" +#define PRODUCT_NAME "̰" +#define PRODUCT_VERSION "V1.1" + +#endif /*̰_PRIVATE_H*/ diff --git "a/snake_09_cpp/\350\264\252\345\220\203\350\233\207_private.rc" "b/snake_09_cpp/\350\264\252\345\220\203\350\233\207_private.rc" new file mode 100644 index 0000000..d8cb2d9 --- /dev/null +++ "b/snake_09_cpp/\350\264\252\345\220\203\350\233\207_private.rc" @@ -0,0 +1,37 @@ +/* THIS FILE WILL BE OVERWRITTEN BY DEV-C++ */ +/* DO NOT EDIT! */ + +#include // include for version info constants + + +A ICON MOVEABLE PURE LOADONCALL DISCARDABLE "̰.ico" + +// +// TO CHANGE VERSION INFORMATION, EDIT PROJECT OPTIONS... +// +1 VERSIONINFO +FILEVERSION 1,1,1,1 +PRODUCTVERSION 1,1,1,1 +FILETYPE VFT_APP +{ + BLOCK "StringFileInfo" + { + BLOCK "080404E4" + { + VALUE "CompanyName", "CZY_JERRYST_Һæ" + VALUE "FileVersion", "V1.1" + VALUE "FileDescription", "̰" + VALUE "InternalName", "̰" + VALUE "LegalCopyright", "" + VALUE "LegalTrademarks", "" + VALUE "OriginalFilename", "̰" + VALUE "ProductName", "̰" + VALUE "ProductVersion", "V1.1" + } + } + BLOCK "VarFileInfo" + { + VALUE "Translation", 0x0804, 1252 + } +} +