c圖形與游戲編程
⑴ 用C或者C++編寫一個小游戲,還需要用帶什麼知識。
C++是一種編程語言,語言是最基礎的。搞應用開發時,就要掌握相關方面的編程知識,比如做一個windows系統上的游戲,第一需要掌握windows編程,然後,游戲需要顯示圖像,這就需要掌握圖像編程如OpenGL 或 DirectX 。windows GDI是操作系統圖形界面的介面,一般不會用於做游戲。再者,游戲要有聲音,通常使用DirectX的介面,或者使用其它聲音工具包如OpenAL Bass OGG等。DirectX是專門為製作多媒體程序尤其是游戲而提供的硬體加速介面,也可以使用商業或非商業的游戲引擎來製作。
建議使用Visual C++ 掌握 Windows編程。
⑵ 我有c語言的基礎,想學c++的圖形編程,需要些什麼軟體和書或視頻教程
圖形編程都是用的純C函數庫, 比如Windows的GDI,還有DirectX,以及開源的OpenGL。這些都是純C庫,也就是說會C語言就能開發圖形界面和游戲了。
C++當然也可以調用這些函數庫,但不是必要的。
學GDI的話最好的書是《Windows程序設計.chm》這個文檔,這東西網上到處都有。
學OpenGL的話,有個網站Nehe有完整的教學,不過是全英文的。現在有人翻譯了這個教程的中文版,google 「Nehe opengl」 就能找到了。
⑶ 請問如何用c語言做一個圖形界面呢比如一個迷宮游戲的界面
圖形界面介面因系統(windows
/Linux)而不一樣。
在windows下因為系統是用C開發的,標准API介面就是C介面,稱好windows
API
這就是常說的API編程
int
WINAPI
WinMain(HINSTANCE
hInstance,
HINSTANCE
hPrevInstance,
LPSTR
lpCmdLine,
int
nCmdShow)
{
WNDCLASSEX
wcex;
wcex.cbSize
=
sizeof(WNDCLASSEX);
wcex.style
=
CS_HREDRAW
|
CS_VREDRAW;
wcex.lpfnWndProc
=
WndProc;
wcex.cbClsExtra
=
0;
wcex.cbWndExtra
=
0;
wcex.hInstance
=
hInstance;
wcex.hIcon
=
LoadIcon(hInstance,
MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor
=
LoadCursor(NULL,
IDC_ARROW);
wcex.hbrBackground
=
(HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName
=
NULL;
wcex.lpszClassName
=
szWindowClass;
wcex.hIconSm
=
LoadIcon(wcex.hInstance,
MAKEINTRESOURCE(IDI_APPLICATION));
if
(!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call
to
RegisterClassEx
failed!"),
_T("Win32
Guided
Tour"),
NULL);
return
1;
}
hInst
=
hInstance;
//
Store
instance
handle
in
our
global
variable
//
The
parameters
to
CreateWindow
explained:
//
szWindowClass:
the
name
of
the
application
//
szTitle:
the
text
that
appears
in
the
title
bar
//
WS_OVERLAPPEDWINDOW:
the
type
of
window
to
create
//
CW_USEDEFAULT,
CW_USEDEFAULT:
initial
position
(x,
y)
//
500,
100:
initial
size
(width,
length)
//
NULL:
the
parent
of
this
window
//
NULL:
this
application
dows
not
have
a
menu
bar
//
hInstance:
the
first
parameter
from
WinMain
//
NULL:
not
used
in
this
application
HWND
hWnd
=
CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
100,
NULL,
NULL,
hInstance,
NULL
);
if
(!hWnd)
{
MessageBox(NULL,
_T("Call
to
CreateWindow
failed!"),
_T("Win32
Guided
Tour"),
NULL);
return
1;
}
//
The
parameters
to
ShowWindow
explained:
//
hWnd:
the
value
returned
from
CreateWindow
//
nCmdShow:
the
fourth
parameter
from
WinMain
ShowWindow(hWnd,
nCmdShow);
UpdateWindow(hWnd);
//
Main
message
loop:
MSG
msg;
while
(GetMessage(&msg,
NULL,
0,
0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return
(int)
msg.wParam;
}
http://msdn.microsoft.com/en-us/library/bb384843.aspx
不過你得知道怎麼建工程,不然就得在命令行編譯、鏈接
⑷ 怎樣用C語言編寫一個小游戲
「貪吃蛇」C代碼:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#define W 78 //游戲框的寬,x軸
#define H 26 //游戲框的高,y軸
int dir=3; //方向變數,初值3表示向「左」
int Flag=0; //吃了食物的標志(1是0否)
int score=0; //玩家得分
struct food{ int x; //食物的x坐標
int y; //食物的y坐標
}fod; //結構體fod有2個成員
struct snake{ int len; //身長
int speed; //速度
int x[100];
int y[100];
}snk; //結構體snk有4個成員
void gtxy( int x,int y) //控制游標移動的函數
{ COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void gtxy( int x,int y); //以下聲明要用到的幾個自編函數
void csh( ); //初始化界面
void keymove( ); //按鍵操作移動蛇
void putFod( ); //投放食物
int Over( ); //游戲結束(1是0否)
void setColor(unsigned short p, unsigned short q); //設定顯示顏色
int main( ) //主函數
{ csh( );
while(1)
{ Sleep(snk.speed);
keymove( );
putFod( );
if(Over( ))
{system(「cls」);
gtxy(W/2+1,H/2); printf(「游戲結束!T__T」);
gtxy(W/2+1,H/2+2); printf(「玩家總分:%d分」,score);
getch( );
break;
}
}
return 0;
}
void csh( ) //初始化界面
{ int i;
gtxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下兩行是隱藏游標的設置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(i=0;i<=W;i=i+2) //橫坐標要為偶數,因為這個要列印的字元佔2個位置
{ setColor(2, 0); //設定列印顏色為綠字黑底
gtxy(i,0); printf("■"); //列印上邊框
gtxy(i,H); printf("■"); //列印下邊框
}
for(i=1;i<H;i++)
{ gtxy(0,i); printf("■"); //列印左邊框
gtxy(W,i); printf("■"); //列印右邊框
}
while(1)
{ srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )
fod.x=rand()%(W-4)+2; //隨機函數rand( )產生一個從0到比」(W-4)」小1的數再加2
fod.y=rand()%(H-2)+1; //隨機函數rand( )產生一個從0到比」(H-2)」小1的數再加1
if (fod.x%2==0) break; //fod.x是食物的橫坐標,要是2的倍數(為偶數)
}
setColor(12, 0); //設定列印顏色為淡紅字黑底
gtxy(fod.x,fod.y); printf("●"); //到食物坐標處列印初試食物
snk.len=3; //蛇身長
snk.speed=350; //刷新蛇的時間,即是移動速度
snk.x[0]=W/2+1; //蛇頭橫坐標要為偶數(因為W/2=39)
snk.y[0]=H/2; //蛇頭縱坐標
setColor(9, 0); //設定列印顏色為淡藍字黑底
gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭
for(i=1;i<snk.len;i++)
{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];
gtxy(snk.x[i],snk.y[i]); printf("■"); //列印蛇身
}
setColor(7, 0); //恢復默認的白字黑底
return;
}
void keymove( ) //按鍵操作移動蛇
{ int key;
if( kbhit( ) ) //如有按鍵輸入才執行下面操作
{ key=getch( );
if (key==224) //值為224表示按下了方向鍵,下面要再次獲取鍵值
{ key=getch( );
if(key==72&&dir!=2)dir=1; //72表示按下了向上方向鍵
if(key==80&&dir!=1)dir=2; //80為向下
if(key==75&&dir!=4)dir=3; //75為向左
if(key==77&&dir!=3)dir=4; //77為向右
}
if (key==32)
{ while(1) if((key=getch( ))==32) break; } //32為空格鍵,這兒用來暫停
}
if (Flag==0) //如沒吃食物,才執行下面操作擦掉蛇尾
{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }
int i;
for (i = snk.len - 1; i > 0; i--) //從蛇尾起每節存儲前一節坐標值(蛇頭除外)
{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }
switch (dir) //判斷蛇頭該往哪個方向移動,並獲取最新坐標值
{ case 1: snk.y[0]--; break; //dir=1要向上移動
case 2: snk.y[0]++; break; //dir=2要向下移動
case 3: snk.x[0]-=2; break; //dir=3要向左移動
case 4: snk.x[0]+=2; break; //dir=4要向右移動
}
setColor(9, 0);
gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭
if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物則執行以下操作
{ printf("