如題,最近自己學習C++和easyX,撰寫了不少小程式。想嘗試使用C++和easyX寫出類似flappybird的程式,
于是馬上著手寫了個概念驗證的小程式,即只實作flappybird中小鳥上下移動和小鳥顯示的部分。
結果程式寫出來之后,發現運行的時候會出現卡頓。這種卡頓是隨機的,發生的時間和卡頓的時間都是隨機的,發生頻率也十分隨機。
于是自己馬上到網上搜索解決辦法,嘗試了將解決方案配置改成release,但是并沒有什么用。搜索了半天一無所獲,自己嘗試一行行排查,試圖找到產生卡頓的原因,也找不到······
希望有大神看到。還望輕噴。
#include <graphics.h>
#include <ctime>
using namespace std;
void IMAGEdsp(int x, int y, LPCSTR IMAGEy, LPCSTR IMAGE1) //圖片顯示函式
{
IMAGE img;
loadimage(&img, _T(IMAGEy));
putimage(x, y, &img, SRCAND);
loadimage(&img, _T(IMAGE1));
putimage(x, y, &img, SRCPAINT);
}
void catup1(int x, int y) { //三個方便圖片呼叫的函式
IMAGEdsp(x, y, "E:\\C++\\NotPaper\\圖片素材\\catup1Y.bmp", "E:\\C++\\NotPaper\\圖片素材\\catup1.bmp");
}
void catup2(int x, int y) {
IMAGEdsp(x, y, "E:\\C++\\NotPaper\\圖片素材\\catup2Y.bmp", "E:\\C++\\NotPaper\\圖片素材\\catup2.bmp");
}
void catdown(int x, int y) {
IMAGEdsp(x, y, "E:\\C++\\NotPaper\\圖片素材\\catdownY.bmp", "E:\\C++\\NotPaper\\圖片素材\\catdown.bmp");
}
int main()
{
int X = 270, Y = 150; //X、Y是圖片的坐標
double dY = 150.000000; //dY同樣是圖片坐標,但僅用于計算
double aY; //用于轉換
double V = 0.000; //移動速度,向上為正
initgraph(640, 480);
setbkcolor(WHITE);
cleardevice();
clock_t delay; //設定延遲
double sec = 0.05;
delay = sec * CLOCKS_PER_SEC;
clock_t start = clock();
bool hit = false; //用于滑鼠檢測
MOUSEMSG ms;
while (Y < 420)
{
clock_t start = clock();
V -= 200 * sec; //速度、坐標運算
dY -= V * sec;
if (hit)V = 200; //左鍵及賦予向上速度
hit = false;
aY = Y;
if (dY >= aY + 1 || dY <= aY - 1) { //僅當像素移動大于 1 時才繪圖
BeginBatchDraw();
Y = dY;
cleardevice();
if (V >= 0)
{
if (V >= 120)catup1(X, Y);
else
catup2(X, Y);
}
if (V < 0)catdown(X, Y);
EndBatchDraw();
}
while (clock() - start < delay) { //延時,檢測滑鼠
ms = GetMouseMsg();
if (ms.uMsg == WM_LBUTTONDOWN)hit = true;
if (ms.uMsg == WM_RBUTTONDOWN)goto end;
}
}
end:closegraph();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/258543.html
標籤:新手樂園
上一篇:求助
下一篇:xcode除錯就跳出這個
