贪吃蛇代码解析:C语言知识巩固

#include "stdafx.h"




int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}


#include "stdafx.h"
#include<windows.h>


#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
// 刷新当前屏幕 //见博客http://www.cnblogs.com/uniqueliu/archive/2011/07/10/2102238.html

inline void Refresh(char q[][22], int grade, int gamespeed)      //c语言 inline函数的总结http://blog.csdn.net/yuan1125/article/details/6225993

{    

    system("cls");     //  清屏  实际上是把屏幕翻页 看不到而已
 int i,j;
 cout << endl;    // 就是回车的意思~相当于C语言里面的printf("");

 for(i=0;i<22;i++) 

 {

     cout << "\t";     //  \t \r \n都是转义字符,空格就是单纯的空格,输入时可以输入空格  \t 的意思是横向跳到下一制表符位置  \r 的意思是回车  \n 的意思是回车换行
  for(j=0;j<22;j++)
   cout<<q[i][j]<<' ';    //  输出贪吃蛇棋盘
        if(i==0) cout << "\t等级为:" << grade;
        if(i==4) cout << "\t自动前进时间";
        if(i==6) cout << "\t间隔为:" << gamespeed << "ms";
  cout<<endl;
 }
}

int main()

{

    char tcsQipan[22][22];     //  贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)
    int i,j;
    for(i=1;i<=20;i++)
        for(j=1;j<=20;j++)
            tcsQipan[i][j]=' ';     //     初始化贪吃蛇棋盘中间空白部分
    for(i=0;i<=21;i++)
        tcsQipan[0][i] = tcsQipan[21][i] = '-';      //初始化贪吃蛇棋盘上下墙壁
    for(i=1;i<=20;i++)
        tcsQipan[i][0] = tcsQipan[i][21] = '|';      //初始化贪吃蛇棋盘左右墙壁

    int tcsZuobiao[2][100];     //蛇的坐标数组  //int tcsZuobiao[2][100];后面的[100]是数组???

    for(i=0; i<4; i++)

{

        tcsZuobiao[0][i] = 1;
        tcsZuobiao[1][i] = i + 1;
    }
    int head = 3,tail = 0;
    for(i=1;i<=3;i++)
        tcsQipan[1][i]='*';    //蛇身
    tcsQipan[1][4]='#';       //蛇头

    int x1, y1;           // 随机出米

    srand(time(0));      //  srand(time(0)) 就是给这个算法一个启动种子,也就是算法的随机种子数,有这个数以后才可以产生随机数

    do

{

  x1=rand()%20+1;
  y1=rand()%20+1;

 }

while(tcsQipan[x1][y1]!=' ');

 tcsQipan[x1][y1]='*';
 cout<<"\n\n\t\t贪吃蛇游戏即将开始 !"<<endl;//准备开始;;
 long start;
    int grade = 1, length = 4;
    int gamespeed = 500;  //前进时间间隔

 for(i=3;i>=0;i--)

{

  start=clock();
  while(clock()-start<=1000);
  system("cls");
  if(i>0)
   cout << "\n\n\t\t进入倒计时:" << i << endl;
  else
   Refresh(tcsQipan,grade,gamespeed);
 }
    int timeover;
    char direction = 77;  // 初始情况下,向右运动
    int x,y;

    while(1)

{

        timeover = 1;
        start = clock();
        while((timeover=(clock()-start<=gamespeed))&&!kbhit());
             //如果有键按下或时间超过自动前进时间间隔则终止循环

        if(timeover)

{

            getch();direction = getch();
        }

        switch(direction)

{

        case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break;     // 向上
        case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break;      // 向下
        case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break;      // 向左
        case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1;           // 向右
        }
        if(!(direction==72||direction==80||direction==75 ||direction==77)){   //  按键非方向键
            cout << "\tGame over!" << endl;return 0;
        }

        if(x==0 || x==21 ||y==0 || y==21)    // 碰到墙壁

    {   

            cout << "\tGame over!" << endl;

            return 0;

        }
        if(tcsQipan[x][y]!=' '&&!(x==x1&&y==y1)){ //   蛇头碰到蛇身

            cout << "\tGame over!" << endl;

            return 0;

        }

        if(x==x1 && y==y1)    //  吃米,长度加1

{      

            length ++;

            if  (length>=8)

{

                length -= 8;
                grade ++;
                if(gamespeed>=200)
                    gamespeed = 550 - grade * 50; // 改变自动前进时间间隔
            }
            tcsQipan[x][y]= '#';
            tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';
            head = (head+1)%100;
            tcsZuobiao[0][head] = x;
            tcsZuobiao[1][head] = y;
            do
            {
                x1=rand()%20+1;
                y1=rand()%20+1;
            }while(tcsQipan[x1][y1]!=' ');
            tcsQipan[x1][y1]='*';
            Refresh(tcsQipan,grade,gamespeed);
        }

        else        //  不吃米

{    

            tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=' ';
            tail=(tail+1)%100;
            tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]='*';
            head=(head+1)%100;
            tcsZuobiao[0][head]=x;
            tcsZuobiao[1][head]=y;
            tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]='#';
            Refresh(tcsQipan,grade,gamespeed);
        }
    }
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用windows api 做的贪吃蛇 #include #include"resource.h" #include"Node.h" #include #include TCHAR szAppname[] = TEXT("Snack_eat"); #define SIDE (x_Client/80) #define x_Client 800 #define y_Client 800 #define X_MAX 800-20-SIDE //点x的范围 #define Y_MAX 800-60-SIDE //点y的范围 #define TIME_ID 1 #define SECOND 100 #define NUM_POINT 10 //点的总个数 #define ADD_SCORE 10 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hwnd; //窗口句柄 MSG msg; //消息 WNDCLASS wndclass; //窗口类 HACCEL hAccel;//加速键句柄 wndclass.style = CS_HREDRAW | CS_VREDRAW; //窗口的水平和垂直尺寸被改变时,窗口被重绘 wndclass.lpfnWndProc = WndProc; //窗口过程为WndProc函数 wndclass.cbClsExtra = 0; //预留额外空间 wndclass.cbWndExtra = 0; //预留额外空间 wndclass.hInstance = hInstance; //应用程序的实例句柄,WinMain的第一个参数 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //设置图标 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //载入预定义的鼠标指针 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //设置画刷 wndclass.lpszMenuName = szAppname; //设置菜单 wndclass.lpszClassName = szAppname; //设置窗口类的名字 if (!RegisterClass(&wndclass))//注册窗口类 { MessageBox(NULL, TEXT("这个程序需要windows NT!"), szAppname, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppname, TEXT("Snack_eat"),//CreateWindow函数调用时,WndProc将受到WM_CREATE WS_OVERLAPPEDWINDOW&~WS_THICKFRAME& ~WS_MAXIMIZEBOX,//普通的层叠窗口&禁止改变大小&禁止最大化 CW_USEDEFAULT, //初始x坐标(默认) CW_USEDEFAULT, //初始y坐标 x_Client, //初始x方向尺寸 770 y_Client, //初始y方向尺寸 750 NULL, //父窗口句柄 NULL, //窗口菜单句柄 hInstance, //程序实例句柄 WinMain函数中第二个参数 NULL); //创建参数 ShowWindow(hwnd, iCmdShow);//显示窗口,iCmdShow是WinMain的第四个参数,决定窗口在屏幕中的初始化显示形式,例:SW_SHOWNORMAL表示正常显示 UpdateWindow(hwnd);//使窗口客户区重绘,通过向WndProc发送一条WM_PAINT消息而完成的 hAccel = LoadAccelerators(hInstance, szAppname);//加载加速键 while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }/* while (GetMessage(&msg, NULL, 0, 0))//GetMessage函数从消息队列中得到消息,填充msg。如果msg.message等于WM_QUIT,返回0,否则返回非0 { TranslateMessage(&msg);//将msg返回给windows已进行某些键盘消息的转换 DispatchMessage(&msg);//将msg再次返回给windows }*/ return msg.wParam;//msg.wParam是PostQuitMessage函数的参数值,通常是0 } ...

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值