Skip to content

Instantly share code, notes, and snippets.

@mumez
Created May 17, 2013 06:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mumez/5597283 to your computer and use it in GitHub Desktop.
Save mumez/5597283 to your computer and use it in GitHub Desktop.
EnchantFromAmber shooting game example
EcSprite subclass: #EcExampleApple
instanceVariableNames: ''
package: 'Enchant-Examples'!
!EcExampleApple methodsFor: 'initialization'!
initialize
self extent: 16@16.
self imageName: 'icon0.png'.
self frame: 15.
! !
EcSprite subclass: #EcExampleEnemyBear
instanceVariableNames: ''
package: 'Enchant-Examples'!
!EcExampleEnemyBear methodsFor: 'initialization'!
initialize
self extent: 32@32.
self imageName: 'chara1.png'.
self frame: 15.
! !
EcSprite subclass: #EcExampleShooterBear
instanceVariableNames: ''
package: 'Enchant-Examples'!
!EcExampleShooterBear methodsFor: 'initialization'!
initialize
self extent: 32@32.
self imageName: 'chara1.png'.
self frame: 5.
! !
EcGameProgram subclass: #EcShootingGameExample
instanceVariableNames: 'score'
package: 'Enchant-Examples'!
!EcShootingGameExample methodsFor: 'accessing'!
score
^score
! !
!EcShootingGameExample methodsFor: 'actions'!
init
super init.
score := 0
!
prepareBeforeLoad
self game assetsBasePath: './game-img/'.
self game preloadAssets: #('chara1.png' 'icon0.png').
self game fps: 20.
!
prepareOnLoad
| scene bear enemy |
scene := self game rootScene.
bear := EcExampleShooterBear new.
scene add: bear.
bear tl delay: (self game fps * 20); then: [self stop].
scene onTouchMove: [:event | bear y: event localY].
scene tl
then: [
enemy := EcExampleEnemyBear new.
enemy scaleX: -1.
enemy moveTo: (self gameWidth@ self gameHeight atRandom).
enemy tl moveBy: ((self gameWidth + 40) negated@0) time: 160.
scene add: enemy
]; delay: 30; loop.
scene onTouchStart: [:event | | apple |
bear y: event localY.
apple := EcExampleApple new.
apple moveTo: (bear width@ (bear y + (bear height / 2))).
apple tl moveBy: (self gameWidth@0) time: 30.
apple onEnterFrame: [ | enemies|
enemies := scene entitiesOf: EcExampleEnemyBear.
apple intersectAnyOf: enemies ifTrue: [:enem |
scene remove: enem; remove: apple.
self incrementScore
]
].
scene add: apple
].
! !
!EcShootingGameExample methodsFor: 'private'!
incrementScore
score := score + 1
!
stop
window alert: ('Game Over!! Score: ', self score).
super stop
! !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment