Matlab中生命游戏java代码代码每一行的注释2015/10/11 22:36:53

生命游戏/Game of Life的Java实现(转)
来源:博客园
首先简单介绍一下《生命游戏》
  生命游戏其实是一个零玩家游戏。它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去;相反,如果周围活细胞过少,这个细胞会因太孤单而死去。具体如下图:

每个格子的生死遵循下面的原则:
  1. 如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生(即该细胞若原先为死,则转为生,若原先为生,则保持不变) 。
  2. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
  3. 在其它情况下,该细胞为死(即该细胞若原先为生,则转为死,若原先为死,则保持不变设定图像中每个像素的初始状态后依据上述的游戏规则演绎生命的变化,由于初始状态和迭代次数不同,将会得到令人叹服的优美图案)。
图案:
有三类图案:
第一类这种图案是固定不变的:
第二类:是按规律变化的:
第三类:是不断变化和移动的,但是有一定的周期性,最著名的就是
滑翔机:

Java实现代码:

 1 package com.cisco.
 2 
 3 
 4 import java.awt.event.ActionE
 5 import java.awt.event.ActionL
 6 
 7 
 8 import javax.swing.JF
 9 import javax.swing.JM
10 import javax.swing.JMenuB
11 import javax.swing.JMenuI
12 
13 
14 public class LifeGame extends JFrame
15 {
16
private final W
17
public LifeGame(int rows, int columns)
19
world = new World(rows, columns);
21
new Thread(world).start();
22
add(world);
23
public static void main(String[] args) 
26
LifeGame frame = new LifeGame(40, 50);
28
JMenuBar menu = new JMenuBar();
30
frame.setJMenuBar(menu);
31
JMenu options = new JMenu("Options");
33
menu.add(options);
34
JMenuItem arrow = options.add("Arrow");
36
arrow.addActionListener(frame.new ArrowActionListener());
37
JMenuItem square = options.add("Square");
39
square.addActionListener(frame.new SquareActionListener());
JMenu help = new JMenu("Help");
menu.add(help);
43
frame.setLocationRelativeTo(null);
45
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
46
frame.setSize();
47
frame.setTitle("Game of Life");
48
frame.setVisible(true);
49
frame.setResizable(false);
50
class ArrowActionListener implements ActionListener
53
public void actionPerformed(ActionEvent e) 
55
world.setArrow();
57
class SquareActionListener implements ActionListener
61
public void actionPerformed(ActionEvent e) 
63
world.setSquare();
65

67 }


1 package com.cisco.
3 import java.awt.G
5 import javax.swing.JP
7 public class World extends JPanel implements Runnable
private final int
 10
private final int
 11
private final CellStatus[][] generation1;
 12
private final CellStatus[][] generation2;
 13
private CellStatus[][] currentG
 14
private CellStatus[][] nextG
 15
private volatile boolean isChanging = false; 
 16
public World(int rows, int columns)
 18
this.rows =
 20
this.columns =
 21
generation1 = new CellStatus[rows][columns];
 22
for(int i = 0; i & i++)
 23
for(int j = 0; j & j++)
 25
generation1[i][j] = CellStatus.D
 27
generation2 = new CellStatus[rows][columns];
 31
for(int i = 0; i & i++)
 32
for(int j = 0; j & j++)
 34
generation2[i][j] = CellStatus.D
 36
currentGeneration = generation1;
 40
nextGeneration = generation2;
 41
@Override
 44
public void run()
 45
while(true)
 47
synchronized(this)
 49
while(isChanging)
 51
try 
 53
this.wait();
 55
} catch (InterruptedException e) 
 56
e.printStackTrace();
 58
repaint();
 62
sleep(1);
 63
for(int i = 0; i & i++)
 65
for(int j = 0; j & j++)
 67
evolve(i, j);
 69
CellStatus[][] temp = null;
 73
temp = currentG
 74
currentGeneration = nextG
 75
nextGeneration =
 76
for(int i = 0; i & i++)
 78
for(int j = 0; j & j++)
 80
nextGeneration[i][j] = CellStatus.D
 82
@Override
 89
public void paintComponent(Graphics g) 
 90
super.paintComponent(g);
 92
for (int i = 0; i & i++) 
 94
for (int j = 0; j & j++) 
 96
if(currentGeneration[i][j] == CellStatus.Active)
 98
