Examples.st 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Widget subclass: #Counter instanceVariableNames: 'count, header' category: 'Examples'! !Counter methodsFor: 'actions'! increase
  2. count := count + 1.
  3. header contents: [:html | html with: count asString] ! decrease
  4. count := count - 1.
  5. header contents: [:html | html with: count asString] ! ! !Counter methodsFor: 'initialization'! initialize
  6. super initialize.
  7. count := 0
  8. ! ! !Counter methodsFor: 'rendering'! renderOn: html
  9. header := html h1
  10. with: count asString;
  11. yourself.
  12. html button
  13. with: '++';
  14. onClick: [self increase].
  15. html button
  16. with: '--';
  17. onClick: [self decrease]
  18. ! ! Widget subclass: #Tetris instanceVariableNames: 'renderingContext, timer, speed, score, rows, movingPiece' category: 'Examples'! !Tetris methodsFor: 'accessing'! width
  19. ^self class width ! height
  20. ^self class height ! squares
  21. ^self class squares ! gluePiece: aPiece
  22. aPiece glueOn: self
  23. ! rows
  24. "An array of rows. Each row is a collection of points."
  25. ^rows ! addRow: aCollection
  26. self rows add: aCollection ! ! !Tetris methodsFor: 'actions'! startNewGame
  27. self newGame.
  28. timer ifNotNil: [timer clearInterval].
  29. timer := [self nextStep] valueWithInterval: speed ! nextStep
  30. movingPiece ifNil: [self newPiece].
  31. (movingPiece canMoveIn: self)
  32. ifTrue: [movingPiece position: movingPiece position + (0@1)]
  33. ifFalse: [self newPiece].
  34. self redraw
  35. ! redraw
  36. renderingContext clearRectFrom: 0@ self width to: 0@ self height.
  37. self
  38. drawMap;
  39. drawPiece ! drawMap
  40. renderingContext
  41. fillStyle: '#fafafa';
  42. fillRectFrom: 0@0 to: self width@self height.
  43. renderingContext
  44. lineWidth: 0.5;
  45. strokeStyle: '#999'.
  46. 0 to: self class squares x do: [:each | | x |
  47. x := each * self class squareSize.
  48. self drawLineFrom: x@0 to: x@self height].
  49. 0 to: self class squares y do: [:each | | y |
  50. y := each * self class squareSize.
  51. self drawLineFrom: 0@y to: self width@y]. ! drawLineFrom: aPoint to: anotherPoint
  52. renderingContext
  53. beginPath;
  54. moveTo: aPoint;
  55. lineTo: anotherPoint;
  56. stroke ! newGame
  57. rows := #().
  58. movingPiece := nil.
  59. speed := 200.
  60. score := 0 ! newPiece
  61. movingPiece := TetrisPiece atRandom ! drawRows
  62. self rows do: [:each |].
  63. movingPiece ifNotNil: [movingPiece drawOn: renderingContext] ! drawPiece
  64. movingPiece ifNotNil: [
  65. movingPiece drawOn: renderingContext] ! ! !Tetris methodsFor: 'initialization'! initialize
  66. super initialize.
  67. self newGame ! ! !Tetris methodsFor: 'rendering'! renderOn: html
  68. html div
  69. class: 'tetris';
  70. with: [
  71. html h3 with: 'Tetris'.
  72. self renderCanvasOn: html.
  73. self renderButtonsOn: html] ! renderCanvasOn: html
  74. | canvas |
  75. canvas := html canvas.
  76. canvas at: 'width' put: self width asString.
  77. canvas at: 'height' put: self height asString.
  78. renderingContext := CanvasRenderingContext tagBrush: canvas.
  79. self redraw ! renderButtonsOn: html
  80. html div
  81. class: 'tetris_buttons';
  82. with: [
  83. html button
  84. with: 'New game';
  85. onClick: [self startNewGame].
  86. html button
  87. with: 'play/pause';
  88. onClick: [self update]] ! ! !Tetris class methodsFor: 'accessing'! squareSize
  89. ^22 ! width
  90. ^self squareSize * (self squares x) ! height
  91. ^self squareSize * (self squares y) ! squares
  92. ^10@15 ! ! Widget subclass: #TetrisPiece instanceVariableNames: 'rotation, position' category: 'Examples'! !TetrisPiece methodsFor: 'accessing'! rotation
  93. ^rotation ifNil: [rotation := 1] ! rotation: aNumber
  94. rotation := aNumber ! position
  95. ^position ifNil: [(Tetris squares x / 2) -1 @ 0] ! position: aPoint
  96. ^position := aPoint ! bounds
  97. self subclassResponsibility ! color
  98. ^'#afa' ! height
  99. ^2 ! ! !TetrisPiece methodsFor: 'drawing'! drawOn: aRenderingContext
  100. aRenderingContext fillStyle: self color.
  101. self bounds do: [:each |
  102. aRenderingContext
  103. fillRectFrom: each + self position* Tetris squareSize to: 1@1 * Tetris squareSize;
  104. strokeStyle: '#999';
  105. lineWidth: 2;
  106. strokeRectFrom: each + self position* Tetris squareSize to: 1@1 * Tetris squareSize] ! ! !TetrisPiece methodsFor: 'testing'! canMove
  107. ^self position y < (Tetris squares y - self height) ! canMoveIn: aTetris
  108. ^self position y < (aTetris squares y - self height) ! ! !TetrisPiece class methodsFor: 'instance creation'! atRandom
  109. ^(self subclasses at: self subclasses size atRandom) new ! ! TetrisPiece subclass: #TetrisPieceO instanceVariableNames: '' category: 'Examples'! !TetrisPieceO methodsFor: 'accessing'! bounds
  110. ^Array new
  111. add: 0@0;
  112. add: 0@1;
  113. add: 1@0;
  114. add: 1@1;
  115. yourself ! ! TetrisPiece subclass: #TetrisPieceL instanceVariableNames: '' category: 'Examples'! !TetrisPieceL methodsFor: 'accessing'! bounds
  116. ^Array new
  117. add: 0@0;
  118. add: 0@1;
  119. add: 0@2;
  120. add: 1@2;
  121. yourself ! color
  122. ^'#ffa' ! height
  123. ^3 ! ! TetrisPiece subclass: #TetrisPieceJ instanceVariableNames: '' category: 'Examples'! !TetrisPieceJ methodsFor: 'accessing'! color
  124. ^'#aaf' ! bounds
  125. ^Array new
  126. add: 1@0;
  127. add: 1@1;
  128. add: 1@2;
  129. add: 0@2;
  130. yourself ! height
  131. ^3 ! ! TetrisPiece subclass: #TetrisPieceI instanceVariableNames: '' category: 'Examples'! !TetrisPieceI methodsFor: 'accessing'! color
  132. ^'#faa' ! bounds
  133. ^Array new
  134. add: 0@0;
  135. add: 0@1;
  136. add: 0@2;
  137. add: 0@3;
  138. yourself ! height
  139. ^4 ! ! TetrisPiece subclass: #TetrisPieceT instanceVariableNames: '' category: 'Examples'! !TetrisPieceT methodsFor: 'accessing'! bounds
  140. ^Array new
  141. add: 0@0;
  142. add: 1@0;
  143. add: 2@0;
  144. add: 1@1;
  145. yourself ! color
  146. ^'#aaf' ! !