Ruby Ruby 冲击波,谁还可以做个更牛的出来

ghjcumt2008 · 2013年04月26日 · 最后由 Chen_Xu 回复于 2013年05月10日 · 11487 次阅读
Wave = []
loop do
  20.times do
      Wave.unshift(1)
      p Wave
      sleep(0.01)
  end
  20.times do
      Wave.pop
      p Wave
      sleep(0.01)
  end
end
1 楼 已删除

很形象,记住什么是 unshift 了

Wave 是常量,但是是数组又可以扩张,不矛盾么?

#6 楼 @chenge 铁打的营盘流水的兵...

#6 楼 @chenge 不是常量

array.unshift(obj, ...)  array

Prepends objects to the front of array. other elements up one. 

   a = [ "b", "c", "d" ]
   a.unshift("a")   #=> ["a", "b", "c", "d"]
   a.unshift(1, 2)  #=> [ 1, 2, "a", "b", "c", "d"]

#9 楼 @ghjcumt2008 他的意思是你大写了

#9 楼 @ghjcumt2008 大写的为常量,Wave 是大写。

#11 楼 @chenge ruby 的常量是可以改变的

#12 楼 @ghjcumt2008 这个 Wave 变量没被重新 assign,只不过是对象本身在变化内部状态,#7 楼 @luikore 已经解释过了

#12 楼 @ghjcumt2008 可以变还叫常量?伪常量

#14 楼 @chenge

4.3 Constant References A constant in Ruby is like a variable, except that its value is supposed to remain constant for the duration of a program. The Ruby interpreter does not actually enforce the con- stancy of constants, but it does issue a warning if a program changes the value of a constant. Lexically, the names of constants look like the names of local variables, except that they begin with a capital letter. By convention, most constants are written in all uppercase with underscores to separate words, LIKE_THIS. Ruby class and module names are also constants, but they are conventionally written using initial capital letters and camel case, LikeThis.

只能这么猜想,Matz 不喜欢常量,喜欢动态的。Ruby 根本没有常量。

#17 楼 @chenge 你想多了。呵呵

#4 楼 @luikore 借花献佛 🌻

x = 0
loop do
  space = ' ' * (30-((1+Math.sin(x+=0.15))*15))
  dot = '-' * ((1+Math.sin(x)) * 15) *2
  puts space + dot
  sleep 0.01
end

... 调幅信号:

x = 0
y = 0
loop do
  puts '-' * ((1 + Math.sin(x += 0.018)) * ((1 + Math.sin(y += 0.15))) * 20)
  sleep 0.01
end

冲击波嘛,必须有声音才行呀

x = 0
loop do
  puts "\a-" * ((1 + Math.sin(x += 0.1)) * 20)
  sleep 0.01
end

#20 楼 @zputee 其实这个居中可以用 center 改进一下:

x = 0
loop do
  dot = '-' * ((1+Math.sin(x += 0.15)) * 15) *2
  puts dot.center 60
  sleep 0.01
end

#24 楼 @luikore 👍

x = 0
y = 0
loop do
  dot = '-' * ((1 + Math.sin(x += 0.018)) * ((1 + Math.sin(y += 0.15))) * 20)
  puts dot.center 80
  sleep 0.01
end

@luikore @zputee 你们写一个动画片得了 👍

#7 楼 @luikore 好厉害的解释

#14 楼 @chenge “铁打的营盘流水的兵..”可能指 wave.unshift(1) 不改变 wave.object_id,

wave = []
wave.object_id    
wave.unshift(1)
wave.unshift(1)
wave.object_id      #object_id 没变

wave=[1]
wave.object_id      #object_id 变了 

#30 楼 @luikore my jaw is on the floor now

我是路过的,参拜下@luikore ,顺便歪个楼,挖个老坟 http://www.guokr.com/article/7268/

#35 楼 @richard020389 我还有 bad apple 的屏保... 当年把 win7 扫雷版的原理搞出来了 (还是 1.9 的代码,现在都用 fiddle 不用 dl 了...)

# coding: utf-8

require "dl"
require "dl/import"
include DL

