-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5c454f
commit f2bf420
Showing
4 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
#include <windows.h> | ||
#include <commctrl.h> | ||
#include <cmath> | ||
#include <iostream> | ||
#include <stdio.h> | ||
#include <tchar.h> | ||
#include <memoryapi.h> | ||
#include <time.h> | ||
#include <stdlib.h> | ||
#include <vector> | ||
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) | ||
#define KEY_UP(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 0:1) | ||
#define DIRECT_NONE 0x0F | ||
#define DIRECT_UP 0x01 | ||
#define DIRECT_DOWN 0x02 | ||
#define DIRECT_LEFT 0x04 | ||
#define DIRECT_RIGHT 0x08 | ||
#define SNAKE_BODY 2 | ||
#define SNAKE_HEADER 1 | ||
#define NOT_SNAKE 0 | ||
#define NewGame() \ | ||
do{\ | ||
snakeLen = 1;\ | ||
snakeTailIndex = 0;\ | ||
snakeHeaderIndex = 0;\ | ||
for (int i = 0; i < iconCnt; i++)\ | ||
{\ | ||
snakes[i].x = 0;\ | ||
snakes[i].y = 0;\ | ||
ListView_SetItemPosition(hSysListView32, i, -100, -100);\ | ||
}\ | ||
ListView_SetItemPosition(hSysListView32, 0, offsetX, offsetY); \ | ||
isSnake = std::vector<std::vector<int>>(blockW, std::vector<int>(blockH, NOT_SNAKE));\ | ||
}\ | ||
while(0) | ||
#define X2P(pos) (pos * stepX + offsetX) | ||
#define Y2P(pos) (pos * stepY + offsetY) | ||
int delay = 100; | ||
int stepX = 80; | ||
int stepY = 80; | ||
bool playing = false; | ||
int moveDirect = DIRECT_NONE; | ||
DWORD WINAPI KeyProc(LPVOID args) | ||
{ | ||
while (true) | ||
{ | ||
if (KEY_DOWN(VK_LEFT) && moveDirect & (DIRECT_DOWN | DIRECT_UP)) | ||
{ | ||
moveDirect = DIRECT_LEFT; | ||
while (KEY_DOWN(VK_LEFT)); | ||
} | ||
else if (KEY_DOWN(VK_RIGHT) && moveDirect & (DIRECT_UP | DIRECT_DOWN)) | ||
{ | ||
moveDirect = DIRECT_RIGHT; | ||
while (KEY_DOWN(VK_RIGHT)); | ||
} | ||
else if (KEY_DOWN(VK_UP) && moveDirect & (DIRECT_LEFT | DIRECT_RIGHT)) | ||
{ | ||
moveDirect = DIRECT_UP; | ||
while (KEY_DOWN(VK_UP)); | ||
} | ||
else if (KEY_DOWN(VK_DOWN) && moveDirect & (DIRECT_LEFT | DIRECT_RIGHT)) | ||
{ | ||
moveDirect = DIRECT_DOWN; | ||
while (KEY_DOWN(VK_DOWN)); | ||
} | ||
else if (KEY_DOWN('F')) | ||
{ | ||
if (delay >= 10) { delay -= 10; } | ||
while (KEY_DOWN('F')); | ||
} | ||
else if (KEY_DOWN('S')) | ||
{ | ||
delay += 10; | ||
while (KEY_DOWN('S')); | ||
} | ||
else if (KEY_DOWN(VK_ESCAPE)) | ||
{ | ||
playing = false; | ||
} | ||
} | ||
} | ||
HWND hSysListView32 = NULL; | ||
BOOL CALLBACK FindSysListView32(HWND hwnd, LPARAM lparam) | ||
{ | ||
TCHAR a[255]; | ||
if (GetClassName(hwnd, a, 255) && lstrcmp(a, _T("WorkerW")) == 0) | ||
{ | ||
HWND hSHELLDLL_DefView = ::FindWindowEx(hwnd, NULL, "SHELLDLL_DefView", NULL); | ||
hSysListView32 = ::FindWindowEx(hSHELLDLL_DefView, NULL, "SysListView32", "FolderView"); | ||
return hSysListView32 == 0; | ||
} | ||
else | ||
{ | ||
return TRUE; | ||
} | ||
} | ||
int iconCnt = 0; | ||
PPOINT iconPrevPos; | ||
void RecoveryIcon() | ||
{ | ||
for (int i = 0; i < iconCnt; i++) | ||
{ | ||
ListView_SetItemPosition(hSysListView32, i, iconPrevPos[i].x, iconPrevPos[i].y); | ||
} | ||
ListView_RedrawItems(hSysListView32, 0, iconCnt - 1); | ||
::UpdateWindow(hSysListView32); | ||
} | ||
BOOL WINAPI BeforeExit(DWORD dwCtrlType) | ||
{ | ||
if (dwCtrlType == CTRL_CLOSE_EVENT) | ||
{ | ||
RecoveryIcon(); | ||
return TRUE; | ||
} | ||
else | ||
{ | ||
return FALSE; | ||
} | ||
} | ||
int main() | ||
{ | ||
//ShowWindow(GetForegroundWindow(),false); | ||
SetConsoleCtrlHandler(BeforeExit, TRUE); | ||
HWND hParent = ::FindWindow("Progman", "Program Manager"); | ||
HWND hSHELLDLL_DefView = ::FindWindowEx(hParent, NULL, "SHELLDLL_DefView", NULL); | ||
hSysListView32 = ::FindWindowEx(hSHELLDLL_DefView, NULL, "SysListView32", "FolderView"); | ||
if (hSysListView32 == 0) | ||
{ | ||
HWND hDesktop = GetDesktopWindow(); | ||
EnumWindows(FindSysListView32, 0); | ||
} | ||
if (hSysListView32 == 0) | ||
{ | ||
MessageBox(GetForegroundWindow(), "Initialize Fail", "Error", MB_OK); | ||
return 0; | ||
} | ||
iconCnt = ListView_GetItemCount(hSysListView32); | ||
iconPrevPos = new POINT[iconCnt]; | ||
CreateThread(NULL, 0, KeyProc, NULL, 0, 0); | ||
int realW = GetSystemMetrics(SM_CXFULLSCREEN); | ||
int realH = GetSystemMetrics(SM_CYFULLSCREEN); | ||
int blockW = realW / stepX; | ||
int blockH = realH / stepY; | ||
int offsetX = (realW - blockW * stepX) / 2; | ||
int offsetY = (realH - blockH * stepY) / 2; | ||
srand(time(0)); | ||
playing = true; | ||
DWORD processId; | ||
GetWindowThreadProcessId(hSysListView32, &processId); | ||
HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, processId); | ||
/* | ||
PVOID prect = VirtualAllocEx(hProcess, NULL, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE); | ||
RECT rect; | ||
ListView_GetItemRect(hSysListView32, 0, prect, LVIR_ICON); | ||
ReadProcessMemory(hProcess, prect, &rect, sizeof(RECT), NULL); | ||
stepX = rect.right - rect.left; | ||
stepY = rect.bottom - rect.top; | ||
VirtualFreeEx(hProcess, prect, 0, MEM_RELEASE); | ||
*/ | ||
LPVOID ori = VirtualAllocEx(hProcess, NULL, sizeof(POINT), MEM_COMMIT, PAGE_READWRITE); | ||
for (int i = 0; i < iconCnt; i++) | ||
{ | ||
ListView_GetItemPosition(hSysListView32, i, ori); | ||
ReadProcessMemory(hProcess, ori, iconPrevPos + i, sizeof(POINT), NULL); | ||
} | ||
VirtualFreeEx(hProcess, ori, 0, MEM_RELEASE); | ||
CloseHandle(hProcess); | ||
std::vector<std::vector<int>> isSnake; | ||
PPOINT snakes = new POINT[iconCnt]; | ||
int snakeLen, snakeTailIndex, snakeHeaderIndex; | ||
NewGame(); | ||
bool newFood = true; | ||
int foodBlockX = 0; | ||
int foodBlockY = 0; | ||
int headerBlockX = 0; | ||
int headerBlockY = 0; | ||
while (playing) | ||
{ | ||
if (!newFood && headerBlockX == foodBlockX && headerBlockY == foodBlockY) | ||
{ | ||
snakeLen++; | ||
std::cout << snakeLen << std::endl; | ||
if (snakeLen == iconCnt) | ||
{ | ||
char buf[255]; | ||
sprintf_s(buf, "You have get %d Point, Do you want to play again?", snakeLen); | ||
int ans = MessageBox(GetForegroundWindow(), buf, "Game Finish", MB_YESNO | MB_ICONQUESTION); | ||
if (ans == IDYES) | ||
{ | ||
NewGame(); | ||
newFood = true; | ||
} | ||
else | ||
{ | ||
break; | ||
} | ||
} | ||
newFood = true; | ||
} | ||
if (newFood) | ||
{ | ||
do | ||
{ | ||
foodBlockX = rand() % blockW; | ||
foodBlockY = rand() % blockH; | ||
} while (isSnake[foodBlockX][foodBlockY] != NOT_SNAKE); | ||
newFood = false; | ||
ListView_SetItemPosition(hSysListView32, snakeLen, X2P(foodBlockX), Y2P(foodBlockY)); | ||
std::cout << "Food Pos:" << foodBlockX << "," << foodBlockY << std::endl; | ||
} | ||
if (moveDirect != DIRECT_NONE) | ||
{ | ||
isSnake[snakes[snakeHeaderIndex].x][snakes[snakeHeaderIndex].y] = SNAKE_BODY; | ||
isSnake[snakes[snakeTailIndex].x][snakes[snakeTailIndex].y] = NOT_SNAKE; | ||
headerBlockX = snakes[snakeTailIndex].x = (snakes[snakeHeaderIndex].x | ||
+ (moveDirect == DIRECT_RIGHT ? 1 : moveDirect == DIRECT_LEFT ? -1 : 0) + blockW) % blockW; | ||
headerBlockY = snakes[snakeTailIndex].y = (snakes[snakeHeaderIndex].y | ||
+ (moveDirect == DIRECT_DOWN ? 1 : moveDirect == DIRECT_UP ? -1 : 0) + blockH) % blockH; | ||
if (isSnake[headerBlockX][headerBlockY] == SNAKE_BODY) | ||
{ | ||
char buf[255]; | ||
sprintf_s(buf, "You have get %d Point, Do you want to play again?", snakeLen); | ||
int ans = MessageBox(GetForegroundWindow(), buf, "Crashed", MB_YESNO | MB_ICONQUESTION); | ||
if (ans == IDYES) | ||
{ | ||
NewGame(); | ||
newFood = true; | ||
} | ||
else | ||
{ | ||
break; | ||
} | ||
} | ||
isSnake[headerBlockX][headerBlockY] = SNAKE_HEADER; | ||
snakeHeaderIndex = snakeTailIndex; | ||
if (snakeTailIndex == 0) { snakeTailIndex = snakeLen - 1; } | ||
else { snakeTailIndex--; } | ||
ListView_SetItemPosition(hSysListView32, snakeHeaderIndex, X2P(headerBlockX), Y2P(headerBlockY)); | ||
std::cout << "Snake Header Pos:" << headerBlockX << "," << headerBlockY << std::endl; | ||
} | ||
ListView_RedrawItems(hSysListView32, 0, iconCnt - 1); | ||
::UpdateWindow(hSysListView32); | ||
Sleep(delay); | ||
} | ||
RecoveryIcon(); | ||
delete[] snakes; | ||
delete[] iconPrevPos; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.29911.84 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Desktop Snake", "Desktop Snake.vcxproj", "{49239BAC-6D6B-46FB-9849-0A2F34057D44}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|x64 = Debug|x64 | ||
Debug|x86 = Debug|x86 | ||
Release|x64 = Release|x64 | ||
Release|x86 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{49239BAC-6D6B-46FB-9849-0A2F34057D44}.Debug|x64.ActiveCfg = Debug|x64 | ||
{49239BAC-6D6B-46FB-9849-0A2F34057D44}.Debug|x64.Build.0 = Debug|x64 | ||
{49239BAC-6D6B-46FB-9849-0A2F34057D44}.Debug|x86.ActiveCfg = Debug|Win32 | ||
{49239BAC-6D6B-46FB-9849-0A2F34057D44}.Debug|x86.Build.0 = Debug|Win32 | ||
{49239BAC-6D6B-46FB-9849-0A2F34057D44}.Release|x64.ActiveCfg = Release|x64 | ||
{49239BAC-6D6B-46FB-9849-0A2F34057D44}.Release|x64.Build.0 = Release|x64 | ||
{49239BAC-6D6B-46FB-9849-0A2F34057D44}.Release|x86.ActiveCfg = Release|Win32 | ||
{49239BAC-6D6B-46FB-9849-0A2F34057D44}.Release|x86.Build.0 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {2A2122E3-96B6-4B90-909E-C00C285E64A7} | ||
EndGlobalSection | ||
EndGlobal |
Oops, something went wrong.