文章插图
// ConsoleApplication1.cpp: 定义控制台应用程序的入口点 。//#include "stdafx.h"#include #include
#include #include #define frame_height 20 //define map size#define frame_width 40#define UP 'w' //define operate key#define DOWN 's'#define LEFT 'a'#define RIGHT 'd'int i, j, k;char ch = UP; //initial directionint grow = 0; //flag: if snake growstruct Food { int x; int y;}food;struct Snake { int x[50]; int y[50]; int len; int speed;}snake; //snake[0] is headvoid init_map(void);void update_food(void);void move_snake(void);int is_alive(void);void get_speed(void);void gotoxy(int x, int y);int main(){ init_map();//初始化地图 while (1) {update_food();//是否产生食物get_speed();//获取速度move_snake();//移动蛇身Sleep(snake.speed);//移动速度if (!(is_alive()))//蛇的死活(撞墙或自食)break; } printf("Game Over!"); _getch(); return 0;}//initializevoid init_map(void){ //initial food srand(time(NULL)); food.x = rand() % (frame_height - 2) + 1; food.y = rand() % (frame_width - 2) + 1; gotoxy(food.x, food.y); printf("!"); //initial snake snake.x[0] = frame_height / 2; snake.y[0] = frame_width / 2; gotoxy(snake.x[0], snake.y[0]); printf("@"); snake.len = 3; snake.speed = 200; for (k = 1; k0; k--) {snake.x[k] = snake.x[k - 1];snake.y[k] = snake.y[k - 1]; } switch (ch) { case UP:snake.x[0]--; break; case DOWN:snake.x[0]++; break; case LEFT:snake.y[0]--; break; case RIGHT:snake.y[0]++; break; default:break; } gotoxy(snake.x[0], snake.y[0]); printf("@"); grow = 0; gotoxy(frame_height, 0);}//is aliveint is_alive(void){ if (snake.x[0] == 0 || snake.x[0] == frame_height - 1 || snake.y[0] == frame_width - 1 || snake.y[0] == 0)return 0; for (k = 1; k【求c语言的贪吃蛇的代码 能运行的】
推荐阅读