module MineSweeper
  extend Importer
  dlload 'user32.dll'

  # ------------------------------------------------------------------------------
  # memory for title match

  MSstr = '扫雷'.encode(Encoding.default_external)
  Mem = CPtr[MSstr + "\0"]
  def Mem.zero
    Mem.size.times do |i|
      Mem[i] = 0
    end
  end

  # ------------------------------------------------------------------------------
  # find minesweeper window handle and assign to $ms_h

  # window title text
  # (handle, char*, sz)
  extern 'int GetWindowText(int, int, int)', :stdcall

  # to find minesweeper window
  # (proc, lparam)
  extern 'int EnumWindows(void*, int)', :stdcall

  # (handle, lparam)
  EnumCallback = bind "int enum_call_back(int, int)" do |h, _|
    Mem.zero
    GetWindowText h, Mem.to_i, Mem.size
    txt = Mem.to_s.strip.force_encoding Encoding.default_external
    if txt == MSstr
      $ms_h = h
      0
    else
      1 # 0 stops it
    end
  end

  EnumWindows EnumCallback, 0
  exit unless $ms_h

  # ------------------------------------------------------------------------------
  # activate minesweeper window

  extern 'int SetForegroundWindow(int)', :stdcall
  SetForegroundWindow $ms_h

  # ------------------------------------------------------------------------------
  # simulate right clicks

  # move cursor
  extern 'int SetCursorPos(int, int)', :stdcall

  # generate a mouse event
  # (event, x, y, _, _)
  extern 'void mouse_event(int, int, int, int, int)', :stdcall
  MOUSEEVENTF_RIGHTDOWN = 0x0008
  MOUSEEVENTF_RIGHTUP = 0x0010

  # for coordinate transformation
  # (handle, point*)
  extern 'int ClientToScreen(int, void*)', :stdcall
  POINT = struct ['int x', 'int y']

  module_function

  # move mouse and right click
  def click x, y
    x = x*18 + 41
    y = y*18 + 41

    p = POINT.malloc
    p.x = x
    p.y = y
    ClientToScreen $ms_h, p

    SetCursorPos p.x, p.y
    sleep 0.01
    mouse_event MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0
    mouse_event MOUSEEVENTF_RIGHTUP, x, y, 0, 0
    sleep 0.05
  end

  # right click twice to remove the little flag
  def unclick x, y
    x = x*18 + 41
    y = y*18 + 41

    p = POINT.malloc
    p.x = x
    p.y = y
    ClientToScreen $ms_h, p

    SetCursorPos p.x, p.y
    sleep 0.01
    mouse_event MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0
    mouse_event MOUSEEVENTF_RIGHTUP, x, y, 0, 0
    mouse_event MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0
    mouse_event MOUSEEVENTF_RIGHTUP, x, y, 0, 0
    sleep 0.05
  end
end

def slop show
  if show
    12.times do |i|
      MineSweeper.click i, i
    end
  else
    12.times do |i|
      MineSweeper.unclick i, i
    end
  end
end

def hori show
  if show
    12.times do |i|
      MineSweeper.click i, 0
    end
  else
    12.times do |i|
      MineSweeper.unclick i, 0
    end
  end
end

5.times {
  slop true
  slop false
  sleep 2
  hori true
  hori false
}

#30 楼 @luikore 奇怪,我在 linux 下运行会报这个语法错误:

➜  ~  ruby -v
ruby 2.1.0dev (2013-04-26) [x86_64-linux]
➜  ~  ruby test.rb
                          test.rb:21:in `block (2 levels) in <main>': wrong number of arguments (0 for 1) (ArgumentError)
    from test.rb:29:in `[]'
    from test.rb:29:in `block (2 levels) in <main>'
    from test.rb:29:in `times'
    from test.rb:29:in `each'
    from test.rb:29:in `map'
    from test.rb:29:in `block in <main>'
    from test.rb:26:in `loop'
    from test.rb:26:in `<main>'
➜  ~  uname -a
Linux qhwa-laptop 3.2.0-40-generic #64-Ubuntu SMP Mon Mar 25 21:22:10 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

#37 楼 @qhwa 刚才加语音时改错了,现在修好了...

#30 楼 @luikore 配上《忐忑》应该效果更好...

@luikore 给跪了。。。

太搞了……

且不论对象本身是否变化,这个场合数组用 Wave 首字母大写形式,没有意义呀。如果在类中被定义,或者不想让它变, 如果变了, 请警告我, 也应该是 `全部大写', 搞的和类名称约定一致,这不是让人犯错误么...

