@@ -20,7 +20,7 @@ abstract class BlockGame(
2020 var framesPerSecond : Int = 50 ,
2121 val messageAreaHeight : Int = 2 ,
2222 val messageAreaBackground : Color = Color .gray.darker.darker
23- ) {
23+ ):
2424 import introprog .PixelWindow
2525
2626 /** Called when a key is pressed. Override if you want non-empty action.
@@ -85,78 +85,63 @@ abstract class BlockGame(
8585 * It draws only updated blocks aiming at the desired frame rate.
8686 * It calls each `onXXX` method if a corresponding event is detected.
8787 */
88- protected def gameLoop (stopWhen : => Boolean ): Unit = while ! stopWhen do {
88+ protected def gameLoop (stopWhen : => Boolean ): Unit = while ! stopWhen do
8989 import PixelWindow .Event
9090 val t0 = System .currentTimeMillis
9191 pixelWindow.awaitEvent(MaxWaitForEventMillis .toLong)
92- while pixelWindow.lastEventType != PixelWindow .Event .Undefined do {
93- pixelWindow.lastEventType match {
92+ while pixelWindow.lastEventType != PixelWindow .Event .Undefined do
93+ pixelWindow.lastEventType match
9494 case Event .KeyPressed => onKeyDown(pixelWindow.lastKey)
9595 case Event .KeyReleased => onKeyUp(pixelWindow.lastKey)
9696 case Event .WindowClosed => onClose()
9797 case Event .MousePressed => onMouseDown(pixelWindow.lastMousePos)
9898 case Event .MouseReleased => onMouseUp(pixelWindow.lastMousePos)
9999 case _ =>
100- }
101100 pixelWindow.awaitEvent(1 )
102- }
103101 gameLoopAction()
104102 drawUpdatedBlocks()
105103 val elapsed = System .currentTimeMillis - t0
106- if (gameLoopDelayMillis - elapsed) < MaxWaitForEventMillis then {
104+ if (gameLoopDelayMillis - elapsed) < MaxWaitForEventMillis then
107105 onFrameTimeOverrun(elapsed)
108- }
109106 Thread .sleep((gameLoopDelayMillis - elapsed) max 0 )
110- }
111107
112108 /** Draw updated blocks and carry out post-update actions if any. */
113- private def drawUpdatedBlocks (): Unit = {
114- for x <- blockBuffer.indices do {
115- for y <- blockBuffer(x).indices do {
116- if isBufferUpdated(x)(y) then {
109+ private def drawUpdatedBlocks (): Unit =
110+ for x <- blockBuffer.indices do
111+ for y <- blockBuffer(x).indices do
112+ if isBufferUpdated(x)(y) then
117113 val pwx = x * blockSize
118114 val pwy = y * blockSize
119115 pixelWindow.fill(pwx, pwy, blockSize, blockSize, blockBuffer(x)(y))
120116 isBufferUpdated(x)(y) = false
121- }
122- }
123- }
124117 toDoAfterBlockUpdates.foreach(_.apply())
125118 toDoAfterBlockUpdates.clear()
126- }
127119
128120 /** Erase all blocks to background color. */
129- def clearWindow (): Unit = {
121+ def clearWindow (): Unit =
130122 pixelWindow.clear()
131123 clearMessageArea()
132- for x <- blockBuffer.indices do {
133- for y <- blockBuffer(x).indices do {
124+ for x <- blockBuffer.indices do
125+ for y <- blockBuffer(x).indices do
134126 blockBuffer(x)(y) = background
135- }
136- }
137- }
138127
139128 /** Paint a block in color `c` at (`x`,`y`) in block coordinates. */
140- def drawBlock (x : Int , y : Int , c : Color ): Unit = {
141- if blockBuffer(x)(y) != c then {
129+ def drawBlock (x : Int , y : Int , c : Color ): Unit =
130+ if blockBuffer(x)(y) != c then
142131 blockBuffer(x)(y) = c
143132 isBufferUpdated(x)(y) = true
144- }
145- }
146133
147134 /** Erase the block at (`x`,`y`) to `background` color. */
148- def eraseBlock (x : Int , y : Int ): Unit = {
149- if blockBuffer(x)(y) != background then {
135+ def eraseBlock (x : Int , y : Int ): Unit =
136+ if blockBuffer(x)(y) != background then
150137 blockBuffer(x)(y) = background
151138 isBufferUpdated(x)(y) = true
152- }
153- }
154139
155140 /** Write `msg` in `color` in the middle of the window.
156141 * The drawing is postponed until the end of the current game loop
157142 * iteration and thus the text drawn on top of any updated blocks.
158143 */
159- def drawCenteredText (msg : String , color : Color = pixelWindow.foreground, size : Int = blockSize): Unit = {
144+ def drawCenteredText (msg : String , color : Color = pixelWindow.foreground, size : Int = blockSize): Unit =
160145 toDoAfterBlockUpdates.append( () =>
161146 pixelWindow.drawText(
162147 msg,
@@ -165,23 +150,19 @@ abstract class BlockGame(
165150 size
166151 )
167152 )
168- }
169153
170154 /** Write `msg` in `color` in the message area at ('x','y') in block coordinates. */
171- def drawTextInMessageArea (msg : String , x : Int , y : Int , color : Color = pixelWindow.foreground, size : Int = blockSize): Unit = {
155+ def drawTextInMessageArea (msg : String , x : Int , y : Int , color : Color = pixelWindow.foreground, size : Int = blockSize): Unit =
172156 require(y < messageAreaHeight && y >= 0 , s " not in message area: y = $y" )
173157 require(x < dim._1 * blockSize && x >= 0 , s " not in message area: x = $x" )
174158 pixelWindow.drawText(msg, x * blockSize, (y + dim._2) * blockSize, color, size)
175- }
176159
177160 /** Clear a rectangle in the message area in block coordinates. */
178- def clearMessageArea (x : Int = 0 , y : Int = 0 , width : Int = dim._1, height : Int = messageAreaHeight): Unit = {
161+ def clearMessageArea (x : Int = 0 , y : Int = 0 , width : Int = dim._1, height : Int = messageAreaHeight): Unit =
179162 require(y < messageAreaHeight && y >= 0 , s " not in message area: y = $y" )
180163 require(x < dim._1 * blockSize && x >= 0 , s " not in message area: x = $x" )
181164 pixelWindow.fill(
182165 x * blockSize, (y + dim._2) * blockSize,
183166 width * blockSize, messageAreaHeight * blockSize + blockSize / 2 ,
184167 messageAreaBackground
185168 )
186- }
187- }
0 commit comments