贪吃蛇(九)限制蛇身回头

贪吃蛇中,只能向一个方向前进,改变方向的时候,不可以回头,这是游戏规则。
上节中我们实现了自由的游走,但是方向可以向后走动,本节对此问题进行解决。

实现思路

判断当前用户即将想改变的方向和当前方向的绝对值是否一致。

#define UP     1
#define DOWN  -1
#define LEFT   2
#define RIGHT -2

例如当前方向为上,值为1,如果想限制不能让其方向为向下,则比较两者的绝对值。

#include"curses.h"
#include "stdlib.h"
#include "pthread.h"

#define UP     1
#define DOWN  -1
#define LEFT   2
#define RIGHT -2

struct SnakeNode
{
   
  int row;
  int col;
  struct SnakeNode* next; 
};


int key;  // user input
int dir;
struct SnakeNode* head = NULL;
struct SnakeNode* tail = NULL;

void addNode();
void mapinit();


void cursesinit()
{
   
   
  initscr();
  keypad(stdscr,1);
  // noecho();
}


void snakeinit()
{
   
  // dir init
  dir = RIGHT;

  // free 
  struct SnakeNode* p;
  while(head != NULL)
  {
   
    p = head;
    head = head->next;
    free(p);
  }
  // create node
  head = (struct SnakeNode*)malloc(sizeof(struct SnakeNode));
  head->row = 2;
  head->col = 2;
  head->next = NULL;
  tail = head;

  addNode();
  addNode();
}

void addNode()
{
     
  struct SnakeNode* node = (struct SnakeNode*)malloc(sizeof(struct SnakeNode));
  node->next = NULL;
  
  switch(dir)
  {
   
	case RIGHT:
	  node->row = tail->row;
          node->col = tail->col + 1;
	  break;
	case LEFT:
	  node->row = tail->row;
          node->col = tail->col - 1;
	  break;
	case DOWN:
	  node->row = tail->row + 1;
          node->col = tail->col;
	  break;
	case UP:
	  node->row = tail->row - 1;
          node->col = tail->col;
	  break;
  }
  
  tail->next = node;
  tail = node;
}

void deleteNode()
{
   
  struct SnakeNode* p;
  p = head;
  head = head->next;
  free(p);
}

void moveSnake()
{
   
  addNode();
  deleteNode();
  // if snake is over.
  if(tail->row == 0 || tail->col == 0 || tail->row >= 20 || tail->col >= 19)
  {
   
    snakeinit();
  }
}


int hasSnake(int row,int col)
{
   
  struct SnakeNode* p = head;
  while(p!=NULL)
  {
   
    if(row == p->row && col == p->col)
	return 1;
    p = p->next;
  }
  return 0; 
}


void mapinit()
{
   
  int row;
  int col;
  move(0,0);
  for(row = 0;row < 20;row++)
  {
   
   // one
   if(row == 0 || row == 19)
   {
   
     for(col = 0;col < 19;col++)
        printw("--");
   }
   // two
   else
   {
   
     for(col = 0;col < 20;col++)
     {
   
        if(col == 0 || col == 19 ) printw("|");
	else if(hasSnake(row,col))
	{
   
	  printw("[]");
	}
	else
	{
   
	  printw("  "); 
	}
     }
   }

    printw("\n");
  }

  printw("By hongzhe\n");
}


void refreshScreen()
{
   
  while(1)
  {
   
	moveSnake(); // snake forward 
	mapinit();   // fresh map
	refresh();   // fresh screen
	usleep(100000);	// sleep(0.5)
  }
  return;
}

void turnDir(int direction)
{
   
  if(abs(dir) != abs(direction))
	dir = direction;
}


void changeDir()
{
   
  
  while(1)
  {
   
    key = getch();
    switch(key)
    {
   
      case KEY_DOWN:
	printw("\rDOWN\t");
	turnDir(DOWN);
	break;
      case KEY_RIGHT:
	printw("\rRIGHT\t");
	turnDir(RIGHT);
	break;
      case KEY_LEFT:
	printw("\rLEFT\t");
	turnDir(LEFT);
	break;
      case KEY_UP:
	printw("\rUP\t");
	turnDir(UP);
	break;

    }
  }
  return;
}




int main()
{
   
  pthread_t t1;
  pthread_t t2;
 
  cursesinit();  
  snakeinit();
  mapinit();
  

  pthread_create(&t1,NULL,refreshScreen,NULL);
  pthread_create(&t2,NULL,changeDir,NULL);

  while(1);  // zu se main thread

  getch();  
  endwin();
  return 0;
}


学习打卡

在这里插入图片描述

相关推荐

  1. 贪吃小游戏

    2023-12-24 09:46:01       54 阅读
  2. 贪吃游戏

    2023-12-24 09:46:01       47 阅读
  3. python贪吃

    2023-12-24 09:46:01       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-24 09:46:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-24 09:46:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-24 09:46:01       82 阅读
  4. Python语言-面向对象

    2023-12-24 09:46:01       91 阅读

热门阅读

  1. 平方矩阵()

    2023-12-24 09:46:01       74 阅读
  2. hdfs.DataStreamer: Exception in createBlockOutputStream XXXXX

    2023-12-24 09:46:01       72 阅读
  3. 超声波测距系统

    2023-12-24 09:46:01       64 阅读
  4. 算法训练营Day23

    2023-12-24 09:46:01       67 阅读
  5. C语言内存管理以及堆空间栈空间区别

    2023-12-24 09:46:01       61 阅读
  6. 优化for循环(js的问题)

    2023-12-24 09:46:01       59 阅读
  7. Shell Tools of Missing Semester(二)

    2023-12-24 09:46:01       46 阅读