g.fillRect(j * 20, i * 20, 20, 20);
100
else
102
g.drawRect(j * 20, i * 20, 20, 20);
public void setArrow()
110
setShape(arrow);
112
public void setSquare()
115
setShape(square);
117
private void setShape(int[][] shape)
120
isChanging = true;
122
int arrowsRows = shape.
124
int arrowsColumns = shape[0].
125
int minimumRows = (arrowsRows & rows) ? arrowsRows:
127
int minimumColumns = (arrowsColumns & columns) ? arrowsColumns :
128
synchronized(this)
130
for(int i = 0; i & i++)
132
for(int j = 0; j & j++)
134
currentGeneration[i][j] = CellStatus.D
136
for(int i = 0; i & minimumR i++)
140
for(int j = 0; j & minimumC j++)
142
if(shape[i][j] == 1)
144
currentGeneration[i][j] = CellStatus.A
146
isChanging = false;
151
this.notifyAll();
152
private void evolve(int x, int y)
156
int activeSurroundingCell = 0;
158
if(isValidCell(x - 1, y - 1) && (currentGeneration[x - 1][y - 1] == CellStatus.Active))
160
activeSurroundingCell++;
162
}
163 
164
if(isValidCell(x, y - 1) && (currentGeneration[x][y - 1] == CellStatus.Active))
165
activeSurroundingCell++;
167
if(isValidCell(x + 1, y - 1) && (currentGeneration[x + 1][y - 1] == CellStatus.Active))
170
activeSurroundingCell++;
172
}
173 
174
if(isValidCell(x + 1, y) && (currentGeneration[x + 1][y] == CellStatus.Active))
175
activeSurroundingCell++;
177
if(isValidCell(x + 1, y + 1) && (currentGeneration[x + 1][y + 1] == CellStatus.Active))
180
activeSurroundingCell++;
182
}
183 
184
if(isValidCell(x, y + 1) && (currentGeneration[x][y + 1] == CellStatus.Active))
185
activeSurroundingCell++;
187
if(isValidCell(x - 1, y + 1) && (currentGeneration[x - 1][y + 1] == CellStatus.Active))
190
activeSurroundingCell++;
192
}
193 
194
if(isValidCell(x - 1, y) && (currentGeneration[x - 1][y] == CellStatus.Active))
195
activeSurroundingCell++;
197
if(activeSurroundingCell == 3)
200
nextGeneration[x][y] = CellStatus.A
202
else if(activeSurroundingCell == 2)
204
nextGeneration[x][y] = currentGeneration[x][y];
206
else
208
nextGeneration[x][y] = CellStatus.D
210
private boolean isValidCell(int x, int y)
214
if((x &= 0) && (x & rows) && (y &= 0) && (y & columns))
216
return true;
218
else
220
return false;
222
private void sleep(int x)
226
try 
228
Thread.sleep(1000 * x);
230
} catch (InterruptedException e) 
231
e.printStackTrace();
233
static enum CellStatus
237
Active,
239
private static final int[][] arrow = {
243
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
244
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
245
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
246
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
247
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
248
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
249
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
250
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
251
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
252
{0,0,0,0,0,0,0,0,0,0,0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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
327 }

View Code
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动生命游戏 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 22时,
生命游戏包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的细胞的数量。如果一个细胞周围的活细胞数量多于 3 个,这个细胞会因为资源匮乏而在下一个时刻死去;如果一个位置周围有 3 个活细胞,则该位置在下一个时刻将诞生一个新的细胞;如果一个位置周围有 2 个活细胞,则该位置的细胞生死状态保持不变;如果一个细胞周围的活细胞少于 2 个,那么这个细胞会因太孤单而死去。这样整个生命世界才不至于太过荒凉或拥挤,而是一种动态的平衡。
代码片段(8)
1.&[代码][C/C++]代码&&&&
///////////////////////////////////////////////////
// 程序名称:生命游戏
// 编译环境:Visual C++ 6.0,EasyX 2011惊蛰版
// 作  者:yangw80 &&
// 最后修改:
#include &graphics.h&
#include &conio.h&
#include &time.h&
// 定义全局变量
__int8 world[102][102] = {0}; // 定义二维世界
IMAGE imgLive, imgE
// 定义活细胞和无细胞区域的图案
// 函数声明
void Init();
void SquareWorld();
// 创建一个细胞以方形分布的世界
void RandWorld();
// 创建一个细胞随机分布的世界
void PaintWorld();
// 绘制世界
void Evolution();
int main()
int Speed = 500;
// 游戏速度(毫秒)
while(true)
if (kbhit() || Speed == 900)
char c = getch();
if (c == ' ' && Speed != 900)
c = getch();
if (c &= '0' && c &= '9')
Speed = ('9' - c) * 100;
SquareWorld(); // 产生默认的细胞以方形分布的世界
RandWorld(); // 产生默认的细胞以方形分布的世界
case VK_ESCAPE:
Evolution();
PaintWorld();
// 绘制世界
if (Speed != 900)
// 速度为 900 时,为按任意键单步执行
Sleep(Speed);
closegraph();
///////////////////////////////////////////////////
// 函数定义
void Init()
// 创建绘图窗口
initgraph(640,480);
// 设置随机种子
srand((unsigned)time(NULL));
// 调整世界图案的大小
Resize(&imgLive,
Resize(&imgEmpty, 4, 4);
// 绘制有生命世界的图案
SetWorkingImage(&imgLive);
setcolor(GREEN);
setfillstyle(GREEN);
fillellipse(0, 0, 3, 3);
// 绘制无生命世界的图案
SetWorkingImage(&imgEmpty);
setcolor(DARKGRAY);
rectangle(1, 1, 2, 2);
// 恢复对默认窗口的绘图
SetWorkingImage(NULL);
// 输出简单说明
setfont(24, 0, "黑体");
outtextxy(254, 18, "生 命 游 戏");
RECT r = {440, 60, 620, 460};
setfont(12, 0, "宋体");
drawtext("生命游戏简介:\n  生命游戏包括一个二维矩形世界,这个世界中的每个方格居住\
着一个活着的或死了的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的细胞\
的数量。如果一个细胞周围的活细胞数量多于 3 个,这个细胞会因为资源匮乏而在下一个时\
刻死去;如果一个位置周围有 3 个活细胞,则该位置在下一个时刻将诞生一个新的细胞;如\
果一个位置周围有 2 个活细胞,则该位置的细胞生死状态保持不变;如果一个细胞周围的活\
细胞少于 2 个,那么这个细胞会因太孤单而死去。这样整个生命世界才不至于太过荒凉或拥\
挤,而是一种动态的平衡。\n\n游戏控制:\n 0-9: 调节速度(慢--快)\n ESC: 退出\n空格: \
暂停|继续\n
S: 创建细胞以方形分布的世界\n
R: 创建细胞随机分布的世界",
&r, DT_WORDBREAK);
// 产生默认的细胞以方形分布的世界
SquareWorld();
// 创建一个细胞以方形分布的世界
void SquareWorld()
memset(world, 0, 102 * 102 * sizeof(__int8));
for(int x = 1; x &= 100; x++)
world[x][1] = world[x][100] = 1;
for(int y = 1; y &= 100; y++)
world[1][y] = world[100][y] = 1;
// 创建一个细胞随机分布的世界
void RandWorld()
for(int x = 1; x &= 100; x++)
for(int y = 1; y &= 100; y++)
world[x][y] = rand() % 2;
// 绘制世界
void PaintWorld()
for(int x = 1; x &= 100; x++)
for(int y = 1; y &= 100; y++)
putimage(16 + x * 4, 56 + y * 4, world[x][y] ? &imgLive : &imgEmpty);
void Evolution()
__int8 tmp[102][102] = {0};
// 临时数组
for(int x = 1; x &= 100; x++)
for(int y = 1; y &= 100; y++)
// 计算周围活着的生命数量
sum = world[x+1][y] + world[x+1][y-1] + world[x][y-1] + world[x-1][y-1]
+ world[x-1][y] + world[x-1][y+1] + world[x][y+1] + world[x+1][y+1];
// 计算当前位置的生命状态
switch(sum)
tmp[x][y] = 1;
tmp[x][y] = world[x][y];
default: tmp[x][y] = 0;
// 将临时数组恢复为世界
memcpy(world, tmp, 102 * 102 * sizeof(__int8));
2.&[代码]汉诺塔移动动画&&&&
///////////////////////////////////////////////////
// 程序名称:汉诺塔移动动画
// 编译环境:Visual C++ 6.0,EasyX 2011惊蛰版
// 作  者:Ronald
// 最后修改:
#include &graphics.h&
#include &conio.h&
#include &stdio.h&
#define MAX 64
// 圆盘的最大数目
#define NULL 0
struct STKNODE
struct STK
STKNODE* stack[MAX];
// 定义全局变量
// 声明三个栈,分别代表一号二号三号钢针上圆盘的状态
int v = 5;
// 调整速度
// 函数声明
void Initstk(STK* s);
// 初始化栈
void Hannoi(int n, char a, char b, char c);
// 汉诺塔递归
void start();
// 开始画面
void Move(int n, char a, char b);
// 移动过程
int switchab(char a);
// 返回钢针号
void adjust();
// 调整速度暂停
void main()
int n, ta[4] = {115, 500, 285, 520};
// 第一个圆盘的位置
printf("尽量小于16\n");
// 因为大于十六时就会显示有误,但程序可以正常运行
printf("请输入汉诺塔的层数(1~64):");
scanf("%d", &n);
p = (STKNODE**)malloc(n * sizeof(STKNODE **)); // 声明一个元素为 n 个的动态 STKNODE 型指针数组
for (int i2 = 0; i2 & i2 ++)
p[i2] = (STKNODE *)malloc(sizeof(STKNODE)); // 为每一个指针申请空间
Initstk(&s[0]);
Initstk(&s[1]);
Initstk(&s[2]);
// 将三个栈初始化
// 呈现开始画面
setfillstyle(YELLOW);
// 圆盘的颜色
for (int i=0; i & i++)
ta[0] += 5;
ta[1] -= 20;
ta[2] -= 5;
ta[3] -= 20;
bar(ta[0], ta[1], ta[2], ta[3]);
// 画出n个从大到小一次叠放的黄色圆盘
for (int i1 = 0; i1 & 4; i1++)
p[i]-&a[i1] = ta[i1];
s[0].stack[s[0].top] = p[i];
// 记录每个矩形的位置,top为圆盘的个数
Hannoi(n, 'a', 'b', 'c');
// 汉诺塔递归函数
system("pause");
printf("\t\t\t\tGame Over!\n");
///////////////////////////////////////////////////
// 函数定义
// 汉诺塔的递归
void Hannoi(int n, char a, char b, char c)
if(n == 1)
Move(1, a, c);
Hannoi(n-1, a, c, b);
Move(n, a, c);
Hannoi(n-1, b, a, c);
// 栈的初始化
void Initstk(STK *s)
s-&top = 0;
for (i = 0; i &= MAX; i++)
s-&stack[++s-&top] = NULL;
s-&top = 0;
// 移动过程
void Move(int n, char a, char b)
int i3, i4 = 0, i5 = 0;
// 目的钢针与源钢针的位置差值
i4 = switchab(a);
// 源钢针钢针号
i5 = switchab(b);
// 目的钢针号
STKNODE *q1, *q0;
// 两个中间结点用于源栈和目的栈间的值得传递,q1为目的栈,q0为源栈
q1 = (STKNODE *)malloc(sizeof(STKNODE));
q0 = (STKNODE *)malloc(sizeof(STKNODE));
// 源栈与目的栈值的传递
q0 = s[i4].stack[s[i4].top];
q1-&a[0] = q0-&a[0] + i3 * 200;
q1-&a[1] = 500 - s[i5].top * 20;
q1-&a[2] = q0-&a[2] + i3 * 200;
q1-&a[3] = 500 - s[i5].top * 20 + 20;
s[i5].stack[s[i5].top] = q1;
// 向上运动
while (q0-&a[1] &= 100)
setfillstyle(YELLOW);
bar(q0-&a[0], q0-&a[1], q0-&a[2], q0-&a[3]);
// 调整函数
Sleep(10 * v);
// 暂停(ms)
setfillstyle(WHITE);
bar(q0-&a[0], q0-&a[1], q0-&a[2], q0-&a[3]);
setcolor(RED);
line((q0-&a[0] + q0-&a[2]) / 2, q0-&a[1], (q0-&a[0] + q0-&a[2]) / 2, q0-&a[3]); // 重新画上被擦掉原有的红线
q0-&a[1] -= 10;
q0-&a[3] -= 10;
if (s[i4].top == 0)
// 重新画上最后一个矩形块擦掉的底座上的两条红线
line(110 + i4 * 200, 500, 290 + i4 * 200, 500);
line(110 + i4 * 200, 493, 290 + i4 * 200, 493);
// 向左或右运动,与 i3 的正负有关
while (q0-&a[2] != q1-&a[2])
setfillstyle(YELLOW);
bar(q0-&a[0], q0-&a[1], q0-&a[2], q0-&a[3]);
Sleep(10 * v);
setfillstyle(WHITE);
bar(q0-&a[0], q0-&a[1], q0-&a[2], q0-&a[3]);
if (i3 & 0)
// i3&0向左移
q0-&a[0] -= 20;
q0-&a[2] -= 20;
// i3&0向右移
q0-&a[0] += 20;
q0-&a[2] += 20;
// 向下运动
while (q0-&a[3] &= q1-&a[3])
setfillstyle(YELLOW);
bar(q0-&a[0], q0-&a[1], q0-&a[2], q0-&a[3]);
Sleep(10 * v);
setfillstyle(WHITE);
bar(q0-&a[0], q0-&a[1], q0-&a[2], q0-&a[3]);
setcolor(RED);
if (q0-&a[1] & 100)
// 重画被擦掉的红线
setcolor(RED);
line((q0-&a[0] + q0-&a[2]) / 2, q0-&a[1], (q0-&a[0] + q0-&a[2]) / 2, q0-&a[3]);
q0-&a[1] += 10;
q0-&a[3] += 10;
// 在目的钢针上的相应位置绘制出黄色矩形块
setfillstyle(YELLOW);
bar(q1-&a[0], q1-&a[1], q1-&a[2], q1-&a[3]);
// 绘制开始界面
void start()
// 初始化画面大小
initgraph(800, 650);
// 背景设为白色
setbkcolor(WHITE);
// 用白色填充整个画面
cleardevice();
// 绘制彩虹,形成一道彩虹,摘自 easyx 帮助文档示例程序
float H, S, L;
setlinestyle(PS_SOLID, NULL, 2);
// 设置线宽为 2
for(int r = 600; r & 544; r--)
setcolor( HSLtoRGB(H, S, L) );
circle(750, 900, r);
setfont(50, 0, "华文楷体");
setcolor(RED);
outtextxy(200, 150, "汉诺塔移动动画");
setfont(20, 0, "黑体");
outtextxy(600, 200, "BY:Ronald");
outtextxy(500, 200, "版本V1.1");
setfont(50, 0, "黑体");
setcolor(GREEN);
outtextxy(200, 350, "随便按一个键开始吧!");
// 绘制运动的矩形
// 检测是否有键盘敲击
if (kbhit())
int a = 40, b = 40;
//初始位置
while (a &= 760)
setfillstyle(YELLOW);
bar(a, b, a + 10, b + 20);
Sleep(10);
setfillstyle(WHITE + a);
bar(a, b, a + 10, b + 20);
if (kbhit())
while (b&=610)
setfillstyle(RED);
bar(a, b, a + 10, b + 20);
Sleep(10);
setfillstyle(WHITE - b);
bar(a, b, a + 10, b + 20);
if (kbhit())
while (a &= 40)
setfillstyle(GREEN);
bar(a, b, a + 10, b + 20);
Sleep(10);
setfillstyle(WHITE - a);
bar(a, b, a + 10, b + 20);
if (kbhit())
while (b &= 40)
setfillstyle(BLUE);
bar(a, b, a + 10, b + 20);
Sleep(10);
setfillstyle(WHITE + b);
bar(a, b, a + 10, b + 20);
// 清空开始界面
cleardevice();
// 绘制运动画面的的环境
setcolor(RED);
// 三根红色线段作为钢针
line(400, 110, 400, 500);
line(600, 110, 600, 500);
line(200, 110, 200, 500);
// 长方体形的底座
setfillstyle(LIGHTGRAY);
bar3d(80, 500, 720, 510, 10, true);
// 暂停按钮
bar(360, 540, 440, 580);
setfont(30, 0, "黑体");
setcolor(GREEN);
outtextxy(370, 550, "暂停");
setfont(20, 0, "宋体");
setcolor(RED);
outtextxy(300, 580, "鼠标暂停后请按空格继续");
// 加速按钮
bar(160, 540, 240, 580);
setfont(30, 0, "黑体");
setcolor(GREEN);
outtextxy(170, 550, "加速");
setfont(20, 0, "宋体");
setcolor(RED);
outtextxy(170, 580, "请按 d");
// 减速按钮
bar(560, 540, 640, 580);
setfont(30, 0, "黑体");
setcolor(GREEN);
outtextxy(570, 550, "减速");
setfont(20, 0, "宋体");
setcolor(RED);
outtextxy(570, 580, "请按 a");
setfont(50, 0, "宋体");
setcolor(GREEN);
outtextxy(10, 10, "正在进行中请欣赏:");
// 判断目的钢针与源钢针的钢针号返回钢针号
int switchab(char a)
switch (a)
// 调整函数,实现加速,减速,暂停
void adjust()
// 接收键盘敲进去的按钮和鼠标点击时赋予的变化值
// 用 f 接受键盘的键入值
if(kbhit())
f = getch();
// 检测鼠标消息
if (MouseHit()==true)
// 接收鼠标消息
MOUSEMSG M
Mouse = GetMouseMsg();
// 响应鼠标消息
if (Mouse.x &= 360 && Mouse.x &= 440 && Mouse.y &= 540 && Mouse.y &= 580 && Mouse.mkLButton)
if (Mouse.x &= 160 && Mouse.x &= 240 && Mouse.y &= 540 && Mouse.y &= 580 && Mouse.mkLButton)
if (Mouse.x &= 560 && Mouse.x &= 640 && Mouse.y &= 540 && Mouse.y &= 580 && Mouse.mkLButton)
// 作用于动画
// 用‘继续’覆盖‘暂停’
setfont(30, 0, "黑体");
setcolor(GREEN);
outtextxy(370, 550, "继续");
// 继续后变回显示‘暂停’
setfont(30, 0, "黑体");
setcolor(GREEN);
outtextxy(370, 550, "暂停");
// 当被点击时,‘减速’位置震动一下
setfillstyle(LIGHTGRAY);
bar(560, 540, 640, 580);
setfont(30, 0, "黑体");
setcolor(GREEN);
outtextxy(575, 545, "减速");
Sleep(30);
setfillstyle(LIGHTGRAY);
bar(560, 540, 640, 580);
setfont(30, 0, "黑体");
setcolor(GREEN);
outtextxy(570, 550, "减速");
setfillstyle(LIGHTGRAY);
bar(160, 540, 240, 580);
setfont(30, 0, "黑体");
setcolor(GREEN);
outtextxy(165, 545, "加速");
Sleep(30);
setfillstyle(LIGHTGRAY);
bar(160, 540, 240, 580);
setfont(30, 0, "黑体");
setcolor(GREEN);
outtextxy(170, 550, "加速");
// v 最小为1
if (v &= 0)
// f 初始化为 r
FlushMouseMsgBuffer();
// 清空鼠标消息
3.&[代码]俄罗斯方块&&&&
////////////////////////////////////////////
// 程序名称:俄罗斯方块
// 编译环境:Visual C++ 6.0 / 2010,EasyX 2011惊蛰版
// 程序编写:krissi &&
// 更新记录:
//      
修改了下落超时的逻辑
#include &graphics.h&
#include &conio.h&
#include &time.h&
/////////////////////////////////////////////
// 定义常量、枚举量、结构体、全局变量
/////////////////////////////////////////////
#define WIDTH 10
// 游戏区宽度
#define HEIGHT 22
// 游戏区高度
#define SIZE 20
// 每个游戏区单位的实际像素
// 定义操作类型
CMD_ROTATE,
// 方块旋转
CMD_LEFT, CMD_RIGHT, CMD_DOWN, // 方块左、右、下移动
// 方块沉底
// 退出游戏
// 定义绘制方块的方法
SHOW, // 显示方块
HIDE, // 隐藏方块
// 固定方块
// 定义七种俄罗斯方块
struct BLOCK
WORD dir[4]; // 方块的四个旋转状态
COLORREF // 方块的颜色
} g_Blocks[7] = { {0x0F00, 0xF00, 0x4444, RED},
{0x0, 0x0, BLUE},
{0xE0, 0x0, MAGENTA}, // L
{0xE20, 0x0, YELLOW}, // 反L
{0x0C60, 0xC60, 0x2640, CYAN},
{0x0, 0x0, GREEN}, // 反Z
{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}}; // T
// 定义当前方块、下一个方块的信息
struct BLOCKINFO
// 方块 ID
char x, // 方块在游戏区中的坐标
byte dir:2; // 方向
} g_CurBlock, g_NextB
// 定义游戏区
BYTE g_World[WIDTH][HEIGHT] = {0};
/////////////////////////////////////////////
// 函数声明
/////////////////////////////////////////////
void Init();
// 初始化游戏
void Quit();
// 退出游戏
void NewGame();
// 开始新游戏
void GameOver();
// 结束游戏
// 获取控制命令
void DispatchCmd(CMD _cmd);
// 分发控制命令
void NewBlock();
// 生成新的方块
bool CheckBlock(BLOCKINFO _block);
// 检测指定方块是否可以放下
void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW); // 画方块
void OnRotate();
// 旋转方块
void OnLeft();
// 左移方块
void OnRight();
// 右移方块
void OnDown();
// 下移方块
void OnSink();
// 沉底方块
/////////////////////////////////////////////
// 函数定义
/////////////////////////////////////////////
void main()
while(true)
c = GetCmd();
DispatchCmd(c);
// 按退出时,显示对话框咨询用户是否退出
if (c == CMD_QUIT)
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("您要退出游戏吗?"), _T("提醒"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
// 初始化游戏
void Init()
initgraph(640, 480);
srand((unsigned)time(NULL));
// 显示操作说明
setfont(14, 0, _T("宋体"));
outtextxy(20, 330, _T("操作说明"));
outtextxy(20, 350, _T("上:旋转"));
outtextxy(20, 370, _T("左:左移"));
outtextxy(20, 390, _T("右:右移"));
outtextxy(20, 410, _T("下:下移"));
outtextxy(20, 430, _T("空格:沉底"));
outtextxy(20, 450, _T("ESC:退出"));
// 设置坐标原点
setorigin(220, 20);
// 绘制游戏区边界
rectangle(-1, -1, WIDTH * SIZE, HEIGHT * SIZE);
rectangle((WIDTH + 1) * SIZE - 1, -1, (WIDTH + 5) * SIZE, 4 * SIZE);
// 开始新游戏
NewGame();
// 退出游戏
void Quit()
closegraph();
// 开始新游戏
void NewGame()
// 清空游戏区
setfillstyle(BLACK);
bar(0, 0, WIDTH * SIZE - 1, HEIGHT * SIZE - 1);
ZeroMemory(g_World, WIDTH * HEIGHT);
// 生成下一个方块
g_NextBlock.id = rand() % 7;
g_NextBlock.dir = rand() % 4;
g_NextBlock.x = WIDTH + 1;
g_NextBlock.y = HEIGHT - 1;
// 获取新方块
NewBlock();
// 结束游戏
void GameOver()
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("游戏结束。\n您想重新来一局吗?"), _T("游戏结束"), MB_YESNO | MB_ICONQUESTION) == IDYES)
NewGame();
// 获取控制命令
CMD GetCmd()
// 获取控制值
while(true)
// 如果超时,自动下落一格
DWORD newtime = GetTickCount();
if (newtime - m_oldtime &= 500)
m_oldtime =
return CMD_DOWN;
// 如果有按键,返回按键对应的功能
if (kbhit())
switch(getch())
case 'W': return CMD_ROTATE;
case 'A': return CMD_LEFT;
case 'D': return CMD_RIGHT;
case 'S': return CMD_DOWN;
case 27: return CMD_QUIT;
case ' ': return CMD_SINK;
case 0xE0:
switch(getch())
case 72: return CMD_ROTATE;
case 75: return CMD_LEFT;
case 77: return CMD_RIGHT;
case 80: return CMD_DOWN;
// 延时 (降低 CPU 占用率)
Sleep(20);
// 分发控制命令
void DispatchCmd(CMD _cmd)
switch(_cmd)
case CMD_ROTATE: OnRotate();
case CMD_LEFT:
case CMD_RIGHT:
OnRight();
case CMD_DOWN:
case CMD_SINK:
case CMD_QUIT:
// 生成新的方块
void NewBlock()
g_CurBlock.id = g_NextBlock.id,
g_NextBlock.id = rand() % 7;
g_CurBlock.dir = g_NextBlock.dir, g_NextBlock.dir = rand() % 4;
g_CurBlock.x = (WIDTH - 4) / 2;
g_CurBlock.y = HEIGHT + 2;
// 下移新方块直到有局部显示
WORD c = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
while((c & 0xF) == 0)
g_CurBlock.y--;
// 绘制新方块
DrawBlock(g_CurBlock);
// 绘制下一个方块
setfillstyle(BLACK);
bar((WIDTH + 1) * SIZE, 0, (WIDTH + 5) * SIZE - 1, 4 * SIZE - 1);
DrawBlock(g_NextBlock);
// 设置计时器,用于判断自动下落
m_oldtime = GetTickCount();
void DrawBlock(BLOCKINFO _block, DRAW _draw)
WORD b = g_Blocks[_block.id].dir[_block.dir];
int color = BLACK;
switch(_draw)
case SHOW: color = g_Blocks[_block.id].
case HIDE: color = BLACK;
case FIX: color = g_Blocks[_block.id].color / 3;
setfillstyle(color);
for(int i=0; i&16; i++)
if (b & 0x8000)
x = _block.x + i % 4;
y = _block.y - i / 4;
if (y & HEIGHT)
if (_draw != HIDE)
bar3d(x * SIZE + 2, (HEIGHT - y - 1) * SIZE + 2, (x + 1) * SIZE - 4, (HEIGHT - y) * SIZE - 4, 3, true);
bar(x * SIZE, (HEIGHT - y - 1) * SIZE, (x + 1) * SIZE - 1, (HEIGHT - y) * SIZE - 1);
// 检测指定方块是否可以放下
bool CheckBlock(BLOCKINFO _block)
WORD b = g_Blocks[_block.id].dir[_block.dir];
for(int i=0; i&16; i++)
if (b & 0x8000)
x = _block.x + i % 4;
y = _block.y - i / 4;
if ((x & 0) || (x &= WIDTH) || (y & 0))
if ((y & HEIGHT) && (g_World[x][y]))
// 旋转方块
void OnRotate()
// 获取可以旋转的 x 偏移量
BLOCKINFO tmp = g_CurB
tmp.dir++;
if (CheckBlock(tmp)) { dx = 0;
tmp.x = g_CurBlock.x - 1; if (CheckBlock(tmp)) { dx = -1;
tmp.x = g_CurBlock.x + 1; if (CheckBlock(tmp)) { dx = 1;
tmp.x = g_CurBlock.x - 2; if (CheckBlock(tmp)) { dx = -2;
tmp.x = g_CurBlock.x + 2; if (CheckBlock(tmp)) { dx = 2;
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.dir++;
g_CurBlock.x +=
DrawBlock(g_CurBlock);
// 左移方块
void OnLeft()
BLOCKINFO tmp = g_CurB
if (CheckBlock(tmp))
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x--;
DrawBlock(g_CurBlock);
// 右移方块
void OnRight()
BLOCKINFO tmp = g_CurB
if (CheckBlock(tmp))
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x++;
DrawBlock(g_CurBlock);
// 下移方块
void OnDown()
BLOCKINFO tmp = g_CurB
if (CheckBlock(tmp))
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.y--;
DrawBlock(g_CurBlock);
OnSink(); // 不可下移时,执行“沉底方块”操作
// 沉底方块
void OnSink()
// 连续下移方块
DrawBlock(g_CurBlock, HIDE);
BLOCKINFO tmp = g_CurB
while (CheckBlock(tmp))
g_CurBlock.y--;
DrawBlock(g_CurBlock, FIX);
// 固定方块在游戏区
WORD b = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
for(i = 0; i & 16; i++)
if (b & 0x8000)
if (g_CurBlock.y - i / 4 &= HEIGHT)
{ // 如果方块的固定位置超出高度,结束游戏
GameOver();
g_World[g_CurBlock.x + i % 4][g_CurBlock.y - i / 4] = 1;
// 检查是否需要消掉行,并标记
int row[4] = {0};
bool bRow =
for(y = g_CurBlock.y; y &= max(g_CurBlock.y - 3, 0); y--)
for(x = 0; x & WIDTH; x++)
if (g_World[x][y] == 1)
if (i == WIDTH)
row[g_CurBlock.y - y] = 1;
setfillstyle(WHITE, DIAGCROSS2_FILL);
bar(0, (HEIGHT - y - 1) * SIZE + SIZE / 2 - 2, WIDTH * SIZE - 1, (HEIGHT - y - 1) * SIZE + SIZE / 2 + 2);
// 延时 200 毫秒
Sleep(200);
// 擦掉刚才标记的行
for(i = 0; i & 4; i++)
if (row[i])
for(y = g_CurBlock.y - i + 1; y & HEIGHT; y++)
for(x = 0; x & WIDTH; x++)
g_World[x][y - 1] = g_World[x][y];
g_World[x][y] = 0;
getimage(&img, 0, 0, WIDTH * SIZE, (HEIGHT - (g_CurBlock.y - i + 1)) * SIZE);
putimage(0, SIZE, &img);
// 产生新方块
NewBlock();
4.&[代码]椭圆组合而成的抽象图案&&&&
// 程序名称:椭圆组合而成的抽象图案
// 编译环境:Visual C++ 6.0/2010,EasyX 2011惊蛰版
// 最初编写:,by (QuickBasic 版本)
// 最后修改:,by
// 高中时候看到一本书的封面有这个图案,就用当时学的 QuickBasic 写了这个程序。
// 现在移植到 VC6 上,做个纪念。
#include &graphics.h&
#include &conio.h&
#include &math.h&
#define PI 3.
// 四舍五入
int round(double x)
return (int)(x & 0 ? x - 0.5 : x + 0.5);
void main()
initgraph(640, 480);
// 创建绘图窗口
setorigin(320, 240);
// 设置原点为屏幕中央
setaspectratio(1, -1);
// 设置 y 轴向上为正
double r = 58;
double csin = sin(2 * PI / 200);
double ccos = cos(2 * PI / 200);
for(int j = 0; j & 100; j++, r -= 0.9)
double tsin = sin(2 * PI * j / 100);
double tcos = cos(2 * PI * j / 100);
double x = 0;
double y =
for(int i = 0; i & 200; i++)
double temp =
x = x * ccos + y *
y = y * ccos - temp *
int scrx = round(x * tcos + y * 4 * tsin);
int scry = round(y * 4 * tcos - x * tsin);
putpixel(scrx, scry, GREEN);
Sleep(20);
closegraph();
5.&[代码]自由运动的点(全屏模糊处理的范例)&&&&
///////////////////////////////////////////////////
// 程序名称:自由运动的点
// 编译环境:Visual C++ 6.0 / 2010,EasyX 2011惊蛰版
// 作  者:yangw80 &&
// 最后修改:
#include &graphics.h&
#include &conio.h&
#include &math.h&
#include &time.h&
#define AMOUNT 64
// 全屏模糊处理
// (为了简化范例,该函数略去了屏幕第一行和最后一行的处理)
void Blur(DWORD* pMem)
for(int i = 640; i & 640 * 479; i++)
pMem[i] = RGB(
(GetRValue(pMem[i]) + GetRValue(pMem[i - 640]) + GetRValue(pMem[i - 1]) + GetRValue(pMem[i + 1]) + GetRValue(pMem[i + 640])) / 5,
(GetGValue(pMem[i]) + GetGValue(pMem[i - 640]) + GetGValue(pMem[i - 1]) + GetGValue(pMem[i + 1]) + GetGValue(pMem[i + 640])) / 5,
(GetBValue(pMem[i]) + GetBValue(pMem[i - 640]) + GetBValue(pMem[i - 1]) + GetBValue(pMem[i + 1]) + GetBValue(pMem[i + 640])) / 5);
// 点的结构
struct SPOT
int targetx,
void main()
initgraph(640, 480);
// 创建绘图窗口
BeginBatchDraw();
// 设置批绘图模式
srand((unsigned)time(NULL)); // 设置随机种子
DWORD* pMem = GetImageBuffer(); // 获取显存地址
// 定义所有点
SPOT spots[AMOUNT];
// 初始化每个点
for(int i = 0; i & AMOUNT; i++)
spots[i].x = spots[i].targetx = rand() % 600 + 20;
spots[i].y = spots[i].targety = rand() % 440 + 20;
spots[i].dx = rand() % 40 - 20;
spots[i].dy = (int)sqrt(400 - spots[i].dx * spots[i].dx) * ((rand() % 2) * 2 - 1);
spots[i].color = HSLtoRGB((float)(rand() % 360), 1.0, 0.5);
while(!kbhit())
for(int i = 0; i & AMOUNT; i++)
setcolor(spots[i].color);
moveto(spots[i].x, spots[i].y);
spots[i].targetx += spots[i].
spots[i].targety += spots[i].
// 判断是否越界,以及越界处理
if (spots[i].targetx &= 0)
spots[i].dx = rand() % 20;
spots[i].dy = (int)sqrt(400 - spots[i].dx * spots[i].dx) * ((rand() % 2) * 2 - 1);
else if (spots[i].targetx &= 639)
spots[i].dx = - rand() % 20;
spots[i].dy = (int)sqrt(400 - spots[i].dx * spots[i].dx) * ((rand() % 2) * 2 - 1);
if (spots[i].targety &= 0)
spots[i].dx = rand() % 40 - 20;
spots[i].dy = (int)sqrt(400 - spots[i].dx * spots[i].dx);
else if (spots[i].targety &= 479)
spots[i].dx = rand() % 40 - 20;
spots[i].dy = - (int)sqrt(400 - spots[i].dx * spots[i].dx);
// 未越界时,有 10% 的概率改变运行方向
if (rand() % 10 & 1)
spots[i].dx = rand() % 40 - 20;
spots[i].dy = (int)sqrt(400 - spots[i].dx * spots[i].dx) * ((rand() % 2) * 2 - 1);
// 计算新点坐标,画线
spots[i].x += (int)((spots[i].targetx - spots[i].x) * 0.1);
spots[i].y += (int)((spots[i].targety - spots[i].y) * 0.1);
lineto(spots[i].x, spots[i].y);
// 全屏模糊处理
Blur(pMem);
FlushBatchDraw();
Sleep(15);
// 按任意键退出
closegraph();
6.&[代码]钟表模拟程序(表针形式)&&&&
////////////////////////////////////////////
// 程序名称:钟表模拟程序(表针形式)
// 编译环境:Visual C++ 6.0,EasyX 2011惊蛰版
// 程序编写:BestAns &&
// 最后更新:
#include &graphics.h&
#include &conio.h&
#include &math.h&
#define PI 3.
void DrawHand(int hour, int minute, int second)
double a_hour, a_min, a_
// 时、分、秒针的弧度值
int x_hour, y_hour, x_min, y_min, x_sec, y_ // 时、分、秒针的末端位置
// 计算时、分、秒针的弧度值
a_sec = second * 2 * PI / 60;
a_min = minute * 2 * PI / 60 + a_sec / 60;
a_hour= hour * 2 * PI / 12 + a_min / 12;
// 计算时、分、秒针的末端位置
x_sec = int(120 * sin(a_sec)); y_sec = int(120 * cos(a_sec));
x_min = int(100 * sin(a_min)); y_min = int(100 * cos(a_min));
x_hour= int(70 * sin(a_hour)); y_hour= int(70 * cos(a_hour));
setlinestyle(PS_SOLID, NULL, 10);
setcolor(WHITE);
line(320 + x_hour, 240 - y_hour, 320 - x_hour / 7, 240 + y_hour / 7);
setlinestyle(PS_SOLID, NULL, 6);
setcolor(LIGHTGRAY);
line(320 + x_min, 240 - y_min, 320 - x_min / 5, 240 + y_min / 5);
setlinestyle(PS_SOLID, NULL, 2);
setcolor(RED);
line(320 + x_sec, 240 - y_sec, 320 - x_sec / 3, 240 + y_sec / 3);
void DrawDial()
// 绘制一个简单的表盘
circle(320, 240, 2);
circle(320, 240, 60);
circle(320, 240, 160);
outtextxy(296, 310, "BestAns");
// 绘制刻度
for (int i=0; i&60; i++)
x = 320 + int(145 * sin(PI * 2 * i / 60));
y = 240 + int(145 * cos(PI * 2 * i / 60));
if (i % 15 == 0)
bar(x - 5, y - 5, x + 5, y + 5);
else if (i % 5 == 0)
circle(x, y, 3);
putpixel(x, y, WHITE);
void main()
initgraph(640, 480);
// 初始化 640 x 480 的绘图窗口
DrawDial();
// 绘制表盘
setwritemode(R2_XORPEN); // 设置 XOR 绘图模式
// 绘制表针
SYSTEMTIME
// 定义变量保存当前时间
while(!kbhit())
// 按任意键退出钟表程序
GetLocalTime(&ti);
// 获取当前时间
DrawHand(ti.wHour, ti.wMinute, ti.wSecond); // 画表针
Sleep(1000);
// 延时 1 秒
DrawHand(ti.wHour, ti.wMinute, ti.wSecond); // 擦表针(擦表针和画表针的过程是一样的)
closegraph();
// 关闭绘图窗口
7.&[代码]模仿 Windows 屏保“变幻线 (Mystify)” VC 源代码&&&&
// 程序名称:变幻线
// 编译环境:VC6 + EasyX v
// 编 写 者:
// 编写日期:
#include &graphics.h&
#include &conio.h&
#include &time.h&
#define WIDTH 640
#define HEIGHT 480
#define MAXSTEP 9
// 节点(多个连续的节点环,组成一个顶点)
struct POINTNODE
POINTNODE*
// 指向下一个节点的指针
// 顶点(四个顶点,组成一个多边形)
class VERTEX
POINTNODE* m_
// 头结点的指针
// 移动步长
// 构造函数
m_head = NULL;
// 析构函数
if (m_head != NULL)
delete[] m_
// 初始化环链
void Init(int count)
// 初始化前进方向
m_step.x = ((rand() % 2) * 2 - 1) * (rand() % MAXSTEP + 1);
m_step.y = ((rand() % 2) * 2 - 1) * (rand() % MAXSTEP + 1);
// 初始化节点环
m_head = new POINTNODE[count];
m_head[0].pos.x = rand() % WIDTH;
m_head[0].pos.y = rand() % HEIGHT;
m_head[0].next
= &m_head[count - 1];
for(int i=1; i&= i++)
m_head[i].pos.x = m_head[i-1].pos.x - m_step.x;
m_head[i].pos.y = m_head[i-1].pos.y - m_step.y;
m_head[i].next
= &m_head[i-1];
// 获取头部节点坐标
POINT GetHead()
return m_head-&
// 获取尾部节点坐标
POINT GetTail()
return m_head-&next-&
// 移动顶点
void Move()
m_head-&next-&pos.x = m_head-&pos.x + m_step.x;
m_head-&next-&pos.y = m_head-&pos.y + m_step.y;
m_head = m_head-&
// 判断顶点是否越界
(m_head-&pos.x & 0)
{ m_head-&pos.x = -m_head-&pos.x;
m_step.x =
rand() % MAXSTEP + 1; }
else if (m_head-&pos.x &= WIDTH) { m_head-&pos.x -= m_head-&pos.x - WIDTH + 1; m_step.x = -rand() % MAXSTEP - 1; }
(m_head-&pos.y & 0)
{ m_head-&pos.y = -m_head-&pos.y;
m_step.y =
rand() % MAXSTEP + 1; }
else if (m_head-&pos.y &= HEIGHT) { m_head-&pos.y -= m_head-&pos.y - HEIGHT + 1; m_step.y = -rand() % MAXSTEP - 1; }
class POLYGON
COLORREF m_
// 多边形的颜色
m_vertex[4]; // 构成多边形的四个顶点
// 构造函数
POLYGON(int lines)
// 初始化颜色
m_color = HSLtoRGB(float(rand()%360), 1.0, 0.5);
// 初始化四个顶点
for (int i=0; i&4; i++)
m_vertex[i].Init(lines);
// 移动多边形
void Move()
// 擦掉多边形的尾部
setcolor(BLACK);
moveto(m_vertex[3].GetTail().x, m_vertex[3].GetTail().y);
for(i=0; i&4; i++)
lineto(m_vertex[i].GetTail().x, m_vertex[i].GetTail().y);
// 移动每个顶点
for (i=0; i&4; i++)
m_vertex[i].Move();
// 画多边形的头部
setcolor(m_color);
moveto(m_vertex[3].GetHead().x, m_vertex[3].GetHead().y);
for(i=0; i&4; i++)
lineto(m_vertex[i].GetHead().x, m_vertex[i].GetHead().y);
// 有 1% 的概率更换颜色
if (rand()%100 == 0)
m_color = HSLtoRGB(float(rand()%360), 1.0, 0.5);
void main()
// 初始化绘图窗口
initgraph(WIDTH, HEIGHT);
// 初始化随机种子
srand((unsigned)time(NULL));
// 定义两个多边形对象,分别有 7 层线和 12 层线
POLYGON s1(7), s2(12);
// 移动多边形,按任意键退出
while(!kbhit())
s1.Move();
s2.Move();
Sleep(20);
// 关闭绘图窗口
closegraph();
8.&[代码]打字母游戏&&&&
////////////////////////////////////////////
// 程序名称:打字母游戏
// 编译环境:Visual C++ 6.0,EasyX 2011惊蛰版
// 程序编写:BestAns &&
// 最后更新:
#include &graphics.h&
#include &conio.h&
#include &time.h&
// 欢迎界面
void welcome()
// 输出屏幕提示
cleardevice();
setcolor(YELLOW);
setfont(64, 0, "黑体");
outtextxy(160, 50, "打字母游戏");
setcolor(WHITE);
setfont(16, 0, "宋体");
outtextxy(100, 200, "就是很传统的那个掉字母然后按相应键就消失的游戏");
outtextxy(100, 240, "只是做了一个简单的实现");
outtextxy(100, 280, "功能并不很完善,比如生命数、分数等都没有写");
outtextxy(100, 320, "感兴趣的自己加进去吧");
// 实现闪烁的“按任意键继续”
int c=255;
while(!kbhit())
setcolor(RGB(c, 0, 0));
outtextxy(280, 400, "按任意键继续");
if (c & 0) c = 255;
Sleep(20);
cleardevice();
// 退出界面
void goodbye()
cleardevice();
setcolor(YELLOW);
setfont(48, 0, "黑体");
outtextxy(104, 180, "多写程序
不老青春");
void main()
initgraph(640, 480);
// 初始化屏幕为 640x480
welcome();
// 显示欢迎界面
srand(time(NULL));
// 设置随机种子
setfont(16, 0, "Arial"); // 设置字母的字体和大小
setfillstyle(BLACK);
// 设置清除字母的填充区域颜色
// 目标字母
// 用户的按键
// 字母的位置
while(true)
target = 65 + rand() % 26;
// 产生任意大写字母
x = rand() % 620;
// 产生任意下落位置
for (y=0; y&460; y++)
setcolor(WHITE);
// 设置字母的颜色
outtextxy(x, y, target); // 显示字母
if(kbhit())
key = getch();
// 获取用户按键
if((key == target) || (key == target + 32))
// 按键正确,“击落”字母(画黑色方块擦除)
bar(x, y, x + 16, y + 16);
// 跳出循环,进行下一个字母
else if (key == 27)
goto EXIT;
// 如果按 ESC,退出游戏主循环
// 延时,并清除字母
Sleep(10);
bar(x, y, x + 16, y + 16);
// 退出部分
goodbye();
// 关闭图形界面
closegraph();
开源中国-程序员在线工具:
程序的注释写的很好。值得学习。赞一个。
2楼:mudan 发表于
3楼:张金富 发表于
巾帼不让须眉!
4楼:redraiment 发表于
后面的汉诺塔、俄罗斯方块……可以拆开来发好几个。
5楼:g_dragon 发表于
太复杂了,
6楼:沈健 发表于
能不能把工程放上来,可以下载。
7楼:姚裘飞 发表于
确实不错,,
8楼:管理猿 发表于
真是规范啊,佩服。
把工程放上来,大家分享下。
9楼:吴跃明 发表于
这个网站不错
10楼:LAN_study 发表于
11楼:fangjiebin 发表于
&很激动 &总有厉害的人 & 我今天看了百度之星 后来了解了 生命游戏 …… &程序真实太伟大 太神奇了
12楼:GodManager 发表于
雨中飞燕?记得她写过类似的库啊
13楼:li_cheng_go_1314 发表于
高手啊!!
14楼:mengdecike 发表于
#include&graphics.h&vc6.0中没有呢,有哪个头文件可以替代吗
15楼:t3409 发表于
很厉害的样子
16楼:开源中国部长- 发表于
KBEngine 是一款开源的游戏服务端引擎,使用简单的约定协议就能够使客户端与服务端进行交互, 使用KBEngine插件能够快速与(Unity3D, OGRE, Cocos2d, HTML5, 等等)技术结合形成一个完整的客户端。
服务端底层框架使用c++编写,游戏逻辑层使用Python(支持热更新),开发者无需重复的实现一些游戏服务端通用的底层技术, 将精力真正集中到游戏开发层面上来,快速的打造各种网络游戏。
(经常被问到承载上限,kbengine底层架构被设计为多进程分布式动态负载均衡方案, 理论上只需要不断扩展硬件就能够不断增加承载上限,单台机器的承载上限取决于游戏逻辑本身的复杂度。)
新增与改善:
新版本API文档
调试工具完善
新增一些API
支持灾难恢复
灾难恢复时可能出现ID资源不足
其他小修正
sources&&&&&:&/kbengine/kbengine/releases&
binarys&&&&&:&https://sourceforge.net/projects/kbengine/files/
Demo sources
unity3d&&&&&:&/kbengine/kbengine_unity3d_demo/releases
unity3d&&&&&:&/kbengine/kbengine_unity3d_warring/releases
ogre&&&&&&&&:&/kbengine/kbengine_ogre_demo/releases
html5&&&&&&&:&/kbengine/kbengine_html5_demo/releases
docs&&&&&&&&:&http://www.kbengine.org/docs/
API&&&&&&&&&:&/kbengine/kbengine/tree/master/docs
Email&&&&&&&:&kbengine_
Maillist&&&&:&/d/forum/kbengine_maillist
开源从代码分享开始
金大海的其它代码

我要回帖

更多关于 生命游戏java代码 的文章

 

随机推荐