funtion.h
#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<iostream>
#define WIDTH 20
#define HEIGHT 15//地图的宽和高
#define SPEED 2000
typedef enum {
WALL,
EMPTY,
SNAKE_BODY,
FOOD
}Mapelement;//定义一个枚举类型Mapelement
typedef struct {
public:
Mapelement **elements;
}Map;//Map的结构体成员是一个Mapelement数组
typedef struct {
int x, y;//记录蛇的位置
int flag;//记录蛇是否死亡
char way;//记录蛇移动方向
}Snake;
void initmap(Map*& map);//初始化地图
void mapdisplay(Map*& map);//地图打印函数
void drawFood(Map*& map);//生成食物
void initSnake(Snake*& head,Snake*& tail); //初始化蛇身
void updateSnake(Map*& map,Snake*& head,Snake*& tail); //更新绘制地图上的蛇
void ctrlSnake(Snake*& head);//控制蛇移动
function.cpp
#include"function.h"
void initmap(Map*& map)
{
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++)
{
if (i == 0 || j == 0 || i == HEIGHT - 1 || j == WIDTH - 1)
{
map->elements[i][j] = WALL;
}
map->elements[i][j] = EMPTY;
}
}
map->elements[1][1] = SNAKE_BODY;
map->elements[1][2] = SNAKE_BODY;
}
void mapdisplay(Map*& map)
{
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
switch (map->elements[i][j]) {
case EMPTY:
printf(" ");
break;
case SNAKE_BODY:
printf("*");
break;
case FOOD:
printf("#");
break;
case WALL:
printf("+");
}
}
printf("\n");
}
}
void drawFood(Map*& map)
{
int res = 1;
srand(time(NULL));
while (res)
{
int x, y;
x = rand() % (HEIGHT - 3) + 1;
y = rand() % (WIDTH - 3) + 1;
if (map->elements[x][y] != SNAKE_BODY)
{
map->elements[x][y] = FOOD;
res = 0;
}
}
}
void initSnake(Snake*& head,Snake*& tail)
{
head->x = 1;
head->y = 2;
tail->flag = head->flag = 1;
head->way = 'd';
tail->x = 1;
tail->y = 1;
tail->way = 'd';
}
void updateSnake(Map*& map, Snake*& head, Snake*& tail)
{
switch (head->way) {
case 'a':
head->y -= 1;
break;
case 'd':
head->y += 1;
break;
case 's':
head->x += 1;
break;
case'w':
head->x -= 1;
break;
}
if (map->elements[head->x][head->y] == WALL || map->elements[head->x][head->y] == SNAKE_BODY)
{
head->flag = 0;
}
if (map->elements[head->x][head->y] == FOOD)
{
map->elements[head->x][head->y] = SNAKE_BODY;
}
else {
map->elements[head->x][head->y] = SNAKE_BODY;
map->elements[head->x][head->y] = EMPTY;
}
}
void ctrlSnake(Snake*& head)
{
char key = 0;
if (_kbhit()) {
char key = _getch();
}
switch (key) {
case 'a':
head->way = 'a';
break;
case 'd':
head->way = 'd';
break;
case 's':
head->way = 's';
break;
case'w':
head->way = 'w';
break;
}
}
main.cpp
#include"function.h"
int main()
{
printf("欢迎来到贪吃蛇小游戏,按1开始游戏");
int res;
scanf_s("%d", &res);
if (res != 1) { return 0; }
Snake* head = (Snake*)malloc(sizeof(Snake));
Snake* tail = (Snake*)malloc(sizeof(Snake));
Map* map = (Map*)malloc(sizeof(Map));
map->elements = (Mapelement**)malloc(HEIGHT * sizeof(Mapelement*));
if (map->elements == NULL) {
// 内存分配失败的处理
free(map); // 释放先前分配的内存
return 1;
}
for (int i = 0; i < HEIGHT; i++) {
map->elements[i] = (Mapelement*)malloc(WIDTH * sizeof(Mapelement));
if (map->elements[i] == NULL) {
// 内存分配失败的处理
// 释放先前分配的内存
for (int j = 0; j < i; j++) {
free(map->elements[j]);
}
free(map);
return 1;
}
}
initSnake(head, tail);
initmap(map);
while (head->flag)
{
drawFood(map);
mapdisplay(map);
Sleep(SPEED);
ctrlSnake(head);
updateSnake(map, head, tail);
}
// 释放内存
for (int i = 0; i < HEIGHT; i++) {
free(map->elements[i]);
}
free(map);
return 0;
}
标签:map,head,elements,22,Map,int,贪吃蛇,Snake,打卡
From: https://www.cnblogs.com/wlxdaydayup/p/17425384.html