#include <graphics.h>#include <conio.h>#include <time.h>#include <stdio.h>#include<iostream>#in

本文介绍了使用C++编程实现贪吃蛇游戏的详细过程,包括游戏界面、游戏规则、蛇与食物的动态更新、用户输入处理、碰撞检测和游戏结束条件。同时,游戏还引入了背景音乐,通过`Winmm.lib`库播放声音文件,增强了游戏体验。
摘要由CSDN通过智能技术生成

#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include<iostream>
#include<string.h>
#include<time.h>
#include <tchar.h>
#include <mmsystem.h>//导入声音头文件
#pragma comment(lib,"Winmm.lib")


#define MAP_HEIGHT 30
#define MAP_WIDE 30
#define SIZE 16
#define SPEED 200

//定义蛇的结点
typedef struct Snakes
{
    int x;//蛇的结点的位置坐标
    int y;
    struct Snakes* next;
}snake;

snake* head, * tail;
//定义食物的结构
struct Food
{
    int x;
    int y;
}food;

int grow = 0; 
int yuan[100];
int aa = -1;
//int COUNT = 0;
char ch1 = 1;
int a1 = 0, a2 = 1, a3 = 0;;//grow =0 表示没吃到食物,grow = 1表示吃到食物
TCHAR str[100];
TCHAR str1[100];
TCHAR str2[100];
TCHAR str3[100];
int score = 0;
int count=1;
char ch = 'a';//初始方向向左
void BGM()
{
    PlaySound(TEXT("平凡之路.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);


}
void login()                                                  //登录界面//
{
    char name[100];
    initgraph(1200, 900);
    setbkcolor(BLUE);
    cleardevice();
    settextcolor(WHITE);
    setfont(100, 0, _T("华文行楷"));
    outtextxy(300, 150, _T("WELCOME"));
    wchar_t username[1000];
    errno_t err;
    //FILE* fb;
    InputBox(username, 100, _T("请输入你的昵称"));
    FILE* fb = fopen("username.txt", "a");
    fprintf(fb, "%S ", username);
    fprintf(fb, "%c", '\n');
    fclose(fb);
   // err = fopen_s(&fp, "username.txt", "a");
    //fprintf(fp, "%d", username);
    //fclose(fp);
}
void Count()
{

    FILE* fp11 = fopen("count.txt", "r");
    fscanf(fp11, "%d", &aa);
    aa = aa + 1;
    fclose(fp11);
    FILE* fp111 = fopen("count.txt", "w");
    fprintf(fp111, "%d", aa);
    fclose(fp111);


}
//void Count()
//{
   
   // FILE* fp11 = fopen("count.txt", "r+");
   // fscanf(fp11, "%d", &aa);
   // aa = aa + 1;
   // fclose(fp11);
   // FILE* fp111 = fopen("count.txt", "w");
   // fprintf(fp111, "%d", aa);
   // fclose(fp111);

//}

void DrawMap()//打印地图
{
    setbkcolor(YELLOW);
    cleardevice(); //调用清屏cleardevice用背景色刷新背景
    for (int i = 0; i < MAP_WIDE; i++)
    {
        setfillcolor(BLUE);
        //上边框
        fillrectangle(i * SIZE, 0, (i + 1) * SIZE, SIZE);
        //下边框
        fillrectangle(i * SIZE, (MAP_HEIGHT - 1) * SIZE, (i + 1) * SIZE, MAP_HEIGHT * SIZE);
    }
    for (int i = 0; i < MAP_HEIGHT; i++)
    {
        setfillcolor(BLUE);
        //左边框
        fillrectangle(0, i * SIZE, SIZE, (i + 1) * SIZE);
        //右边框
        fillrectangle((MAP_WIDE - 1) * SIZE, i * SIZE, MAP_WIDE * SIZE, (i + 1) * SIZE);

    }
    settextcolor(GREEN);
    _stprintf_s(str1, _T("%d"), score);

    outtextxy(500, 20, str1);
}
//打印结点
void PrintNode(int x, int y)
{
    setfillcolor(RED);
    fillcircle(x * SIZE, y * SIZE, SIZE / 2);
    setcolor(WHITE);
    circle(x * SIZE, y * SIZE, SIZE / 2);
}
//删除结点
void DeleteNode(int x, int y)
{
    setfillcolor(YELLOW);
    fillcircle(x * SIZE, y * SIZE, SIZE / 2);
    setcolor(YELLOW);
    circle(x * SIZE, y * SIZE, SIZE / 2);

}

void InitMap()
{
    //初始化蛇
    head = (snake*)malloc(sizeof(snake));
    head->x = (MAP_WIDE) / 2;
    head->y =

  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值