1、用C語言編寫的小游戲迷宮
如果是GUI,可以用Windows API,可以通過《Winodws程序設計》學習知一下。還可以使用MFC,推薦看道《深入淺出MFC》,另外還有幾個比較好的圖形界面庫,像GTK+和QT,是跨內平台的。
如果是更高級的圖像編程,3D程序設計的話,可以使用DirectX和Opengl,還有個比較不錯的ogre,這幾個是做游戲比較常用容的。
2、塊語言編程游戲最後一關怎麼過
var cs1 = 0;
var cs2 = 180;
while(true){
var random = Math.random()*360;
move(random);
shut(random);
}
function move(random){
swim(cs1);
}
function shut(random){
if(scan(cs1,20)<=70){
if(scan(cs2,20)<=70){
cs2 = cs1-15;
cannon(cs1-5,scan(cs1-10,10))
}else{
cs1 += 5;
cs2 = cs1-15;
cannon(cs1-5,scan(cs1-10,10))
}
}else if(scan(cs2,20)<=70){
cs1 -= 5;
cs2 = cs1-15;
cannon(cs1-5,scan(cs1-10,10))
}else{
cs1 += 20;
}
}
————————————————
版權聲明:本文為CSDN博主「貓兒不哭」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:
/* sniper */
/* strategy: since a scan of the entire battlefield can be done in 90 */
/* degrees from a corner, sniper can scan the field quickly. */
/* external variables, that can be used by any function */
var corner = 0; /* current corner 0, 1, 2, or 2 */
var sc = 0; /* current scan start */
var range; /* range to target */
/* initialize the corner info */
/* x and y location of a corner, and starting scan degree */
var c1x = 2, c1y = 2, s1 = 0;
var c2x = 2, c2y = 98, s2 = 270;
var c3x = 98, c3y = 98, s3 = 180;
var c4x = 98, c4y = 2, s4 = 90;
var closest = Infinity;
new_corner(); /* start at a random corner */
var d = damage(); /* get current damage */
var dir = sc; /* starting scan direction */
while (true) { /* loop is executed forever */
while (dir < sc + 90) { /* scan through 90 degree range */
range = scan(dir, 2); /* look at a direction */
if (range <= 70) {
while (range > 0) { /* keep firing while in range */
closest = range; /* set closest flag */
cannon(dir, range); /* fire! */
range = scan(dir, 1); /* check target again */
if (d + 15 > damage()) /* sustained several hits, */
range = 0; /* goto new corner */
}
dir -= 10; /* back up scan, in case */
}
dir += 2; /* increment scan */
if (d != damage()) { /* check for damage incurred */
new_corner(); /* we're hit, move now */
d = damage();
dir = sc;
}
}
if (closest == Infinity) { /* check for any targets in range */
new_corner(); /* nothing, move to new corner */
d = damage();
dir = sc;
} else { /* targets in range, resume */
dir = sc;
}
closest = Infinity;
}
/* new corner function to move to a different corner */
function new_corner() {
var x, y;
var rand = Math.floor(Math.random() * 4); /* pick a random corner */
if (rand == corner) /* but make it different than the */
corner = (rand + 1) % 4;/* current corner */
else
corner = rand;
if (corner == 0) { /* set new x,y and scan start */
x = c1x;
y = c1y;
sc = s1;
}
if (corner == 1) {
x = c2x;
y = c2y;
sc = s2;
}
if (corner == 2) {
x = c3x;
y = c3y;
sc = s3;
}
if (corner == 3) {
x = c4x;
y = c4y;
sc = s4;
}
/* find the heading we need to get to the desired corner */
var angle = plot_course(x,y);
/* start drive train, full speed */
/* keep traveling until we are within 15 meters */
/* speed is checked in case we run into wall, other robot */
/* not terribly great, since were are doing nothing while moving */
while (distance(getX(), getY(), x, y) > 15)
drive(angle, 100);
/* cut speed, and creep the rest of the way */
while (distance(getX(), getY(), x, y) > 1)
drive(angle, 20);
/* stop drive, should coast in the rest of the way */
drive(angle, 0);
} /* end of new_corner */
/* classical pythagorean distance formula */
function distance(x1, y1, x2, y2) {
var x = x1 - x2;
var y = y1 - y2;
return Math.sqrt((x * x) + (y * y));
}
/* plot course function, return degree heading to */
/* reach destination x, y; uses atan() trig function */
function plot_course(xx, yy) {
var d;
var x,y;
var curx, cury;
curx = getX(); /* get current location */
cury = getY();
x = curx - xx;
y = cury - yy;
/* atan only returns -90 to +90, so figure out how to use */
/* the atan() value */
if (x == 0) { /* x is zero, we either move due north or south */
if (yy > cury)
d = 90; /* north */
else
d = 270; /* south */
} else {
if (yy < cury) {
if (xx > curx)
d = 360 + Math.atan_deg(y / x); /* south-east, quadrant 4 */
else
d = 180 + Math.atan_deg(y / x); /* south-west, quadrant 3 */
} else {
if (xx > curx)
d = Math.atan_deg(y / x); /* north-east, quadrant 1 */
else
d = 180 + Math.atan_deg(y / x); /* north-west, quadrant 2 */
}
}
return d;
}
/* rabbit */
// rabbit runs around the field, randomly and never fires; use as a target.
/* go - go to the point specified */
function go (dest_x, dest_y) {
var course = plot_course(dest_x, dest_y);
while (distance(getX(), getY(), dest_x, dest_y) > 5) {
drive(course, 25);
}
while (speed() > 0) {
drive(course, 0);
}
}
/* distance forumula. */
function distance(x1, y1, x2, y2) {
var x = x1 - x2;
var y = y1 - y2;
return Math.sqrt((x * x) + (y * y));
}
/* plot_course - figure out which heading to go. */
function plot_course(xx, yy) {
var d;
var curx = getX();
var cury = getY();
var x = curx - xx;
var y = cury - yy;
if (x == 0) {
if (yy > cury) {
d = 90;
} else {
d = 270;
}
} else {
if (yy < cury) {
if (xx > curx) {
d = 360 + Math.atan_deg(y / x);
} else {
d = 180 + Math.atan_deg(y / x);
}
} else {
if (xx > curx) {
d = Math.atan_deg(y / x);
} else {
d = 180 + Math.atan_deg(y / x);
}
}
}
return d;
}
while (true) {
// Go somewhere in the field.
var x = Math.random() * 100;
var y = Math.random() * 100;
go(x, y);
}
/* counter */
/* scan in a counter-clockwise direction (increasing degrees) */
/* moves when hit */
var range;
var last_dir = 0;
var res = 2;
var d = damage();
var angle = Math.random() * 360;
while (true) {
while ((range = scan(angle, res)) != Infinity) {
if (range > 70) { /* out of range, head toward it */
drive(angle, 50);
var i = 1;
while (i++ < 50) /* use a counter to limit move time */
;
drive (angle, 0);
if (d != damage()) {
d = damage();
run();
}
angle -= 3;
} else {
while (!cannon(angle, range))
;
if (d != damage()) {
d = damage();
run();
}
angle -= 15;
}
}
if (d != damage()) {
d = damage();
run();
}
angle += res;
angle %= 360;
}
/* run moves around the center of the field */
function run() {
var i = 0;
var x = getX();
var y = getY();
if (last_dir == 0) {
last_dir = 1;
if (y > 51) {
drive(270, 100);
while (y - 10 < getY() && i++ < 50)
;
drive(270, 0);
} else {
drive(90, 100);
while (y + 10 > getY() && i++ < 50)
;
drive(90, 0);
}
} else {
last_dir = 0;
if (x > 51) {
drive(180, 100);
while (x - 10 < getX() && i++ < 50)
;
drive(180, 0);
} else {
drive(0, 100);
while (x + 10 > getX() && i++ < 50)
;
drive(0, 0);
}
}
}
/* rook.r - scans the battlefield like a rook, i.e., only 0,90,180,270 */
/* move horizontally only, but looks horz and vertically */
/* move to center of board */
if (getY() < 50) {
while (getY() < 40) /* stop near center */
drive(90, 100); /* start moving */
} else {
while (getY() > 60) /* stop near center */
drive(270, 100); /* start moving */
}
drive(0, 0);
while (speed() > 0)
;
/* initialize starting parameters */
var d = damage();
var course = 0;
var boundary = 99;
drive(course, 30);
/* main loop */
while(true) {
/* look all directions */
look(0);
look(90);
look(180);
look(270);
/* if near end of battlefield, change directions */
if (course == 0) {
if (getX() > boundary || speed() == 0)
change();
}
else {
if (getX() < boundary || speed() == 0)
change();
}
}
/* look somewhere, and fire cannon repeatedly at in-range target */
function look(deg) {
var range;
while ((range = scan(deg, 4)) <= 70) {
drive(course, 0);
cannon(deg, range);
if (d + 20 != damage()) {
d = damage();
change();
}
}
}
function change() {
if (course == 0) {
boundary = 1;
course = 180;
} else {
boundary = 99;
course = 0;
}
drive(course, 30);
}
var west = false;
while (true) {
x();
deg();
}
function x(){
for(var i = 0; i <= 360; i += 20){
if(scan(i, 20) <= 70){
cannon(i, scan(i, 20));
}
}
}
function deg(){
if (west) {
if (getX() > 25) {
swim(180, 100);
} else {
west = false;
swim(0, 0);
}
} else {
if (getX() < 75) {
swim(0, 100);
} else {
west = true;
swim(0, 0);
}
}
}
3、請問如何用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 (!(&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
不過你得知道怎麼建工程,不然就得在命令行編譯、鏈接
4、C++編程問題:編一個迷宮游戲程序
在書找的,只打了個結構體(太多了),我利用stack做的!希望能對你有所幫助!程序如下:
typedef struct{
int ord;//通道塊的路徑上的序號
PosType seat;//通道塊在迷宮的坐標位置
int di;//從此通道塊走向下一個通道塊的方向
}SelemType;//stack的元素類型
Status MazePath(MazeType maze,PosType start,PosType end){
//若迷宮 maze中存在從入口start到出口end的通道,所以,求得一條存放在stack中
//(從stack底到頂)並返回TRUE,否則返回FALSE
InitStack(S);
curpos=start;//設置當前的位置為入口位置
curstep=1;//探索第一步
do{
if(Pass(curpos)){//當前位置可以通過,即是未曾走過的通道塊
FootPrint(curpos);//留下足跡
e=(curstep,curpos,1);
Push(s,e);//加入路徑
if(curpos==end)
return(TRUE);//到達終點
curpos=NextPos(curpos,1);//下一位置是當前位置的東部
curstep++;//探索下一步
}
else{//當前位置不能通過
if(!StackEmpty(S)){
Pop(S,e);
while(e.di==4&&!StackEmpty(S)){
MarkPrint(e.seat);//留下不能通過的標記,並退回一步
Pop(S,e);
}
if(e.di<4){
e.di++;
Push(s.e);//換下一個方向探索
curpos=NextPos(e.seat e.di);//設定當前位置是該新方向上的相鄰塊
}//if
}//if
}//else
}while(!StackEmpty(S));
return(FALSE);
}//MazePath
5、用flash怎麼做迷宮游戲
要用代碼實現,就是AS.
ActionScript(簡稱抄AS)是由Macromedia(現已被Adobe收購)為其襲Flash產品開發的 ,最初是一種簡單的腳本語言,現在最新版百本ActionScript3.0,是一種完全的面向對象的編程語言,功能度強大,類庫豐富,語法類似問JavaScript,多用於Flash互動性、娛樂性、實用性開發答,網頁製作和RIA(網際網路應用程序)開發。