Skip to content

Commit

Permalink
rewrite Tetris
Browse files Browse the repository at this point in the history
  • Loading branch information
halcat0x15a committed Dec 26, 2011
1 parent bd7145d commit 1e6d02f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 56 deletions.
4 changes: 4 additions & 0 deletions core/src/main/scala/twitter4z/http/TwitterPromise.scala
Expand Up @@ -30,4 +30,8 @@ object TwitterPromise {
}
}

implicit def TwitterPromiseEach = new Each[TwitterPromise] {
def each[A](m: TwitterPromise[A], f: A => Unit): Unit = m.value |>| (_ |>| (_.value |> f))
}

}
11 changes: 1 addition & 10 deletions tetris/src/main/scala/twitter4z/tetris/Block.scala
@@ -1,14 +1,5 @@
package twitter4z.tetris

import java.awt.image.BufferedImage
import java.net.URL
import javax.imageio.ImageIO
/*
case class Block(image: BufferedImage, coord: Coord)

object Block {
def apply(url: URL): ImageBlock = ImageBlock(ImageIO.read(url))
}
*/
case class Block(image: BufferedImage, coord: Coord, text: String)
12 changes: 12 additions & 0 deletions tetris/src/main/scala/twitter4z/tetris/Coord.scala
@@ -0,0 +1,12 @@
package twitter4z.tetris

import scalaz._
import Scalaz._

case class Coord(x: Int, y: Int)

object Coord {

implicit lazy val CoordEqual: Equal[Coord] = equalA

}
94 changes: 59 additions & 35 deletions tetris/src/main/scala/twitter4z/tetris/Tetris.scala
@@ -1,6 +1,7 @@
package twitter4z.tetris

import java.awt.Color._
import java.awt.RenderingHints
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import swing._
import event._
Expand All @@ -11,66 +12,89 @@ import Scalaz._
import twitter4z.Twitter._

object Tetris extends SimpleSwingApplication {
/*
lazy val blocks: Ref[List[Block]] = Ref(EmptyField)

lazy val tetrimino: Ref[Option[Tetrimino]] = Ref(None)
lazy val block: Ref[Option[Block]] = Ref(None)

lazy val blocksWorker: SwingWorker = new SwingWorker {
def act(): Unit = {
loop {
receive {
case tetrimino: Tetrimino => atomic { implicit txn: InTxn =>
blocks.transform(_ ++ tetrimino.blocks)
}
}
}
}
}
lazy val blocks: TSet[Block] = TSet.empty

lazy val tetriminoWorker: SwingWorker = new SwingWorker {
def act(): Unit = {
def move(f: Tetrimino => Tetrimino) = atomic { implicit txn: InTxn =>
tetrimino.transform(_.map(f))
fieldWorker ! tetrimino.get
}
loop {
receive {
case Key.Left => move(_.left)
case Key.Right => move(_.right)
case Key.Down => move(_.down)
def move(x: Int, y: Int): Unit = {
block.single() |>| {
case b@Block(image, Coord(xx, yy), _) => {
atomic { implicit txn: InTxn =>
val xxx = (x |+| xx) |> (xxx => (xxx < 0).fold(0, xxx)) |> (xxx => ((xxx |+| 1) > Column).fold(Column - 1, xxx))
val yyy = (y |+| yy) |> (yyy => (yyy < 0).fold(0, yyy)) |> (yyy => ((yyy |+| 1) > Row).fold(Row - 1, yyy))
block() = b.copy(coord=Coord(xxx, yyy)).some
}
}
}
}
*/

lazy val panel = new Panel {
focusable = true
preferredSize = new Dimension(Width, Height)
override def paint(g: Graphics2D): Unit = {
g.setFont(DefaultFont)
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
g.clearRect(0, 0, Width, Height)
block.single() match {
case Some(Block(image, Coord(x, y), text)) => {
val len = Width / FontSize
text.sliding(len, len).zipWithIndex |>| {
case (text, i) => g.drawString(text, i, (i |+| 1) * FontSize)
}
g.drawImage(image, null, x * Size, y * Size)
}
case None => {
g.drawString("Loading...", 0, FontSize)
}
}
blocks.single.withFilter(null !=).foreach {
case Block(image, Coord(x, y), text) => {
g.drawImage(image, null, x * Size, y * Size)
}
}
}
listenTo(keys)
reactions += {
case KeyPressed(_, key, _, _) => {
key match {
case Key.Escape => quit()
case Key.Right => move(1, 0)
case Key.Left => move(-1, 0)
case Key.Space => move(0, Row)
case _ =>
}
repaint()
}
}
}

def updateBlock(): Unit = {
for (statuses <- publicTimeline()) {
atomic { implicit txn: InTxn =>
val status = statuses.head
block() |>| { block =>
blocks.add(block)
}
block() = Block(ImageIO.read(status.user.profile.imageURL), Coord(5, 0), status.text).some
}
}
}

lazy val mainWorker: SwingWorker = new SwingWorker {
def act(): Unit = {
val fps = 1000 / 60
@annotation.tailrec
def loop(icons: List[Image]): Unit = {
updateBlock()
loop {
move(0, 1)
panel.repaint()
Thread.sleep(fps)
loop(icons)
block.single() |>| { block =>
val y = block.coord.y |+| 1
(y === Row || blocks.single.exists(_.coord === Coord(block.coord.x, y))) when {
updateBlock()
}
}
Thread.sleep(SleepTime)
}
for {
statuses <- publicTimeline()
} yield statuses.map(_.user.profile.imageURL).map(ImageIO.read(_))
}
}

Expand Down
20 changes: 9 additions & 11 deletions tetris/src/main/scala/twitter4z/tetris/package.scala
@@ -1,29 +1,27 @@
package twitter4z

import java.awt.Font

import scalaz._
import Scalaz._

package object tetris {

// type Field = List[List[Block]]

type Coord = (Int, Int)

type Coords = (Coord, Coord, Coord, Coord)

type Image = java.awt.image.BufferedImage

val Row = 20
val Row = 15

val Column = 10

val Size = 20
val Size = 48

val Width = Column * Size

val Height = Row * Size

// val EmptyField: Field = List.fill(Row, Column)(EmptyBlock)
val SleepTime = 1000

val FontSize = Size / 2

val DefaultFont: Font = new Font(Font.SANS_SERIF, Font.PLAIN, FontSize)

}

0 comments on commit 1e6d02f

Please sign in to comment.