//snake.h
#define SHM_KEY 1234
#define UP 119
#define DOWN 115
#define LEFT 97
#define RIGHT 100
#define QUIT 122
#define direct (*((int *)p_shm))
extern void *p_shm;
extern int v_speed;
extern void snake_init();
extern int snake_nextstep();
#include "snake.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <sys/types.h>
#define START_X 10
#define START_Y 10
#define INIT_SIZ 5
#define H_LEN 80
#define V_LEN 35
int screen_arr[H_LEN * V_LEN];
int snake_len = 0;
extern int v_speed;
/* Snake Body Node */
typedef struct node{
int pos_x;
int pos_y;
struct node *next;
struct node *pre;
}BNode;
BNode *s_head, *s_end;
/************************************************
* functions declare
***********************************************/
extern void snake_init();
static void snake_display();
static inline void snake_feed();
static inline void snake_head_grow(int px, int py);
static inline void snake_tail_cut();
extern void snake_init()
{
s_head = (BNode *)malloc(sizeof(BNode));
s_head->pos_x = START_X;
s_head->pos_y = START_Y;
s_head->next = NULL;
s_head->pre = NULL;
s_end = s_head;
screen_arr[START_X + START_Y * H_LEN] = 1;
int i;
for (i = 1; i < INIT_SIZ; i++)
snake_head_grow(START_X + i, START_Y);
snake_feed();
snake_display();
}
extern int snake_nextstep()
{
int next_px = s_head->pos_x;
int next_py = s_head->pos_y;
switch (direct) {
case UP: next_py--; break;
case RIGHT: next_px++; break;
case DOWN: next_py++; break;
case LEFT: next_px--; break;
}
int pos = next_px + next_py * H_LEN;
/* grip itself */
if (screen_arr[pos] == 1)
return ERR;
/* up against the wall */
if (next_px < 0 || next_px >= H_LEN ||
next_py < 0 || next_py >= V_LEN)
return ERR;
int grow = 0;
/* eat a food */
if (screen_arr[pos] == 2) {
grow = 1;
snake_len++;
snake_feed();
}
snake_head_grow(next_px, next_py);
if (!grow)
snake_tail_cut();
snake_display();
return OK;
}
static void snake_display()
{
printw(" Hungry Snake -- Enjoy it! :)\n");
printw(" +---------------------------------------------"
"-----------------------------------+\n");
int i, j;
for (i = 0; i < V_LEN; i++){
printw(" |");
for(j = 0; j < H_LEN; j++) {
int pos = i * H_LEN + j;
printw("%c", screen_arr[pos] == 0 ? ' ' :
(screen_arr[pos] == 1) ? '#' : '$');
}
printw("|\n");
}
printw(" +---------------------------------------------"
"-----------------------------------+\n");
printw(" Direction: %s\n", direct == UP ? "Up" :
(direct == DOWN ? "Down" : (direct == LEFT ? "Left" : "Right")));
printw(" Scores: %d", snake_len);
refresh();
}
/********************************************************
* static functions
********************************************************/
static inline void snake_feed()
{
int pos = rand() % (H_LEN * V_LEN);
while (screen_arr[pos] == 1)
pos = rand() % (H_LEN * V_LEN);
screen_arr[pos] = 2;
v_speed -= 10;
}
static inline void snake_head_grow(int px, int py)
{
BNode *p = (BNode *)malloc(sizeof(BNode));
p->pos_x = px;
p->pos_y = py;
p->pre = NULL;
p->next = s_head;
s_head->pre = p;
s_head = p;
screen_arr[px + py * H_LEN] = 1;
}
static inline void snake_tail_cut()
{
screen_arr[s_end->pos_x + s_end->pos_y * H_LEN] = 0;
s_end = s_end->pre;
free(s_end->next);
s_end->next = NULL;
}
//usr_handler.c
#include "snake.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <time.h>
static void wait_user_handle();
void *p_shm = NULL;
int shmid = 0;;
int v_speed = 200;
int main(int argc, char *argv[])
{
pid_t pid;
initscr();
if ((pid = fork()) == 0) {
sleep(2);
shmid =
shmget((key_t) SHM_KEY, sizeof(int), 0666 | IPC_CREAT);
p_shm = shmat(shmid, (void *) 0, 0);
struct timespec slp = { (time_t) 0, 1000000 * v_speed };
snake_init();
while (true) {
if (direct == QUIT)
break;
clear();
snake_nextstep(); /* sleep specified nanoseconds and continue to run. */
slp.tv_nsec = 1000000 * v_speed;
nanosleep(&slp, NULL);
}
shmdt(p_shm);
} else { /* create shared memory by parent process. */
shmid =
shmget((key_t) SHM_KEY, sizeof(int), 0666 | IPC_CREAT);
if (shmid == -1) {
printw("shmget failed!\n");
exit(EXIT_FAILURE);
}
p_shm = shmat(shmid, (void *) 0, 0);
wait_user_handle();
waitpid(pid, NULL, 0); /* destroy shared memory by parent process. */
shmdt(p_shm);
shmctl(shmid, IPC_RMID, 0);
endwin();
} exit(EXIT_SUCCESS);
}
static void wait_user_handle()
{
int i;
int run = true;
int UP_DOWN = UP + DOWN;
int LEFT_RIGHT = LEFT + RIGHT;
int sum;
while (run) {
noecho();
i = getch();
echo();
switch (i) {
case UP:
case DOWN:
case LEFT:
case RIGHT:
sum = i + direct;
if(sum != UP_DOWN && sum != LEFT_RIGHT)
direct = i;
break;
case QUIT:
run = false;
direct = i;
break;
}
}
}
分享到:
- 2008-10-23 23:49
- 浏览 1158
- 评论(0)
- 论坛回复 / 浏览 (0 / 2145)
- 查看更多
相关推荐
C语言实现贪食蛇小游戏。C语言实现贪食蛇小游戏。C语言实现贪食蛇小游戏。C语言实现贪食蛇小游戏。C语言实现贪食蛇小游戏。
简单创意的html5贪食蛇小游戏代码 简单创意的html5贪食蛇小游戏代码
java贪食蛇游戏java贪食蛇游戏java贪食蛇游戏java贪食蛇游戏java贪食蛇游戏 java贪食蛇游戏java贪食蛇游戏java贪食蛇游戏java贪食蛇游戏java贪食蛇游戏 java贪食蛇游戏java贪食蛇游戏java贪食蛇游戏java贪食蛇游戏...
自制贪食蛇小游戏,自制贪食蛇小游戏,自制贪食蛇小游戏,
C#编写的经典小游戏贪食蛇,界面比较简单,有需要参考或初学者可以下载看看。
Java 实现 -- 贪食蛇小游戏 Java 实现 -- 贪食蛇小游戏
java游戏贪食蛇的完整代码,包含说明文档以及全部代码,可直接打开。
能运行的贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏贪食蛇游戏
用vc++编译运行,MFC写的一个简单贪食蛇小游戏,希望对大家有所帮助。
用C#编写的贪食蛇小游戏 适合大学课程设计
很好的贪食蛇小游戏源代码,是用QT编写的
产生一个固定大小没有边界的游戏区域,蛇从区域的中心开始,由玩家通过键盘控制蛇的运动方向,用蛇头去吃随机分布在游戏区域内的食物; 蛇的运动限制在游戏区域内,游戏区域没有边界,所以蛇在区域内作循环运动; 蛇...
java写的Android贪食蛇小游戏,实训一个月的成果,欢迎大家下载参考
MFC制作贪食蛇小游戏 参数可自行修改