snake C

相关标签: # windows
main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
char username[20] = { 0 };
void welcome() {
gotoxy(10,5);
printf("/*****************************/");
gotoxy(14, 8);
printf("WELCOME TO THE GAME OF RETRO SNAKE");
gotoxy(14, 12);
printf("wasd控制蛇身上下左右运动,反向移动等于吃到自己导致游戏结束,除了wasd任意键暂停");
gotoxy(10, 20);
printf("/**********************************/");
gotoxy(14, 16);
printf("请输入姓名:");
scanf("%s",username);
system("cls");
}
int main() {
welcome();
creategraph(username);
creatfood();
initSnake();
clickControl(username);
return 0;
}
game.h
#pragma once
#include
#include
#include
#include
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'
struct Food
{
int x;
int y;
}food;
typedef struct Snakes {
int x;
int y;
struct Snakes* next;
}snake;
snake* head;
static int score = 0;
void gotoxy(int x, int y);
void gotoDelete(int x, int y);
void gotoprint(int x, int y);
void creategraph(char username[20]);
int clickControl(char username[20]);
void creatfood();
void initSnake();
game.c
#include "game.h"
void gotoxy(int x, int y) {
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void gotoDelete(int x, int y) {
gotoxy(x,y);
printf(" ");
}
void gotoprint(int x, int y) {
gotoxy(x, y);
printf("■");
}
void creategraph(char username[20]) {
int i;
//横向围墙 58 / 2 == 29;
for (i = 0; i < 58; i += 2) {
gotoprint(i, 0);
gotoprint(i, 26);
}
//竖墙 围墙 1-25 中间有25行 + 头尾两行 27行
for ( i = 1; i < 26; i++)
{
gotoprint(0,i);
gotoprint(56,i);
}
gotoxy(60, 5);
printf("hello,%s", username);
gotoxy(60,15);
printf("你当前的得分为: ");
gotoxy(76,15);
printf("%d", 0);
}
void creatfood() {
int flags = 0;
while (!flags) {
srand((unsigned int)time(NULL));
food.x = rand() % 53 + 2;
food.y = rand() % 25 + 1;
if (food.x % 2 != 0) {
food.x = food.x + 1;
}
//不能与蛇身重复
snake* p = head;
while (p) {
if (p->x == food.x && p->y == food.y) {
break;
}
p = p->next;
}
flags = 1;
}
gotoxy(food.x, food.y);
printf("#");
}
void initSnake() {
head = (snake*)malloc(sizeof(snake));
snake * p = (snake*)malloc(sizeof(snake));
snake * q = (snake*)malloc(sizeof(snake));
head->x = 16;
head->y = 15;
p->x = 16;
p->y = 16;
q->x = 16;
q->y = 17;
head->next = p;
p->next = q;
q->next = NULL;
gotoprint(16, 15);
gotoprint(16, 16);
gotoprint(16, 17);
}
void Finish(char username[20], int score) {
gotoxy(10, 10);
printf("/******************************/");
gotoxy(10, 20);
printf("/******************************/");
gotoxy(15, 14);
printf("GAME OVER o(* ̄▽ ̄*)o");
gotoxy(18, 16);
printf("%s, 你的得分 %d ", username, score);
gotoxy(15, 18);
printf("还不错哦, 继续努力O(∩_∩)O");
gotoxy(0, 27);
snake* p = head, * q;
while (p != NULL) {
q = p->next;
free(p);
p = q;
}
system("PAUSE");
}
void movingBoby(char ch) {
//坐标修改
int x = head->x;
int y = head->y;
int i = 0;
switch (ch) {
case UP:
y -= 1;
break;
case DOWN:
y += 1;
break;
case LEFT:
x -= 2;
break;
case RIGHT:
x += 2;
break;
default:
ch = 0;
break;
}
if (ch) {
//消除尾节点
snake* p = head;
while (p->next->next != NULL) {
p = p->next;
}
snake* q = p->next;
gotoDelete(q->x, q->y);
free(q);
p->next = NULL;
snake* newNode = (snake*)malloc(sizeof(snake));
newNode->x = x;
newNode->y = y;
newNode->next = head;
head = newNode;
gotoprint(x, y);
Sleep(100);
if (score <= 10) {
Sleep(500);
}
else if (score > 10 && score <= 50) {
Sleep(300);
}
else if (score > 50 && score <= 200) {
Sleep(100);
}
else {
Sleep(50);
}
}
}
int collisionDetect(char username[20]) {
if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26) {
Finish(username, score);
return 0;
}
snake* p = head->next;
while (p != NULL) {
if (head->x == p->x && head->y == p->y) {
Finish(username, score);
return 1;
}
p = p->next;
}
if (head->x == food.x && head->y == food.y) {
creatfood();
snake* newNode = (snake*)malloc(sizeof(snake));
newNode->next = NULL;
snake* p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = newNode;
score += 10;
gotoxy(76, 15);
printf("%d", score);
}
return 0;
}
int clickControl(char username[20]) {
char ch ='w';
while (1) {
if (collisionDetect(username) == 1)
return 0;
if (_kbhit()) {
ch = _getch();
}
movingBoby(ch);
}
return 1;
}
文章来源: https://blog.51cto.com/u_11158451/5947368
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报