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("