❶ 求一個簡單又有趣的java小游戲代碼

那你就自己做個猜數字好了
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("請輸入一個100以內的整數:");
for (int i=0;i<=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shu>num)
System.out.println("輸入的數大了,輸小點的!");
else if (shu<num)
System.out.println("輸入的數小了,輸大點的!");
else {
System.out.println("恭喜你,猜對了!");
if (i<=2)
System.out.println("你真是個天才!");
else if (i<=6)
System.out.println("還將就,你過關了!");
else if (i<=8)
System.out.println("但是你還……真笨!");
else
System.out.println("你和豬沒有兩樣了!");

break;}
}
}

}

❷ 用C++編寫的小游戲源代碼

五子棋的代碼:

#include<iostream>

#include<stdio.h>

#include<stdlib.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 <= N&&pos.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 (len>4)

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 (len>4)

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("***電腦獲勝*** ");

else

printf("***恭喜玩家%d獲勝*** ", player);

}

else

printf("***雙方和棋*** ");

return true; //已經有人獲勝

}

return false; //沒有人獲勝

}

private:

char _ChessBoard[N+1][N+1];

};

(2)小游戲的代碼擴展閱讀:

設計思路

1、進行問題分析與設計,計劃實現的功能為,開局選擇人機或雙人對戰,確定之後比賽開始。

2、比賽結束後初始化棋盤,詢問是否繼續比賽或退出,後續可加入復盤、悔棋等功能。

3、整個過程中,涉及到了棋子和棋盤兩種對象,同時要加上人機對弈時的AI對象,即涉及到三個對象。

❸ 求C語言小游戲源程序

簡易「推箱子」游戲C代碼:

#include <stdio.h>

#include <conio.h>

#include<stdlib.h>

#include<windows.h>

int m =0; //m代表第幾關

struct maps{short a[9][11]; };

struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5關,每關9行

0,1,1,1,1,1,1,1,0,0,0,

0,1,0,0,0,0,0,1,1,1,0,

1,1,4,1,1,1,0,0,0,1,0, //0空地,1牆

1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人

1,0,3,3,1,0,4,0,1,1,0, //3是目的地

1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)

0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,

0,0,1,1,1,1,0,0,0,0,0,

0,0,1,5,0,1,1,1,0,0,0,

0,0,1,0,4,0,0,1,0,0,0,

0,1,1,1,0,1,0,1,1,0,0,

0,1,3,1,0,1,0,0,1,0,0,

0,1,3,4,0,0,1,0,1,0,0,

0,1,3,0,0,0,4,0,1,0,0,

0,1,1,1,1,1,1,1,1,0,0,

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,1,1,1,1,1,1,1,0,

0,0,1,1,0,0,1,0,5,1,0,

0,0,1,0,0,0,1,0,0,1,0,

0,0,1,4,0,4,0,4,0,1,0,

0,0,1,0,4,1,1,0,0,1,0,

1,1,1,0,4,0,1,0,1,1,0,

1,3,3,3,3,3,0,0,1,0,0,

1,1,1,1,1,1,1,1,1,0,0,

0,1,1,1,1,1,1,1,1,1,0,

0,1,0,0,1,1,0,0,0,1,0,

0,1,0,0,0,4,0,0,0,1,0,

0,1,4,0,1,1,1,0,4,1,0,

0,1,0,1,3,3,3,1,0,1,0,

1,1,0,1,3,3,3,1,0,1,1,

1,0,4,0,0,4,0,0,4,0,1,

1,0,0,0,0,0,1,0,5,0,1,

1,1,1,1,1,1,1,1,1,1,1,

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,1,1,1,1,1,1,0,0,

0,1,1,1,0,0,0,0,1,0,0,

1,1,3,0,4,1,1,0,1,1,0,

1,3,3,4,0,4,0,0,5,1,0,

1,3,3,0,4,0,4,0,1,1,0,

1,1,1,1,1,1,0,0,1,0,0,

0,0,0,0,0,1,1,1,1,0,0,

0,0,0,0,0,0,0,0,0,0,0 };

void DrMap( ) //繪制地圖

{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隱藏游標的設置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

printf(" 推箱子");

printf(" ");

for (int i = 0; i < 9; i++)

{for (int j = 0; j < 11; j++)

{switch (map[m].a[i][j])

{case 0: printf(" "); break;

case 1: printf("■"); break;

case 3: printf("◎");break;

case 4: printf("□"); break;

case 5: printf("♀"); break; //5是人

case 7: printf("□"); break; //4 + 3箱子在目的地中

case 8: printf("♀");break; // 5 + 3人在目的地中

}

}

printf(" ");

}

}

void gtxy(int x, int y) //控制游標位置的函數

{ COORD coord;

coord.X = x;

coord.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void start( ) //開始游戲

{ int r, c; //存儲人的下標

for (int i = 0; i < 9; i++)

{ for (int j = 0; j < 11; j++)

{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下標

}

char key;

key = getch( );

switch (key)

{case 'W':

case 'w':

case 72:

if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)

{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置輸出字元

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)

{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)

{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;

map[m]. a [r][c] -= 5; }

} break;

case 'S':

case 's':

case 80:

if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)

{ gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)

{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)

{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'A':

case 'a':

case 75:

if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)

{ gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)

{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)

{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'D':

case 'd':

case 77:

if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)

{ gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)

{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)

{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

}

}

int ifwan( ) //是否完成(1是0否)

{ if(m==0){if(map[m].a[5][2]==7&& map[m].a[5][3]==7&&

map[m].a[6][2]==7&& map[m].a[6][3]==7) return 1;}

if(m==1){if(map[m].a[5][2]==7&& map[m].a[6][2]==7&&

map[m].a[7][2]==7) return 1;}

if(m==2){if(map[m].a[7][1]==7&& map[m].a[7][2]==7&& map[m].a[7][3]==7&&

map[m].a[7][4]==7&& map[m].a[7][5]==7) return 1;}

if(m==3){if(map[m].a[4][4]==7&& map[m].a[4][5]==7&& map[m].a[4][6]==7&&

map[m].a[5][4]==7&& map[m].a[5][5]==7&& map[m].a[5][6]==7) return 1;}

if(m==4){if(map[m].a[3][2]==7&& map[m].a[4][1]==7&& map[m].a[4][2]==7&&

map[m].a[5][1]==7&& map[m].a[5][2]==7) return 1;}

return 0;

}

int main( ) //主函數

{ while (1)

{ system("cls");

DrMap( );

while (1)

{ start( );

if(ifwan()){printf("07");break;} //完成後響鈴

}

m+=1;

}

return 0;

}

❹ 製作游戲代碼

看完這幾本書你就會了
1。《C程序設計》清華大學出版社,譚浩強寫的那本。
這個是版最基礎的C語言教材。權
2.《C++ Primer》
這個是學C++的經典。
有了C的基礎你就可以去學C++了,當然如果你覺得你意志力很強的話可以直接學C++。
有人說先學C在學C++會對思維有一些舒服,但是C++比較難如果直接學的話很可能會堅持不下去。先學哪個要根據你自己情況決定。
3.《windows游戲編程大師技巧》,《3D游戲編程大師技巧》
心急的話,看完前面兩本九看是看這個吧
這兩本書是同一個作者的,先看第一本再看第二本。這兩本是游戲編程的經典之作。學游戲編程必須看這兩本。
程序編寫和AI裡面都有。基本上游戲編程的內容這兩本書里都有。

❺ 用C語言編寫的小游戲代碼是什麼

「猜數字小游戲」,每個數字後按空格,最後按回車確認

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int a[4],b[4];

int count=0; //計算猜測次數

void csh( ); //初始化

void start( ); //開始游戲

int main( )

{ csh( );

start( );

}

void csh( ) //初始化

{ printf(" 猜 數 字 小 游 戲 ");

printf(「 猜四個數字,如數字與順序都正確記為A,數字正確位置不對記為B. 」);

}

void start( ) //開始游戲

