?? snake.c
字號:
y++;
}
if (ch_score[i] == '5')
while (n<=4) {
mvprintw(y, x+length, "%s", *(five+n));
n++;
y++;
}
if (ch_score[i] == '6')
while (n<=4) {
mvprintw(y, x+length, "%s", *(six+n));
n++;
y++;
}
if (ch_score[i] == '7')
while (n<=4) {
mvprintw(y, x+length, "%s", *(seven+n));
n++;
y++;
}
if (ch_score[i] == '8')
while (n<=4) {
mvprintw(y, x+length, "%s", *(eight+n));
n++;
y++;
}
if (ch_score[i] == '9')
while (n<=4) {
mvprintw(y, x+length, "%s", *(nine+n));
n++;
y++;
}
n = 0;
y = 13;
i++;
length += 6;
}
attroff(A_BOLD);
refresh();
}
int create_snake(int i, int j)
{
int m;
for (m=SNAKE_LONG-1; m>=0; m--) {
snake_box[i][j-m] = 1;
move_box[i][j-m] = 'r';
}
}
int create_food(void)
{
int tmp_n, tmp_l;
do {
tmp_n = random_food(1, N-2);
tmp_l = random_food(1, L-2);
} while (snake_box[tmp_n][tmp_l] != 0);
food_n = tmp_n;
food_l = tmp_l;
snake_box[food_n][food_l] = 2;
}
void initial() /* .... curses .. */
{
initscr();
cbreak();
nl();
noecho();
curs_set(0); // set off _
intrflush(stdscr,FALSE);
keypad(stdscr,TRUE);
refresh();
}
int snake_right()
{
current_way = 'r';
if (snake_box[head_n][head_l+1] == 1) {
is_game_over = 0;
return(0);
}
if (snake_box[head_n][head_l+1] == 2) { // food
snake_box[head_n][head_l+1] = 1;
move_box[head_n][head_l] = 'r';
head_l++;
score += level*2;
create_food();
return(1);
}
snake_box[rear_n][rear_l] = 0;
snake_box[head_n][head_l+1] = 1;
move_box[head_n][head_l] = 'r';
head_l++; // head + 1
if (move_box[rear_n][rear_l] == 'r') {
rear_l++;
} else if (move_box[rear_n][rear_l] == 'd') {
rear_n++;
} else if (move_box[rear_n][rear_l] == 'l') {
rear_l--;
} else if (move_box[rear_n][rear_l] == 'u') {
rear_n--;
}
}
int snake_down()
{
current_way = 'd';
if (snake_box[head_n+1][head_l] == 1) {
is_game_over = 0;
return(0);
}
if (snake_box[head_n+1][head_l] == 2) { // food
snake_box[head_n+1][head_l] = 1;
move_box[head_n][head_l] = 'd';
head_n++;
score += level*2;
create_food();
return(1);
}
snake_box[rear_n][rear_l] = 0;
snake_box[head_n+1][head_l] = 1;
move_box[head_n][head_l] = 'd';
head_n++;
if (move_box[rear_n][rear_l] == 'r') {
rear_l++;
} else if (move_box[rear_n][rear_l] == 'd') {
rear_n++;
} else if (move_box[rear_n][rear_l] == 'l') {
rear_l--;
} else if (move_box[rear_n][rear_l] == 'u') {
rear_n--;
}
}
int snake_left()
{
current_way = 'l';
if (snake_box[head_n][head_l-1] == 1) {
is_game_over = 0;
return(0);
}
if (snake_box[head_n][head_l-1] == 2) { // food
snake_box[head_n][head_l-1] = 1;
move_box[head_n][head_l] = 'l';
head_l--;
score += level*2;
create_food();
return(1);
}
snake_box[rear_n][rear_l] = 0;
snake_box[head_n][head_l-1] = 1;
move_box[head_n][head_l] = 'l';
head_l--;
if (move_box[rear_n][rear_l] == 'r') {
rear_l++;
} else if (move_box[rear_n][rear_l] == 'd') {
rear_n++;
} else if (move_box[rear_n][rear_l] == 'l') {
rear_l--;
} else if (move_box[rear_n][rear_l] == 'u') {
rear_n--;
}
}
int snake_up()
{
current_way = 'u';
if (snake_box[head_n-1][head_l] == 1) {
is_game_over = 0;
return(0);
}
if (snake_box[head_n-1][head_l] == 2) { // food
snake_box[head_n-1][head_l] = 1;
move_box[head_n][head_l] = 'u';
head_n--;
score += level*2;
create_food();
return(1);
}
snake_box[rear_n][rear_l] = 0;
snake_box[head_n-1][head_l] = 1;
move_box[head_n][head_l] = 'u';
head_n--;
if (move_box[rear_n][rear_l] == 'r') {
rear_l++;
} else if (move_box[rear_n][rear_l] == 'd') {
rear_n++;
} else if (move_box[rear_n][rear_l] == 'l') {
rear_l--;
} else if (move_box[rear_n][rear_l] == 'u') {
rear_n--;
}
}
unsigned char my_random(void)
{
static int seeded;
unsigned int uint_res;
unsigned char c1, c2, c3, c4;
if (!seeded)
{
struct timeval tv;
int retval = gettimeofday(&tv, NULL);
if (retval != 0)
{
perror("gettimeofday");
exit(0);
}
srand((unsigned)tv.tv_usec);
seeded = 1;
}
uint_res = rand();
c1 = uint_res & 0x000000ff;
c2 = (uint_res >> 8) & 0x000000ff;
c3 = (uint_res >> 16) & 0x000000ff;
c4 = (uint_res >> 24) & 0x000000ff;
return c1 ^ c2 ^ c3 ^ c4;
}
int random_food(int min, int max)
{
int scaled_value;
int value = my_random();
value <<= 8;
value != my_random();
scaled_value = (double)min;
scaled_value += ((double)value / (double)65536)*
((double)max - min + 1);
value = (unsigned short)scaled_value;
return(value);
}
void snake_log()
{
int openfd;
char buf[128];
struct timeval tv;
struct tm* ptm;
int retval = gettimeofday(&tv, NULL);
ptm = (struct tm*)localtime(&tv, NULL);
if ((openfd= open("score.txt", O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|
S_IWUSR | S_IRGRP |
S_IWGRP | S_IROTH)) == -1) {
perror("snake log error");
exit(0);
}
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S ", ptm);
write(openfd, buf, strlen(buf));
sprintf(buf, "%s\n", ch_score);
write(openfd, buf, strlen(buf));
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -