c语言第一个小游戏:贪吃蛇小游戏07
贪吃蛇吃饭喽
所谓贪吃蛇的食物,也就是创建一个和蛇身一样的结构体,只是这个结构体不是链表,也是将这个结构体设置hang和lie坐标,放进gamepic进行扫描,扫到了就也是做操作将 ## 打出来
#include <curses.h>
#include <stdlib.h>
struct snake{
int hang;
int lie;
struct snake *next;
};
struct snake food;
struct snake *head;
struct snake *tail;
int key;
int dir;
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
int hasSnakeNode(int i,int j)
{
struct snake *p;
p = head;
while(p != NULL){
if(p->hang==i && p->lie==j){
return 1;
}
p=p->next;
}
return 0;
}
int ifSnakeFood(int i,int j)
{
if(food.hang==i && food.lie==j){
return 1;
}
return 0;
}
void gamepic()
{
int hang;
int lie;
move(0,0);
for(hang=0;hang<20;hang++){
if(hang == 0){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
if(hang>=0 && hang<=19){
for(lie=0;lie<=20;lie++){
if(lie==0||lie==20){
printw("|");
}else if(hasSnakeNode(hang,lie)){
printw("[]");
}else if(ifSnakeFood(hang,lie)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(hang == 19){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
}
printw("by shijintao\n");
printw("key =%d\n",key);
}
void addNode()
{
struct snake *new;
new =(struct snake *)malloc(sizeof(struct snake));
new->next=NULL;
switch(dir){
case UP:
new->hang=tail->hang-1;
new->lie=tail->lie;
tail->next=new;
tail = new;
break;
case DOWN:
new->hang=tail->hang+1;
new->lie=tail->lie;
tail->next=new;
tail = new;
break;
case LEFT:
new->lie=tail->lie-1;
new->hang=tail->hang;
tail->next=new;
tail = new;
break;
case RIGHT:
new->lie=tail->lie+1;
new->hang=tail->hang;
tail->next=new;
tail = new;
break;
}
}
void initFood() //初始化食物 和蛇一样要初始化位置 行和列
{
static int x=4;
static int y=4;
food.hang=x;
food.lie=y;
x+=2;
y+=2;
}
void initSnake()
{
struct snake *p;
while(head != NULL){
p=head;
head=head->next;
free(p);
}
initFood();
head = (struct snake *)malloc(sizeof(struct snake));
dir = RIGHT;
head->hang=2;
head->lie=2;
head->next=NULL;
tail = head;
addNode();
addNode();
}
void deleteNode()
{
struct snake *p;
p = head;
head = head->next;
free(p);
}
void moveSnake()
{
addNode();
if(ifSnakeFood(tail->hang,tail->lie)){
initFood();
}else{
deleteNode();
}
if(tail->hang==0||tail->hang==20||tail->lie==20||tail->lie==0){
initSnake();
}
}
//如果是在移动过程中吃到食物了,也是跟那个撞墙一个原理都要进行判断,但是此原理非彼原 理,不管是撞墙还是吃食物还是正常移动,都是要增加删除 ,把增加放在前面,就是为了如果吃到食物的话就可以增加,而不会删除,就会多一个节点,显示出吃了一个食物变成的效果。
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_RIGHT:
turn(RIGHT);
break;
case KEY_LEFT:
turn(LEFT);
break;
}
}
}
void * gamerefresh()
{
while(1){
moveSnake();
gamepic();
refresh();
usleep(100000);
}
}
int main()
{
pthread_t th1;
pthread_t th2;
initNcurse();
initSnake();
gamepic();
pthread_create(&th2,NULL,gamerefresh,NULL);
pthread_create(&th1,NULL,changeDir,NULL);
while(1);
getch();
endwin();
return 0;
}
好的 现在发现食物不随机,那么我们修改一下代码
要用到c语言的rand()函数
让rand()%20 取余数20 那么值永远都为1~19
非常符合食物的范围hang和lie的值
#include <curses.h>
#include <stdlib.h>
struct snake{
int hang;
int lie;
struct snake *next;
};
struct snake food;
struct snake *head;
struct snake *tail;
int key;
int dir;
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
int ifSnakeNode(int i,int j)
{
struct snake *p;
p = head;
while(p != NULL){
if(p->hang==i && p->lie==j){
return 1;
}
p=p->next;
}
return 0;
}
int ifSnakeFood(int i,int j)
{
if(food.hang==i && food.lie==j){
return 1;
}
return 0;
}
void gamepic()
{
int hang;
int lie;
move(0,0);
for(hang=0;hang<20;hang++){
if(hang == 0){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
if(hang>=0 && hang<=19){
for(lie=0;lie<=20;lie++){
if(lie==0||lie==20){
printw("|");
}else if(ifSnakeNode(hang,lie)){
printw("[]");
}else if(ifSnakeFood(hang,lie)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(hang == 19){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
}
printw("by shijintao\n");
printw("key =%d\n",key);
}
void addNode()
{
struct snake *new;
new =(struct snake *)malloc(sizeof(struct snake));
new->next=NULL;
switch(dir){
case UP:
new->hang=tail->hang-1;
new->lie=tail->lie;
tail->next=new;
tail = new;
break;
case DOWN:
new->hang=tail->hang+1;
new->lie=tail->lie;
tail->next=new;
tail = new;
break;
case LEFT:
new->lie=tail->lie-1;
new->hang=tail->hang;
tail->next=new;
tail = new;
break;
case RIGHT:
new->lie=tail->lie+1;
new->hang=tail->hang;
tail->next=new;
tail = new;
break;
}
}
void initFood() //这段代码就是关键了 将食物的位置随机化用了rand()函数 并%20 这样x的值永远都会 0<=?<20.
{
Int x=rand()%20;
int y=rand()%20;
food.hang=x;
food.lie=y;
}
void initSnake()
{
struct snake *p;
while(head != NULL){
p=head;
head=head->next;
free(p);
}
initFood();
head = (struct snake *)malloc(sizeof(struct snake));
dir = RIGHT;
head->hang=2;
head->lie=2;
head->next=NULL;
tail = head;
addNode();
addNode();
}
void deleteNode()
{
struct snake *p;
p = head;
head = head->next;
free(p);
}
void moveSnake()
{
addNode();
if(hasSnakeFood(tail->hang,tail->lie)){
initFood();
}else{
deleteNode();
}
if(tail->hang==0||tail->hang==20||tail->lie==20||tail->lie==0){
initSnake();
}
}
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_RIGHT:
turn(RIGHT);
break;
case KEY_LEFT:
turn(LEFT);
break;
}
}
}
void * gamerefresh()
{
while(1){
moveSnake();
gamepic();
refresh();
usleep(100000);
}
}
int main()
{
pthread_t th1;
pthread_t th2;
initNcurse();
initSnake(); //要注意蛇和食物都在这个函数里面初始化
gamepic();
pthread_create(&th2,NULL,gamerefresh,NULL);
pthread_create(&th1,NULL,changeDir,NULL);
while(1);
getch();
endwin();
return 0;
}