"LearnSqueak Breakout, checkpoint 09: Finish a playable game. Standalone: file in this entire file, then evaluate LearnSqueakBreakout open. Verified with Squeak 6.1 (23976, 64-bit). No external packages or sound." Morph subclass: #LearnSqueakBreakout instanceVariableNames: 'ball velocity paddleX running state stepWorld bricks score lives' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Breakout'! !LearnSqueakBreakout methodsFor: 'initialization'! initialize super initialize. self extent: 640@460. self color: (Color r: 0.035 g: 0.055 b: 0.11). running := false. self restart! ! !LearnSqueakBreakout methodsFor: 'state'! restart self pause. score := 0. lives := 3. state := #ready. paddleX := 320.0. self buildBricks. self serve. self changed! ! !LearnSqueakBreakout methodsFor: 'state'! serve ball := paddleX @ 368.0. velocity := 180.0 @ -270.0. self changed! ! !LearnSqueakBreakout methodsFor: 'geometry'! field "All game geometry uses a fixed 640 by 460 logical coordinate system." ^ 16@70 corner: 624@416! ! !LearnSqueakBreakout methodsFor: 'geometry'! paddleBounds ^ (paddleX - 48) @ 388 extent: 96@12! ! !LearnSqueakBreakout methodsFor: 'geometry'! ballRadius ^ 7.0! ! !LearnSqueakBreakout methodsFor: 'geometry'! viewPoint: aPoint "Map logical coordinates to the Morph's current position and size." ^ self position + ((aPoint x * self width / 640) @ (aPoint y * self height / 460))! ! !LearnSqueakBreakout methodsFor: 'geometry'! viewRectangle: aRectangle ^ (self viewPoint: aRectangle origin) corner: (self viewPoint: aRectangle corner)! ! !LearnSqueakBreakout methodsFor: 'geometry'! logicalXFor: aPoint ^ (aPoint x - self left) * 640 / (self width max: 1)! ! !LearnSqueakBreakout methodsFor: 'drawing'! ink ^ Color r: 0.82 g: 0.88 b: 0.97! ! !LearnSqueakBreakout methodsFor: 'drawing'! drawText: aString at: aPoint color: aColor on: aCanvas aCanvas drawString: aString at: (self viewPoint: aPoint) rounded font: (TextStyle defaultFont) color: aColor! ! !LearnSqueakBreakout methodsFor: 'drawing'! drawOn: aCanvas super drawOn: aCanvas. aCanvas fillRectangle: (self viewRectangle: self field) color: (Color r: 0.06 g: 0.095 b: 0.17). aCanvas frameRectangle: (self viewRectangle: self field) width: 2 color: (Color r: 0.18 g: 0.28 b: 0.40). self drawText: 'BREAKOUT / MORPHIC' at: 22@12 color: Color white on: aCanvas. self drawText: '09 Finish a playable game' at: 22@40 color: (Color r: 0.37 g: 0.84 b: 0.91) on: aCanvas. self drawPaddleAndBallOn: aCanvas. self drawBricksOn: aCanvas. self drawScoreOn: aCanvas. self drawStatusOn: aCanvas. self drawText: 'Mouse / A D / arrows | Space: pause | R: restart' at: 22@435 color: self ink on: aCanvas! ! !LearnSqueakBreakout methodsFor: 'drawing'! drawPaddleAndBallOn: aCanvas aCanvas fillRectangle: (self viewRectangle: self paddleBounds) color: (Color r: 0.20 g: 0.82 b: 0.88). aCanvas fillRectangle: (self viewRectangle: (self paddleBounds origin extent: 96@3)) color: (Color r: 0.65 g: 0.97 b: 1.0). state = #gameOver ifTrue: [^ self]. aCanvas fillOval: (self viewRectangle: (Rectangle center: ball extent: (self ballRadius * 2) asPoint)) color: (Color r: 1.0 g: 0.86 b: 0.39)! ! !LearnSqueakBreakout methodsFor: 'input'! movePaddleTo: anX paddleX := (anX max: self field left + 48) min: self field right - 48. state = #ready ifTrue: [ball := paddleX @ 368.0]. self changed! ! !LearnSqueakBreakout methodsFor: 'input'! handlesMouseDown: anEvent ^ true! ! !LearnSqueakBreakout methodsFor: 'input'! wantsEveryMouseMove ^ true! ! !LearnSqueakBreakout methodsFor: 'input'! mouseMove: anEvent self movePaddleTo: (self logicalXFor: anEvent position)! ! !LearnSqueakBreakout methodsFor: 'input'! mouseDown: anEvent anEvent hand newKeyboardFocus: self. self movePaddleTo: (self logicalXFor: anEvent position). state = #ready ifTrue: [self start]! ! !LearnSqueakBreakout methodsFor: 'control'! pause running := false. self stopStepping. stepWorld ifNotNil: [stepWorld stopStepping: self]. stepWorld := nil. self changed! ! !LearnSqueakBreakout methodsFor: 'control'! windowIsClosing "The window owns the game's lifetime. Never fork an animation Process." self activeHand keyboardFocus == self ifTrue: [ self activeHand newKeyboardFocus: nil]. self pause! ! !LearnSqueakBreakout methodsFor: 'control'! delete self windowIsClosing. super delete! ! !LearnSqueakBreakout methodsFor: 'control'! wantsSteps ^ running = true! ! !LearnSqueakBreakout methodsFor: 'control'! start (#(won gameOver) includes: state) ifTrue: [^ self]. state := #playing. running := true. stepWorld := self world. self startStepping. self changed! ! !LearnSqueakBreakout methodsFor: 'stepping'! stepTime ^ 20! ! !LearnSqueakBreakout methodsFor: 'stepping'! step "One deterministic 20 ms simulation tick, requested by Morphic." running ifFalse: [^ self]. self advanceBy: self stepTime / 1000.0! ! !LearnSqueakBreakout methodsFor: 'stepping'! advanceBy: seconds "Small moves prevent crossing a wall, paddle, or brick in one jump. Bound external requests to 50 ms; ordinary steps use 20 ms." | dt count slice | running ifFalse: [^ self]. dt := (seconds max: 0.0) min: 0.05. dt = 0 ifTrue: [^ self]. count := (((velocity x abs max: velocity y abs) * dt / (self ballRadius / 2)) ceiling) max: 1. slice := dt / count. 1 to: count do: [:ignored | running ifTrue: [self moveBy: slice]]. self changed! ! !LearnSqueakBreakout methodsFor: 'physics'! moveBy: dt | oldX oldY | oldX := ball x. ball := (ball x + (velocity x * dt)) @ ball y. self bounceSideWalls. self collideBrickOn: #x from: oldX. running ifFalse: [^ self]. oldY := ball y. ball := ball x @ (ball y + (velocity y * dt)). self bounceTop. self collideBrickOn: #y from: oldY. running ifFalse: [^ self]. self bouncePaddleFrom: oldY. self checkMiss! ! !LearnSqueakBreakout methodsFor: 'physics'! bounceSideWalls | low high | low := self field left + self ballRadius. high := self field right - self ballRadius. ball x < low ifTrue: [ ball := low @ ball y. velocity := velocity x abs @ velocity y]. ball x > high ifTrue: [ ball := high @ ball y. velocity := velocity x abs negated @ velocity y]! ! !LearnSqueakBreakout methodsFor: 'physics'! bounceTop ball y < (self field top + self ballRadius) ifTrue: [ ball := ball x @ (self field top + self ballRadius). velocity := velocity x @ velocity y abs]! ! !LearnSqueakBreakout methodsFor: 'drawing'! drawStatusOn: aCanvas | message | message := self statusText. message isEmpty ifFalse: [ aCanvas fillRectangle: (self viewRectangle: (112@249 extent: 416@72)) color: (Color r: 0.085 g: 0.14 b: 0.24). self drawText: message at: 132@262 color: Color white on: aCanvas. self drawText: self statusHint at: 132@289 color: self ink on: aCanvas]! ! !LearnSqueakBreakout methodsFor: 'drawing'! statusText state = #won ifTrue: [^ 'YOU CLEARED THE WALL!!']. state = #gameOver ifTrue: [^ 'GAME OVER']. state = #ready ifTrue: [^ 'READY TO SERVE']. running ifFalse: [^ 'PAUSED']. ^ ''! ! !LearnSqueakBreakout methodsFor: 'drawing'! statusHint (#(won gameOver) includes: state) ifTrue: [^ 'Press Restart for a fresh game.']. state = #ready ifTrue: [^ 'Move the paddle, then click the board or Start.']. ^ 'Press Start to continue from this position.'! ! !LearnSqueakBreakout methodsFor: 'physics'! bouncePaddleFrom: oldY | paddle offset | velocity y <= 0 ifTrue: [^ self]. paddle := self paddleBounds. ((oldY + self ballRadius <= paddle top) and: [ball y + self ballRadius >= paddle top] and: [ball x + self ballRadius >= paddle left] and: [ball x - self ballRadius <= paddle right]) ifTrue: [ ball := ball x @ (paddle top - self ballRadius). offset := (((ball x - paddleX) / 48) max: -1.0) min: 1.0. velocity := (offset * 280.0) @ -290.0]! ! !LearnSqueakBreakout methodsFor: 'physics'! checkMiss ball y - self ballRadius > self field bottom ifTrue: [ self pause. lives := lives - 1. lives = 0 ifTrue: [state := #gameOver] ifFalse: [state := #ready. self serve]]! ! !LearnSqueakBreakout methodsFor: 'bricks'! buildBricks bricks := OrderedCollection new. 0 to: 3 do: [:row | 0 to: 7 do: [:column | bricks add: ((32 + (column * 73)) @ (100 + (row * 28)) extent: 65@19)]]! ! !LearnSqueakBreakout methodsFor: 'drawing'! brickColor: aBrick | row | row := ((aBrick top - 100) / 28) rounded + 1. ^ {Color r: 1.0 g: 0.38 b: 0.42. Color r: 1.0 g: 0.65 b: 0.26. Color r: 0.34 g: 0.80 b: 0.72. Color r: 0.37 g: 0.59 b: 0.96} at: row! ! !LearnSqueakBreakout methodsFor: 'drawing'! drawBricksOn: aCanvas bricks do: [:brick | aCanvas fillRectangle: (self viewRectangle: brick) color: (self brickColor: brick). aCanvas fillRectangle: (self viewRectangle: (brick origin extent: brick width@3)) color: (self brickColor: brick) muchLighter]! ! !LearnSqueakBreakout methodsFor: 'bricks'! brickAtBallOn: axis from: previous "The square hitbox must cross a face while moving toward it. Strict transverse overlap excludes a neighboring brick's tangent edge." | current movement radius | current := axis = #x ifTrue: [ball x] ifFalse: [ball y]. movement := current - previous. movement = 0 ifTrue: [^ nil]. radius := self ballRadius. ^ bricks detect: [:brick | axis = #x ifTrue: [ (ball y + radius > brick top) and: [ball y - radius < brick bottom] and: [movement > 0 ifTrue: [(previous + radius <= brick left) and: [current + radius >= brick left]] ifFalse: [(previous - radius >= brick right) and: [current - radius <= brick right]]]] ifFalse: [ (ball x + radius > brick left) and: [ball x - radius < brick right] and: [movement > 0 ifTrue: [(previous + radius <= brick top) and: [current + radius >= brick top]] ifFalse: [(previous - radius >= brick bottom) and: [current - radius <= brick bottom]]]]] ifNone: [nil]! ! !LearnSqueakBreakout methodsFor: 'bricks'! collideBrickOn: axis from: previous | brick | brick := self brickAtBallOn: axis from: previous. brick ifNil: [^ self]. axis = #x ifTrue: [ ball := (velocity x > 0 ifTrue: [brick left - self ballRadius] ifFalse: [brick right + self ballRadius]) @ ball y. velocity := velocity x negated @ velocity y] ifFalse: [ ball := ball x @ (velocity y > 0 ifTrue: [brick top - self ballRadius] ifFalse: [brick bottom + self ballRadius]). velocity := velocity x @ velocity y negated]. bricks remove: brick. score := score + 10. bricks isEmpty ifTrue: [state := #won. self pause]! ! !LearnSqueakBreakout methodsFor: 'drawing'! drawScoreOn: aCanvas self drawText: 'SCORE ', score printString at: 430@12 color: Color white on: aCanvas. self drawText: 'LIVES ', lives printString at: 535@40 color: (Color r: 1.0 g: 0.78 b: 0.34) on: aCanvas! ! !LearnSqueakBreakout methodsFor: 'control'! togglePause running ifTrue: [self pause] ifFalse: [self start]! ! !LearnSqueakBreakout methodsFor: 'input'! handlesKeyboard: anEvent ^ true! ! !LearnSqueakBreakout methodsFor: 'input'! keyStroke: anEvent self handleKey: anEvent keyCharacter! ! !LearnSqueakBreakout methodsFor: 'input'! handleKey: aCharacter | key | key := aCharacter asLowercase. ((key = $a) or: [aCharacter asciiValue = 28]) ifTrue: [^ self movePaddleTo: paddleX - 28]. ((key = $d) or: [aCharacter asciiValue = 29]) ifTrue: [^ self movePaddleTo: paddleX + 28]. key = Character space ifTrue: [^ self togglePause]. key = $r ifTrue: [^ self restart]! ! !LearnSqueakBreakout methodsFor: 'accessing'! ballPosition ^ ball! ! !LearnSqueakBreakout methodsFor: 'accessing'! ballVelocity ^ velocity! ! !LearnSqueakBreakout methodsFor: 'accessing'! paddleCenter ^ paddleX! ! !LearnSqueakBreakout methodsFor: 'accessing'! score ^ score! ! !LearnSqueakBreakout methodsFor: 'accessing'! lives ^ lives! ! !LearnSqueakBreakout methodsFor: 'accessing'! gameState ^ state! ! !LearnSqueakBreakout methodsFor: 'accessing'! running ^ running! ! !LearnSqueakBreakout methodsFor: 'accessing'! brickCount ^ bricks size! ! !LearnSqueakBreakout methodsFor: 'testing'! ballPosition: aPoint velocity: aVelocity "Deterministic examples and tests can arrange a collision without waiting." ball := aPoint. velocity := aVelocity! ! !LearnSqueakBreakout methodsFor: 'testing'! bricks: aCollection "Copy the test fixture; do not retain a mutable collection supplied by a caller." bricks := aCollection asOrderedCollection copy! ! !LearnSqueakBreakout class methodsFor: 'opening'! button: aLabel target: aTarget action: aSelector ^ SimpleButtonMorph new label: aLabel; target: aTarget; actionSelector: aSelector; yourself! ! !LearnSqueakBreakout class methodsFor: 'opening'! open | game window | game := self new. window := SystemWindow new model: game; setLabel: 'Breakout - 09 / Finish a playable game'; paneColor: (Color r: 0.91 g: 0.94 b: 0.98); yourself. window addMorph: game frame: (0@0 corner: 1@0.88). window addMorph: (self button: 'Start' target: game action: #start) frame: (0.035@0.9 corner: 0.20@0.975). window addMorph: (self button: 'Pause' target: game action: #pause) frame: (0.22@0.9 corner: 0.385@0.975). window addMorph: (self button: 'Restart' target: game action: #restart) frame: (0.405@0.9 corner: 0.60@0.975). window addMorph: (StringMorph contents: 'Click the board for keyboard focus') frame: (0.63@0.92 corner: 0.98@0.97). window openInWorld; extent: 700@590. ^ window! !