clock_t a, b....
/*難度隨長度的增加而提高*/
hard_len = (double)snake_length / (double)(m * n);
//調節時間,單位是ms
a = clock();
while (1)
{
b = clock();
if (b - a >= (int)(400 - 30 * hard) * (1 - sqrt(hard_len)))break;
}
就是不太懂這里,我試過如果把這些去除掉,就會直接游戲結束,就是直接輸,不去除,就可以正常玩,所以我在想這個是不是可以控制運行速度呢?????

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<ctime>//clock_t
#include<conio.h>
#include<windows.h>
using namespace std;
/*游標定位*/
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
void locate(int x, int y)
{
coord.X = y;
coord.Y = x;
SetConsoleCursorPosition(hout, coord);
};
/*游標隱藏*/
void hide()
{
CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; //bVisible=0;隱藏游標
SetConsoleCursorInfo(hout, &cursor_info);//獲取游標狀態
}
/*生成亂數*/
double random(double start, double end)
{
return start + (end - start) * rand() / (RAND_MAX + 1.0);//生成一個數,大于等于start,小于end;
}
/*定義地圖的長和寬*/
int m, n;
/*定義蛇 的長度,坐標,方向,食物的位置*/
struct node
{
int x, y;
}snake[1000];//蛇的坐標
int snake_length, dir;//蛇的長度,方向
node food;
int direct[4][2] = { {-1,0}, {1,0}, {0,-1}, {0,1} };//食物的位置
/*輸出圍墻:一個矩形框*/
void print_wall()
{
//輸出第一行 “----------”
cout << " ";
for (int i = 1; i <= n; i++)
{
cout << "-";//(0,1)
}
cout << endl;
//輸出第一列“|”,中間輸入空格,最后一列輸出“|”
for (int j = 0; j <= m - 1; j++)
{
cout << "|";//(1,0)
for (int i = 1; i <= n; i++)
cout << " ";
cout << "|" << endl;
}
cout << " ";
//輸出最后一行“----------”
for (int i = 1; i <= n; i++)
cout << "-";
}
/*首次輸出蛇,其中snake[0]代表頭部*/
//蛇的外型:“@*****”
void print_snake()
{
locate(snake[0].x, snake[0].y);
cout << "@";
for (int i = 1; i < snake_length - 1; i++)
{
locate(snake[i].x, snake[i].y);
cout << "*";
}
}
/*判斷是否撞墻或者頭部是否碰到身體的任意一個部位,碰到則游戲失敗*/
bool is_correct()
{
if (snake[0].x == 0 || snake[0].y == 0 || snake[0].x == m + 1 || snake[0].y == n + 1) return false;//頭部碰到邊緣
for (int i = 1; i <= snake_length - 1; i++)
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y)return false;//頭部碰到身體的任意一個部位
return true;
}
/*隨機生成食物的位置*/
bool print_food()
{
srand((unsigned)time(0));//隨機種子
bool e;
while (1)
{
e = true;
int i = (int)random(0, m) + 1;
int j = (int)random(0, n) + 1;
food.x = i; food.y = j;//食物的位置隨機
for (int k = 0; k <= snake_length - 1; k++) //食物不能出現在蛇的身體的任意位置處
{
if (snake[k].x == food.x && snake[k].y == food.y)
{
e = false;
break;
}
}
if (e)break;
}
//在食物的位置處標記,食物符號為“$”;
locate(food.x, food.y);
cout << "$";
return true;
}
/*蛇的前進*/
bool go_ahead()
{
node tmp;
bool e = false;
tmp = snake[snake_length - 1];//蛇尾
for (int i = snake_length - 1; i >= 1; i--)
{
snake[i] = snake[i - 1];//后移一位
}
snake[0].x += direct[dir][0];
snake[0].y += direct[dir][1];
locate(snake[1].x, snake[1].y);//定位到頭部的后一位
cout << "*";
/*吃到食物*/
if (snake[0].x == food.x && snake[0].y == food.y)
{
snake_length++;
e = true;
snake[snake_length - 1] = tmp;
}
/*輸出此時蛇狀態*/
if (!e)//e為false才執行
{
locate(tmp.x, tmp.y);
cout << " ";
}
else
print_food();
locate(snake[0].x, snake[0].y);
cout << "@";
/*** 如果自撞 ***/
if (!is_correct())
{
system("cls");
cout << "You lose!" << endl << "Length: " << snake_length << endl;
return false;
}
return true;
}
int main()
{
//游戲提示:
cout << "--------------------貪吃蛇---------------------" << endl;
cout << "請先輸入兩個數,表示地圖大小.要求長寬均不小于10." << endl;
cout << "請注意視窗大小,以免發生錯位.建議將視窗調為最大." << endl;
cout << "再選擇難度.請在1-10中輸入1個數,1最簡單,10則最難" << endl;
cout << "然后進入游戲畫面,以方向鍵控制方向.祝你游戲愉快!" << endl;
cout << "-----------------------------------------------" << endl;
cin >> m >> n;
if (m < 10 || n < 10 || m>25 || n>40)
{
cout << "ERROR" << endl;
system("pause");
return 0;
}
//輸入難度系數:1-10;
int hard;
cin >> hard;
if (hard <= 0 || hard > 10)
{
cout << "ERROR" << endl;
system("pause");
return 0;
}
//資料初始化:蛇的位置,長度,方向
snake_length = 5;//長度
clock_t a, b;//clock_t clock(void) ;,包含于頭檔案ctime(或time.h)中,用來記錄一段時間內的clocks數,即CPU的運行單元時間。
char ch;
double hard_len;
for (int i = 0; i <= 4; i++)//位置
{
snake[i].x = 1;
snake[i].y = 5 - i;//snake[0]=(1,5),snake[1]=(1,4),snake[4]=(1,1)
}
dir = 3;//方向
//輸出原始地圖、食物和蛇
system("cls");
hide();
print_wall();
print_food();
print_snake();
//在(0,m+2)出顯示長度:
locate(m + 2, 0);
cout << "Now Length:";
//開始游戲
while (1)
{ /*難度隨長度的增加而提高*/
hard_len = (double)snake_length / (double)(m * n);
//調節時間,單位是ms
a = clock();
while (1)
{
b = clock();
if (b - a >= (int)(400 - 30 * hard) * (1 - sqrt(hard_len)))break;
}
//接收鍵盤輸入的方向
//https://blog.csdn.net/wenweimin/article/details/105561
if (_kbhit())
{
ch = _getch();
if (ch == -32)
{
ch = _getch();
switch (ch)
{
case 72:if (dir == 2 || dir == 3)dir = 0; break;
case 80:if (dir == 2 || dir == 3)dir = 1; break;//下
case 75:if (dir == 0 || dir == 1)dir = 2; break;//左
case 77:if (dir == 0 || dir == 1)dir = 3; break;//右
}
}
}
//前進
if (!go_ahead())break;
//輸出此時的長度
locate(m + 2, 12);
cout << snake_length;
}
system("pause");
return 0;
}
uj5u.com熱心網友回復:
a = clock();while (1)
{
b = clock();
if (b - a >= (int)(400 - 30 * hard) * (1 - sqrt(hard_len)))break;
}
程式運行到這里會回圈等待到指指的時間間才繼續下面的,起到了調整速度的作用,不過這個設計不是很好,回圈一直在計算,CPU占用率會居高不下。
uj5u.com熱心網友回復:
想問一下,他是怎么調整速度的?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/93123.html
標籤:C++ 語言
上一篇:一道c++的類定義題目,新手剛學,有很多不會下不了手啊
下一篇:測驗組播性能的工具
