c++中pHead指向第一个元素,并非是一个空

pHead->val就是第一个元素值。


一直困扰好久,今天测试了一下。不然老是影响写程序。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于C语言的贪吃蛇游戏代码: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <Windows.h> #define ROWS 20 #define COLS 30 #define MAX_LENGTH (ROWS * COLS) typedef struct node { int row; int col; struct node* next; }node; node* head; node* food; int snakeLength; int score; int speed; char map[ROWS][COLS]; void setCursorPosition(int x, int y) //设置光标位置函数 { HANDLE hOutput; COORD loc; loc.X = x; loc.Y = y; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOutput, loc); } void drawMap() //绘制游戏地图 { int i, j; setCursorPosition(0, 0); for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { if (i == 0 || i == ROWS - 1 || j == 0 || j == COLS - 1) map[i][j] = '#'; else map[i][j] = ' '; } } for (node* p = head; p; p = p->next) { map[p->row][p->col] = '*'; } map[food->row][food->col] = '@'; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { printf("%c", map[i][j]); } printf("\n"); } printf("Score: %d\n", score); printf("Length: %d\n", snakeLength); printf("Speed: %d\n", 550 - speed * 50); } int isHitWall(node* pHead) //判断是否撞墙 { return pHead->row == 0 || pHead->row == ROWS - 1 || pHead->col == 0 || pHead->col == COLS - 1; } int isHitSelf(node* pHead, node* pFood) //判断是否撞到自己 { node* p = pHead->next; while (p != NULL) { if (p->row == pHead->row && p->col == pHead->col) { return 1; } p = p->next; } if (pHead->row == pFood->row && pHead->col == pFood->col) { return 1; } return 0; } void generateFood() //生成食物 { node* p = head; while (p) { int row = rand() % (ROWS - 2) + 1; int col = rand() % (COLS - 2) + 1; if (map[row][col] == ' ') { food->row = row; food->col = col; break; } p = p->next; } } void changeSpeed() //改变游戏速度 { if (snakeLength >= 5 && snakeLength < 10) { speed = 1; } else if (snakeLength >= 10 && snakeLength < 15) { speed = 2; } else if (snakeLength >= 15 && snakeLength < 20) { speed = 3; } else if (snakeLength >= 20 && snakeLength < 25) { speed = 4; } else if (snakeLength >= 25) { speed = 5; } } int main() { int direction = 0; //0 - up, 1 - down, 2 - left, 3 - right head = (node*)malloc(sizeof(node)); head->row = ROWS / 2; head->col = COLS / 2; head->next = NULL; food = (node*)malloc(sizeof(node)); score = 0; speed = 0; snakeLength = 1; generateFood(); drawMap(); while (1) { if (_kbhit()) { int ch = _getch(); if (ch == 27) { //按ESC键退出游戏 break; } else if (ch == 'w' || ch == 'W') { direction = 0; } else if (ch == 's' || ch == 'S') { direction = 1; } else if (ch == 'a' || ch == 'A') { direction = 2; } else if (ch == 'd' || ch == 'D') { direction = 3; } } Sleep(550 - speed * 50); //根据游戏速度控制Sleep时间,使得游戏速度不会过快或过慢 node* tail = head; while (tail->next) { tail = tail->next; } if (tail->row == food->row && tail->col == food->col) { //吃到食物,生成新的食物 node* newTail = (node*)malloc(sizeof(node)); newTail->row = tail->row; newTail->col = tail->col; tail->next = newTail; newTail->next = NULL; snakeLength++; score = score + 10; generateFood(); changeSpeed(); } else { //没有吃到食物,尾部移动即可 node* p = head; while (p->next != tail) { p = p->next; } map[tail->row][tail->col] = ' '; p->next = NULL; tail->row = head->row; tail->col = head->col; tail->next = head->next; head->next = tail; head->row = head->row + (direction == 0 ? -1 : (direction == 1 ? 1 : 0)); head->col = head->col + (direction == 2 ? -1 : (direction == 3 ? 1 : 0)); if (isHitWall(head) || isHitSelf(head, food)) { //游戏结束 printf("GAME OVER!\n"); break; } } drawMap(); } return 0; } ``` 注意:此代码可能存在一些漏洞和问题,仅供参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值