From 355b33e21b088cea18645414ae2e48e7534f5dd6 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 13 Oct 2024 23:34:25 +0200 Subject: [PATCH] add player --- a-orig.pac | 31 ------------------------------- a.pac | 29 ++++++++++++++++++++++++++--- b-orig.pac | 28 ---------------------------- b.pac | 3 --- display.c | 30 +++++++++++++++++++++++++----- pacman.c | 36 +++++++++++++++++++++++++++++------- time.c | 10 ++++++++++ time.h | 8 ++++++++ 8 files changed, 98 insertions(+), 77 deletions(-) delete mode 100644 a-orig.pac delete mode 100644 b-orig.pac delete mode 100644 b.pac create mode 100644 time.c create mode 100644 time.h diff --git a/a-orig.pac b/a-orig.pac deleted file mode 100644 index ce4ee6b..0000000 --- a/a-orig.pac +++ /dev/null @@ -1,31 +0,0 @@ -width: 28 -height: 29 -tab: "F------------7F------------7" - "| || |" - "| F--7 F---7 || F---7 F--7 |" - "| | | | | || | | | | |" - "| L--J L---J LJ L---J L--J |" - "| |" - "| F--7 F7 F------7 F7 F--7 |" - "| L--J || L--7F--J || L--J |" - "| || || || |" - "L----7 |L--7 || F--J| F----J" - " | |F--J LJ L--7| | " - " | || || | " - " | || F------7 || | " - "-----J LJ | | LJ L-----" - " | | " - "-----7 F7 | | F7 F-----" - " | || L------J || | " - " | || || | " - " | |L--7 F7 F--J| | " - "F----J |F--J || L--7| L----7" - "| || || || |" - "| F--7 || F--JL--7 || F--7 |" - "| L--J LJ L------J LJ L--J |" - "| |" - "| F--7 F---7 F7 F---7 F--7 |" - "| | | | | || | | | | |" - "| L--J L---J || L---J L--J |" - "| || |" - "L------------JL------------J" diff --git a/a.pac b/a.pac index 18e8b04..0059ad9 100644 --- a/a.pac +++ b/a.pac @@ -1,3 +1,26 @@ -width: 28 -height: 29 -tab: "F------------7F------------7| || || F--7 F---7 || F---7 F--7 || | | | | || | | | | || L--J L---J LJ L---J L--J || || F--7 F7 F------7 F7 F--7 || L--J || L--7F--J || L--J || || || || |L----7 |L--7 || F--J| F----J | |F--J LJ L--7| | | || || | | || F------7 || | -----J LJ | | LJ L----- | | -----7 F7 | | F7 F----- | || L------J || | | || || | | |L--7 F7 F--J| | F----J |F--J || L--7| L----7| || || || || F--7 || F--JL--7 || F--7 || L--J LJ L------J LJ L--J || || F--7 F---7 F7 F---7 F--7 || | | | | || | | | | || L--J L---J || L---J L--J || || |L------------JL------------J" +width: 39 +height: 24 +tab: "F----------7 |.L-----J.| F----------7" + "|*.........| |....c....| |.........*|" + "|.F-7.F--7.| |.F-----7.| |.F--7.F-7.|" + "|.L-J.L--J.L--J.L-7 F-J.L--J.L--J.L-J.|" + "|................*| |*................|" + "|.F-7.F-----7.F---J L---7.F-----7.F-7.|" + "|.| |.L---7 |.L---------J.| F---J.| |.|" + "|.| |....*| |.............| |*....| |.|" + "|.| |.F-7.| |.F---7xF---7.| |.F-7.| |.|" + "|.L-J.| |.L-J.|F--JxL--7|.L-J.| |.L-J.|" + "|.....| |.....|| ||.....| |.....|" + "L---7.| L---7.||F7 F7||.F---J |.F---J" + "F---J.| F---J.|||L---J|||.L---7 |.L---7" + "|.....| |.....||L-----J||.....| |.....|" + "|.F-7.| |.F-7.|L-------J|.F-7.| |.F-7.|" + "|.| |.L-J.| |.L---------J.| |.L-J.| |.|" + "|.| |....*| |.............| |*....| |.|" + "|.| |.F---J |.F---------7.| L---7.| |.|" + "|.L-J.L-----J.L---7 F---J.L-----J L-J.|" + "|................*| |*................|" + "|.F-7.F--7.F--7.F-J L-7.F--7.F--7.F-7.|" + "|.L-J.L--J.| |.L-----J.| |.L--J.L-J.|" + "|*.........| |.........| |.........*|" + "L----------J |.F-----7.| L----------J" diff --git a/b-orig.pac b/b-orig.pac deleted file mode 100644 index 80a3335..0000000 --- a/b-orig.pac +++ /dev/null @@ -1,28 +0,0 @@ -width: 35 -height: 26 -tab: "F-----------7 | | F-----------7" - "| | | | | |" - "| F--7 F--7 | | | | F--7 F--7 |" - "| | | | | | | | | | | | | |" - "| L--J L--J L---J L---J L--J L--J |" - "| |" - "| F--7 F--------7 F--------7 F--7 |" - "| | | L---7 F--J L--7 F---J | | |" - "| | | | | | | | | |" - "| | | F-7 | | F---7 | | F-7 | | |" - "| L--J | | L-J | | L-J | | L--J |" - "| | | | | | | |" - "L----7 | L---7 | | F---J | F----J" - "F----J | F---J | | L---7 | L----7" - "| | | | | | | |" - "| F--7 | | F-7 | | F-7 | | F--7 |" - "| | | L-J | | L---J | | L-J | | |" - "| | | | | | | | | |" - "| | | F---J L--7 F--J L---7 | | |" - "| L--J L--------J L--------J L--J |" - "| |" - "| F--7 F--7 F---7 F---7 F--7 F--7 |" - "| | | | | | | | | | | | | |" - "| L--J L--J | | | | L--J L--J |" - "| | | | | |" - "L-----------J | | L-----------J" diff --git a/b.pac b/b.pac deleted file mode 100644 index 1ece4a2..0000000 --- a/b.pac +++ /dev/null @@ -1,3 +0,0 @@ -width: 35 -height: 26 -tab: "F-----------7 | | F-----------7| | | | | || F--7 F--7 | | | | F--7 F--7 || | | | | | | | | | | | | || L--J L--J L---J L---J L--J L--J || || F--7 F--------7 F--------7 F--7 || | | L---7 F--J L--7 F---J | | || | | | | | | | | || | | F-7 | | F---7 | | F-7 | | || L--J | | L-J | | L-J | | L--J || | | | | | | |L----7 | L---7 | | F---J | F----JF----J | F---J | | L---7 | L----7| | | | | | | || F--7 | | F-7 | | F-7 | | F--7 || | | L-J | | L---J | | L-J | | || | | | | | | | | || | | F---J L--7 F--J L---7 | | || L--J L--------J L--------J L--J || || F--7 F--7 F---7 F---7 F--7 F--7 || | | | | | | | | | | | | || L--J L--J | | | | L--J L--J || | | | | |L-----------J | | L-----------J" diff --git a/display.c b/display.c index 59b23bc..14e975a 100644 --- a/display.c +++ b/display.c @@ -103,14 +103,34 @@ void displayelement (board_t *board, int x, int y) case '+': element = ACS_PLUS; break; - case 'C': + case 'c': setcolor (yellow_black); element = 'C'; break; - case 'O': + case 'C': + setcolor (red_black); + element = 'C'; + break; + case 'o': setcolor (yellow_black); element = 'O'; break; + case 'O': + setcolor (red_black); + element = 'O'; + break; + case '.': + setcolor (brown_black); + element = ACS_BULLET; + break; + case '*': + setcolor (brown_black); + element = ACS_DIAMOND; + break; + case 'x': + setcolor (blue_black); + element = ACS_CKBOARD; + break; } mvaddch (board->yoffset + y, board->xoffset + x, element); setcolor (gray_black); @@ -207,8 +227,8 @@ char *savewindow (int length, int xoffset, int yoffset) void msgwindow (char *msg, int xoffset, int yoffset, int length) { - setcolor (gray_black); - _dobound ((length > 0) ? length : (int)strlen (msg), 1, xoffset, yoffset); + setcolor (black_gray); + //_dobound ((length > 0) ? length : (int)strlen (msg), 1, xoffset, yoffset); if (length > 0) { int i; for (i = 0; i < length; i++) { @@ -216,7 +236,7 @@ void msgwindow (char *msg, int xoffset, int yoffset, int length) } } mvaddstr (yoffset, xoffset + ((length > 0) ? (length - (int)strlen (msg)) / 2 : 0), msg); - setcolor (black_gray); + setcolor (gray_black); } /* vim: set ts=4 sw=4 et: */ diff --git a/pacman.c b/pacman.c index d642d00..eb15086 100644 --- a/pacman.c +++ b/pacman.c @@ -24,7 +24,8 @@ int savelen = 12; int height = 20; int width = 40; int xoffset = 1; -int yoffset = 0; +int yoffset = 1; +int scorelen = 15; char *help = " Move up cursor\n" @@ -136,6 +137,8 @@ int main (int argc, char *argv[]) start_color (); /* window positions (board) */ + int xscore = (board->width - scorelen) / 2; + int yscore = 0; int xboard = board->xoffset = xoffset; int yboard = board->yoffset = yoffset; int xsave = max (xboard + (board->width - savelen) / 2, 1); @@ -145,7 +148,7 @@ int main (int argc, char *argv[]) /* pacman position */ int x = 0; int y = 0; - if (findchar (board, 'C', &x, &y)) { + if (findchar (board, 'c', &x, &y)) { VERBOSE (WARNING, printf ("can't find Pacman\n")); } int nx = x; @@ -154,6 +157,8 @@ int main (int argc, char *argv[]) /* event loop */ int stop = 0; int draw = 1; + int mode = 0; + int score = 0; while (!stop) { /* draw board */ @@ -162,20 +167,25 @@ int main (int argc, char *argv[]) draw = 0; } + /* display score */ + char msg[128] = {0}; + sprintf (msg, "score: % 5d", score); + msgwindow (msg, xscore, yscore, scorelen); + /* move pacman */ if ((nx != x) || (ny != y)) { - *getcell (board, x, y) = 'O'; + *getcell (board, x, y) = (mode == 0) ? 'o' : 'O'; displayelement (board, x, y); refresh (); - msleep (250); + msleep (150); *getcell (board, x, y) = ' '; displayelement (board, x, y); x = nx; y = ny; - *getcell (board, x, y) = 'C'; + *getcell (board, x, y) = (mode == 0) ? 'c' : 'C'; displayelement (board, x, y); refresh (); - msleep (250); + msleep (150); } int ch = getch (); @@ -233,7 +243,19 @@ int main (int argc, char *argv[]) nx = ((x < board->width - 1) ? x : -1) + 1; break; } - if (*getcell (board, nx, ny) != ' ') { + + /* check new position */ + char *cell = getcell (board, nx, ny); + switch (*cell) { + case '*': + mode = 1; + /* fallthrough */ + case '.': + score++; + /* fallthrough */ + case ' ': + break; + default: nx = x; ny = y; } diff --git a/time.c b/time.c new file mode 100644 index 0000000..ba0084e --- /dev/null +++ b/time.c @@ -0,0 +1,10 @@ +#include + +#include "time.h" + +void msleep (int msec) +{ + usleep (msec * 1000); +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/time.h b/time.h new file mode 100644 index 0000000..bc21d7f --- /dev/null +++ b/time.h @@ -0,0 +1,8 @@ +#ifndef __TIME_H__ +#define __TIME_H__ + +void msleep (int msec); + +#endif /* __TIME_H__ */ + +/* vim: set ts=4 sw=4 et: */ -- 2.30.2