[Win32::Console]Perl终端版生命游戏
2014-09-22 16:25
357 查看
环境,WinXP/Win7 Perl 5.16
默认循环1000次,按ESC提前退出
默认循环1000次,按ESC提前退出
use strict; use Term::ReadKey; use Win32::Console; use Time::HiRes 'sleep'; use IO::Handle; STDOUT->autoflush(1); system("mode con lines=40 cols=100"); our $OUT=Win32::Console->new(STD_OUTPUT_HANDLE); $OUT->Cursor(20, 20, 99, 0); #hide cursor my ($i, $j); our ($rows, $cols) = (40, 40); our ($mxrow, $mxcol) = ($rows-1, $cols-1); # init our @coord; my (@h, @n); my $y = 0; foreach (<DATA>) { s/\r?\n$//; tr/\*\./10/; @{$h[$y++]} = ( split("", $_) ); } foreach $i (0 .. $mxrow) { foreach $j (0 .. $mxcol) { $coord[$i][$j]{'x'} = $j*2; $coord[$i][$j]{'y'} = $i; $h[$i][$j] = 0 unless (defined $h[$i][$j]); $n[$i][$j] = 0; } } &Draw(\@n, \@h); foreach (0..1000) { sleep 0.05; @n = (); &NextBuffer(\@h, \@n); &Draw(\@h, \@n); @h = (@n); &KeyFunc(); } sub NextBuffer { my ($ra, $rb) = (shift, shift); my ($i, $j, $sum); my ($L, $R, $U, $D); foreach $i (0 .. $mxrow) { $U = ($i-1) < 0 ? $mxrow : ($i-1); $D = ($i+1) > $mxrow ? 0 : ($i+1); foreach $j (0 .. $mxcol) { $L = ($j-1) < 0 ? $mxcol : ($j-1); $R = ($j+1) > $mxcol ? 0 : ($j+1); $sum = $ra->[$U][$L] + $ra->[$U][$j] + $ra->[$U][$R] + $ra->[$i][$L] + 0 + $ra->[$i][$R] + $ra->[$D][$L] + $ra->[$D][$j] + $ra->[$D][$R]; if ($sum == 3) { $rb->[$i][$j] = 1; } elsif ($sum == 2) { $rb->[$i][$j] = $ra->[$i][$j]; } else { $rb->[$i][$j] = 0; } } } } sub Draw { my ($ra, $rb) = (shift, shift); foreach $i (0 .. $mxrow) { foreach $j (0 .. $mxcol) { if ($rb->[$i][$j] != $ra->[$i][$j]) { &Point( $coord[$i][$j]{'x'}, $coord[$i][$j]{'y'}, $rb->[$i][$j], ); } } } } sub Point { my ($mx, $my, $light) = (shift, shift, shift); my $color; if ($light == 1) { $color = $FG_WHITE|$BG_GRAY; } else { $color = $FG_WHITE|$BG_BLACK; } $OUT->Cursor($mx, $my); $OUT->FillAttr($color, 2, $mx, $my); } sub KeyFunc { my $key; $key = ReadKey(-1); return if (not defined $key); if ( ord($key) == 27 ) { exit; } } __DATA__ ......................**............... ......................**............... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... .........*..........**...**............ .......*.*............***.............. ......*.*............*...*............. **...*..*.............*.*.............. **....*.*..............*............... .......*.*......*.*.................... .........*......**..................... .................*...*................. .....................**......*......... ....................*.*......*.*....... ...............*..............*.*....** ..............*.*.............*..*...** .............*...*............*.*...... ..............***............*.*....... ............**...**..........*......... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... ....................................... ...............**...................... ...............**......................
相关文章推荐
- Console Win32 API Native Methods Helper
- [游戏模版8] Win32 透明贴图
- [游戏模版13] Win32 透明贴图 主角移动
- 用c语言演绎“生命游戏”
- 生命游戏Java实现
- 3D塔防游戏实现 3 怪物门以及生命塔(Feekood语言)
- 终端,Shell、tty、console有什么区别?
- 生命游戏 题解
- Python,OpenGL生命游戏
- API日志3 (Difs from Win32ap&Win32 Console)
- 生命游戏 c语言实现
- Win32应用程序使用Console窗口输出调试信息
- 编码操练招式——康威生命游戏
- 建立Win32 Console Project时会出“error LNK1123” 错误
- 终端 /dev/console /dev/tty tty
- 使用PySide实现生命游戏
- win32_吃砖块游戏
- 终端(terminal)、tty、shell、控制台(console)、bash之间的区别与联系
- 【转】linux下tty,控制台,虚拟终端,串口,console(控制台终端)详解----不错
- Win32游戏编程之---致我们曾经怀念的李逍遥