自己拿ruby写的一个简单的生命游戏
2011-12-24 13:36
369 查看
朋友买书送了本Ruby完全自学手册,遂学之
第一次写ruby代码,感觉挺适合写算法的东西,使用了TK库,有兴趣的同学可以玩玩。另,Tk canvas界面
很卡(更新界面代码没有new新的控件),有人若是知道原因能否指明,先谢过
第一次写ruby代码,感觉挺适合写算法的东西,使用了TK库,有兴趣的同学可以玩玩。另,Tk canvas界面
很卡(更新界面代码没有new新的控件),有人若是知道原因能否指明,先谢过
require 'tk' $g_ground_width = 600 $g_ground_height = 600 root = TkRoot.new do title "LifeGame" geometry $g_ground_width.to_s + "x" + $g_ground_height.to_s end $canvas = TkCanvas.new(root) $canvas.grid :stick => 'news', :column => 0, :row => 0 TkGrid.columnconfigure( root, 0, :weight => 1) TkGrid.rowconfigure( root, 0, :weight => 1) class Cell @@size = 10 def initialize #puts "come in Cell's init" @alive = false; @readyAlive = false; @x = 0 @y = 0 @tkRect #puts "end of Cell's init" end def self.size @@size end def setX(x) @x = x end def setY(y) @y = y end def getX @x end def getY @y end def wakeUp @alive = true @tkRect[:fill] = "black" end def kill @alive = false @tkRect[:fill] = "white" end def readyAlive(alive) @readyAlive = alive @readyAlive end def getReadyAlive @readyAlive end def isAlive @alive end def initTkRect @tkRect = TkcRectangle.new($canvas, @x, @y, @x + @@size, @y + @@size) @tkRect[:fill] = "white" end end class LifeGame def initialize @col = $g_ground_width / Cell.size @row = $g_ground_height / Cell.size @cellMap = Array.new(@col){ Array.new(@row) } #puts "come in LifeGame's init" num = 0 for i in (0..@col-1) for j in (0..@row-1) @cellMap[i][j] = Cell.new @cellMap[i][j].setX(i * Cell.size) @cellMap[i][j].setY(j * Cell.size) @cellMap[i][j].initTkRect num += 1 end end puts "total new "+num.to_s+"Cell" =begin too mass @cellMap[20][20].wakeUp @cellMap[21][20].wakeUp @cellMap[23][20].wakeUp @cellMap[24][20].wakeUp @cellMap[20][22].wakeUp @cellMap[21][22].wakeUp @cellMap[23][22].wakeUp @cellMap[24][22].wakeUp @cellMap[22][18].wakeUp @cellMap[22][19].wakeUp @cellMap[22][20].wakeUp @cellMap[22][21].wakeUp @cellMap[22][22].wakeUp @cellMap[22][23].wakeUp @cellMap[22][24].wakeUp =end =begin @cellMap[29][29].wakeUp @cellMap[28][30].wakeUp @cellMap[29][30].wakeUp @cellMap[30][30].wakeUp =end for i in (0..300) @cellMap[rand(40)+10][rand(40)+10].wakeUp end puts "end of LifeGame's init" end def findNeighbor(i, j) aliveNum = 0 [-1, 0, 1].each do |dx| [-1, 0, 1].each do |dy| if (dx == 0 and dy == 0) next end if (i + dx < 0 or i + dx >= @col) next end if (j + dy < 0 or j + dy >= @row) next end if (@cellMap[i+dx][j+dy].isAlive) aliveNum += 1 end end end aliveNum end def step for i in (0..@col-1) for j in (0..@row-1) #TODO num = findNeighbor(i, j) #puts "end findNeighbor" #puts "before change" if num == 3 @cellMap[i][j].readyAlive(true) elsif num == 2 @cellMap[i][j].readyAlive(@cellMap[i][j].isAlive) else #if num >0 # puts i.to_s+", "+j.to_s+": "+num.to_s #end @cellMap[i][j].readyAlive(false) end #puts "end step" end end for i in (0..@col-1) for j in (0..@row-1) if @cellMap[i][j].getReadyAlive @cellMap[i][j].wakeUp else @cellMap[i][j].kill end @cellMap[i][j].readyAlive(false) end end end def renderAll for i in (0..@col-1) for j in (0..@row-1) if @cellMap[i][j].isAlive() #puts i.to_s+","+j.to_s+" wakeUp !" #puts @cellMap[i][j].getX.to_s #puts @cellMap[i][j].getY.to_s #puts Cell.size().to_s TkcRectangle.new($canvas, @cellMap[i][j].getX, @cellMap[i][j].getY, @cellMap[i][j].getX + Cell.size, @cellMap[i][j].getY + Cell.size).fill 'black' end end end end end $g_gameThread; refreshThread = Thread.new do puts "start thread" puts $g_gameThread $g_gameThread = LifeGame.new puts "new LifeGame" puts $g_gameThread loop do #sleep(0.1) $g_gameThread.step() puts "======================================================" #$g_gameThread.renderAll() #x = rand(590) #y = rand(590) #TkcRectangle.new(@canvas, x,y,x+20,y+20).fill 'black' end end Tk.mainloop
相关文章推荐
- 生命游戏——一个简单的元胞自动机
- 自己开发个游戏其实挺简单(这是一个神奇的游戏-看透美女其实很容易)
- c写的一个简单的打飞机游戏
- 自己写的一个简单得shell得菜单
- cocos2dx 制作一个简单的三消游戏
- 自己第一次写触发器(oracle)-一个简单的触发器
- 自己写的一个简单mt图片幻灯类
- Tizen系统开发尝试,写一个简单的游戏
- cocos2d-x 2.2.1制作一个简单的游戏
- 自己写一个简单的Spring IOC容器
- 自己写的一个VB密码本,功能简单实用,献丑了。
- 设计一个简单的猜数游戏
- 在自己的WSASOCKET服务端/客户端中做了一个简单的解包程序处理粘包的问题
- [ckeditor系列]ckeditor 自己写的一个简单的image上传js 运用iframe的ajax上传
- 一个简单的2d游戏引擎
- 自己写了一个简单的数据库访问类
- 自己实现一个简单的优先队列-二叉堆
- 自己简单的封装一个OKHttp工具类
- 一个简单的游戏服务器框架
- go实现一个简单的游戏服务器框架(lotou)多节点