linux查看文件夹个球,[原创]linux下面的打小球游戏

[原创]linux下面的打小球游戏

(2012-04-10 04:51:44)

标签:

linux

游戏

杂谈

[原创]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 发表

玩了一下,下面负责接的那块挡板跑的越快,小球也就跑的越快,不知道这个是楼主故意留的还是bug?

不是故意的,也不是bug。

用户按键和小球的运动应该在两个线程里面进行(小球要不停运动,同时还要检测是否有用户按键)。我为了简单,把它们放在一个线程里面。

for (;;) {

ch = getch();

move_ball();

if (ch != ERR)

move_pad(ch);

if (state == OVER || state == QUIT)

break;

}

从代码可以看出,小球跑得越快,按键时挡板的反应就越快。在这个无限循环中,小球的速度受到计算机系统本身的制约,而不是由挡板的快慢决定的。

你只看到了现象,把原因和结果颠倒了。

[ 本帖最后由 jerryfleming 于 2007-3-21 15:10 编辑 ]原帖由 中国大表哥 于 2007-3-21

14:05 发表

是的。你是怎么搞的?

这很简单。把原来位置的去掉,在新的位置写上。回复 8楼 中国大表哥

的帖子我的MSN:jerry.fleming@saybot.com

QQ 34302

分享:

a4c26d1e5885305701be709a3d33442f.png喜欢

0

a4c26d1e5885305701be709a3d33442f.png赠金笔

加载中,请稍候......

评论加载中,请稍候...

发评论

登录名: 密码: 找回密码 注册记住登录状态

昵   称:

评论并转载此博文

a4c26d1e5885305701be709a3d33442f.png

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值