机缘
刚开始接触CSDN是为了在上面找一些解题思路,学了一段C语言后,为了巩固所学的知识点,会把那些知识点用博客写出来,加深自己的印象,也锻炼一下自己的表达能力。
收获
1.获得了92个粉丝的关注
2.获得了137个赞和5391的访问量
3.因为我还是初学者,还没有过多的交流。
日常
我并没有把写博客当成一种负担,写博客已成为我生活中的一部分,写博客可以及时找到对知识点理解的不足。由于我现在还是初学者,所以我会把主要精力放在学习新的技术,平常对博客先进行构思,然后在周六和周日来创作。
成就
目前我写的最长的代码就是这个贪吃蛇代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<stdbool.h>
#include<locale.h>
#include<time.h>
#define pos_x 24
#define pos_y 5
#define BODY L'●'
#define MAP L'□'
#define FOOD L'★'
enum direction
{
UP,
DOWN,
LEFT,
RIGHT
};
enum status
{
OK,
KILL_BY_WALL,
KILL_BY_SELF,
END_NORMAL
};
typedef struct SnakeNode
{
struct SnakeNode* next;
int x;
int y;
}SnakeNode,*pSnakeNode;
typedef struct Snake
{
pSnakeNode pSnake;
pSnakeNode pFood;
enum direction dir;
enum status sta;
int score;
int food_weight;
int sleep_time;
}Snake,*PSnake;
void SetPos(int x, int y);//设置光标位置的函数
void GameStart(PSnake ps);//蛇运动前的准备函数
void GameRun(PSnake ps);//蛇运动函数
void GameEnd(PSnake ps);//游戏善后
#define _CRT_SECURE_NO_WARNINGS
#include"snake.h"
void SetPos(int x, int y)//设置光标位置的函数
{
HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = { x,y };
SetConsoleCursorPosition(houtput, pos);
}
void WelcomePrint()//打印欢迎界面和功能介绍界面
{
SetPos(40, 14);
wprintf(L"欢迎来到贪吃蛇");
SetPos(39, 20);
system("pause");
system("cls");
SetPos(25, 14);
wprintf(L"用↑.↓.←.↑来控制蛇的方向,F3加速,F4减速");
SetPos(25, 15);
wprintf(L"加速可获得更高的分数,反之获得分数减少");
SetPos(25, 16);
wprintf(L"按空格暂停,ESC退出");
SetPos(39, 20);
system("pause");
system("cls");
}
void CreateMap()//打印地图
{
for (int i = 0; i < 29; i++)
{
wprintf(L"%lc", MAP);
}
SetPos(0, 26);
for (int i = 0; i < 29; i++)
{
wprintf(L"%lc", MAP);
}
for (int i = 1; i <=25; i++)
{
SetPos(0, i);
wprintf(L"%lc", MAP);
}
for (int i = 1; i <= 25; i++)
{
SetPos(56, i);
wprintf(L"%lc", MAP);
}
}
void InItSnake(PSnake ps)//初始化蛇相关数据
{
pSnakeNode cur = NULL;
for (int i = 0; i < 5; i++)
{
cur=(pSnakeNode)malloc(sizeof(SnakeNode));
if (cur == NULL)
{
perror("InItSnake()::malloc()");
return;
}
cur->next = NULL;
cur->x = pos_x+i*2;
cur->y = pos_y;
if (ps->pSnake == NULL)
{
ps->pSnake = cur;
}
else
{
cur->next = ps->pSnake;
ps->pSnake = cur;
}
}
cur = ps->pSnake;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%lc",BODY);
cur = cur->next;
}
ps->food_weight = 10;
ps->score = 0;
ps->sleep_time = 200;
ps->dir = RIGHT;
ps->sta = OK;
}
void CreateFood(PSnake ps)//创建食物
{
pSnakeNode food=(pSnakeNode)malloc(sizeof(SnakeNode));
if (food == NULL)
{
perror("CreateFood()::malloc()");
return;
}
food->next = NULL;
int x = 0;
int y = 0;
again:
do
{
x = rand() % 53 + 2;
y = rand() % 25 + 1;
} while (x % 2 != 0);
pSnakeNode cur = ps->pSnake;
while (cur)
{
if (cur->x == x && cur->y == y)
{
goto again;
}
cur = cur->next;
}
food->x = x;
food->y = y;
ps->pFood = food;
SetPos(food->x, food->y);
wprintf(L"%lc", FOOD);
}
void PrintHelpInfo()
{
SetPos(64, 14);
wprintf(L" 不能撞墙,不能撞到自己");
SetPos(64, 15);
wprintf(L"按↑.↓.←.↑来控制蛇的方向");
SetPos(64, 16);
wprintf(L"F3加速,F4减速");
SetPos(64, 17);
wprintf(L"按空格暂停,ESC退出");
SetPos(64, 19);
wprintf(L"刘梓奕制作");
}
void GameStart(PSnake ps)//蛇运动前的准备函数
{
//调整控制台的大小
system("mode con cols=100 lines=30");
//设置控制台名称
system("title 贪吃蛇");
//改变光标信息,为了美观,隐藏光标
HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorinfo;
GetConsoleCursorInfo(houtput, &cursorinfo);
cursorinfo.bVisible = false;
SetConsoleCursorInfo(houtput, &cursorinfo);
//打印欢迎界面和功能介绍
WelcomePrint();
//打印地图
CreateMap();
//初始化贪吃蛇有关信息
InItSnake(ps);
//创建食物
CreateFood(ps);
}
#define KeyCheck(vk)((GetAsyncKeyState(vk)&1)?1:0)
void CheckSpace()
{
while (1)
{
Sleep(200);
if (KeyCheck(VK_SPACE))
{
break;
}
}
}
int IsFood(PSnake ps, pSnakeNode con)//判断下一个坐标是否为食物
{
return (ps->pFood->x == con->x && ps->pFood->y == con->y);
}
void EatFood(PSnake ps,pSnakeNode con)//吃食物
{
ps->pFood->next = ps->pSnake;
ps->pSnake = ps->pFood;
free(con);
con = NULL;
pSnakeNode cur = ps->pSnake;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%lc", BODY);
cur = cur->next;
}
ps->score += ps->food_weight;
CreateFood(ps);
}
void NoFood(PSnake ps, pSnakeNode con)//吃空白节点
{
con->next = ps->pSnake;
ps->pSnake = con;
pSnakeNode cur = ps->pSnake;
while (cur->next->next!=NULL)
{
SetPos(cur->x, cur->y);
wprintf(L"%lc", BODY);
cur = cur->next;
}
SetPos(cur->next->x, cur->next->y);
printf(" ");
free(cur->next);
cur->next = NULL;
}
void KillByWall(PSnake ps)//判断是否撞墙
{
if(ps->pSnake->x == 0 || ps->pSnake->x == 56\
|| ps->pSnake->y == 0 || ps->pSnake->y == 26)
{
ps->sta = KILL_BY_WALL;
}
}
void KillBySelf(PSnake ps)//判断是否撞到自己
{
pSnakeNode cur = ps->pSnake->next;
while (cur)
{
if (ps->pSnake->x == cur->x && ps->pSnake->y == cur->y)
{
ps->sta = KILL_BY_SELF;
break;
}
cur = cur->next;
}
}
void SnakeMove(PSnake ps)//蛇动一步的函数
{
pSnakeNode nextnode=(pSnakeNode)malloc(sizeof(SnakeNode));
if (nextnode == NULL)
{
perror("SnakeMove()::malloc()");
return;
}
nextnode->next = NULL;
switch (ps->dir)
{
case UP:
nextnode->x = ps->pSnake->x;
nextnode->y = ps->pSnake->y - 1;
break;
case DOWN:
nextnode->x = ps->pSnake->x;
nextnode->y = ps->pSnake->y + 1;
break;
case LEFT:
nextnode->x = ps->pSnake->x - 2;
nextnode->y = ps->pSnake->y;
break;
case RIGHT:
nextnode->x = ps->pSnake->x + 2;
nextnode->y = ps->pSnake->y;
break;
}
//下一个坐标为食物
if (IsFood(ps, nextnode))
{
//吃食物
EatFood(ps, nextnode);
}
//下一个坐标为空
else
{
//吃空白节点
NoFood(ps, nextnode);
}
//判断是否撞墙
KillByWall(ps);
//判断是否撞到自己
KillBySelf(ps);
}
//蛇运动函数
void GameRun(PSnake ps)
{
PrintHelpInfo();//帮助信息
//判断按键状态
do
{
SetPos(64, 10);
printf("总分为:%-2d\n", ps->score);
SetPos(64, 11);
printf("当前一个食物的分数:%-2d\n", ps->food_weight);
if (KeyCheck(VK_UP) && ps->dir != DOWN)
{
ps->dir = UP;
}
else if (KeyCheck(VK_DOWN) && ps->dir != UP)
{
ps->dir = DOWN;
}
else if (KeyCheck(VK_LEFT) && ps->dir != RIGHT)
{
ps->dir = LEFT;
}
else if (KeyCheck(VK_RIGHT) && ps->dir != LEFT)
{
ps->dir = RIGHT;
}
else if (KeyCheck(VK_SPACE))
{
CheckSpace();
}
else if (KeyCheck(VK_ESCAPE))
{
ps->sta = END_NORMAL;
}
else if (KeyCheck(VK_F3))
{
if (ps->sleep_time > 80)
{
ps->sleep_time -= 30;
ps->food_weight += 2;
}
}
else if (KeyCheck(VK_F4))
{
if (ps->food_weight > 2)
{
ps->sleep_time += 30;
ps->food_weight -= 2;
}
}
//蛇移动一步的函数
SnakeMove(ps);
Sleep(ps->sleep_time);//决定了蛇移动的快慢
} while (ps->sta == OK);
}
//游戏善后
void GameEnd(PSnake ps)
{
SetPos(24, 12);
switch (ps->sta)
{
case KILL_BY_WALL:
printf("很遗憾,您撞到了墙\n");
break;
case KILL_BY_SELF:
printf("很遗憾,您撞到了自己\n");
break;
case END_NORMAL:
printf("正常退出,期待您的再次使用\n");
break;
}
pSnakeNode cur = ps->pSnake;
while (cur)
{
pSnakeNode del = cur;
cur = cur->next;
free(del);
del = NULL;
}
free(ps->pFood);
ps->pFood = NULL;
}
#define _CRT_SECURE_NO_WARNINGS
#include"snake.h"
void test()
{
int ch = 0;
do
{
system("cls");
Snake snake = { 0 };
GameStart(&snake);
GameRun(&snake);
GameEnd(&snake);
SetPos(20, 15);
printf("还想再来一次吗?(Y/N):");
ch=getchar();
while (getchar() != '\n')
{
;
}
} while (ch == 'Y' || ch == 'y');
SetPos(0, 27);
}
int main()
{
setlocale(LC_ALL, "");
srand((unsigned int)time(NULL));
test();
return 0;
}
憧憬
我希望以后可以写出质量更好的博客,并成为一个优秀的C++后台工程师。