本文目錄一覽:
C語言可以編3D遊戲嗎?
完全可以。如果需要我可以給你些簡單的源碼。我現在是用C++寫3D遊戲。不過,鑒於你目前的水平。估計你看不懂。先把語言學會了,想編3D還有點早。告訴你吧。最簡單的顯示個立方體的3D都需要大概兩三百以上行代碼。大多是框架。
不過,你先搞懂怎麼顯示圖像了再3D吧。 大概來說,在windows上先要創建個窗口類,然後在DC上畫圖。等等。。
c語言小遊戲代碼
最基礎的貪吃蛇的代碼
#includestdio.h
#includewindows.h//基本型態定義。支援型態定義函數。使用者界面函數 圖形裝置界面函數。
#includeconio.h //用戶通過按鍵盤產生的對應操作 (控制台)
#includestdlib.h
#includetime.h //日期和時間頭文件
#define LEN 30
#define WID 25
int Snake[LEN][WID] = {0}; //數組的元素代表蛇的各個部位
char Sna_Hea_Dir = ‘a’;//記錄蛇頭的移動方向
int Sna_Hea_X, Sna_Hea_Y;//記錄蛇頭的位置
int Snake_Len = 3;//記錄蛇的長度
clock_t Now_Time;//記錄當前時間,以便自動移動
int Wait_Time ;//記錄自動移動的時間間隔
int Eat_Apple = 1;//吃到蘋果表示為1
int Level ;
int All_Score = -1;
int Apple_Num = -1;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //獲取標準輸出的句柄 windows.h
//句柄 :標誌應用程序中的不同對象和同類對象中的不同的實例 方便操控,
void gotoxy(int x, int y)//設置光標位置
{
COORD pos = {x,y}; //定義一個字符在控制台屏幕上的坐標POS
SetConsoleCursorPosition(hConsole, pos); //定位光標位置的函數windows.h
}
void Hide_Cursor()//隱藏光標 固定函數
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, cursor_info);
}
void SetColor(int color)//設置顏色
{
SetConsoleTextAttribute(hConsole, color);
//是API設置字體顏色和背景色的函數 格式:SetConsoleTextAttribute(句柄,顏色);
}
void Print_Snake()//打印蛇頭和蛇的脖子和蛇尾
{
int iy, ix, color;
for(iy = 0; iy WID; ++iy)
for(ix = 0; ix LEN; ++ix)
{
if(Snake[ix][iy] == 1)//蛇頭
{
SetColor(0xf); //oxf代表分配的內存地址 setcolor:34行自定義設置顏色的函數
gotoxy(ix*2, iy);
printf(“※”);
}
if(Snake[ix][iy] == 2)//蛇的脖子
{
color = rand()%15 + 1; //rand()函數是產生隨機數的一個隨機函數。C語言里還有 srand()函數等。
//頭文件:stdlib.h
if(color == 14)
color -= rand() % 13 + 1; //變色
SetColor(color);
gotoxy(ix*2, iy);
printf(“■”);
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2, iy);
SetColor(0xe);
printf(“≈”);
}
}
}
void Clear_Snake()//擦除貪吃蛇
{
int iy, ix;
for(iy = 0; iy WID; ++iy)
for(ix = 0; ix LEN; ++ix)
{
gotoxy(ix*2, iy);
if(Snake[ix][iy] == Snake_Len)
printf(” “);
}
}
void Rand_Apple()//隨機產生蘋果
{
int ix, iy;
do
{
ix = rand() % LEN;
iy = rand() % WID;
}while(Snake[ix][iy]);
Snake[ix][iy] = -1;
gotoxy(ix*2, iy);
printf(“⊙”);
Eat_Apple = 0;
}
void Game_Over()//蛇死掉了
{
gotoxy(30, 10);
printf(“Game Over”);
Sleep(3000);
system(“pause nul”);
exit(0);
}
void Move_Snake()//讓蛇動起來
{
int ix, iy;
for(ix = 0; ix LEN; ++ix)//先標記蛇頭
for(iy = 0; iy WID; ++iy)
if(Snake[ix][iy] == 1)
{
switch(Sna_Hea_Dir)//根據新的蛇頭方向標誌蛇頭
{
case ‘w’:
if(iy == 0)
Game_Over();
else
Sna_Hea_Y = iy – 1;
Sna_Hea_X = ix;
break;
case ‘s’:
if(iy == (WID -1))
Game_Over();
else
Sna_Hea_Y = iy + 1;
Sna_Hea_X = ix;
break;
case ‘a’:
if(ix == 0)
Game_Over();
else
Sna_Hea_X = ix – 1;
Sna_Hea_Y = iy;
break;
case ‘d’:
if(ix == (LEN – 1))
Game_Over();
else
Sna_Hea_X = ix + 1;
Sna_Hea_Y = iy;
break;
default:
break;
}
}
if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1Snake[Sna_Hea_X][Sna_Hea_Y]!=0Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over();
if(Snake[Sna_Hea_X][Sna_Hea_Y] 0)//吃到蘋果
{
++Snake_Len;
Eat_Apple = 1;
}
for(ix = 0; ix LEN; ++ix)//處理蛇尾
for(iy = 0; iy WID; ++iy)
{
if(Snake[ix][iy] 0)
{
if(Snake[ix][iy] != Snake_Len)
Snake[ix][iy] += 1;
else
Snake[ix][iy] = 0;
}
}
Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//處理蛇頭
}
void Get_Input()//控制蛇的移動方向
{
if(kbhit())
{
switch(getch())
{
case 87:
Sna_Hea_Dir = ‘w’;
break;
case 83:
Sna_Hea_Dir = ‘s’;
break;
case 65:
Sna_Hea_Dir = ‘a’;
break;
case 68:
Sna_Hea_Dir = ‘d’;
break;
default:
break;
}
}
if(clock() – Now_Time = Wait_Time)//蛇到時間自動行走
{
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time = clock();
}
}
void Init()//初始化
{
system(“title 貪吃毛毛蛇”);
system(“mode con: cols=80 lines=25”);
Hide_Cursor();
gotoxy(61, 4);
printf(“You Score:”);
gotoxy(61, 6);
printf(“You Level:”);
gotoxy(61, 8);
printf(“The Lenght:”);
gotoxy(61, 10);
printf(“The Speed:”);
gotoxy(61, 12);
printf(“Apple Num:”);
int i;
for(i = 0; i Snake_Len; ++i)//生成蛇
Snake[10+i][15] = i+1;
int iy, ix;//打印蛇
for(iy = 0; iy WID; ++iy)
for(ix = 0; ix LEN; ++ix)
{
if(Snake[ix][iy])
{
SetColor(Snake[ix][iy]);
gotoxy(ix*2, iy);
printf(“■”);
}
}
}
void Pri_News()//打印信息
{
SetColor(0xe);
gotoxy(73,4);
All_Score += Level;
printf(“%3d”, All_Score);
gotoxy(73, 6);
printf(“%3d”, Level);
gotoxy(73, 8);
printf(“%3d”,Snake_Len);
gotoxy(73, 10);
printf(“0.%3ds”, Wait_Time/10);
gotoxy(73, 12);
printf(“%d”, Apple_Num);
}
void Lev_Sys()//等級系統
{
if(((Apple_Num-1) / 10) == Level)
{
++Level;
if(Wait_Time 50)
Wait_Time -= 50;
else
if(Wait_Time 10)
Wait_Time -= 10;
else
Wait_Time -= 1;
}
}
int main(void)
{
Init();
srand((unsigned)time(NULL));//設置隨機數的種子
Now_Time = clock();
int speed1=1000,speed2,a;
printf(“\n”);
printf(“請輸入你想要的速度\n”);
scanf(“%d”,speed2);
Level=1;
Wait_Time=speed1-speed2;
printf(“請輸入你想要的蘋果數\n”);
scanf(“%d”,a);
while(a–)
Rand_Apple();
while(1)
{
if(Eat_Apple)
{
++Apple_Num;
Rand_Apple();
Lev_Sys();
Pri_News();
}
Get_Input();
Sleep(10);
}
return 0;
}
用C++編寫的小遊戲源代碼
五子棋的代碼:
#includeiostream
#includestdio.h
#includestdlib.h
#include time.h
using namespace std;
const int N=15; //15*15的棋盤
const char ChessBoardflag = ‘ ‘; //棋盤標誌
const char flag1=’o’; //玩家1或電腦的棋子標誌
const char flag2=’X’; //玩家2的棋子標誌
typedef struct Coordinate //坐標類
{
int x; //代表行
int y; //代表列
}Coordinate;
class GoBang //五子棋類
{
public:
GoBang() //初始化
{
InitChessBoard();
}
void Play() //下棋
{
Coordinate Pos1; // 玩家1或電腦
Coordinate Pos2; //玩家2
int n = 0;
while (1)
{
int mode = ChoiceMode();
while (1)
{
if (mode == 1) //電腦vs玩家
{
ComputerChess(Pos1,flag1); // 電腦下棋
if (GetVictory(Pos1, 0, flag1) == 1) //0表示電腦,真表示獲勝
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
else //玩家1vs玩家2
{
PlayChess(Pos1, 1, flag1); // 玩家1下棋
if (GetVictory(Pos1, 1, flag1)) //1表示玩家1
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
}
cout “***再來一局***” endl;
cout “y or n :”;
char c = ‘y’;
cin c;
if (c == ‘n’)
break;
}
}
protected:
int ChoiceMode() //選擇模式
{
int i = 0;
system(“cls”); //系統調用,清屏
InitChessBoard(); //重新初始化棋盤
cout “***0、退出 1、電腦vs玩家 2、玩家vs玩家***” endl;
while (1)
{
cout “請選擇:”;
cin i;
if (i == 0) //選擇0退出
exit(1);
if (i == 1 || i == 2)
return i;
cout “輸入不合法” endl;
}
}
void InitChessBoard() //初始化棋盤
{
for (int i = 0; i N + 1; ++i)
{
for (int j = 0; j N + 1; ++j)
{
_ChessBoard[i][j] = ChessBoardflag;
}
}
}
void PrintChessBoard() //打印棋盤,這個函數可以自己調整
{
system(“cls”); //系統調用,清空屏幕
for (int i = 0; i N+1; ++i)
{
for (int j = 0; j N+1; ++j)
{
if (i == 0) //打印列數字
{
if (j!=0)
printf(“%d “, j);
else
printf(” “);
}
else if (j == 0) //打印行數字
printf(“%2d “, i);
else
{
if (i N+1)
{
printf(“%c |”,_ChessBoard[i][j]);
}
}
}
cout endl;
cout ” “;
for (int m = 0; m N; m++)
{
printf(“–|”);
}
cout endl;
}
}
void PlayChess(Coordinate pos, int player, int flag) //玩家下棋
{
PrintChessBoard(); //打印棋盤
while (1)
{
printf(“玩家%d輸入坐標:”, player);
cin pos.x pos.y;
if (JudgeValue(pos) == 1) //坐標合法
break;
cout “坐標不合法,重新輸入” endl;
}
_ChessBoard[pos.x][pos.y] = flag;
}
void ComputerChess(Coordinate pos, char flag) //電腦下棋
{
PrintChessBoard(); //打印棋盤
int x = 0;
int y = 0;
while (1)
{
x = (rand() % N) + 1; //產生1~N的隨機數
srand((unsigned int) time(NULL));
y = (rand() % N) + 1; //產生1~N的隨機數
srand((unsigned int) time(NULL));
if (_ChessBoard[x][y] == ChessBoardflag) //如果這個位置是空的,也就是沒有棋子
break;
}
pos.x = x;
pos.y = y;
_ChessBoard[pos.x][pos.y] = flag;
}
int JudgeValue(const Coordinate pos) //判斷輸入坐標是不是合法
{
if (pos.x 0 pos.x = Npos.y 0 pos.y = N)
{
if (_ChessBoard[pos.x][pos.y] == ChessBoardflag)
{
return 1; //合法
}
}
return 0; //非法
}
int JudgeVictory(Coordinate pos, char flag) //判斷有沒有人勝負(底層判斷)
{
int begin = 0;
int end = 0;
int begin1 = 0;
int end1 = 0;
//判斷行是否滿足條件
(pos.y – 4) 0 ? begin = (pos.y – 4) : begin = 1;
(pos.y + 4) N ? end = N : end = (pos.y + 4);
for (int i = pos.x, j = begin; j + 4 = end; j++)
{
if (_ChessBoard[i][j] == flag_ChessBoard[i][j + 1] == flag
_ChessBoard[i][j + 2] == flag_ChessBoard[i][j + 3] == flag
_ChessBoard[i][j + 4] == flag)
return 1;
}
//判斷列是否滿足條件
(pos.x – 4) 0 ? begin = (pos.x – 4) : begin = 1;
(pos.x + 4) N ? end = N : end = (pos.x + 4);
for (int j = pos.y, i = begin; i + 4 = end; i++)
{
if (_ChessBoard[i][j] == flag_ChessBoard[i + 1][j] == flag
_ChessBoard[i + 2][j] == flag_ChessBoard[i + 3][j] == flag
_ChessBoard[i + 4][j] == flag)
return 1;
}
int len = 0;
//判斷主對角線是否滿足條件
pos.x pos.y ? len = pos.y – 1 : len = pos.x – 1;
if (len 4)
len = 4;
begin = pos.x – len; //橫坐標的起始位置
begin1 = pos.y – len; //縱坐標的起始位置
pos.x pos.y ? len = (N – pos.x) : len = (N – pos.y);
if (len4)
len = 4;
end = pos.x + len; //橫坐標的結束位置
end1 = pos.y + len; //縱坐標的結束位置
for (int i = begin, j = begin1; (i + 4 = end) (j + 4 = end1); ++i, ++j)
{
if (_ChessBoard[i][j] == flag_ChessBoard[i + 1][j + 1] == flag
_ChessBoard[i + 2][j + 2] == flag_ChessBoard[i + 3][j + 3] == flag
_ChessBoard[i + 4][j + 4] == flag)
return 1;
}
//判斷副對角線是否滿足條件
(pos.x – 1) (N – pos.y) ? len = (N – pos.y) : len = pos.x – 1;
if (len 4)
len = 4;
begin = pos.x – len; //橫坐標的起始位置
begin1 = pos.y + len; //縱坐標的起始位置
(N – pos.x) (pos.y – 1) ? len = (pos.y – 1) : len = (N – pos.x);
if (len4)
len = 4;
end = pos.x + len; //橫坐標的結束位置
end1 = pos.y – len; //縱坐標的結束位置
for (int i = begin, j = begin1; (i + 4 = end) (j – 4 = end1); ++i, –j)
{
if (_ChessBoard[i][j] == flag_ChessBoard[i + 1][j – 1] == flag
_ChessBoard[i + 2][j – 2] == flag_ChessBoard[i + 3][j – 3] == flag
_ChessBoard[i + 4][j – 4] == flag)
return 1;
}
for (int i = 1; i N + 1; ++i) //棋盤有沒有下滿
{
for (int j =1; j N + 1; ++j)
{
if (_ChessBoard[i][j] == ChessBoardflag)
return 0; //0表示棋盤沒滿
}
}
return -1; //和棋
}
bool GetVictory(Coordinate pos, int player, int flag) //對JudgeVictory的一層封裝,得到具體那個玩家獲勝
{
int n = JudgeVictory(pos, flag); //判斷有沒有人獲勝
if (n != 0) //有人獲勝,0表示沒有人獲勝
{
PrintChessBoard();
if (n == 1) //有玩家贏棋
{
if (player == 0) //0表示電腦獲勝,1表示玩家1,2表示玩家2
printf(“***電腦獲勝***\n”);
else
printf(“***恭喜玩家%d獲勝***\n”, player);
}
else
printf(“***雙方和棋***\n”);
return true; //已經有人獲勝
}
return false; //沒有人獲勝
}
private:
char _ChessBoard[N+1][N+1];
};
擴展資料:
設計思路
1、進行問題分析與設計,計劃實現的功能為,開局選擇人機或雙人對戰,確定之後比賽開始。
2、比賽結束後初始化棋盤,詢問是否繼續比賽或退出,後續可加入復盤、悔棋等功能。
3、整個過程中,涉及到了棋子和棋盤兩種對象,同時要加上人機對弈時的AI對象,即涉及到三個對象。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/236525.html