[GOODIE]BlackSheep - a simple game

Karl Ramberg karl.ramberg at chello.se
Sat May 20 14:52:37 UTC 2000


Hello,
Here is a simple game I've put toghether. It's probably one of
the least fun games ever but I would like if someone
could comment on the way it's written, since it's one of my first
Squeak programs and I would like to develop a good coding style :-)

Karl

PS: Tested on latest 2.8a image. Runs in Morphic:
WorldMenu/NewMorph/Games/BlackSheep


-------------- next part --------------
'From Squeak2.8alpha of 19 February 2000 [latest update: #2158] on 20 May 2000 at 4:43:45 pm'!
RectangleMorph subclass: #BlackSheep
	instanceVariableNames: 'theLed mover sheep jumpTimer score life lifeDisplay theBackground '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Morphic-Games'!

!BlackSheep methodsFor: 'initialization' stamp: 'kfr 5/16/2000 15:39'!
addCloseBox
	| closeBox |
	closeBox _ SimpleButtonMorph new borderWidth: 1;borderColor: #raised;
			cornerStyle: #square;
			label: '' font: Preferences standardButtonFont; color: Color darkGray;
			actionSelector: #delete; target: self; extent: 10 at 10.
	closeBox addMorph: (StringMorph new contents: 'x'; font:(StrikeFont familyName: #ComicPlain size: 16); position: 2 @ -5; color: Color black).
	self addMorph: (closeBox position: 8 at 5)! !

!BlackSheep methodsFor: 'initialization' stamp: 'kfr 5/20/2000 16:42'!
addGamePieces
self addMorph:(Morph new color:Color white; position:9 at 180;extent: 180 at 24 ).
self addMorph:(mover _ Morph new color:Color white; position:179 at 150; extent: 10 at 30 ).


self addMorph:(sheep _ Morph new color:Color black; position:40 at 165;extent: 15 at 15 ).! !

!BlackSheep methodsFor: 'initialization' stamp: 'kfr 5/20/2000 13:04'!
initialize
	
	| moveBar |
	super initialize.
	jumpTimer _ score _ life _ 0.
	self extent: 200 at 300; color: Color gray; borderColor: #raised; borderWidth: 3; useRoundedCorners; beSticky.
	self addCloseBox.
	 moveBar _ RectangleMorph new extent: 164 at 10; color: Color darkGray; borderColor: #raised; borderWidth: 1; beSticky; position: 26 at 5.
	moveBar eventHandler: (EventHandler new on: #mouseDown send: #move to: self).
	self addMorph: moveBar.
	self addMorph: (theBackground _ RectangleMorph new extent: 182 at 182; color: (Color r:0.0 g: 0.6 b: 0.0); borderColor: #inset; borderWidth: 1; beSticky; position: 8 at 23).
	self addMorph: (RectangleMorph new extent: 67 at 17; color: Color gray; borderColor: #inset; borderWidth: 1; beSticky; position:122 at 213).
 	self addMorph: (theLed _ LedMorph new extent: 65 at 15; position: 123 at 214; digits: 6).
	self addMorph: (SimpleButtonMorph new borderWidth: 1;borderColor: #raised;
			cornerStyle: #square;label: 'New Game' font: Preferences standardButtonFont; color: Color darkGray; actionSelector: #newGame; target: self; extent: 70 at 17;position: 8 at 213).
	self addMorph: (SimpleButtonMorph new borderWidth: 1;borderColor: #raised;cornerStyle: #square; label: 'Jump!!' font: Preferences standardButtonFont; color: Color darkGray;actWhen: #buttonDown; actionSelector: #jump; target: self; extent: 45 at 45;position: 76 at 240).
 self addGamePieces.
self addMorph:(StringMorph new position: 150 at 240; contents: 'Life:').
self addMorph:(lifeDisplay _ StringMorph new position: 180 at 240; contents: (life asString))
	
! !

!BlackSheep methodsFor: 'initialization' stamp: 'kfr 5/20/2000 13:44'!
jump
	life > 0
	ifTrue:[
	jumpTimer = 0
			ifTrue:[
	jumpTimer _ 10.
	sheep position:(sheep position - (0 at 40))]]! !

!BlackSheep methodsFor: 'initialization' stamp: 'kfr 5/20/2000 13:44'!
jumping 
	jumpTimer _ jumpTimer - 1.
	jumpTimer = 0 
		ifTrue:[sheep position:(sheep position + (0 at 40))]! !

!BlackSheep methodsFor: 'initialization' stamp: 'kfr 5/16/2000 15:45'!
move
	
		^  self activeHand grabMorph: self 
! !

!BlackSheep methodsFor: 'initialization' stamp: 'kfr 5/20/2000 13:40'!
newGame
	life _ 5.
  lifeDisplay contents: (life asString).
	score _ 0.
theLed value:score! !

!BlackSheep methodsFor: 'initialization' stamp: 'kfr 5/20/2000 13:44'!
step

life > 0
	ifTrue:[	
   | i |
	i _ (bounds topLeft).
mover position:( mover position - (4 at 0)).
	mover position <= ((i)+(9 at 150))
		ifTrue:[mover position: (i) + (179 at 150).
score _ score +1.
theLed value:score].
(sheep bounds intersects: mover bounds) 
		 ifTrue:[	
					mover position: (i) + (179 at 150).
					
					life _ life - 1.
					lifeDisplay contents: (life asString).
					(Delay forMilliseconds: 500) wait.
					].
	(jumpTimer > 0)
	ifTrue:[ self jumping]]
			! !

!BlackSheep methodsFor: 'initialization' stamp: 'kfr 5/19/2000 14:26'!
stepTime
	"Answer the desired time between steps in milliseconds."

	^ 30! !


More information about the Squeak-dev mailing list