@fsword 虽然 Wave 的确没有变化 (它的 object_id 始终引用同一个数组), 但是这里用常量的意义不在于没有被重新 assign, 而在于:我不想它被 assign , 或者 希望总可以被访问. 而这些条件下,不应该用和类一致的约定。而应该用全部大写。以楼主的例子来说,我看不出来非要用 常量 的意义。

#7 楼 @luikore 师傅觉得我的理解有没有问题?

@zw963 ...你总是能深入挖掘...

#30 楼 @luikore 碉堡了!! 堡了!!! 堡!!! 了!!! 了了了!!!!

威武威武

#43 楼 @luikore

师傅在夸我吗?哈哈!我要再接再厉,努力努力!

用 ruby 开发个 游戏吧。。Don't Starve (別挨餓)(飢荒)

这个有什么用?

哈哈,挺漂亮的!

a = [" ", "0", "0", "0", "0", "0", "0", " ", " ", " ", " ", " ", " ", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", "0", "0", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", "0", " ", " ", " ", " ", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", "0", "\n", " ", "0", "0", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", "0", "0", "0", "0", "\n", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", " ", " ", " ", " ", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n"]

puts a.join

#51 楼 @zfjoy520 👍

!(a=[" ", "0", "0", "0", "0", "0", "0", " ", " ", " ", " ", " ", " ", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", "0", "0", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", "0", " ", " ", " ", " ", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", "0", "\n", " ", "0", "0", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", "0", "0", "0", "0", "\n", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", " ", " ", " ", " ", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n"];)
10.times{
  puts ("\n\n\n")
  !a.each {|x| print x; sleep 0.008 }
}
"over"

#54 楼 @zputee 👍 但是干吗加个感叹号啊。

#55 楼 @zgm 第二个是个笔误。第一个 pry 中会显示数组,需要“q”退出后,才运行后面的循环。

@luikore@zfjoy520 @zgm 致敬

dot = ->{
  i = 0
  j = 150
  -> {
    i += 1
    if i % 2== 0
      i = 0
      j += 1
    end
    if j > 231
      j = 16
    end
    "\e[38;5;#{j}m█"
  }
}[]
!(a=["\n","\n",
" ", "0", "0", "0", "0", "0", "0", " ", " ", " ", " ", " ", " ", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", "0", "0", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", "0", " ", " ", " ", " ", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", "0", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", 
" ", "0", "0", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", "0", "0", "0", "0", "\n", 
" ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0","0", "\n","\n"];)

10.times{
  a.each {|x| print ((x=="0")? dot[]:x) ; sleep 0.009 }
  a.each {|x| print x; sleep 0.006}
}
"over"

北风乱,夜未央,你的影子。。。

dot = ->{
  i = 0; j = 150
  -> {
    i += 1; (i=0 ; j+=1) if i % 2== 0
    j = 16 if  j > 231
    "\e[38;5;#{j}m█"
  }
}[]

class Array
  def show_chars
    print  "\n" * 2
    each do |x|
        x.each_char{|x| print x; sleep 0.06};
        print "\n"*2
    end
    true
  end
end
(
a=["\n\n",
" ", "0", "0", "0", "0", "0", "0", " ", " ", " ", " ", " ", " ", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", "0", "0", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", "0", " ", " ", " ", " ", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", "0", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", 
" ", "0", "0", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", "0", "0", "0", "0", "\n", 
" ", "0", "0", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", "0", "0", " ", " ", " ", "0", "0", " ", " ", " ", "0", "0", "\n", 
" ", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0", "0", "0", " ", " ", " ", " ", "0", "0", " ", " ", " ", " ", "0", "0","0","\n",
"-","-","-","-","-","-","-","-","-","-","-","-","-","-","-",];
header =[
"北风乱,夜未央,你的影子",
"剪不断,徒留我孤单在",
"湖面成雙!",
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",];
footer = [
"OOOOOnly youuuuuuu\no   祝所有母亲   u\no   五月十二日   u\no   节日快乐!!   u\nOOOOOnly youuuuuuu\n\n",]
)
(
print "\n"*25
header.show_chars
3.times{
  a.each {|x| print ((x=="0")? dot[]:x) ; sleep 0.006 }
  a.join.split("\n").reverse.join("\n").each_char {|x| print x; sleep 0.003}
}; 
footer.show_chars
)

"^_^"

#59 楼 @zputee 玩出花了啊。

#60 楼 @chenge @zgm ^_^ , 又小改了一下。

#61 楼 @zputee 牛 B 啊。。几小时没上。大神多了不少。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号