{int m,n; //m是完全猜對的個數,n是順序不對的個數

while(1)

{srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )

while(1) { for(int i=0;i<4;i++) a[i]=rand( )%10; //rand( )函數每次隨機產生一個0-9的數

if( (a[3]!=a[2]&&a[3]!=a[1]&&a[3]!=a[0])&&

(a[2]!=a[1]&&a[2]!=a[0])&&a[1]!=a[0] ) break; } //4個隨機數各自不相等

printf(" 請依次輸入4個一位整數: ");

while(1)

{for(int i=0;i<4;i++) scanf(「%d」,&b[i]);

printf(" 你輸入的是:%d %d %d %d ",b[0],b[1],b[2],b[3]);

m=0;n=0;

for(int i=0;i<4;i++)

{for(int j=0;j<4;j++)

{ if(b[i]==a[j]&&i==j)m=m+1; if(b[i]==a[j]&&i!=j)n=n+1; }

}

count=count+1;

printf(" %dA %dB 你試了%d次 ",m,n,count);

if(m==4)break;

if(count==8){ count=0; break; }

}

printf(" ");

if(m==4)printf(" 你猜對了(^-^)! 就是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

else printf(" 你輸了(T-T)!哈哈!應該是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

int z;

printf(" (要繼續嗎?1或0) ");

scanf(「%d」,&z);

if(z==0) break;

}

}

❻ 小游戲的代碼

#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /*食物的結構體*/
{
int x; /*食物的橫坐標*/
int y; /*食物的縱坐標*/
int yes; /*食物是否出現的變數*/
}food;
struct Snack /*蛇的結構體*/
{
int x[N];
int y[N];
int node; /*蛇的節數*/
int direction; /*蛇的方向*/
int life; /*蛇的生命,0活著,1死亡*/
}snake;
void Init(void); /*圖形驅動*/
void Close(void); /*關閉游戲函數*/
void DrawK(void); /*畫圖函數*/
void GameOver(void);/*輸出失敗函數*/
void GamePlay(); /*游戲控制函數 主要程序*/
void PrScore(void); /*分數輸出函數*/

DELAY(char ch)/*調節游戲速度*/
{
if(ch=='3')
{
delay(gamespeed); /*delay是延遲函數*/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}

Menu()/*游戲開始菜單*/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers..\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}

/*主函數*/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}

void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}

void DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49); /*畫出上邊框*/
rectangle(i,451,i+10,460); /*畫出下邊框*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*畫出左邊框*/
rectangle(601,i,610,i+10); /*畫出右邊框*/
}
}

void GamePlay(char ch)
{
randomize(); /*隨機數發生器*/
food.yes=1; /*1代表要出現食物,0表示以存在食物*/
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1) /*可以重復游戲*/
{
while(!kbhit()) /*在沒有按鍵的情況下蛇自己移動*/
{
if(food.yes==1) /*需要食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60; /*使用rand函數隨機產生食物坐標*/
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++; /*判斷食物是否出現在整格里*/
food.yes=0; /*現在有食物了*/
}
if(food.yes==0) /*有食物了就要顯示出來*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--) /*貪吃蛇的移動演算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*貪吃蛇的身體移動演算法*/
}

switch(snake.direction) /*貪吃蛇的頭部移動演算法,以此來控制移動*/
{
case 1:snake.x[0]+=10;break;
case 2:snake.x[0]-=10;break;
case 3:snake.y[0]-=10;break;
case 4:snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++) /*判斷是否頭部與身體相撞*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
/*下面是判斷是否撞到牆壁*/
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)
{
GameOver();
snake.life=1;
}
if(snake.life==1) /*如果死亡就退出循環*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y) /*判斷蛇是否吃到食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10); /*吃的食物後用黑色將食物擦去*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*現把增加的一節放到看不到的地方去*/

snake.node++;
food.yes=1;
score+=10;
PrScore();
}
setcolor(4); /*每次移動後將後面的身體擦去*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
DELAY(ch);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);

}
if(snake.life==1)
break;
key=bioskey(0); /*接受按鍵*/
if(key==ESC)
break;
else
if(key==UP&&snake.direction!=4)/*判斷是否改變方向*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}
}

void GameOver(void)
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}

void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}

貪吃蛇

❼ 小游戲的代碼怎麼看

樓主,你在你想要上傳的小游戲游戲畫面中,滑鼠附近會不會出現一個回下載的小圖標(前答提你電腦上必須安裝迅雷),如果有了,就點擊一下,出現迅雷下載的窗口,在上面有一行網址(URL),請你把後面的那個地址右鍵復制起來(這個地址其實就是小游戲的FLASH地址),然後在你日誌中插入FLASH模塊,把剛才復制的FLASH地址右鍵粘貼在那個FLASH地址框里,還有其他設置你可以自己任意選擇,最後點擊確定,在發表日誌,就能夠看到FLASH小游戲的畫面咯~也就OK大功告成啦~