贪吃蛇
来源:互联网 发布:软件开发需求统计表 编辑:程序博客网 时间:2024/04/30 12:40
发现了一个贪吃蛇的源码…没有用graphics.h
我自己改了改…
不过玩的时候会一直闪屏…
#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <time.h>#include <windows.h>//这里设置游戏界面大小#define width 78#define high 20enum block{ BLANK = 0, FOOD, SNAKE, HEAD};int snake[800][2] = {0};int len = 0;int food[2] = {0};enum block map[high][width] = {HEAD};int gameOver = 0;int complex=1;//玩游戏的难度等级void CreatFood( void ){ int flag = 1; int i; while( flag == 1 ) { flag = 0; food[0] = rand()%width; food[1] = rand()%high; printf( "%d,%d\n", food[0], food[1] ); for ( i = 0; i < len; ++i ) { if ( snake[i][0] == food[0] && snake[i][1] == food[1] ) { flag = 1; } } }}void Init( void ){ int i; //初始化蛇 len = 3; for ( i = 0; i < len; ++i ) { snake[i][0] = len-i; } //初始化食物 CreatFood();}void Draw( void ){ int i,j; for ( i = 0; i < high; ++i ) { for ( j = 0; j < width; ++j ) { map[i][j] = BLANK; } } //把蛇画到map里 map[snake[0][1]][snake[0][0]] = HEAD; for ( i = 1; i < len; ++i ) { map[snake[i][1]][snake[i][0]] = SNAKE; } //把食物画到map里 map[food[1]][food[0]] = FOOD; system( "cls" );// printf( "%d,%d\n", food[0], food[1] ); for ( i = 0; i < high; ++i ) { for ( j = 0; j < width; ++j ) { switch ( map[i][j] ) { case BLANK: putchar(' '); break; case FOOD: putchar('$'); break; case SNAKE: putchar('*'); break; case HEAD: putchar('@'); break; default: break; } } putchar('|'); putchar('\n'); } puts("-------------------------------------------------------------------------------"); printf("Rank is %d\n",complex);}int Eat( void ){ if ( snake[0][0] == food[0] && snake[0][1] == food[1] ) { complex=len/10+1; return 1; } return 0;}int Die( void ){ int i; //撞边界死 if ( snake[0][0] < 0 || snake[0][0] > width ) { return 1; } if ( snake[0][1] < 0 || snake[0][1] > high ) { return 1; } //撞自己死 for ( i = 1; i < len; ++i ) { if ( snake[i][0] == snake[0][0] && snake[i][1] == snake[0][1] ) { return 1; } } return 0;}void Move( char key ){ int x, y; int pos = 0; int i; int d[4][2] = { {0,-1}, {-1,0}, {0,1}, {1,0}, }; switch ( key ) { case 'w': pos = 0; break; case 'a': pos = 1; break; case 's': pos = 2; break; case 'd': pos = 3; break; default: break; } x = snake[len-1][0]; y = snake[len-1][1]; for ( i = len-1; i > 0; --i ) { snake[i][0] = snake[i-1][0]; snake[i][1] = snake[i-1][1]; } snake[0][0] += d[pos][0]; snake[0][1] += d[pos][1]; if ( Eat() ) { snake[len][0] = x; snake[len][1] = y; ++len; CreatFood(); } else if ( Die() ) { gameOver = 1; }}char Judge( char key, char lastKey ){ if ( key == 'd' && lastKey == 'a' ) { return lastKey; } if ( key == 'a' && lastKey == 'd' ) { return lastKey; } if ( key == 'w' && lastKey == 's' ) { return lastKey; } if ( key == 's' && lastKey == 'w' ) { return lastKey; } return key;}int main( int argc, char * argv[] ){ char lastKey = 'd'; char key = lastKey; int lastTime = GetTickCount(); srand( (unsigned int)GetTickCount() ); Init(); Draw(); while ( 1 ) { while ( 1) { while ( GetTickCount()-lastTime < 500/complex ) { Sleep(1); if(kbhit()) goto enen; } lastTime = GetTickCount(); Move( key ); if ( !gameOver ) { Draw(); } } enen:key = getch(); key = Judge( key, lastKey ); lastKey = key; Move( key ); lastTime = GetTickCount(); if ( !gameOver ) { Draw(); } else { puts( "You Lose!"); break; } } return 0;}
只有一个关卡,但是蛇的速度会越来越快.
可以尝试增加关卡,恩.
可行.
0 0
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 读书笔记之head first设计模式1
- CXF学习笔记(2)-HelloWorld!-客户端调用
- oracle express edition 11g hibernate连接配置
- Maven Artifact 导航
- C语言实现的链表结构
- 贪吃蛇
- Png的使用技巧
- oracle sql语句
- Matrixtsl有趣的电子积木网络连接板
- ural 2004. Scientists from Spilkovo 构造
- Java Web入门之css页面布局基础知识
- jsp 自定义标签
- Web.config配置文件详解
- Google地图10周年,看它曾经做过的那些疯狂事