forked from EEErinzzz/ENGG1340
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamedemo.cpp
102 lines (87 loc) · 2.85 KB
/
gamedemo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <ncurses.h>
// "ncurses.h" is one of the cpp libraries I think we can use in our game design because it creates a interactive interface and can detect keyboard hit.
// However, it may not be so easy to learn (TAT).
// compile process is a little different as we need to specify -lncurses in compilation.
/* here is the script for compilation
g++ -pedantic-errors -std=c++11 gamedemo.cpp -o game -lncurses
./game
*/
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
int getmillis() {
// This is a self-defined function for getting the system time in millisecond.
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast < std::chrono::milliseconds>(now);
auto value = now_ms.time_since_epoch();
return static_cast<int>(value.count());
}
const int BW = 40;
const int BH = 20;
// BW BH stand for Borad Width and Board Height, respectively.
int main() {
// set up of the gameplace
initscr();
cbreak();
noecho();
// noecho denotes there will be no standard output in the terminal (console)
keypad(stdscr, TRUE);
curs_set(0);
// The follwing lines of code is the board initiation (line 29-34)
char board[BH][BW];
for (int i = 0; i < BH;i++) {
for (int j = 0; j < BW; j++) {
board[i][j] = ' ';
}
}
int player_x = BW / 2;
int player_y = BH - 1;
board[player_y][player_x] = 'X';
srand(time(NULL)); // random initiation
int ball_x = rand() % BW;
int ball_y = 0;
board[ball_y][ball_x] = '*';
const int Game_Speed = 100;
const int Scan_Interval = 1000;
while (true) {
// The follwing lines prints out the board by mvaddch(int y_coordinate, int x_coordinate, const char obj.)
for (int i = 0; i < BH; i++) {
for (int j = 0; j < BW; j++) {
mvaddch(i,j,board[i][j]);
}
}
board[ball_y][ball_x] = ' ';
ball_y++;
if (ball_y >= BH) {
ball_x = rand() % BW;
ball_y = 0;
}
board[ball_y][ball_x] = '*';
int ch = getch(); // getting keyboard input
switch (ch) {
case KEY_LEFT:
if (player_x > 0) {
board[player_y][player_x] = ' ';
player_x--;
board[player_y][player_x] = 'X';
}
break;
case KEY_RIGHT:
if (player_x < BW - 1) {
board[player_y][player_x] = ' ';
player_x++;
board[player_y][player_x] = 'X';
}
break;
}
// note: we can find the corresponding code representation for keyboard input
// E.g., KEY_LEFT for "left key"
move(0,0);
napms(Game_Speed);
// napms sets the "refreshing time of the game" to 100 millisecond.
}
endwin();
// window close.
return 0;
}