Worms -- OOP Redo  2016-01
Worms-Redo Project as a learning vehicle.
control.cpp
Go to the documentation of this file.
1 
2 /* control.cpp game control */
3 
4 /* parameters for the 'graphical' (such as it is) display */
5 int slowness; // slowness number
6 int paused = 0; // == 1 iff paused
7 int idelay = 10; // delay increment
8 int tdelay; // total delay yet to do
9 
10 
11 int userKeyPress(int c)
12 {
13  switch (c) {
14  case '+': slowness -= min(slowness, 100); break;
15  case '-': slowness += 100; break;
16  case 'f': slowness = 0; break;
17  case 's': /* for you todo: highlight a worm */ break;
18  case 'k': /* for you todo: kill the highlighted worm */ break;
19  case 'w': createWorm((WORM_KIND)random(3), say[random(Nsay)]); break;
20  case ' ': paused ^= 1; break;
21  }
22  return c;
23 }
24 
25 /* User can control the speed, etc. The total delay required, tdelay,
26  is doled out in increments of idelay so that keyboard interaction
27  is more responsive. pre: none; post: returns ESC or '\0';; */
28 
30 {
31  sprintf
32  (msg,
33  "SPC %s, ESC terminates, k kills-, w creates-, s shows-a-worm" EOLN
34  "%2d Vegetarians,%2d Cannibals,%2d Scissor-heads,%2d hi-water-mark" EOLN
35  "%04d slowness, - increases, + reduces, f full-speed" EOLN,
36  (paused? "resumes" : "pauses "),
37  nVegetarians, nCannibals, nScissors, hxWorms, slowness);
38  showMsg(asogRows + 1);
39  for (tdelay = slowness+1; tdelay > 0; tdelay -= idelay) {
40  char c = userKeyPress(getChar()); // no-delay
41  if (c == ESC) return ESC;
42  if (paused) tdelay += idelay;
43  if (idelay > 0)
44  usleep(1000 * idelay);
45  }
46  return '\0';
47 }
48 
int paused
Definition: control.cpp:6
int userControl()
Definition: control.cpp:29
int userKeyPress(int c)
Definition: control.cpp:11
int idelay
Definition: control.cpp:7
int asogRows
Definition: display.cpp:18
#define EOLN
Definition: worms.h:31
char msg[1024]
Definition: display.cpp:4
int hxWorms
int tdelay
Definition: control.cpp:8
#define getChar()
Definition: worms.h:28
void showMsg(int y)
Definition: display.cpp:6
#define random(x)
Definition: worm.hpp:10
#define ESC
Definition: worm.hpp:13
#define min(a, b)
Definition: worm.hpp:12
int slowness
Definition: control.cpp:5