基本信息
源码名称:c++ 贪吃蛇 A0.1 示例源码
源码大小:5.28KB
文件格式:.cpp
开发语言:C/C++
更新时间:2018-01-10
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
#include <iostream> #include <string> #include <ctime> #include <windows.h> #include <conio.h> using namespace std; int food[2] = { 9, 9 }; int snake[1000][2]; int length = 1; int headX, headY; int speed = 500; int score = 0; int level = 1; string name; void gotoxy(short x, short y); int setdirection(int x); void changesnake(int x); void ifchangefood(); void makefood(); bool judgelife(); void drawsnake(); void drawfood(); void drawwall(); void drawscore(); void draw(); int main() { SetConsoleTitle("贪吃蛇游戏"); int po = 2; snake[0][0] = 7; snake[0][1] = 7; headX = snake[0][0]; headY = snake[0][1]; gotoxy(30, 7); cout << "欢迎来到贪吃蛇游戏"; gotoxy(30, 9); cout << "作者:陈伟豪 版本0.1"; gotoxy(30, 11); system("cls"); gotoxy(30, 7); cout << "游戏控制方式:"; gotoxy(30, 9); cout << "W键:向上 S键:向下"; gotoxy(30, 11); cout << "A键:向左 D键:向右"; gotoxy(30, 13); cout << "空格键:暂停"; gotoxy(30, 15); cout << "将游戏窗口最大化之后"; gotoxy(30, 17); cout << "按回车键开始游戏..."; cin.get(); cin.get(); system("cls"); while (true) { po = setdirection(po); system("cls"); changesnake(po); ifchangefood(); if (!judgelife()) break; draw(); Sleep(speed); } gotoxy(30, 10); cout << "Game Over!!!"; Sleep(2000); gotoxy(28, 12); system("pause"); return 0; } void gotoxy(short x, short y) { COORD position = { x, y }; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, position); } int setdirection(int x) { char ch; if (_kbhit()) { ch = _getch(); switch (ch) { case 'w': x = 1; break; case 's': x = 2; break; case 'a': x = 3; break; case 'd': x = 4; break; case ' ': gotoxy(37, 16); cout << "游 戏 暂 停. . ."; gotoxy(37, 18); system("pause"); break; default: break; } } return x; } void changesnake(int x) { switch (x) { case 1: headY -= 1; break; case 2: headY = 1; break; case 3: headX -= 1; break; case 4: headX = 1; break; default: break; } for (int i = length; i > 0; --i) { for (int j = 0; j < 2; j) { snake[i][j] = snake[i - 1][j]; } } snake[0][0] = headX; snake[0][1] = headY; } void ifchangefood() { if (snake[0][0] == food[0] && snake[0][1] == food[1]) { length ; makefood(); score; if (length > 5) { speed = 450; level = 2; } if (length > 10) { speed = 400; level = 3; } if (length > 15) { speed = 350; level = 4; } if (length > 20) { speed = 300; level = 5; } if (length > 25) { speed = 250; level = 6; } if (length > 30) { speed = 200; level = 7; } if (length > 35) { speed = 150; level = 8; } if (length > 40) { speed = 100; level = 9; } if (length > 45) { speed = 50; level = 10; } } } void makefood() { srand((unsigned)time(NULL)); food[0] = rand() % 30 2; food[1] = rand() % 30 4; for (int m = 0; m < length; m) { if (food[0] == snake[m][0] && food[1] == snake[m][1]) { makefood(); break; } } } bool judgelife() { for (int x = 1; x < length; x) { if (headX == snake[x][0] && headY == snake[x][1]) { return false; } } if (headX < 1 || headY < 3 || headX > 34 || headY > 34) return false; else return true; } void drawsnake() { gotoxy(snake[0][0], snake[0][1]); cout << "@"; for (int n = 1; n < length; n) { gotoxy(snake[n][0], snake[n][1]); cout << "#"; } } void drawfood() { gotoxy(food[0], food[1]); cout << "$"; } void drawwall() { gotoxy(0, 0); cout << "------------------------------------"; gotoxy(16, 1); cout << "贪吃蛇"; gotoxy(0, 2); cout << " "; gotoxy(0, 35); cout << "------------------------------------"; for (int x = 0; x < 35; x) { gotoxy(0, x); cout << "|"; gotoxy(35, x); cout << "|"; } } void drawscore() { gotoxy(37, 10); cout << "分数:" << score; gotoxy(37, 12); cout << "等级:" << level; } void draw() { drawsnake(); drawfood(); drawwall(); drawscore(); }