贪吃蛇小游戏的源程序--c++简例教程
#include "stdafx.h" #include<windows.h> //#pragma comment(lib,"winmm.linb") #include<iostream> #include <string> #include<conio.h> #include<iomanip> #include<ctime> #include <vector> using namespace std; SYSTEMTIME sys; class basic { public: void outxy(int x, int y, string s) { HANDLE h; COORD c; c.X = x; c.Y = y; h = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(h, c); cout << s << endl; } void gotoxy(int x, int y) { HANDLE h; COORD c; c.X = x; c.Y = y; h = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(h, c); } void setcolor(int a) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a); } int inkey() { int c; c = _getch(); return c; } void hide_cursor() { HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursor_info; GetConsoleCursorInfo(h_GAME, &cursor_info); cursor_info.bVisible = false; SetConsoleCursorInfo(h_GAME, &cursor_info); } void show_cursor() { HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursor_info; GetConsoleCursorInfo(h_GAME, &cursor_info); cursor_info.bVisible = true; SetConsoleCursorInfo(h_GAME, &cursor_info); } void delay(long n) { Sleep(n); } /*void sound(char *s) { PlaySound(s, 0, 1); }*/ }; class game :public basic { private: struct xy { int x, y; }; xy t; vector<xy> a; int L0, H0; int L, H; int l, h; int key1, key2; int count; int i; int m; int time_0; int len; public: game(int LL0, int HH0, int LL, int HH, int ll, int hh, int k1, int k2) { L0 = LL0; H0 = HH0; L = LL; H = HH; l = ll; h = hh; key1 = k1; key2 = k2; count = 0; m = 0; time_0 = (unsigned)time(NULL); len = 20; t.x = l; t.y = h; a.push_back(t); for (i = 1; i < len; ++i) { t.x = a[i - 1].x - 2; t.y = a[i - 1].y; a.push_back(t); } } void background(); void draw(); void erase(); void move(); void key(); void timer(); }; void game::timer() { GetLocalTime(&sys); gotoxy(30, 0); cout << "用时:"; cout << setprecision(5) << setiosflags(ios::left); cout << time(NULL) - time_0; } void game::background() { int m; string ss; GetLocalTime(&sys); m = sys.wMilliseconds; L0 = m % 80; H0 = m % 24 + 1; gotoxy(L0, H0); cout << "◎"; gotoxy(L, H); cout << "得分:" << count; } void game::draw() { if (m != 0) { for (i=len-1;i>0;--i) { a[i].x = a[i - 1].x; a[i].y = a[i - 1].y; } a[0].x = l; a[0].y = h; } setcolor(12); gotoxy(a[0].x, a[0].y); cout << "●"; setcolor(15); for (i = l; i < len; ++i) { if (a[i].x >= 0 && a[i].x <= 78 && a[i].y >= 1 && a[i].y <= 24) { gotoxy(a[i].x, a[i].y); cout << "●"; } } } void game::erase() { if (m!=0) { if (a[len - 1].x >= 0 && a[len - 1].x <= 78 && a[len - 1].y >= 1 && a[len - 1].y <= 24) { gotoxy(a[len - 1].x, a[len - 1].y); cout << " "; } } } void game::move() { while (!_kbhit()) { draw(); timer(); if (key2 == 72 || key2 == 80) delay(60); else delay(50); if (key1 != 0) erase(); if ((l==L0-1||l==L0+1)&&h==H0&&(key2==72||key2==80)) { ++count; background(); //sound("sound.wav"); } if (l == L0&&h == H0) { count = count + 5; background(); //sound("sound.wav"); } if ((l == L0 - 1 || l == L0 + 1) && h == H0 && (key2 == 75 || key2 == 77)) { count = count + 5; background(); //sound("sound.wav"); } switch (key2) { case 72:h = h <= 1 ? 24 : h - 1; break; case 80:h = h >= 24 ? 1 : h + 1;break; case 75:l = l <= 1 ? 78 : l - 2;break; case 77:l = l >= 78 ? 0 : l + 2; break; } } } void game::key() { while (key1 != 27) { move(); key1 = inkey(); switch (key1) { case 32: m = 0; key2 = 0; break; case 224: m = 1; erase(); key2 = inkey(); break; } } } int main() { game m1(5, 2, 70, 0, 20, 10, 1, 0); m1.hide_cursor(); m1.background(); m1.key(); return 0; }