speed = ;
cnt = ;
score = ;
level = ;
width = ;
height = ;
state = NORMAL;
*statestr = ;
bw = ;
brick[] = ;
bricks[][];
pad;
ball[];
dir[] = { , - };
ch = ;
*tStart[] = {
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
};
*tExit[] = {
,
,
,
,
,
,
,
,
,
,
};
*tOver[] = {
,
,
,
,
,
,
,
,
};
*tGoon[] = {
,
,
,
,
,
,
,
,
,
,
};
*tHelp =
;
resizescr( sig)
{
statestr = ;
getmaxyx(stdscr, height, width);
}
print_msg( *msg[], y_n_key, clearmsg)
{
WINDOW *txtwin = ;
length = ;
tmplength = ;
line = ;
i;
left;
top;
emptymsg[width];
(;;) {
tmplength = strlen(msg[line]);
(tmplength == )
;
(length < tmplength)
length = tmplength;
line++;
}
assert(length < width);
assert(line != );
top = (height - line) / ;
left = (width - length) / ;
txtwin = newwin(line, length, top, left);
wattrset(txtwin, COLOR_PAIR(TXT));
assert(txtwin != );
(i = ; i < line; i++) {
mvwaddstr(txtwin, i, , msg[i]);
}
wrefresh(txtwin);
timeout();
(;;) {
ch = getch();
(y_n_key)
;
{
(ch == ERR || ch == || ch == ) {
ch = ;
;
}
(ch == || ch == ) {
ch = ;
;
}
}
}
(clearmsg ==
|| (clearmsg == && ch == )
|| (clearmsg == && ch == )
) {
(i = ; i < length; i++)
emptymsg[i] = ;
wattrset(txtwin, COLOR_PAIR(TXT));
(i = ; i < line; i++) {
mvwaddstr(txtwin, i, , emptymsg);
}
wrefresh(txtwin);
delwin(txtwin);
}
i = ch, ch = ;
timeout();
(i == )
FALSE;
TRUE;
}
draw_board()
{
i, j;
barstr[width];
level++;
cnt = ;
dir[] = , dir[] = -;
i = width % (bw + ) / ;
j = ;
attrset(COLOR_PAIR(BLOCK));
(;;) {
mvaddstr(j, i, brick);
bricks[cnt][] = i;
bricks[cnt][] = j;
bricks[cnt][] = ;
i = i + bw + ;
cnt++;
(i >= width - bw) {
(cnt > )
;
i = width % (bw + ) / ;
j += ;
}
}
bricks[cnt][] = -;
(level == ) {
attrset(COLOR_PAIR(BLOCK));
pad = rand() % (width - bw - );
mvaddstr(height - , pad, brick);
(i = ; i < width; i++)
barstr[i] = ;
attrset(COLOR_PAIR(BAR));
mvaddstr(height - , , barstr);
mvprintw(height - , , , score,
level, statestr);
}
attrset(COLOR_PAIR(BALL));
ball[] = pad, ball[] = height - ;
(abs(ball[] - pad) < )
ball[] = rand() % (width - bw - );
mvaddstr(ball[], ball[], );
refresh();
}
quit_game( ch)
{
(ch == ) {
(cnt <
|| (cnt > && print_msg(tExit, FALSE, ))
) {
state = QUIT;
statestr = ;
}
} (ch == ) {
state = OVER;
statestr = ;
print_msg(tOver, TRUE, );
} (state == PAUSE) {
state = NORMAL;
statestr = ;
} {
state = PAUSE;
statestr = ;
}
attrset(A_NORMAL | COLOR_PAIR(BAR));
mvprintw(height - , , , score, level,
statestr);
refresh();
}
move_ball()
{
i;
tmpball[] = { ball[], ball[] };
(state != NORMAL)
;
attrset(A_REVERSE | COLOR_PAIR(BALL));
mvaddstr(ball[], ball[], );
attrset(A_NORMAL | COLOR_PAIR(BLOCK));
mvaddstr(height - , pad, brick);
tmpball[] += dir[], tmpball[] += dir[];
(tmpball[] < )
dir[] = -dir[];
(tmpball[] < || tmpball[] > width)
dir[] = -dir[];
attrset(A_REVERSE | COLOR_PAIR(BLOCK));
(i = ;; i++) {
(bricks[i][] == -)
;
(bricks[i][] == )
;
(bricks[i][] <= tmpball[]
&& bricks[i][] + bw >= tmpball[]
&& bricks[i][] == tmpball[]
) {
bricks[i][] = ;
dir[] = abs(dir[]);
score += ;
cnt--;
refresh();
mvaddstr(bricks[i][], bricks[i][], brick);
(cnt <= ) {
(print_msg(tGoon, FALSE, )) {
draw_board();
} {
quit_game();
}
}
}
}
(tmpball[] == height - ) {
(tmpball[] < pad || tmpball[] > pad + bw)
quit_game();
dir[] = -abs(dir[]);
(tmpball[] == pad || tmpball[] == pad + bw) {
dir[] = -dir[];
}
}
attrset(A_NORMAL | COLOR_PAIR(BAR));
mvprintw(height - , , , score, level,
statestr);
ball[] += dir[], ball[] += dir[];
attrset(A_NORMAL | COLOR_PAIR(BALL));
mvaddstr(ball[], ball[], );
refresh();
}
move_pad( ch)
{
move;
(ch) {
KEY_LEFT:
move = -;
;
KEY_RIGHT:
move = ;
;
:
quit_game(ch);
;
:
quit_game(ch);
;
:
;
}
attrset(A_REVERSE | COLOR_PAIR(BLOCK));
mvaddstr(height - , pad, brick);
pad = pad + move;
(pad < )
pad = ;
(pad > width - bw)
pad = width - bw;
attrset(A_NORMAL | COLOR_PAIR(BLOCK));
mvaddstr(height - , pad, brick);
refresh();
;
}
main()
{
initscr();
start_color();
init_pair(BLOCK, COLOR_BLACK, COLOR_RED);
init_pair(TXT, COLOR_CYAN, COLOR_BLACK);
init_pair(BAR, COLOR_WHITE, COLOR_BLUE);
init_pair(BALL, COLOR_BLACK, COLOR_WHITE);
noecho();
raw();
timeout();
cbreak();
keypad(stdscr, TRUE);
curs_set();
getmaxyx(stdscr, height, width);
signal(SIGWINCH, resizescr);
(height >= && width >= ) {
refresh();
print_msg(tStart, TRUE, );
clear();
draw_board();
(;;) {
ch = getch();
move_ball();
(ch != ERR)
move_pad(ch);
(state == OVER || state == QUIT)
;
}
} {
state = QUIT;
statestr = ;
}
refresh();
sleep(), flash();
sleep(), flash();
sleep(), flash();
curs_set();
keypad(stdscr, FALSE);
nocbreak();
notimeout(stdscr, TRUE);
noraw();
echo();
endwin();
puts(statestr);
exit();
}