❶ 求一个简单又有趣的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大功告成啦~