1. 实现贪吃蛇四方向的疯烧走位(149.19)
#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
struct Snake
{
int line;
int list;
struct Snake *next;
};
struct Snake *head=NULL;
struct Snake *tail=NULL;
int key;
int dir;
void initNcurse()
{
initscr();
keypad(stdscr,1);
}
void addNode()
{
struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
switch(dir){
case UP:
new->line=tail->line-1;
new->list=tail->list;
break;
case DOWN:
new->line=tail->line+1;
new->list=tail->list;
break;
case LEFT:
new->line=tail->line;
new->list=tail->list-1;
break;
case RIGHT:
new->line=tail->line;
new->list=tail->list+1;
break;
}
new->next=NULL;
tail->next=new;
tail=new;
}
void initSnake()
{
struct Snake *p;
dir=RIGHT;
while(head != NULL){
p=head;
head=head->next;
free(p);
}
head=(struct Snake*)malloc(sizeof(struct Snake));
tail=(struct Snake*)malloc(sizeof(struct Snake));
head->line=1;
head->list=1;
head->next=NULL;
tail=head;
addNode();
addNode();
addNode();
addNode();
}
int hasSnakeNode(int line,int list)
{
struct Snake *p=head;
while(p != NULL){
if(p->line==line && p->list==list){
return 1;
}
p=p->next;
}
return 0;
}
void gamePic()
{
int line;
int list;
move(0,0);
for(line=0;line<20;line++){
if(line==0){
for(list=0;list<20;list++){
printw("--");
}
printw("\n");
}
if(line>=0 && line<=19){
for(list=0;list<=20;list++){
if(list==0 || list==20){
printw("|");
}else if(hasSnakeNode(line,list)){
printw("[]");
}
else{
printw(" ");
}
}
printw("\n");
}
if(line==19){
for(list=0;list<20;list++){
printw("--");
}
printw("\nBy Jessie,key=%d\n",key);
}
}
}
void deleNode()
{
struct Snake *p=head;
head=head->next;
free(p);
}
void moveSnake()
{
addNode();
deleNode();
if(tail->line==0||tail->line==20||tail->list==0||tail->list==20){
initSnake();
}
}
void* refreshInt()
{
while(1){
moveSnake();
gamePic();
refresh();
usleep(100000);
}
}
void* changeDir()
{
while(1){
key=getch();
switch(key){
case KEY_DOWN:
dir=DOWN;
break;
case KEY_UP:
dir=UP;
break;
case KEY_LEFT:
dir=LEFT;
break;
case KEY_RIGHT:
dir=RIGHT;
break;
}
}
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initSnake();
gamePic();
pthread_create( &t1, NULL, refreshInt, NULL );
pthread_create( &t2, NULL, changeDir, NULL );
while(1);
getch();
endwin();
return 0;
}
2. 绝对值方式解决不合理走位一(150.20)
#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake
{
int line;
int list;
struct Snake *next;
};
struct Snake *head=NULL;
struct Snake *tail=NULL;
int key;
int dir;
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
void addNode()
{
struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
switch(dir){
case UP:
new->line=tail->line-1;
new->list=tail->list;
break;
case DOWN:
new->line=tail->line+1;
new->list=tail->list;
break;
case LEFT:
new->line=tail->line;
new->list=tail->list-1;
break;
case RIGHT:
new->line=tail->line;
new->list=tail->list+1;
break;
}
new->next=NULL;
tail->next=new;
tail=new;
}
void initSnake()
{
struct Snake *p;
dir=RIGHT;
while(head != NULL){
p=head;
head=head->next;
free(p);
}
head=(struct Snake*)malloc(sizeof(struct Snake));
tail=(struct Snake*)malloc(sizeof(struct Snake));
head->line=1;
head->list=1;
head->next=NULL;
tail=head;
addNode();
addNode();
addNode();
addNode();
}
int hasSnakeNode(int line,int list)
{
struct Snake *p=head;
while(p != NULL){
if(p->line==line && p->list==list){
return 1;
}
p=p->next;
}
return 0;
}
void gamePic()
{
int line;
int list;
move(0,0);
for(line=0;line<20;line++){
if(line==0){
for(list=0;list<20;list++){
printw("--");
}
printw("\n");
}
if(line>=0 && line<=19){
for(list=0;list<=20;list++){
if(list==0 || list==20){
printw("|");
}else if(hasSnakeNode(line,list)){
printw("[]");
}
else{
printw(" ");
}
}
printw("\n");
}
if(line==19){
for(list=0;list<20;list++){
printw("--");
}
printw("\nBy Jessie,key=%d\n",key);
}
}
}
void deleNode()
{
struct Snake *p=head;
head=head->next;
free(p);
}
void moveSnake()
{
addNode();
deleNode();
if(tail->line==0||tail->line==20||tail->list==0||tail->list==20){
initSnake();
}
}
void* refreshInt()
{
while(1){
moveSnake();
gamePic();
refresh();
usleep(100000);
}
}
void turn(int direction)
{
if(abs(dir)!=abs(direction)){
dir=direction;
}
}
void* changeDir()
{
while(1){
key=getch();
switch(key){
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initSnake();
gamePic();
pthread_create( &t1, NULL, refreshInt, NULL );
pthread_create( &t2, NULL, changeDir, NULL );
while(1);
getch();
endwin();
return 0;
}
3. 绝对值方式解决不合理走位二(151.21)
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
4. 贪吃蛇吃饭咯(152.22)
#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake
{
int line;
int list;
struct Snake *next;
};
struct Snake *head=NULL;
struct Snake *tail=NULL;
int key;
int dir;
struct Snake food;
void initFood()
{
static int x=3;
static int y=4;
food.line=x;
food.list=y;
x+=2;
y+=2;
}
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
void addNode()
{
struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
switch(dir){
case UP:
new->line=tail->line-1;
new->list=tail->list;
break;
case DOWN:
new->line=tail->line+1;
new->list=tail->list;
break;
case LEFT:
new->line=tail->line;
new->list=tail->list-1;
break;
case RIGHT:
new->line=tail->line;
new->list=tail->list+1;
break;
}
new->next=NULL;
tail->next=new;
tail=new;
}
void initSnake()
{
struct Snake *p;
dir=RIGHT;
while(head != NULL){
p=head;
head=head->next;
free(p);
}
initFood();
head=(struct Snake*)malloc(sizeof(struct Snake));
tail=(struct Snake*)malloc(sizeof(struct Snake));
head->line=1;
head->list=1;
head->next=NULL;
tail=head;
addNode();
addNode();
addNode();
addNode();
}
int hasSnakeNode(int line,int list)
{
struct Snake *p=head;
while(p != NULL){
if(p->line==line && p->list==list){
return 1;
}
p=p->next;
}
return 0;
}
int hasFood(int line,int list)
{
if(food.line==line && food.list==list){
return 1;
}
return 0;
}
void gamePic()
{
int line;
int list;
move(0,0);
for(line=0;line<20;line++){
if(line==0){
for(list=0;list<20;list++){
printw("--");
}
printw("\n");
}
if(line>=0 && line<=19){
for(list=0;list<=20;list++){
if(list==0 || list==20){
printw("|");
}else if(hasSnakeNode(line,list)){
printw("[]");
}else if(hasFood(line,list)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(line==19){
for(list=0;list<20;list++){
printw("--");
}
printw("\nBy Jessie,key=%d\n",key);
}
}
}
void deleNode()
{
struct Snake *p=head;
head=head->next;
free(p);
}
void moveSnake()
{
addNode();
if(hasFood(tail->line,tail->list)){
initFood();
}else{
deleNode();
}
if(tail->line==0||tail->line==20||tail->list==0||tail->list==20){
initSnake();
}
}
void* refreshInt()
{
while(1){
moveSnake();
gamePic();
refresh();
usleep(100000);
}
}
void turn(int direction)
{
if(abs(dir)!=abs(direction)){
dir=direction;
}
}
void* changeDir()
{
while(1){
key=getch();
switch(key){
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initSnake();
gamePic();
pthread_create( &t1, NULL, refreshInt, NULL );
pthread_create( &t2, NULL, changeDir, NULL );
while(1);
getch();
endwin();
return 0;
}
5. 贪吃蛇食物位置随机(153.23)
#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake
{
int line;
int list;
struct Snake *next;
};
struct Snake *head=NULL;
struct Snake *tail=NULL;
int key;
int dir;
struct Snake food;
void initFood()
{
int x=rand()%20;
int y=rand()%20;
food.line=x;
food.list=y;
}
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
void addNode()
{
struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
switch(dir){
case UP:
new->line=tail->line-1;
new->list=tail->list;
break;
case DOWN:
new->line=tail->line+1;
new->list=tail->list;
break;
case LEFT:
new->line=tail->line;
new->list=tail->list-1;
break;
case RIGHT:
new->line=tail->line;
new->list=tail->list+1;
break;
}
new->next=NULL;
tail->next=new;
tail=new;
}
void initSnake()
{
struct Snake *p;
dir=RIGHT;
while(head != NULL){
p=head;
head=head->next;
free(p);
}
initFood();
head=(struct Snake*)malloc(sizeof(struct Snake));
tail=(struct Snake*)malloc(sizeof(struct Snake));
head->line=1;
head->list=1;
head->next=NULL;
tail=head;
addNode();
addNode();
addNode();
addNode();
}
int hasSnakeNode(int line,int list)
{
struct Snake *p=head;
while(p != NULL){
if(p->line==line && p->list==list){
return 1;
}
p=p->next;
}
return 0;
}
int hasFood(int line,int list)
{
if(food.line==line && food.list==list){
return 1;
}
return 0;
}
void gamePic()
{
int line;
int list;
move(0,0);
for(line=0;line<20;line++){
if(line==0){
for(list=0;list<20;list++){
printw("--");
}
printw("\n");
}
if(line>=0 && line<=19){
for(list=0;list<=20;list++){
if(list==0 || list==20){
printw("|");
}else if(hasSnakeNode(line,list)){
printw("[]");
}else if(hasFood(line,list)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(line==19){
for(list=0;list<20;list++){
printw("--");
}
printw("\nBy Jessie,key=%d,food.line=%d,food.list=%d\n",key,food.line,food.list);
}
}
}
void deleNode()
{
struct Snake *p=head;
head=head->next;
free(p);
}
void moveSnake()
{
addNode();
if(hasFood(tail->line,tail->list)){
initFood();
}else{
deleNode();
}
if(tail->line==0||tail->line==20||tail->list==0||tail->list==20){
initSnake();
}
}
void* refreshInt()
{
while(1){
moveSnake();
gamePic();
refresh();
usleep(100000);
}
}
void turn(int direction)
{
if(abs(dir)!=abs(direction)){
dir=direction;
}
}
void* changeDir()
{
while(1){
key=getch();
switch(key){
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initSnake();
gamePic();
pthread_create( &t1, NULL, refreshInt, NULL );
pthread_create( &t2, NULL, changeDir, NULL );
while(1);
getch();
endwin();
return 0;
}
6. 贪吃蛇撞墙找死和想不开咬死自己来结束游戏的代码优化(154.24)
#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake
{
int line;
int list;
struct Snake *next;
};
struct Snake *head=NULL;
struct Snake *tail=NULL;
int key;
int dir;
struct Snake food;
void initFood()
{
int x=rand()%20;
int y=rand()%20;
food.line=x;
food.list=y;
}
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
void addNode()
{
struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
switch(dir){
case UP:
new->line=tail->line-1;
new->list=tail->list;
break;
case DOWN:
new->line=tail->line+1;
new->list=tail->list;
break;
case LEFT:
new->line=tail->line;
new->list=tail->list-1;
break;
case RIGHT:
new->line=tail->line;
new->list=tail->list+1;
break;
}
new->next=NULL;
tail->next=new;
tail=new;
}
void initSnake()
{
struct Snake *p;
dir=RIGHT;
while(head != NULL){
p=head;
head=head->next;
free(p);
}
initFood();
head=(struct Snake*)malloc(sizeof(struct Snake));
tail=(struct Snake*)malloc(sizeof(struct Snake));
head->line=1;
head->list=1;
head->next=NULL;
tail=head;
addNode();
addNode();
addNode();
addNode();
}
int hasSnakeNode(int line,int list)
{
struct Snake *p=head;
while(p != NULL){
if(p->line==line && p->list==list){
return 1;
}
p=p->next;
}
return 0;
}
int hasFood(int line,int list)
{
if(food.line==line && food.list==list){
return 1;
}
return 0;
}
void gamePic()
{
int line;
int list;
move(0,0);
for(line=0;line<20;line++){
if(line==0){
for(list=0;list<20;list++){
printw("--");
}
printw("\n");
}
if(line>=0 && line<=19){
for(list=0;list<=20;list++){
if(list==0 || list==20){
printw("|");
}else if(hasSnakeNode(line,list)){
printw("[]");
}else if(hasFood(line,list)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(line==19){
for(list=0;list<20;list++){
printw("--");
}
printw("\nBy Jessie,key=%d,food.line=%d,food.list=%d\n",key,food.line,food.list);
}
}
}
void deleNode()
{
struct Snake *p=head;
head=head->next;
free(p);
}
int ifDie()
{
struct Snake *p;
p=head;
if(tail->line<0||tail->line==20||tail->list==0||tail->list==20){
return 1;
}
while(p->next!=NULL){
if(head->line==tail->line && head->list==tail->list){
return 1;
}
p=p->next;
}
return 0;
}
void moveSnake()
{
addNode();
if(hasFood(tail->line,tail->list)){
initFood();
}else{
deleNode();
}
if(ifDie()){
initSnake();
}
}
void* refreshInt()
{
while(1){
moveSnake();
gamePic();
refresh();
usleep(200000);
}
}
void turn(int direction)
{
if(abs(dir)!=abs(direction)){
dir=direction;
}
}
void* changeDir()
{
while(1){
key=getch();
switch(key){
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initSnake();
gamePic();
pthread_create( &t1, NULL, refreshInt, NULL );
pthread_create( &t2, NULL, changeDir, NULL );
while(1);
getch();
endwin();
return 0;
}
7. 贪吃蛇小游戏总结(155.25)
- ncurse、ncurse按键响应、贪吃蛇项目整体逻辑规划、地图规划及算法优化、蛇的节点和完整身子、Linux线程、蛇的移动和走位同时进行、蛇吃食物