diff options
| author | Patrick Schönberger | 2020-09-26 13:49:45 +0200 |
|---|---|---|
| committer | Patrick Schönberger | 2020-09-26 13:49:45 +0200 |
| commit | 19140d850bf8dc6623331478226459d462185cb8 (patch) | |
| tree | 0bbbb2356bd3043123800e008b8847f9a5c0bdce | |
| parent | c47a4ede7263d01732182b3393b6a20163b8e2eb (diff) | |
| download | npengine-19140d850bf8dc6623331478226459d462185cb8.tar.gz npengine-19140d850bf8dc6623331478226459d462185cb8.zip | |
success
| -rw-r--r-- | lvl/1.txt | 58 | ||||
| -rw-r--r-- | src/main.cpp | 277 |
2 files changed, 211 insertions, 124 deletions
@@ -1,27 +1,31 @@ -┌-------------------------------------------┐
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| |
-| ? ? |
-| |
-| |
-| xxx xxx O |
-| xxxxx|
-| xxxxxxxxxxx|
-| xxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx|
-| xxxxxxxxxxxxxxxxx/\/\xxxxxxxxxxxxxxxx|
-└-------------------------------------------┘
\ No newline at end of file ++--------------------------------------------------------+
+| |
+| |
+| |
+| |
+| |
+| |
+| |
+| |
+| S |
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- -XXXXXXXXXXXXXXXXXXXXXXXXX|
+|XXXXXXXXXXXXXXXXXXXXXXXXXX- O -XXXXXXXXXXXXXXXXXXXXXXXXX|
++--------------------------------------------------------+
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c0272ac..4c7d99d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,10 @@ #define DIRECTINPUT_VERSION 0x0800
-#include <windows.h>
#include <dinput.h>
+#include <fstream>
#include <stdio.h>
-#include <time.h>
#include <string>
-#include <fstream>
+#include <time.h>
+#include <windows.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "dinput8.lib")
@@ -12,15 +12,20 @@ HWND hwnd = NULL;
-const int WIDTH = 43, HEIGHT = 25;
+STARTUPINFOA si;
+PROCESS_INFORMATION pi;
+
+const int WIDTH = 56, HEIGHT = 29;
int x = 0, y = 0;
+int spawn_x, spawn_y;
bool right = true;
clock_t update_clock = clock();
double update_time = 40;
-bool keys[4] = { false, false, false, false };
-bool keys_old[4] = { false, false, false, false };
+BYTE dikeys[256];
+bool keys[4] = {false, false, false, false};
+bool keys_old[4] = {false, false, false, false};
int jumping = 0;
@@ -31,6 +36,10 @@ int jump_height = 3; DWORD wait_time = 10;
+int text_speed = 2;
+
+std::string map;
+
void press_down(WORD vk) {
INPUT ip;
@@ -51,6 +60,7 @@ void press_up(WORD vk) { ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
+ ip.ki.wVk = vk;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}
@@ -58,12 +68,9 @@ void press_up(WORD vk) { void press(WORD vk) {
press_down(vk);
press_up(vk);
- GetAsyncKeyState(vk);
}
void key_down(char c) {
- if (hwnd == NULL)
- puts("oh no");
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
@@ -76,8 +83,6 @@ void key_down(char c) { }
void key_up(char c) {
- if (hwnd == NULL)
- puts("oh no");
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
@@ -120,8 +125,9 @@ void destroydikeyboard() { #define keydown(name, key) (name[key] & 0x80)
-std::string read_map() {
- std::ifstream ifs("lvl/1.txt", std::ios::in | std::ios::binary);
+std::string read_map(int lvl) {
+ std::ifstream ifs("lvl/" + std::to_string(lvl) + ".txt",
+ std::ios::in | std::ios::binary);
if (!ifs.good())
puts("mist");
ifs.seekg(0, std::ios::end);
@@ -135,10 +141,9 @@ std::string read_map() { delete buffer;
return result;
}
-std::string map = read_map();
char get_block(int x, int y) {
- char result = map[WIDTH + 8 + y * (WIDTH + 4) + x + 1];
+ char result = map[(y + 1) * (WIDTH + 4) + x + 1];
return result;
}
@@ -176,33 +181,49 @@ void move(int dx, int dy) { }
draw();
-
+
x += dx;
y += dy;
-
- printf("%d %d\n", x, y);
-}
-void move_to(int _x, int _y) {
- move(_x - x, _y - y);
+ printf("%d %d\n", x, y);
}
-void print_text(int text_x, int text_y, const char *text, int delay) {
- for (int i = x; i < text_x; i++) press(VK_RIGHT);
- for (int i = x; i > text_x; i--) press(VK_LEFT);
- for (int i = y; i < text_y; i++) press(VK_DOWN);
- for (int i = y; i > text_y; i--) press(VK_UP);
+void move_to(int _x, int _y) { move(_x - x, _y - y); }
+
+void print_text(int text_x, int text_y, const char *text, int delay,
+ bool move = true) {
+ if (move) {
+ for (int i = x; i < text_x; i++)
+ press(VK_RIGHT);
+ for (int i = x; i > text_x; i--)
+ press(VK_LEFT);
+ for (int i = y; i < text_y; i++)
+ press(VK_DOWN);
+ for (int i = y; i > text_y; i--)
+ press(VK_UP);
+ }
int len = strlen(text);
for (int i = 0; i < len; i++) {
+ if (text[i] == '\r')
+ continue;
press(VK_DELETE);
key(text[i]);
+ if (move) {
+ map[(text_y + 1) * (WIDTH + 4) + text_x + 1 + i] = text[i];
+ }
Sleep(delay);
}
- for (int i = text_x + len; i < x; i++) press(VK_RIGHT);
- for (int i = text_x + len; i > x; i--) press(VK_LEFT);
- for (int i = text_y; i < y; i++) press(VK_DOWN);
- for (int i = text_y; i > y; i--) press(VK_UP);
+ if (move) {
+ for (int i = text_x + len; i < x; i++)
+ press(VK_RIGHT);
+ for (int i = text_x + len; i > x; i--)
+ press(VK_LEFT);
+ for (int i = text_y; i < y; i++)
+ press(VK_DOWN);
+ for (int i = text_y; i > y; i--)
+ press(VK_UP);
+ }
draw();
@@ -213,13 +234,9 @@ void update_play(bool can_jump = true, int x_min = 0, int x_max = WIDTH - 1) { if (get_dur(update_clock) >= update_time) {
update_clock = clock();
- if (keys[0] &&
- x > x_min &&
- get_block(x - 1, y) != 'x')
+ if (keys[0] && x > x_min && get_block(x - 1, y) != 'X')
move(-1, 0);
- if (keys[1] &&
- x < x_max &&
- get_block(x + 1, y) != 'x')
+ if (keys[1] && x < x_max && get_block(x + 1, y) != 'X')
move(+1, 0);
}
@@ -230,78 +247,161 @@ void update_play(bool can_jump = true, int x_min = 0, int x_max = WIDTH - 1) { }
if (jumping != 0) {
if (jumping < jump_height && get_dur(jump_clock) > jump_time1) {
- if (get_block(x, y - 1) != 'x') {
- move(0, -1);
- jumping++;
- jump_clock = clock();
+ if (get_block(x, y - 1) != 'X') {
+ move(0, -1);
+ jumping++;
+ jump_clock = clock();
} else {
jumping = jump_height;
}
- }
- else if (jumping == jump_height && get_dur(jump_clock) > jump_time2) {
+ } else if (jumping == jump_height && get_dur(jump_clock) > jump_time2) {
jumping = 0;
}
}
- if (!jumping && get_block(x, y + 1) != 'x' && y < HEIGHT - 1)
- move(0, +1);
+ if (!jumping && get_block(x, y + 1) != 'X' && y < HEIGHT - 1)
+ move(0, +1);
+
+ char b = get_block(x, y);
+ if (b == '/' || b == '\\' || b == '-')
+ move_to(spawn_x, spawn_y);
+}
+
+void redraw() {
+ press_down(VK_CONTROL);
+ press('A');
+ press_up(VK_CONTROL);
+ Sleep(100);
+ press(VK_DELETE);
+ Sleep(100);
+ int _x = x, _y = y;
+ print_text(0, 0, map.c_str(), 1, false);
+ for (int i = 0; i < 100; i++)
+ press(VK_UP);
+ for (int i = 0; i < 100; i++)
+ press(VK_LEFT);
+ x = y = 0;
+ press(VK_DOWN);
+ press(VK_RIGHT);
+ press(VK_RIGHT);
+ move_to(_x, _y);
}
int lvl = 0;
+void setup() {
+ x = y = 0;
+ press(VK_DOWN);
+ press(VK_RIGHT);
+ press(VK_RIGHT);
+
+ for (int i = 0; i < WIDTH; i++)
+ for (int j = 0; j < HEIGHT; j++)
+ if (get_block(i, j) == 'S') {
+ spawn_x = i;
+ spawn_y = j;
+ }
+
+ move_to(spawn_x, spawn_y);
+}
+
+void load_level(int l, bool terminate = true) {
+ lvl = l;
+ map = read_map(lvl);
+ if (terminate)
+ TerminateProcess(pi.hProcess, 0);
+
+ char cmd[100];
+ sprintf(cmd, "notepad.exe lvl/%d.txt", lvl);
+
+ if (!CreateProcessA(NULL, // No module name (use command line)
+ cmd, // Command line
+ NULL, // Process handle not inheritable
+ NULL, // Thread handle not inheritable
+ FALSE, // Set handle inheritance to FALSE
+ 0, // No creation flags
+ NULL, // Use parent's environment block
+ NULL, // Use parent's starting directory
+ &si, // Pointer to STARTUPINFO structure
+ &pi) // Pointer to PROCESS_INFORMATION structure
+ ) {
+ printf("CreateProcess failed (%d).\n", GetLastError());
+ return;
+ }
+
+ Sleep(100);
+
+ char title[100];
+ sprintf(title, "%d.txt - Editor", lvl);
+ hwnd = FindWindowA(NULL, title);
+
+ setup();
+}
+
void intro() {
static int progress = 0;
switch (progress) {
case 0:
- press(VK_DOWN);
- press(VK_RIGHT);
- press(VK_RIGHT);
-
- move_to(1, 24);
-
- print_text(4, 2, "Move with left/right.", 30);
+ print_text(4, 2, "Move with left/right.", text_speed);
progress++;
break;
case 1:
update_play(false);
if (x == 5) {
- print_text(4, 4, "Jump with up.", 30);
- print_text(4, 6, "Stand on x.", 30);
+ print_text(4, 4, "Jump with up.", text_speed);
+ print_text(4, 6, "Stand on x.", text_speed);
progress++;
}
break;
case 2:
update_play();
if (x == 8) {
- print_text(4, 8, "Collect ? for ???.", 30);
+ print_text(4, 8, "Collect ? for ???.", text_speed);
progress++;
}
break;
case 3:
update_play(true, 0, 22);
if (get_block(x, y) == '?') {
- print_text(4, 10, "Avoid /\\.", 30);
+ print_text(4, 10, "Avoid /\\.", text_speed);
progress++;
}
break;
case 4:
update_play();
+ if (x == 39) {
+ print_text(4, 14, "Finish lvl by reaching O.", text_speed);
+ progress++;
+ }
+ break;
+ case 5:
+ update_play();
+ if (get_block(x, y) == 'O') {
+ load_level(1);
+ }
break;
}
}
+void lvl1() { update_play(); }
+
void update_game() {
switch (lvl) {
case 0:
intro();
break;
+ case 1:
+ lvl1();
+ break;
}
}
/*
Todo:
- - Restart
- - Next Level
+ - Msg Box Intro
+ - Multi Jump
+ - more blocks/lvls
+ - Set Accessibility
*/
int main(int argc, char **argv) {
// Dies zu programmieren mit der reduzierten Inputrate.
@@ -310,56 +410,39 @@ int main(int argc, char **argv) { // printf("%c", get_block(6, 23));
// printf("%c", get_block(6, 24));
- STARTUPINFOA si;
- PROCESS_INFORMATION pi;
-
- ZeroMemory( &si, sizeof(si) );
+ ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
- ZeroMemory( &pi, sizeof(pi) );
-
- //MessageBoxA(NULL, "Guten Tag.", "Spiel Name???", MB_OK);
-
- // Start the child process.
- if( !CreateProcessA( NULL, // No module name (use command line)
- "notepad.exe lvl/1.txt", // Command line
- NULL, // Process handle not inheritable
- NULL, // Thread handle not inheritable
- FALSE, // Set handle inheritance to FALSE
- 0, // No creation flags
- NULL, // Use parent's environment block
- NULL, // Use parent's starting directory
- &si, // Pointer to STARTUPINFO structure
- &pi ) // Pointer to PROCESS_INFORMATION structure
- )
- {
- printf( "CreateProcess failed (%d).\n", GetLastError() );
- return 0;
- }
-
- Sleep(100);
+ ZeroMemory(&pi, sizeof(pi));
- hwnd = FindWindowA(NULL, "1.txt - Editor");
+ // MessageBoxA(NULL, "Guten Tag.", "Spiel Name???", MB_OK);
HRESULT hr;
- BYTE dikeys[256];
initializedirectinput8();
createdikeyboard();
+ load_level(0, false);
+
MSG Msg;
- while (true) {
+ while (true) {
hr = keyboard->GetDeviceState(256, dikeys);
if (keydown(dikeys, DIK_ESCAPE)) {
TerminateProcess(pi.hProcess, 0);
- //MessageBoxA(NULL, "beendet...", "Schönes Wochenende.", MB_OK);
- break;
- }
+ // MessageBoxA(NULL, "beendet...", "Schönes Wochenende.", MB_OK);
+ break;
+ }
+ if (keydown(dikeys, DIK_R)) {
+ redraw();
+ }
keys[0] = keydown(dikeys, DIK_LEFTARROW);
keys[1] = keydown(dikeys, DIK_RIGHTARROW);
keys[2] = keydown(dikeys, DIK_UPARROW);
- if (keys[0] && !keys_old[0]) press(VK_RIGHT);
- if (keys[1] && !keys_old[1]) press(VK_LEFT);
- if (keys[2] && !keys_old[2]) press(VK_DOWN);
+ if (keys[0] && !keys_old[0])
+ press(VK_RIGHT);
+ if (keys[1] && !keys_old[1])
+ press(VK_LEFT);
+ if (keys[2] && !keys_old[2])
+ press(VK_DOWN);
update_game();
@@ -368,16 +451,16 @@ int main(int argc, char **argv) { keys_old[2] = keys[2];
keys_old[3] = keys[3];
- WaitForSingleObject( pi.hProcess, wait_time);
+ WaitForSingleObject(pi.hProcess, wait_time);
- SetWindowPos(hwnd, HWND_TOPMOST, 100, 100, 750, 750, SWP_SHOWWINDOW);
+ SetWindowPos(hwnd, HWND_TOPMOST, 100, 100, 500, 500, SWP_SHOWWINDOW);
}
destroydikeyboard();
- // Close process and thread handles.
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
+ // Close process and thread handles.
+ CloseHandle(pi.hProcess);
+ CloseHandle(pi.hThread);
return 0;
}
\ No newline at end of file |
