贪吃蛇大作战

写了大概有一个星期的贪吃蛇,最后崩盘了,没有写成功
也许是个错误的构思,用的二维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

下次再开车

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值