日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C語言基于EasyX庫實現(xiàn)有顏色彈跳小球_C 語言

作者:summery456 ? 更新時間: 2022-03-28 編程語言

本文實例為大家分享了基于EasyX庫實現(xiàn)有顏色彈跳小球的具體代碼,供大家參考,具體內(nèi)容如下

1.目標要求

1.實現(xiàn)一個有顏色小球在窗口中彈跳
2.遇到邊界彈跳

2.C語言代碼

#include<graphics.h>?
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>

#define High 480
#define Width 640//畫布尺寸

/*
刷屏問題

? 移動的間距小、延時短,動畫就會越細膩。但當畫面較復(fù)雜時,會帶來畫面的閃爍。

? 1.BeginBatchDraw這個函數(shù)用于開始批量繪圖。執(zhí)行后,任何繪圖操作都將暫時不輸出到屏幕上,直到執(zhí)行
FlushBatchDraw或EndBatchDraw才將之前的繪圖輸出
? 2.FlushBatchDraw這個函數(shù)用于執(zhí)行未完成的繪制任務(wù)。
? 3.EndBatchDraw這個函數(shù)用于結(jié)束批量繪制,并執(zhí)行未完成的繪制任務(wù)。
*/

void HideCursor(){?? ?//隱藏光標位置 ,這個函數(shù)復(fù)制代碼就行?
?? ?CONSOLE_CURSOR_INFO cursor_info={1,0};?
?? ?SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void gotoxy(int x,int y){?? ?//把光標放在(0,0)位置 ,這個函數(shù)復(fù)制代碼就行
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle,pos);
}?

int IsEnd;//是否結(jié)束?
int scores;//分數(shù)?
int slow_v,slow_v_num;//移動變慢的值(越大速度越慢),移動變慢的變量
int ball_x,ball_y;//小球坐標
int ball_r;//小球半徑
int ballv_x,ballv_y;//小球速度

void startup(){?? ?//【數(shù)據(jù)初始化】?
?? ?
?? ?HideCursor();//不顯示光標?
?? ?IsEnd = 0;
?? ?scores=0;?? ?
?? ?slow_v=1;
?? ?slow_v_num=1;?
?? ?initgraph(Width,High);//展示畫布
?? ?ball_x=Width/2;
?? ?ball_y=High/2;
?? ?ballv_x=1;
?? ?ballv_y=1;
?? ?ball_r=20;

?? ?
}
void show_begin(){//【初始頁面展示】?
?? ?BeginBatchDraw();
}?
void show(){?? ?//【顯示畫面】?
?? ?setcolor(YELLOW);//設(shè)置顏色
?? ?setfillcolor(GREEN);//設(shè)置填充顏色
?? ?fillcircle(ball_x,ball_y,ball_r);//填充圓
?? ?FlushBatchDraw();//更新一次畫面,解決畫面閃的問題,需要配合BeginBatchDraw函數(shù)使用
?? ?Sleep(10);//延時
?? ?cleardevice();//清除之前的畫跡
?? ?ball_x += ballv_x;
?? ?ball_y += ballv_y;
?? ?
?? ?
}
void update_outinput(){?? ?//【與輸入無關(guān)的更新】?
?? ?if(ball_x+ball_r>=Width||ball_x-ball_r<=0){
?? ??? ?ballv_x *= -1;
?? ?}
?? ?if(ball_y+ball_r>=High||ball_y-ball_r<=0){
?? ??? ?ballv_y *= -1;
?? ?}
?? ?
?? ?
}
void update_input(){//【與輸入有關(guān)的更新】?
?? ?char input;
?? ?if(kbhit()){
?? ??? ?input = getch();
?? ??? ??? ??? ?
?? ?}
}
void show_end(){//【顯示失敗界面】?
?? ?EndBatchDraw();
}

int main(){
?? ?startup();?? ?//數(shù)據(jù)初始化
?? ?show_begin();//初始頁面?
?? ?while(!IsEnd){?? ?//游戲循環(huán)執(zhí)行?
?? ??? ?show();?? ?// 顯示畫面?
?? ??? ?update_outinput();?? ?//與輸入無關(guān)的更新?
?? ??? ?update_input();?? ?//與輸入有關(guān)的更新?
?? ?}
?? ?show_end(); //顯示失敗界面?
?? ?return 0;
}

3.運行結(jié)果

原文鏈接:https://blog.csdn.net/weixin_43503632/article/details/105470704

欄目分類
最近更新