Advertisement
anykey

Untitled

Apr 18th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.36 KB | None | 0 0
  1. require 'curses'
  2. include Curses
  3.  
  4. PIECE1 = <<PIECE
  5. 111100000000
  6. 111100000000
  7. 111111111111
  8. 111111111111
  9. PIECE
  10.  
  11. PIECE2 = <<PIECE
  12. 000000001111
  13. 000000001111
  14. 111111111111
  15. 111111111111
  16. PIECE
  17.  
  18. PIECE3 = <<PIECE
  19. 11111111
  20. 11111111
  21. 11111111
  22. 11111111
  23. PIECE
  24.  
  25. PIECE4 = <<PIECE
  26. 1111
  27. 1111
  28. 1111
  29. 1111
  30. 1111
  31. 1111
  32. 1111
  33. 1111
  34. PIECE
  35.  
  36. PIECE5 = <<PIECE
  37. 000011110000
  38. 000011110000
  39. 111111111111
  40. 111111111111
  41. PIECE
  42.  
  43. def write(x, y, text)
  44.   Curses.setpos(y, x)
  45.   Curses.addstr(text)
  46. end
  47.  
  48. def draw_piece(x, y, piece, color=@color)
  49.   Curses.attron(color_pair(color))
  50.   blocks = piece.split.map { |i| i.split(//) }
  51.   blocks.each_with_index do |row,i|
  52.     row.each_with_index do |c,j|
  53.       write(x+j, y+i, c) if c == "1"
  54.     end
  55.   end
  56.   Curses.attroff(color_pair(COLOR_RED))
  57. end
  58.  
  59. def move_piece(dx, dy)
  60.   return nil if @posX + dx < 0
  61.   return nil if @posY + dy < 0
  62.  
  63.   max_x = @piece.split.max_by { |i| i.size }.size
  64.   return nil if (@posX + dx + max_x) >= @width
  65.   max_y = @piece.split.size
  66.  
  67.   # Piece has reached the bottom
  68.   if (@posY + dy + max_y) > @height
  69.     @bottom_pieces << [@posX, @posY, @piece, @color]
  70.     @posX  = @width / 2
  71.     @posY  = 0
  72.     @piece = @pieces.sample
  73.     @color = @colors.sample
  74.   end
  75.  
  76.   @posX += dx
  77.   @posY += dy
  78. end
  79.  
  80. def init_colors
  81.   Curses.init_pair(COLOR_RED, COLOR_RED, COLOR_RED)
  82.   Curses.init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_CYAN)
  83.   Curses.init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_GREEN)
  84.   @colors = [COLOR_RED,COLOR_CYAN,COLOR_GREEN]
  85. end
  86.  
  87. def init_screen
  88.   Curses.noecho
  89.   Curses.curs_set(0)
  90.   Curses.init_screen
  91.   Curses.start_color
  92.   init_colors
  93.   Curses.stdscr.keypad(true)
  94.   begin
  95.     yield
  96.   ensure
  97.     Curses.close_screen
  98.   end
  99. end
  100.  
  101. init_screen do
  102.   @width  = Curses.cols
  103.   @height = Curses.lines
  104.   @posX = @width / 2
  105.   @posY = 0
  106.   @pieces = [PIECE1, PIECE2, PIECE3, PIECE4, PIECE5]
  107.   @piece = @pieces.sample
  108.   @bottom_pieces = []
  109.   @color = @colors.sample
  110.  
  111.   draw_piece(@posX, @posY, @piece)
  112.  
  113.   loop do
  114.     case Curses.getch
  115.       when Curses::Key::UP    then move_piece(0, -4)
  116.       when Curses::Key::DOWN  then move_piece(0, 4)
  117.       when Curses::Key::LEFT  then move_piece(-4, 0)
  118.       when Curses::Key::RIGHT then move_piece(4, 0)
  119.     end
  120.  
  121.     Curses.clear
  122.     draw_piece(@posX, @posY, @piece)
  123.     @bottom_pieces.each { |p| draw_piece(*p) }
  124.     Curses.refresh
  125.   end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement