Examples.st 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. Widget subclass: #Counter
  2. instanceVariableNames: 'count header'
  3. category: 'Examples'!
  4. !Counter methodsFor: 'actions'!
  5. increase
  6. count := count + 1.
  7. header contents: [:html | html with: count asString]
  8. !
  9. decrease
  10. count := count - 1.
  11. header contents: [:html | html with: count asString]
  12. ! !
  13. !Counter methodsFor: 'initialization'!
  14. initialize
  15. super initialize.
  16. count := 0
  17. ! !
  18. !Counter methodsFor: 'rendering'!
  19. renderOn: html
  20. header := html h1
  21. with: count asString;
  22. yourself.
  23. html button
  24. with: '++';
  25. onClick: [self increase].
  26. html button
  27. with: '--';
  28. onClick: [self decrease]
  29. ! !
  30. smalltalk.setup(smalltalk.Counter);
  31. Widget subclass: #Tetris
  32. instanceVariableNames: 'renderingContext timer speed score rows movingPiece'
  33. category: 'Examples'!
  34. !Tetris methodsFor: 'accessing'!
  35. width
  36. ^self class width
  37. !
  38. height
  39. ^self class height
  40. !
  41. squares
  42. ^self class squares
  43. !
  44. gluePiece: aPiece
  45. aPiece glueOn: self
  46. !
  47. rows
  48. "An array of rows. Each row is a collection of points."
  49. ^rows
  50. !
  51. addRow: aCollection
  52. self rows add: aCollection
  53. ! !
  54. !Tetris methodsFor: 'actions'!
  55. startNewGame
  56. self newGame.
  57. timer ifNotNil: [timer clearInterval].
  58. timer := [self nextStep] valueWithInterval: speed
  59. !
  60. nextStep
  61. movingPiece ifNil: [self newPiece].
  62. (movingPiece canMoveIn: self)
  63. ifTrue: [movingPiece position: movingPiece position + (0@1)]
  64. ifFalse: [self newPiece].
  65. self redraw
  66. !
  67. redraw
  68. renderingContext clearRectFrom: 0@ self width to: 0@ self height.
  69. self
  70. drawMap;
  71. drawPiece
  72. !
  73. drawMap
  74. renderingContext
  75. fillStyle: '#fafafa';
  76. fillRectFrom: 0@0 to: self width@self height.
  77. renderingContext
  78. lineWidth: 0.5;
  79. strokeStyle: '#999'.
  80. 0 to: self class squares x do: [:each | | x |
  81. x := each * self class squareSize.
  82. self drawLineFrom: x@0 to: x@self height].
  83. 0 to: self class squares y do: [:each | | y |
  84. y := each * self class squareSize.
  85. self drawLineFrom: 0@y to: self width@y].
  86. !
  87. drawLineFrom: aPoint to: anotherPoint
  88. renderingContext
  89. beginPath;
  90. moveTo: aPoint;
  91. lineTo: anotherPoint;
  92. stroke
  93. !
  94. newGame
  95. rows := #().
  96. movingPiece := nil.
  97. speed := 200.
  98. score := 0
  99. !
  100. newPiece
  101. movingPiece := TetrisPiece atRandom
  102. !
  103. drawRows
  104. self rows do: [:each |].
  105. movingPiece ifNotNil: [movingPiece drawOn: renderingContext]
  106. !
  107. drawPiece
  108. movingPiece ifNotNil: [
  109. movingPiece drawOn: renderingContext]
  110. ! !
  111. !Tetris methodsFor: 'initialization'!
  112. initialize
  113. super initialize.
  114. self newGame
  115. ! !
  116. !Tetris methodsFor: 'rendering'!
  117. renderOn: html
  118. html div
  119. class: 'tetris';
  120. with: [
  121. html h3 with: 'Tetris'.
  122. self renderCanvasOn: html.
  123. self renderButtonsOn: html]
  124. !
  125. renderCanvasOn: html
  126. | canvas |
  127. canvas := html canvas.
  128. canvas at: 'width' put: self width asString.
  129. canvas at: 'height' put: self height asString.
  130. renderingContext := CanvasRenderingContext tagBrush: canvas.
  131. self redraw
  132. !
  133. renderButtonsOn: html
  134. html div
  135. class: 'tetris_buttons';
  136. with: [
  137. html button
  138. with: 'New game';
  139. onClick: [self startNewGame].
  140. html button
  141. with: 'play/pause';
  142. onClick: [self update]]
  143. ! !
  144. !Tetris class methodsFor: 'accessing'!
  145. squareSize
  146. ^22
  147. !
  148. width
  149. ^self squareSize * (self squares x)
  150. !
  151. height
  152. ^self squareSize * (self squares y)
  153. !
  154. squares
  155. ^10@15
  156. ! !
  157. smalltalk.setup(smalltalk.Tetris);
  158. Widget subclass: #TetrisPiece
  159. instanceVariableNames: 'rotation position'
  160. category: 'Examples'!
  161. !TetrisPiece methodsFor: 'accessing'!
  162. rotation
  163. ^rotation ifNil: [rotation := 1]
  164. !
  165. rotation: aNumber
  166. rotation := aNumber
  167. !
  168. position
  169. ^position ifNil: [(Tetris squares x / 2) -1 @ 0]
  170. !
  171. position: aPoint
  172. ^position := aPoint
  173. !
  174. bounds
  175. self subclassResponsibility
  176. !
  177. color
  178. ^'#afa'
  179. !
  180. height
  181. ^2
  182. ! !
  183. !TetrisPiece methodsFor: 'drawing'!
  184. drawOn: aRenderingContext
  185. aRenderingContext fillStyle: self color.
  186. self bounds do: [:each |
  187. aRenderingContext
  188. fillRectFrom: each + self position* Tetris squareSize to: 1@1 * Tetris squareSize;
  189. strokeStyle: '#999';
  190. lineWidth: 2;
  191. strokeRectFrom: each + self position* Tetris squareSize to: 1@1 * Tetris squareSize]
  192. ! !
  193. !TetrisPiece methodsFor: 'testing'!
  194. canMove
  195. ^self position y < (Tetris squares y - self height)
  196. !
  197. canMoveIn: aTetris
  198. ^self position y < (aTetris squares y - self height)
  199. ! !
  200. !TetrisPiece class methodsFor: 'instance creation'!
  201. atRandom
  202. ^(self subclasses at: self subclasses size atRandom) new
  203. ! !
  204. smalltalk.setup(smalltalk.TetrisPiece);
  205. TetrisPiece subclass: #TetrisPieceO
  206. instanceVariableNames: ''
  207. category: 'Examples'!
  208. !TetrisPieceO methodsFor: 'accessing'!
  209. bounds
  210. ^Array new
  211. add: 0@0;
  212. add: 0@1;
  213. add: 1@0;
  214. add: 1@1;
  215. yourself
  216. ! !
  217. smalltalk.setup(smalltalk.TetrisPieceO);
  218. TetrisPiece subclass: #TetrisPieceL
  219. instanceVariableNames: ''
  220. category: 'Examples'!
  221. !TetrisPieceL methodsFor: 'accessing'!
  222. bounds
  223. ^Array new
  224. add: 0@0;
  225. add: 0@1;
  226. add: 0@2;
  227. add: 1@2;
  228. yourself
  229. !
  230. color
  231. ^'#ffa'
  232. !
  233. height
  234. ^3
  235. ! !
  236. smalltalk.setup(smalltalk.TetrisPieceL);
  237. TetrisPiece subclass: #TetrisPieceJ
  238. instanceVariableNames: ''
  239. category: 'Examples'!
  240. !TetrisPieceJ methodsFor: 'accessing'!
  241. color
  242. ^'#aaf'
  243. !
  244. bounds
  245. ^Array new
  246. add: 1@0;
  247. add: 1@1;
  248. add: 1@2;
  249. add: 0@2;
  250. yourself
  251. !
  252. height
  253. ^3
  254. ! !
  255. smalltalk.setup(smalltalk.TetrisPieceJ);
  256. TetrisPiece subclass: #TetrisPieceI
  257. instanceVariableNames: ''
  258. category: 'Examples'!
  259. !TetrisPieceI methodsFor: 'accessing'!
  260. color
  261. ^'#faa'
  262. !
  263. bounds
  264. ^Array new
  265. add: 0@0;
  266. add: 0@1;
  267. add: 0@2;
  268. add: 0@3;
  269. yourself
  270. !
  271. height
  272. ^4
  273. ! !
  274. smalltalk.setup(smalltalk.TetrisPieceI);
  275. TetrisPiece subclass: #TetrisPieceT
  276. instanceVariableNames: ''
  277. category: 'Examples'!
  278. !TetrisPieceT methodsFor: 'accessing'!
  279. bounds
  280. ^Array new
  281. add: 0@0;
  282. add: 1@0;
  283. add: 2@0;
  284. add: 1@1;
  285. yourself
  286. !
  287. color
  288. ^'#aaf'
  289. ! !
  290. smalltalk.setup(smalltalk.TetrisPieceT);