本文目錄一覽:
- 1、如何用C語言製作一個3D的動態火焰效果?
- 2、c語言play sound同時播放背景音樂和音樂特效要代碼
- 3、怎樣用C語言編寫閃電特效?
- 4、誰能給個實現BMP圖像的顯示與特效(百葉窗的)的C語言代碼
- 5、怎麼用C語言寫下雪的動畫效果
如何用C語言製作一個3D的動態火焰效果?
嗯,我來說兩句。
C語言是可以實現火焰粒子特效的
你的創作思路是:在網上搜集關於火焰粒子特效的文章,比如百度文庫,新浪文庫、
然後著手編程
編程要注意,既然是C,你可以包含DirectX的庫,然後調用別人寫好的庫函數實現一些基本功能,比如畫點,上色,定時,Z緩存,你可以搜directx的使用說明,多得很
動態火焰效果是遊戲編程的一部分,額。。涉及挺多的東西,代碼無法給你,抱歉
c語言play sound同時播放背景音樂和音樂特效要代碼
需要包含的頭文件#include windows.h#include mmsystem.h//需要包含的庫文件#pragma comment(lib,”winmm.lib”) int main(int argc, char *argv[]){ //調用PlaySound函數 //該函數只支持.wav格式的聲音文件,其中: //acquired-chs.wav是WIN7系統自帶的,位於C:\Windows\System32下面 //SND_FILENAME 表示從文件讀取資源 //SND_SYNC表示同步播放,即播放完成後,再做後面的操作 //如果想播放的時候,做其它操作,可將SND_SYNC改成SND_ASYNC表示非同步播放 PlaySound(“acquired-chs.wav”, NULL, SND_FILENAME | SND_SYNC); return 0;}
怎樣用C語言編寫閃電特效?
示常式序:
#includewindows.h
#includestdio.h
main()
{
HANDLE hStdout;
COORD fcoord,Cursor;
char *flag = “-|/\\”;
char *ch = “Baid”;
int i = 0, j = 0;
AllocConsole();
/* get standered handles */
fcoord.X = fcoord.Y = 0;
Cursor.X = -1;
Cursor.Y = 1;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
while (TRUE)
{
Sleep(300);
SetConsoleCursorPosition(hStdout, fcoord);
printf(“%c”,flag[i++]);
if(i == 3)
{
i = 0;
Cursor.X += 1;
SetConsoleCursorPosition(hStdout, Cursor);
printf(“%c”,ch[j]);
j++;
}
if(j == 4)
break;
}
//getch();
}
說明:1.RT,那就別用清屏函數三;
2.一個一個字輸出?用fopen()從文件讀入就可以不從程序輸入了;
3.示常式序ch字元串如果是漢字程序將失去效果,這個應該是Unicode的問題,解決方法我暫時還不知道,但是奇怪的是:
#include “stdio.h”
#includewindows.h
int main()
{
char *s = “醉拳是天下第一拳”;
int i;
for (i=0; s[i]!=’\0′;i++)
{
printf(“%c”,s[i]);
Sleep(150);
}
getch();
} 這個卻可以;
4.不要試圖用TC系列的編譯器編譯。
誰能給個實現BMP圖像的顯示與特效(百葉窗的)的C語言代碼
#include graphics.h /* 打開圖形函數頭文件 */
#define N 45 /* 定義百葉窗扇葉寬度為45像素 */
void initgr(void) /* 圖形驅動函數 */
{
int gd = DETECT, gm = 0;
registerbgidriver(EGAVGA_driver);/*登錄已連接進來的圖形驅動程序代碼*/
initgraph(gd, gm, “”);/*初始化圖形系統*/
}
void draw(int color)/* 自定義函數,實現水平百葉窗效果 */
{
int i,j;
setcolor(color); /* 設置前景色 */
for(i=0;iN;i++) /* 實現百葉窗效果 */
{
for(j=0;j480;j+=N)
{
line(0,j+i,639,j+i);/*在指定兩點間畫一直線*/
delay(3000);/*作用是讓當前進程等待[毫秒數],防止100%CPU佔有率*/
}
}
}
void main(void)
{
int i;
initgr();/* 調用圖形驅動函數 */
getch();/* 暫停一下 */
for(i=0;i16;i++)
draw(i);
getch();
closegraph();/* 關閉圖形驅動模式 */
}
怎麼用C語言寫下雪的動畫效果
#include stdio.h
#include stdlib.h
#include string.h
#include time.h
/*
* 清除屏幕的shell 命令/控制台命令,還有一些依賴平台的實現
* 如果定義了 __GNUC__ 就假定是 使用gcc 編譯器,為Linux平台
* 否則 認為是 Window 平台
*/
#if defined(__GNUC__)
//下面是依賴 Linux 實現
#include unistd.h
#define sleep_ms(m) \
usleep(m * 1000)
//向上移動游標函數 Linux
static void __curup(int height)
{
int i = -1;
while (++iheight)
printf(“\033[1A”); //先回到上一行
}
#else
// 創建等待函數 1s 60 幀 相當於 16.7ms = 1幀, 我們取16ms
// 咱么的這屏幕 推薦 1s 25幀吧 40ms
// 這裡創建等待函數 以毫秒為單位 , 需要依賴操作系統實現
#include Windows.h
#define sleep_ms(m) \
Sleep(m)
//向上移動游標
static void __curup(int height)
{
COORD cr = {0,0};
// GetStdHandle(STD_OUTPUT_HANDLE) 獲取屏幕對象, 設置游標
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cr);
}
#endif /*__GNUC__ 跨平台的代碼都很醜陋 */
// 定義初始屏幕的寬高像素宏
#define _INT_WIDTH (100)
#define _INT_HEIGHT (50)
// 屏幕刷新幀的速率
#define _INT_FRATE (40)
// 雪花飄落的速率,相對於 屏幕刷新幀 的倍數
#define _INT_VSNOW (10)
/*
* 錯誤處理宏,msg必須是””括起來的字元串常量
* __FILE__ : 文件全路徑
* __func__ : 函數名
* __LINE__ : 行數行
* __VA_ARGS__ : 可變參數宏,
* ##表示直接連接, 例如 a##b = ab
*/
#define cerr(msg,…) \
fprintf(stderr, “[%s:%s:%d]” msg “\n”,__FILE__,__func__,__LINE__,##__VA_ARGS__);
/*
* 屏幕結構體, 具有 寬高
* frate : 繪製一幀的周期, 單位是 毫秒
* width : 屏幕的寬,基於窗口的左上角(0,0)
* height : 屏幕的高
* pix : 用一維模擬二維 主要結構如下
* 0 0 0 1 0 0 1 0 1 0
* 0 1 0 1 0 1 0 1 2 0
* . . .
* = 0表示沒像素, 1表示1個像素,2表示2個像素….
*/
struct screen {
int frate; // 也可以用 unsigned 結構
int width;
int height;
char *pix;
};
/*
* 創建一個 屏幕結構指針 返回
*
* int frate : 繪製一幀的周期
* int width : 屏幕寬度
* int height : 屏幕高度
* return : 指向屏幕結構的指針
* */
struct screen* screen_create(int frate, int width, int height);
/*
* 銷毀一個 屏幕結構指針, 並為其置空
* struct screen** : 指向 屏幕結構指針的指針, 二級銷毀一級的
* */
void screen_destory(struct screen** pscr);
/**
* 屏幕繪製函數,主要生成一個雪花效果
*
* struct screen* : 屏幕數據
* return : 0表示可以繪製了,1表示圖案不變
*/
int screen_draw_snow(struct screen* scr);
/**
* 屏幕繪製動畫效果, 繪製雪花動畫
*
* struct screen* : 屏幕結構指針
*/
void screen_flash_snow(struct screen* scr);
// 主函數,主業務在此運行
int main(int argc, char *argv[])
{
struct screen* scr = NULL;
//創建一個屏幕對象
scr = screen_create(_INT_FRATE, _INT_WIDTH, _INT_HEIGHT);
if (NULL == scr)
exit(EXIT_FAILURE);
//繪製雪花動畫
screen_flash_snow(scr);
//銷毀這個屏幕對象
screen_destory(scr);
return 0;
}
/*
* 創建一個 屏幕結構指針 返回
*
* int frate : 繪製一幀的周期
* int width : 屏幕寬度
* int height : 屏幕高度
* return : 指向屏幕結構的指針
* */
struct screen*
screen_create(int frate, int width, int height)
{
struct screen *scr = NULL;
if (frate0 || width = 0 || height = 0) {
cerr(“[WARNING]check is frate0 || width=0 || height=0 err!”);
return NULL;
}
//後面是 為 scr-pix 分配的內存 width*height
scr = malloc(sizeof(struct screen) + sizeof(char)*width*height);
if (NULL == scr) {
cerr(“[FATALG]Out of memory!”);
return NULL;
}
scr-frate = frate;
scr-width = width;
scr-height = height;
//減少malloc次數,malloc消耗很大,內存泄露呀,內存碎片呀
scr-pix = ((char *)scr) + sizeof(struct screen);
return scr;
}
/*
* 銷毀一個 屏幕結構指針, 並為其置空
* struct screen** : 指向 屏幕結構指針的指針, 二級銷毀一級的
* */
void
screen_destory(struct screen** pscr)
{
if (NULL == pscr || NULL == *pscr)
return;
free(*pscr);
// 避免野指針
*pscr = NULL;
}
//構建開頭 的雪花,下面宏表示每 _INT_SHEAD 個步長,一個雪花,需要是2的冪
//static 可以理解為 private, 宏,位操作代碼多了確實難讀
#define _INT_SHEAD (12)
static void __snow_head(char* snow, int len)
{
int r = 0;
//數據需要清空
memset(snow, 0, len);
for (;;) {
//取餘一個技巧 2^3 – 1 = 7 = 111 , 並就是取餘數
int t = rand() (_INT_SHEAD – 1);
if (r + t = len)
break;
snow[r + t] = 1;
r += _INT_SHEAD;
}
}
#undef _INT_SHEAD
//通過 上一個 scr-pix[scr-width*(idx-1)] = scr-pix[scr-width*idx]
//下面的宏 規定 雪花左右搖擺 0 向左一個像素, 1 表示 不變, 2表示向右一個像素
#define _INT_SWING (3)
static void __snow_next(struct screen* scr, int idx)
{
int width = scr-width;
char* psnow = scr-pix + width*(idx – 1);
char* snow = psnow + width;
int i, j, t; // i索引, j保存下一個瞬間雪花的位置,t 臨時補得,解決雪花重疊問題
//為當前行重置
memset(snow, 0, width);
//通過上一次雪花位置 計算下一次雪花位置
for (i = 0; iwidth; ++i) {
for (t = psnow[i]; t0; –t) { // 雪花可以重疊
// rand()%_INT_SWING – 1 表示 雪花 橫軸的偏移量,相對上一次位置
j = i + rand() % _INT_SWING – 1;
j = j0 ? width – 1 : j = width ? 0 : j; // j如果越界了,左邊越界讓它到右邊,右邊越界到左邊
++snow[j];
}
}
}
/**
* 屏幕繪製函數,主要生成一個雪花效果
*
* struct screen* : 屏幕數據
* return : 0表示可以繪製了,1表示圖案不變
*/
int
screen_draw_snow(struct screen* scr)
{
// 靜態變數,默認初始化為0,每次都共用
static int __speed = 0;
int idx;
if (++__speed != _INT_VSNOW)
return 1;
//下面 就是 到了雪花飄落的時刻了 既 __speed == _INT_VSNOW
__speed = 0;
//這裡重新構建雪花界面,先構建頭部,再從尾部開始構建
for (idx = scr-height – 1; idx 0; –idx)
__snow_next(scr, idx);
//構建頭部
__snow_head(scr-pix, scr-width);
return 0;
}
//buf 保存scr 中pix 數據,構建後為 (width+1)*height, 後面宏是雪花圖案
#define _CHAR_SNOW 『*『
static void __flash_snow_buffer(struct screen* scr, char* buf)
{
int i, j, rt;
int height = scr-height, width = scr-width;
int frate = scr-frate; //刷新的幀頻率
//每次都等一下
for (;;sleep_ms(frate)) {
//開始繪製屏幕
rt = screen_draw_snow(scr);
if (rt)
continue;
for (i = 0;iheight; ++i) {
char* snow = scr-pix + i*width;
for (j = 0; jwidth; ++j)
buf[rt++] = snow[j] ? _CHAR_SNOW : 『 『;
buf[rt++] = 『\n『;
}
buf[rt – 1] = 『\0『;
//正式繪製到屏幕上
puts(buf);
//清空老屏幕,屏幕游標回到最上面
__curup(height);
}
}
#undef _CHAR_SNOW
/**
* 屏幕繪製動畫效果, 繪製雪花動畫
*
* struct screen* : 屏幕結構指針
*/
void
screen_flash_snow(struct screen* scr)
{
char* buf = NULL;
// 初始化隨機數種子,改變雪花軌跡
srand((unsigned)time(NULL));
buf = malloc(sizeof(char)*(scr-width + 1)*scr-height);
if (NULL == buf) {
cerr(“[FATAL]Out of memory!”);
exit(EXIT_FAILURE);
}
__flash_snow_buffer(scr, buf);
//1.這裡理論上不會執行到這,沒加控制器. 2.對於buf=NULL,這種代碼 可以省掉,看編程習慣
free(buf);
buf = NULL;
}
原創文章,作者:IJVA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/140872.html