- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 Gosu 在 Ruby 中创建生命游戏。我的代码如下。现在,当我运行“ruby gosu.rb”时,它会打开具有适当尺寸的窗口,并按原样预填充 World。
但是一旦我取消注释“@game.tick!”在 gosu 文件更新操作中,运行“ruby gosu.rb”时出现黑屏,没有预先填充的世界,我无法关闭。这是为什么以及如何解决这个问题?
You can find github repo with rest of the code here.任何帮助都很棒。
这是我的 game_of_life.rb
class Game
attr_accessor :world
def initialize(world=World.new, seeds=[])
@world = world
seeds.each do |seed|
world.cell_board[seed[0]][seed[1]].alive = true
end
end
def tick!
next_round_live_cells = []
next_round_dead_cells = []
world.cells.each do |cell|
# Rule 1:
# Any live cell with fewer than two live neighbours dies
if cell.alive? && world.live_neighbours_around_cell(cell).count < 2
next_round_dead_cells << cell
end
# Rule 2:
# Any live cell with two or three live neighbours lives on to the next generation
if cell.alive? && world.live_neighbours_around_cell(cell).count == (2 || 3)
next_round_live_cells << cell
end
# Rule 3:
# Any live cell with more than three live neighbours dies
if cell.alive? && world.live_neighbours_around_cell(cell).count > 3
next_round_dead_cells << cell
end
# Rule 4:
# Any dead cell with exactly three live neighbours becomes a live cell
if cell.dead? && world.live_neighbours_around_cell(cell).count == 3
next_round_live_cells << cell
end
end
next_round_live_cells.each do |cell|
cell.revive!
end
next_round_dead_cells.each do |cell|
cell.die!
end
end
end
class World
attr_accessor :rows, :cols, :cell_board, :cells
# Scheme of default initialized world matrix
#------------------------
# 0 1 2
# 0 [ dead, dead, dead ]
# 1 [ dead, alive, dead ]
# 2 [ dead, dead, dead ]
#-----------------------
def initialize(rows=3, cols=3)
@rows = rows
@cols = cols
@cells = []
@cell_board = Array.new(rows) do |row|
Array.new(cols) do |col|
Cell.new(col, row) # note col is 1st, than is row
end
end
cell_board.each do |row|
row.each do |element|
if element.is_a?(Cell)
cells << element
end
end
end
end
def live_cells
cells.select { |cell| cell.alive }
end
def dead_cells
cells.select { |cell| cell.alive == false }
end
def live_neighbours_around_cell(cell)
live_neighbours = []
live_cells.each do |live_cell|
# Neighbour to the North
if live_cell.x == cell.x - 1 && live_cell.y == cell.y
live_neighbours << live_cell
end
# Neighbour to the North-East
if live_cell.x == cell.x - 1 && live_cell.y == cell.y + 1
live_neighbours << live_cell
end
# Neighbour to the East
if live_cell.x == cell.x && live_cell.y == cell.y + 1
live_neighbours << live_cell
end
# Neighbour to the South-East
if live_cell.x == cell.x + 1 && live_cell.y == cell.y + 1
live_neighbours << live_cell
end
# Neighbour to the South
if live_cell.x == cell.x + 1 && live_cell.y == cell.y
live_neighbours << live_cell
end
# Neighbour to the South-West
if live_cell.x == cell.x + 1 && live_cell.y == cell.y - 1
live_neighbours << live_cell
end
# Neighbour to the West
if live_cell.x == cell.x && live_cell.y == cell.y - 1
live_neighbours << live_cell
end
# Neighbour to the North-West
if live_cell.x == cell.x - 1 && live_cell.y == cell.y - 1
live_neighbours << live_cell
end
end
live_neighbours
end
def randomly_populate
cells.each do |cell|
cell.alive = [true, false].sample
end
end
end
class Cell
attr_accessor :x, :y, :alive #, :height, :width
def initialize(x=0, y=0)
@x = x
@y = y
@alive = false
# Gosu
# @height = height
# @width = width
end
def alive?
alive
end
def dead?
!alive
end
def die!
@alive = false
end
def revive!
@alive = true # same as > self.alive = true
end
end
这是我的gosu代码
require 'gosu'
require_relative 'gol.rb'
class GameOfLifeWindow < Gosu::Window
def initialize(height=800, width=600)
# Basics
@height = height
@width = width
super height, width, false, 500
self.caption = 'My Game of Life'
# Colors
@white = Gosu::Color.new(0xffededed)
@black = Gosu::Color.new(0xff121212)
# Game world
@rows = height/10
@cols = width/10
world = World.new(@cols, @rows)
@game = Game.new(world)
@row_height = height/@rows
@col_width = width/@cols
@game.world.randomly_populate
@generation = 0
end
def update
# unless @game.world.live_cells.count == 0
# @game.tick!
@generation += 1
# end
end
def draw
@game.world.cells.each do |cell|
if cell.alive?
draw_quad(cell.x * @col_width, cell.y * @row_height, @black,
cell.x * @col_width + @col_width, cell.y * @row_height, @black,
cell.x * @col_width + @col_width, cell.y * @row_height + @row_height, @black,
cell.x * @col_width, cell.y * @row_height + @row_height, @black)
end
end
end
def button_down(id)
case id
when Gosu::KbSpace
@game.world.randomly_populate
when Gosu::KbEscape
close
end
end
def draw_background
draw_quad(0, 0, @white,
width, 0, @white,
width, height, @white,
0, height, @white)
end
end
window = GameOfLifeWindow.new
window.show
最佳答案
好的,我现在知道代码是正确的,只是循环 tick!
方法需要很多时间。
我制作了视频教程,内容涉及使用 Rspec 和 Gosu 游戏库使用 TDD 在 Ruby 中制作生命游戏,您可以在此处查看 > http://svenduplic.com/2013/03/25/conways-game-of-life-in-ruby.html
它们包含从头到尾的游戏制作过程,我对每一行都进行了解释。最后我解释了为什么这里的代码需要很长时间才能完成并适本地优化它。
关于与 Gosu 的 Ruby 生命游戏 - 更新操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422642/
最近我将我的 Guidewire 工作室从版本 8 升级到版本 9。从那时起,数据库查询在暂存器中运行时会抛出错误。相同的代码在版本 8 中工作正常。请在下面找到代码和错误。 代码 var objec
来自I gather , Gosu 只是 JVM 的 C#(这是一件好事)。这是真的吗? Gosu 和 C# 之间有什么区别(除了类库和它在 JVM 上运行的事实)? 最佳答案 我们没有将 Gosu
我的代码没有给出所需的输出并显示上述错误。 require 'gosu' def media_path(file) File.join(File.dirname(File.dirname(
我正在使用 Gosu 制作一个简单的游戏,但是我不想在我的游戏中使用外部 Sprite ,我宁愿使用代码绘制图像,是否可以像使用外部图像一样使用代码绘制的图像。 注意:我只想画基本的形状,比如正方形。
我想知道Gosu类和增强类之间的区别。因为我们在增强功能方面可以做的一切,那么我们在Gosu类中可以做到的也需要Gosu Enhancement。 最佳答案 Gosu类就像Java类一样。 使您感到困
我在 Guidewire (GOSU) 工作,我注意到互联网上没有太多关于这种语言的信息。在这里试试我的运气。 我正在 guidewire studio 中创建一个 GX 模型,我正在尝试映射一个属性
我一直无法将 Gosu 库中的文本居中放置在屏幕的绝对中间。 require 'gosu' class GameWindow < Gosu::Window def initialize (widt
我对 Gosu 不是很了解,所以这段代码让我很困惑。我用谷歌搜索等,但无济于事。这段代码中的反斜杠有什么作用? GET_STUFF.execute(\ -> { var gig = wit
它具体化了泛型……它是如何实现的?与 C# 的泛型有什么显着区别? 最佳答案 与C#泛型的区别:针对泛型类的不同实例化,C#在运行时生成类。 Gosu 没有:一个泛型类对应一个在运行时使用的类。类型信
Gosu 无法在我的 Mountain Lion (10.8.3) 上安装。这是我得到的错误: Building native extensions. This could take a while
我正在使用 Gosu gem 进行一些图形编程。问题是,当我创建一个窗口时,我的鼠标指针被隐藏了。我可以猜到鼠标在某个时刻的位置,我可以凭直觉点击,但我的用户可能不会。 如何显示指针? 最佳答案 如果
我正在尝试制作一个非常简单的点击计数器,但我坚持让点击计数器在屏幕上和代码本身中递增。 require 'rubygems' require 'gosu' module ZOrder BACK
我正在尝试在 FreeBSD 上安装这个 Ruby gem,但失败并出现以下错误: # gem install gosu Building native extensions. This could
我正在尝试在我的 Ubuntu 计算机上安装 Gosu。为了安装 gem,我尝试了“gem install gosu”,但我收到一条我无法理解的错误消息。它说“无法构建 gem native 扩展”。
Docker 总是有一个 USER 命令以特定用户身份运行进程,但通常很多东西都必须以 ROOT 身份运行。 我见过很多使用 ENTRYPOINT 和 gosu 来降低进程运行的图像。 我仍然对
我试着 gem install gosu 但我收到很多关于缺少头文件的编译器消息,包括 sndfile.h。是否有编译 gosu gem 所需的 debian 软件包列表? 最佳答案 见 https:
我坐在这里,我认为这是一个非常简单的错误,我只是想不通.. 我试图学习如何使用 Ruby 使用 Gosu gem 制作游戏,但遇到了减速带。这是我的代码。 require "gosu" class H
我想避免创建 switch case,而是使用 enum,但在编写以下代码时,我收到编译错误,提示 unexpected token public: public enum Status { IN
我在调用玩家的 x 位置和 y 位置时遇到问题。当我用 Projectile 类调用 origin_object.x 和 origin_object.y 时,它给我这个错误: asteroids.rb
我正在使用 Gosu 在 Ruby 中创建生命游戏。我的代码如下。现在,当我运行“ruby gosu.rb”时,它会打开具有适当尺寸的窗口,并按原样预填充 World。 但是一旦我取消注释“@game
我是一名优秀的程序员,十分优秀!