贪吃蛇大作战

来源:互联网 发布:网络电玩游戏平台 编辑:程序博客网 时间:2024/06/10 15:02

写了大概有一个星期的贪吃蛇,最后崩盘了,没有写成功
也许是个错误的构思,用的二维int数组做的,写到最后一步的时候,已经实现的功能是在蛇屏幕上移动,还有判断game over ,还有随机产生果实


在写贪吃蛇的过程中,学到了不少,

  • kbhit()判断是否有键入,没有返回0,else return 非0; #include
#include<stdio.h>#include<conio.h>#include<stdlib.h>int main(void){char ch;while(ch!=27){printf("HelloWorld\n");if(kbhit())ch=getch();}printf("End!\n");system("pause");return 0;}
  • getch()和getchar()是有区别的
    getchar();需要enter键,
    getch();并不需要

  • 随机

#include <time.h>time_t t;srand((unsigned)time(&t));int randomNum=rand()%k+1;
  • 贪吃蛇地图
#include <stdio.h>#include <conio.h>#include <windows.h>int main(){    int width = 30, height = width;  //宽度和高度    int x, y;  //x、y分别表示当前行和列    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);    //设置窗口大小    system("mode con: cols=64 lines=32");    //打印背景,按行输出    for(x=0; x<width; x++){        for(y=0; y<height; y++){            if(y==0 || y==width-1 || x==0 || x==height-1){  //输出边框                SetConsoleTextAttribute(hConsole, 4 );                printf("□");            }else{  //贪吃蛇活动区域                SetConsoleTextAttribute(hConsole, 2 );                printf("■");            }        }        printf("\n");    }    //暂停    getch();    return 0;}//http://c.biancheng.net/cpp/html/2942.html

下次再开车

1 0