linux查看文件夹个球,[原创]linux下面的打小球游戏
[原創(chuàng)]linux下面的打小球游戲
(2012-04-10 04:51:44)
標簽:
linux
游戲
雜談
[原創(chuàng)]linux下面的打小球游戲linux下面的C編程,我還是新手,請大家別笑話。
#include
#include
#include
#include
#include
#include
#include
#include
#define NORMAL 1
#define PAUSE2
#define OVER 3
#define QUIT 4
#define BLOCK1
#define TXT 2
#define BAR 3
#define BALL 4
#define CLEAR5
int speed = 10;
int cnt = 0;
int score = 0;
int level = 0;
int width = 40;
int height = 50;
short state = NORMAL;
char *statestr = "Normal";
const int bw = 7;
const char brick[] = " ";
int bricks;
int pad;
int ball;
int dir = { 1, -1 };
int ch = 0;
const char *tStart[] = {
"______ _ _ ___ ______ _ _
",
" (____\\ (_) | | / _ \\ (____\\ | | |
",
"____)) ____ _____| |_ ( (_) ) ____))_____| | |
",
" |__( / ___) |/ ___) |_/ ) ) _ ( |__((____ | | |
",
" | |__)) | | ( (___|_ ( ( (/\\ | |__)) ___ | | |
",
" |______/|_| |_|\\____)_| \\_)
\\__/\\_)|______/\\_____|\\_)_) ",
" ",
" ",
" ",
" by Jerry Fleming
",
" ",
" ",
" GAME STARTING! PRESS ANY KEY TO GO ON ...
",
" ",
"
------------------------------------------------------------
",
"| P: Pause / Resume
->: Move Right / LeftY:YesN: No |
",
"
------------------------------------------------------------
",
""
};
const char *tExit[] = {
" 88888 88888",
"88888888888 88 ad88888ba 88 8b d8 d8
88",
"88 \"\" ,d
d8\" \"8b 88Y8, ,8P ,8P'
88",
"88 88 \"\" a8P
88 Y8,,8P d8\" 88",
"88aaaaa 8b, ,d8 88 MM88MMM
,a8P\" 88
\"8aa8\" ,8P'
8b,dPPYba,88",
"88\"\"\"\"\"
`Y8, ,8P'88 88 d8\" 88 `88' d8\"
88P' `\"8a 88",
"88 )888( 88 88
\"\" 88 88 ,8P' 88 88
88",
"88 ,d8\"
\"8b,88 88, aa 88 88d8\" 88 88
88",
"88888888888 8P' `Y8 88 \"Y88888
88 88 8P' 88 88 88",
" 88888 88888",
""
};
const char *tOver[] = {
",ad8888ba, 88",
" d8\"' `\"8b
88",
"d8' 88",
"88 ,adPPYYba, 88,dPYba,,adPYba, ,adPPYba,
,adPPYba,8b d8,adPPYba, 8b,dPPYba, 88",
"88 88888 \"\"
`Y8 88P' \"88\"
\"8a a8P_____88 a8\"
\"8a `8b d8' a8P_____88 88P' \"Y8
88",
"Y8, 88 ,adPPPPP88 88 88 88
8PP\"\"\"\"\"\"\"
8b d8`8b
d8'8PP\"\"\"\"\"\"\"
88 \"\"",
" Y8a. .a88 88, ,88 88 88 88
\"8b, ,aa \"8a,
,a8\" `8b,d8' \"8b, ,aa 88
aa",
"`\"Y88888P\"`\"8bbdP\"Y8
88 88 88`\"Ybbd8\"'
`\"YbbdP\"'
\"8\"
`\"Ybbd8\"' 88
88",
""
};
const char *tGoon[] = {
" 88888 88888",
",ad8888ba, ad88888ba 88 8b d8 d8
88",
" d8\"' `\"8b
d8\" \"8b 88Y8, ,8P ,8P'
88",
"d8' \"\" a8P 88
Y8,,8P d8\" 88",
"88 ,adPPYba, ,adPPYba,8b,dPPYba,
,a8P\" 88
\"8aa8\" ,8P'
8b,dPPYba,88",
"88 88888 a8\"
\"8a a8\" \"8a
88P' `\"8a d8\" 88 `88'
d8\" 88P' `\"8a
88",
"Y8, 88 8b d8 8b d8 88 88
\"\" 88 88 ,8P' 88 88
88",
" Y8a. .a88 \"8a,
,a8\" \"8a,
,a8\" 88 88 aa 88 88d8\" 88 88
88",
"`\"Y88888P\"
`\"YbbdP\"'
`\"YbbdP\"'88 88 88 88 88 8P' 88
88 88",
" 88888 88888",
""
};
const char *tHelp =
"Use left and right arrow keys to move the pad and
bounce the ball so that it won't hit the ground. Hit the brick and
score.";
void resizescr(int sig)
{
statestr = "Quit because screen
resized.";
state = QUIT;
}
bool print_msg(const char *msg[], const bool y_n_key, const int
clearmsg)
{
WINDOW *txtwin = NULL;
int length = 0;
int tmplength = 0;
int line = 0;
int i;
int left;
int top;
char emptymsg;
for (;;) {
tmplength = strlen(msg);
if (tmplength == 0)
break;
if (length < tmplength)
length = tmplength;
line++;
}
assert(length < width);
assert(line != 0);
top = (height - line) / 2;
left = (width - length) / 2;
txtwin = newwin(line, length, top, left);
wattrset(txtwin, COLOR_PAIR(TXT));
assert(txtwin != NULL);
for (i = 0; i < line; i++) {
mvwaddstr(txtwin, i, 0, msg);
}
wrefresh(txtwin);
timeout(3000);
for (;;) {
ch = getch();
if (y_n_key)
break;
else {
if (ch == ERR || ch == 'y' || ch == 'Y') {
ch = 'Y';
break;
}
if (ch == 'n' || ch == 'N') {
ch = 'N';
break;
}
}
}
if (clearmsg == 1
|| (clearmsg == 2 && ch ==
'Y')
|| (clearmsg == 3 && ch ==
'N')
) {
for (i = 0; i < length; i++)
emptymsg = ' ';
wattrset(txtwin, COLOR_PAIR(TXT));
for (i = 0; i < line; i++) {
mvwaddstr(txtwin, i, 0, emptymsg);
}
wrefresh(txtwin);
delwin(txtwin);
}
i = ch, ch = 0;
timeout(100);
if (i == 'N')
return FALSE;
return TRUE;
}
void draw_board(void)
{
int i, j;
char barstr;
level++;
cnt = 0;
dir = 1, dir = -1;
i = width % (bw + 1) / 2;
j = 1;
attrset(COLOR_PAIR(BLOCK));
for (;;) {
mvaddstr(j, i, brick);
bricks = i;
bricks = j;
bricks = 1;
i = i + bw + 1;
cnt++;
if (i >= width - bw) {
if (cnt > 50)
break;
i = width % (bw + 1) / 2;
j += 2;
}
}
bricks = -1;
if (level == 1) {
attrset(COLOR_PAIR(BLOCK));
pad = rand() % (width - bw - 1);
mvaddstr(height - 2, pad, brick);
for (i = 0; i < width; i++)
barstr = ' ';
attrset(COLOR_PAIR(BAR));
mvaddstr(height - 1, 0, barstr);
mvprintw(height - 1, 0, "SCORE: dLEVEL: %dSTATE:
%-9s", score,
level, statestr);
}
attrset(COLOR_PAIR(BALL));
ball = pad, ball = height - 3;
while (abs(ball - pad) < 15)
ball = rand() % (width - bw - 1);
mvaddstr(ball, ball, " ");
refresh();
}
void quit_game(const int ch)
{
if (ch == 'q') {
if (cnt < 0
|| (cnt > 0 &&
print_msg(tExit, FALSE, 3))
) {
state = QUIT;
statestr = "Quit game";
}
} else if (ch == 'o') {
state = OVER;
statestr = "Game over";
print_msg(tOver, TRUE, 0);
} else if (state == PAUSE) {
state = NORMAL;
statestr = "Normal";
} else {
state = PAUSE;
statestr = "Pause";
}
attrset(A_NORMAL | COLOR_PAIR(BAR));
mvprintw(height - 1, 0, "SCORE: dLEVEL: %dSTATE:
%-9s", score, level,
statestr);
refresh();
}
void move_ball()
{
int i;
int tmpball[] = { ball, ball };
if (state != NORMAL)
return;
attrset(A_REVERSE | COLOR_PAIR(BALL));
mvaddstr(ball, ball, " ");
attrset(A_NORMAL | COLOR_PAIR(BLOCK));
mvaddstr(height - 2, pad, brick);
tmpball += dir, tmpball += dir;
if (tmpball < 0)
dir = -dir;
if (tmpball < 0 || tmpball >
width)
dir = -dir;
attrset(A_REVERSE | COLOR_PAIR(BLOCK));
for (i = 0;; i++) {
if (bricks == -1)
break;
if (bricks == 0)
continue;
if (bricks <= tmpball
&& bricks + bw >=
tmpball
&& bricks == tmpball
) {
bricks = 0;
dir = abs(dir);
score += 10;
cnt--;
refresh();
mvaddstr(bricks, bricks, brick);
if (cnt <= 0) {
if (print_msg(tGoon, FALSE, 2)) {
draw_board();
} else {
quit_game('q');
}
}
}
}
if (tmpball == height - 2) {
if (tmpball < pad || tmpball > pad +
bw)
quit_game('o');
dir = -abs(dir);
if (tmpball == pad || tmpball == pad + bw) {
dir = -dir;
}
}
attrset(A_NORMAL | COLOR_PAIR(BAR));
mvprintw(height - 1, 0, "SCORE: dLEVEL: %dSTATE:
%-9s", score, level,
statestr);
ball += dir, ball += dir;
attrset(A_NORMAL | COLOR_PAIR(BALL));
mvaddstr(ball, ball, " ");
refresh();
}
int move_pad(const int ch)
{
int move = 0;
switch (ch) {
case KEY_LEFT:
move = -2;
break;
case KEY_RIGHT:
move = 2;
break;
case 'q':
quit_game(ch);
break;
case 'p':
quit_game(ch);
break;
default:
return 0;
}
attrset(A_REVERSE | COLOR_PAIR(BLOCK));
mvaddstr(height - 2, pad, brick);
pad = pad + move;
if (pad < 0)
pad = 0;
if (pad > width - bw)
pad = width - bw;
attrset(A_NORMAL | COLOR_PAIR(BLOCK));
mvaddstr(height - 2, pad, brick);
refresh();
return 0;
}
int main(void)
{
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(100);
cbreak();
keypad(stdscr, TRUE);
curs_set(0);
getmaxyx(stdscr, height, width);
signal(SIGWINCH, resizescr);
if (height >= 20 &&
width >= 80) {
refresh();
print_msg(tStart, TRUE, 0);
clear();
draw_board();
for (;;) {
ch = getch();
move_ball();
if (ch != ERR)
move_pad(ch);
if (state == OVER || state == QUIT)
break;
}
} else {
state = QUIT;
statestr = "Screen too small. Game
aborted.";
}
sleep(1), flash();
sleep(1), flash();
curs_set(1);
keypad(stdscr, FALSE);
nocbreak();
notimeout(stdscr, TRUE);
noraw();
echo();
endwin();
puts(statestr);
exit(0);
}
編譯http://www.goigh.com/:
gcc -Wall -pedantic --std=c99 -g -o game game.c -lncurses
我還沒有搞懂你什么意思?是1不停http://www.aiku99.com/的移動位置嗎?玩了一下,下面負責接的那塊擋板跑的越快,小球也就跑的越快,不知道這個是樓主故意留的還是bug?原帖由
cugb_cat 于 2007-3-21 13:28 發(fā)表
玩了一下,下面負責接的那塊擋板跑的越快,小球也就跑的越快,不知道這個是樓主故意留的還是bug?
不是故意的,也不是bug。
用戶按鍵和小球的運動應該在兩個線程里面進行(小球要不停運動,同時還要檢測是否有用戶按鍵)。我為了簡單,把它們放在一個線程里面。
for (;;) {
ch = getch();
move_ball();
if (ch != ERR)
move_pad(ch);
if (state == OVER || state == QUIT)
break;
}
從代碼可以看出,小球跑得越快,按鍵時擋板的反應就越快。在這個無限循環(huán)中,小球的速度受到計算機系統(tǒng)本身的制約,而不是由擋板的快慢決定的。
你只看到了現(xiàn)象,把原因和結(jié)果顛倒了。
[ 本帖最后由 jerryfleming 于 2007-3-21 15:10 編輯 ]原帖由 中國大表哥 于 2007-3-21
14:05 發(fā)表
是的。你是怎么搞的?
這很簡單。把原來位置的去掉,在新的位置寫上。回復 8樓 中國大表哥
的帖子我的MSN:jerry.fleming@saybot.com
QQ 34302
分享:
喜歡
0
贈金筆
加載中,請稍候......
評論加載中,請稍候...
發(fā)評論
登錄名: 密碼: 找回密碼 注冊記住登錄狀態(tài)
昵???稱:
評論并轉(zhuǎn)載此博文
發(fā)評論
以上網(wǎng)友發(fā)言只代表其個人觀點,不代表新浪網(wǎng)的觀點或立場。
總結(jié)
以上是生活随笔為你收集整理的linux查看文件夹个球,[原创]linux下面的打小球游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 鸟哥写过的c扩展,Yaconf
- 下一篇: Linux 父进程 子进程 回收,回收