#define DIRECTINPUT_VERSION 0x0800\r
-#include <windows.h>\r
#include <dinput.h>\r
+#include <fstream>\r
#include <stdio.h>\r
-#include <time.h>\r
#include <string>\r
-#include <fstream>\r
+#include <time.h>\r
+#include <windows.h>\r
\r
#pragma comment(lib, "user32.lib")\r
#pragma comment(lib, "dinput8.lib")\r
\r
HWND hwnd = NULL;\r
\r
-const int WIDTH = 43, HEIGHT = 25;\r
+STARTUPINFOA si;\r
+PROCESS_INFORMATION pi;\r
+\r
+const int WIDTH = 56, HEIGHT = 29;\r
int x = 0, y = 0;\r
+int spawn_x, spawn_y;\r
bool right = true;\r
\r
clock_t update_clock = clock();\r
double update_time = 40;\r
\r
-bool keys[4] = { false, false, false, false };\r
-bool keys_old[4] = { false, false, false, false };\r
+BYTE dikeys[256];\r
+bool keys[4] = {false, false, false, false};\r
+bool keys_old[4] = {false, false, false, false};\r
\r
int jumping = 0;\r
\r
\r
DWORD wait_time = 10;\r
\r
+int text_speed = 2;\r
+\r
+std::string map;\r
+\r
void press_down(WORD vk) {\r
INPUT ip;\r
\r
ip.ki.time = 0;\r
ip.ki.dwExtraInfo = 0;\r
\r
+ ip.ki.wVk = vk;\r
ip.ki.dwFlags = KEYEVENTF_KEYUP;\r
SendInput(1, &ip, sizeof(INPUT));\r
}\r
void press(WORD vk) {\r
press_down(vk);\r
press_up(vk);\r
- GetAsyncKeyState(vk);\r
}\r
\r
void key_down(char c) {\r
- if (hwnd == NULL)\r
- puts("oh no");\r
INPUT ip;\r
ip.type = INPUT_KEYBOARD;\r
ip.ki.time = 0;\r
}\r
\r
void key_up(char c) {\r
- if (hwnd == NULL)\r
- puts("oh no");\r
INPUT ip;\r
ip.type = INPUT_KEYBOARD;\r
ip.ki.time = 0;\r
\r
#define keydown(name, key) (name[key] & 0x80)\r
\r
-std::string read_map() {\r
- std::ifstream ifs("lvl/1.txt", std::ios::in | std::ios::binary);\r
+std::string read_map(int lvl) {\r
+ std::ifstream ifs("lvl/" + std::to_string(lvl) + ".txt",\r
+ std::ios::in | std::ios::binary);\r
if (!ifs.good())\r
puts("mist");\r
ifs.seekg(0, std::ios::end);\r
delete buffer;\r
return result;\r
}\r
-std::string map = read_map();\r
\r
char get_block(int x, int y) {\r
- char result = map[WIDTH + 8 + y * (WIDTH + 4) + x + 1];\r
+ char result = map[(y + 1) * (WIDTH + 4) + x + 1];\r
return result;\r
}\r
\r
}\r
\r
draw();\r
- \r
+\r
x += dx;\r
y += dy;\r
- \r
- printf("%d %d\n", x, y);\r
-}\r
\r
-void move_to(int _x, int _y) {\r
- move(_x - x, _y - y);\r
+ printf("%d %d\n", x, y);\r
}\r
\r
-void print_text(int text_x, int text_y, const char *text, int delay) {\r
- for (int i = x; i < text_x; i++) press(VK_RIGHT);\r
- for (int i = x; i > text_x; i--) press(VK_LEFT);\r
- for (int i = y; i < text_y; i++) press(VK_DOWN);\r
- for (int i = y; i > text_y; i--) press(VK_UP);\r
+void move_to(int _x, int _y) { move(_x - x, _y - y); }\r
+\r
+void print_text(int text_x, int text_y, const char *text, int delay,\r
+ bool move = true) {\r
+ if (move) {\r
+ for (int i = x; i < text_x; i++)\r
+ press(VK_RIGHT);\r
+ for (int i = x; i > text_x; i--)\r
+ press(VK_LEFT);\r
+ for (int i = y; i < text_y; i++)\r
+ press(VK_DOWN);\r
+ for (int i = y; i > text_y; i--)\r
+ press(VK_UP);\r
+ }\r
\r
int len = strlen(text);\r
for (int i = 0; i < len; i++) {\r
+ if (text[i] == '\r')\r
+ continue;\r
press(VK_DELETE);\r
key(text[i]);\r
+ if (move) {\r
+ map[(text_y + 1) * (WIDTH + 4) + text_x + 1 + i] = text[i];\r
+ }\r
Sleep(delay);\r
}\r
- for (int i = text_x + len; i < x; i++) press(VK_RIGHT);\r
- for (int i = text_x + len; i > x; i--) press(VK_LEFT);\r
- for (int i = text_y; i < y; i++) press(VK_DOWN);\r
- for (int i = text_y; i > y; i--) press(VK_UP);\r
+ if (move) {\r
+ for (int i = text_x + len; i < x; i++)\r
+ press(VK_RIGHT);\r
+ for (int i = text_x + len; i > x; i--)\r
+ press(VK_LEFT);\r
+ for (int i = text_y; i < y; i++)\r
+ press(VK_DOWN);\r
+ for (int i = text_y; i > y; i--)\r
+ press(VK_UP);\r
+ }\r
\r
draw();\r
\r
if (get_dur(update_clock) >= update_time) {\r
update_clock = clock();\r
\r
- if (keys[0] &&\r
- x > x_min &&\r
- get_block(x - 1, y) != 'x')\r
+ if (keys[0] && x > x_min && get_block(x - 1, y) != 'X')\r
move(-1, 0);\r
- if (keys[1] &&\r
- x < x_max &&\r
- get_block(x + 1, y) != 'x')\r
+ if (keys[1] && x < x_max && get_block(x + 1, y) != 'X')\r
move(+1, 0);\r
}\r
\r
}\r
if (jumping != 0) {\r
if (jumping < jump_height && get_dur(jump_clock) > jump_time1) {\r
- if (get_block(x, y - 1) != 'x') {\r
- move(0, -1);\r
- jumping++;\r
- jump_clock = clock();\r
+ if (get_block(x, y - 1) != 'X') {\r
+ move(0, -1);\r
+ jumping++;\r
+ jump_clock = clock();\r
} else {\r
jumping = jump_height;\r
}\r
- }\r
- else if (jumping == jump_height && get_dur(jump_clock) > jump_time2) {\r
+ } else if (jumping == jump_height && get_dur(jump_clock) > jump_time2) {\r
jumping = 0;\r
}\r
}\r
- if (!jumping && get_block(x, y + 1) != 'x' && y < HEIGHT - 1)\r
- move(0, +1);\r
+ if (!jumping && get_block(x, y + 1) != 'X' && y < HEIGHT - 1)\r
+ move(0, +1);\r
+\r
+ char b = get_block(x, y);\r
+ if (b == '/' || b == '\\' || b == '-')\r
+ move_to(spawn_x, spawn_y);\r
+}\r
+\r
+void redraw() {\r
+ press_down(VK_CONTROL);\r
+ press('A');\r
+ press_up(VK_CONTROL);\r
+ Sleep(100);\r
+ press(VK_DELETE);\r
+ Sleep(100);\r
+ int _x = x, _y = y;\r
+ print_text(0, 0, map.c_str(), 1, false);\r
+ for (int i = 0; i < 100; i++)\r
+ press(VK_UP);\r
+ for (int i = 0; i < 100; i++)\r
+ press(VK_LEFT);\r
+ x = y = 0;\r
+ press(VK_DOWN);\r
+ press(VK_RIGHT);\r
+ press(VK_RIGHT);\r
+ move_to(_x, _y);\r
}\r
\r
int lvl = 0;\r
\r
+void setup() {\r
+ x = y = 0;\r
+ press(VK_DOWN);\r
+ press(VK_RIGHT);\r
+ press(VK_RIGHT);\r
+\r
+ for (int i = 0; i < WIDTH; i++)\r
+ for (int j = 0; j < HEIGHT; j++)\r
+ if (get_block(i, j) == 'S') {\r
+ spawn_x = i;\r
+ spawn_y = j;\r
+ }\r
+ \r
+ move_to(spawn_x, spawn_y);\r
+}\r
+\r
+void load_level(int l, bool terminate = true) {\r
+ lvl = l;\r
+ map = read_map(lvl);\r
+ if (terminate)\r
+ TerminateProcess(pi.hProcess, 0);\r
+\r
+ char cmd[100];\r
+ sprintf(cmd, "notepad.exe lvl/%d.txt", lvl);\r
+\r
+ if (!CreateProcessA(NULL, // No module name (use command line)\r
+ cmd, // Command line\r
+ NULL, // Process handle not inheritable\r
+ NULL, // Thread handle not inheritable\r
+ FALSE, // Set handle inheritance to FALSE\r
+ 0, // No creation flags\r
+ NULL, // Use parent's environment block\r
+ NULL, // Use parent's starting directory\r
+ &si, // Pointer to STARTUPINFO structure\r
+ &pi) // Pointer to PROCESS_INFORMATION structure\r
+ ) {\r
+ printf("CreateProcess failed (%d).\n", GetLastError());\r
+ return;\r
+ }\r
+\r
+ Sleep(100);\r
+\r
+ char title[100];\r
+ sprintf(title, "%d.txt - Editor", lvl);\r
+ hwnd = FindWindowA(NULL, title);\r
+\r
+ setup();\r
+}\r
+\r
void intro() {\r
static int progress = 0;\r
switch (progress) {\r
case 0:\r
- press(VK_DOWN);\r
- press(VK_RIGHT);\r
- press(VK_RIGHT);\r
-\r
- move_to(1, 24);\r
-\r
- print_text(4, 2, "Move with left/right.", 30);\r
+ print_text(4, 2, "Move with left/right.", text_speed);\r
\r
progress++;\r
break;\r
case 1:\r
update_play(false);\r
if (x == 5) {\r
- print_text(4, 4, "Jump with up.", 30);\r
- print_text(4, 6, "Stand on x.", 30);\r
+ print_text(4, 4, "Jump with up.", text_speed);\r
+ print_text(4, 6, "Stand on x.", text_speed);\r
progress++;\r
}\r
break;\r
case 2:\r
update_play();\r
if (x == 8) {\r
- print_text(4, 8, "Collect ? for ???.", 30);\r
+ print_text(4, 8, "Collect ? for ???.", text_speed);\r
progress++;\r
}\r
break;\r
case 3:\r
update_play(true, 0, 22);\r
if (get_block(x, y) == '?') {\r
- print_text(4, 10, "Avoid /\\.", 30);\r
+ print_text(4, 10, "Avoid /\\.", text_speed);\r
progress++;\r
}\r
break;\r
case 4:\r
update_play();\r
+ if (x == 39) {\r
+ print_text(4, 14, "Finish lvl by reaching O.", text_speed);\r
+ progress++;\r
+ }\r
+ break;\r
+ case 5:\r
+ update_play();\r
+ if (get_block(x, y) == 'O') {\r
+ load_level(1);\r
+ }\r
break;\r
}\r
}\r
\r
+void lvl1() { update_play(); }\r
+\r
void update_game() {\r
switch (lvl) {\r
case 0:\r
intro();\r
break;\r
+ case 1:\r
+ lvl1();\r
+ break;\r
}\r
}\r
\r
/*\r
Todo:\r
- - Restart\r
- - Next Level\r
+ - Msg Box Intro\r
+ - Multi Jump\r
+ - more blocks/lvls\r
+ - Set Accessibility \r
*/\r
int main(int argc, char **argv) {\r
// Dies zu programmieren mit der reduzierten Inputrate.\r
// printf("%c", get_block(6, 23));\r
// printf("%c", get_block(6, 24));\r
\r
- STARTUPINFOA si;\r
- PROCESS_INFORMATION pi;\r
-\r
- ZeroMemory( &si, sizeof(si) );\r
+ ZeroMemory(&si, sizeof(si));\r
si.cb = sizeof(si);\r
- ZeroMemory( &pi, sizeof(pi) );\r
- \r
- //MessageBoxA(NULL, "Guten Tag.", "Spiel Name???", MB_OK);\r
-\r
- // Start the child process. \r
- if( !CreateProcessA( NULL, // No module name (use command line)\r
- "notepad.exe lvl/1.txt", // Command line\r
- NULL, // Process handle not inheritable\r
- NULL, // Thread handle not inheritable\r
- FALSE, // Set handle inheritance to FALSE\r
- 0, // No creation flags\r
- NULL, // Use parent's environment block\r
- NULL, // Use parent's starting directory \r
- &si, // Pointer to STARTUPINFO structure\r
- &pi ) // Pointer to PROCESS_INFORMATION structure\r
- ) \r
- {\r
- printf( "CreateProcess failed (%d).\n", GetLastError() );\r
- return 0;\r
- }\r
- \r
- Sleep(100);\r
+ ZeroMemory(&pi, sizeof(pi));\r
\r
- hwnd = FindWindowA(NULL, "1.txt - Editor");\r
+ // MessageBoxA(NULL, "Guten Tag.", "Spiel Name???", MB_OK);\r
\r
HRESULT hr;\r
- BYTE dikeys[256];\r
initializedirectinput8();\r
createdikeyboard();\r
\r
+ load_level(0, false);\r
+\r
MSG Msg;\r
- while (true) {\r
+ while (true) {\r
hr = keyboard->GetDeviceState(256, dikeys);\r
if (keydown(dikeys, DIK_ESCAPE)) {\r
TerminateProcess(pi.hProcess, 0);\r
- //MessageBoxA(NULL, "beendet...", "Schönes Wochenende.", MB_OK);\r
- break;\r
- }\r
+ // MessageBoxA(NULL, "beendet...", "Schönes Wochenende.", MB_OK);\r
+ break;\r
+ }\r
+ if (keydown(dikeys, DIK_R)) {\r
+ redraw();\r
+ }\r
keys[0] = keydown(dikeys, DIK_LEFTARROW);\r
keys[1] = keydown(dikeys, DIK_RIGHTARROW);\r
keys[2] = keydown(dikeys, DIK_UPARROW);\r
\r
- if (keys[0] && !keys_old[0]) press(VK_RIGHT);\r
- if (keys[1] && !keys_old[1]) press(VK_LEFT);\r
- if (keys[2] && !keys_old[2]) press(VK_DOWN);\r
+ if (keys[0] && !keys_old[0])\r
+ press(VK_RIGHT);\r
+ if (keys[1] && !keys_old[1])\r
+ press(VK_LEFT);\r
+ if (keys[2] && !keys_old[2])\r
+ press(VK_DOWN);\r
\r
update_game();\r
\r
keys_old[2] = keys[2];\r
keys_old[3] = keys[3];\r
\r
- WaitForSingleObject( pi.hProcess, wait_time);\r
+ WaitForSingleObject(pi.hProcess, wait_time);\r
\r
- SetWindowPos(hwnd, HWND_TOPMOST, 100, 100, 750, 750, SWP_SHOWWINDOW);\r
+ SetWindowPos(hwnd, HWND_TOPMOST, 100, 100, 500, 500, SWP_SHOWWINDOW);\r
}\r
\r
destroydikeyboard();\r
\r
- // Close process and thread handles. \r
- CloseHandle( pi.hProcess );\r
- CloseHandle( pi.hThread );\r
+ // Close process and thread handles.\r
+ CloseHandle(pi.hProcess);\r
+ CloseHandle(pi.hThread);\r
\r
return 0;\r
}
\ No newline at end of file