C语言 贪吃蛇小游戏

/ 0评 / 0

这只是一个简单的C语言小游戏,供大家参考,新手写的不好勿喷。

以下是全部代码:

//头文件
#ifndef __SNACK_H__
#define __SNACK_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <conio.h>
#include<windows.h>


#define WIDE 60
#define HIGH 20
struct BODY {
    int x;
    int y;
};
struct SNACK {
    struct BODY body[WIDE*HIGH];
    int size;
}snack;
struct FOOD{
    int x;
    int y;
}food;
int score = 0;
int x1 = 0;
int y2 = 0;
int lastX = 0;
int lastY = 0;
void initFood(void);
void initSnack(viod);
void initUI(void);
void initWall(void);
void PlayGame(void);
void Showsore(void);
#endif
//源文件
#include "snack.h"
void initSnack(viod)
{
    snack.size = 2;
    snack.body[0].x = WIDE/ 2;
    snack.body[0].y = HIGH / 2;
    snack.body[1].x = WIDE / 2 - 1;
    snack.body[1].y = HIGH / 2;
    return;
}
void initFood(void)
{
    food.x = rand()%WIDE - 1;
    food.y = rand() % HIGH - 1;
    return;
}
void initUI(void)
{
    COORD coord = {0};
    for (size_t i = 0; i < snack.size; i++)
    {   
        coord.X = snack.body[i].x;
        coord.Y = snack.body[i].y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
        if (i == 0)
        {
            putchar('@');
        }
        else{
            putchar('*');
        }
    }
    coord.X = lastX;
    coord.Y = lastY;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    putchar(' ');


    coord.X = food.x;
    coord.Y = food.y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    putchar('#');

}
void initWall(void)
{
    for (size_t i = 0; i <=  HIGH; i++)
    {
        for (size_t j = 0; j <=WIDE; j++)
        {
            if (j == WIDE)
            {
                printf("|");
            }
            else if (i == HIGH)
            {
                printf("_");
            }
            else{
                printf(" ");
            }
        }
        printf("\n");
    }
}
void PlayGame(void)
{
    char key = 'a';

    while (snack.body[0].x >= 0 && snack.body[0].x < WIDE &&snack.body[0].y >= 0 && snack.body[0].y < HIGH)
    {
        // initWall();
        initUI();
        if (_kbhit()){
        fflush(stdin);
        key = _getch();

    }
        switch (key)
    {
    case 'w': x1 = 0; y1 = -1; break;
    case 's': x1= 0; y1= +1; break;
    case 'd': x1 = +1; y1 =0; break;
    case 'a': x1 =-1; y1 = 0; break;
    default:
        break;
    }

        for (size_t i = 1; i < snack.size; i++)
        {
            if (snack.body[0].x == snack.body[i].x &&snack.body[0].y == snack.body[i].y)
            {
                return;
            }
        }
        if (snack.body[0].x == food.x&&snack.body[0].y == food.y)
        {
            initFood();
            snack.size++;
            score++;
        }
        lastX = snack.body[snack.size - 1].x;
        lastY = snack.body[snack.size - 1].y;
        for (size_t i = snack.size-1; i > 0; i--)
        {
            snack.body[i].x = snack.body[i - 1].x;
            snack.body[i].y = snack.body[i - 1].y;
        }
        snack.body[0].x += x1;
        snack.body[0].y += y1;
        Sleep(300);
    }
            return;
}
void Showsore()
{
    COORD corrd;
    corrd.X = 0;
    corrd.Y = HIGH + 2;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), corrd);
    printf("GAME OVER\n");
    printf("成绩为:%d\n", score);
}
int main(void)
{
    CONSOLE_CURSOR_INFO cci;
    cci.bVisible = FALSE;
    cci.dwSize = sizeof(cci);
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
    srand(time(NULL));
    initFood();
    initSnack();
    initWall();
    initUI();
    PlayGame();
     Showsore();
    system("pause");
    return 0;
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注