Skip to content

Chapter 2: Player Movement

Feluxus edited this page Jul 9, 2024 · 3 revisions

Level 01: Initialize Player

  • newly visible classes and methods:
    • Player *
      • initialize
      • readonly game getter/setter
    • Game
      • initializePlayers (use center over position)
      • readonly player left/right getter setter
  • Initial state:
    • getter/setter done already
    • rest is empty with comment -> maybe show how to init 1 player, maybe orient from initializeBall
  • Explain/Task:
    • Initialize the Player with color, extent and speed 0.
    • initialize Player from Game: set center and addMorph for both players
CodePieces:
  • IPTPlayer/initialize
initialize
°°°°°
2
1
°°
	"Please set the color to white, extent to 15x100 and speed to 0"
	super initialize.
°°
	super initialize.
	self 
		color: Color white;
		extent: (15@100);
		stop.
  • IPTGame/initializePlayers
initializePlayers
°°°°°
2
1
°°
	"Initialize the left and right player like you did with the ball. For each player, be sure to set the center, and set the game to self."
	| offset | 
	offset := (self extent x / 2 - 30 @ 0).
°°
	| offset | 
	offset := (self extent x / 2 - 30 @ 0).
	self
		playerLeft: (IPTPlayer new
			center: self center - offset;
			game: self);
		playerRight: (IPTPlayer new
			center:  self center + offset;
			game: self);
		addMorph: self playerLeft;
		addMorph: self playerRight.
  • IPTPlayer/game
game
°°°°°
2
1
°°
°°
	^ game.
  • IPTPlayer/game:
game: aGame
°°°°°
2
1
°°
°°
	game := aGame.
  • IPTGame/playerLeft
playerLeft
°°°°°
2
1
°°
°°
	^ playerLeft.
  • IPTGame/playerLeft:
playerLeft: aPlayer
°°°°°
2
1
°°
°°
	playerLeft := aPlayer.
  • IPTGame/playerRight
playerRight
°°°°°
2
1
°°
°°
	^ playerRight.
  • IPTGame/playerRight:
playerRight: aPlayer
°°°°°
2
1
°°
°°
	playerRight := aPlayer.

Level 02: Player Movement

  • newly visible classes and methods:
    • Player
      • up / down / stop
      • readonly defaultSpeed
      • readonly speed getter/setter
      • step
      • move (rework complicated statement to clamp)
    • Game
      • readonly initializePlayer -> up
  • Initial state:
    • default speed is constant
    • speed getter/setter given
    • initializePlayer sends players up
  • Explain/Task:
    • set speed in up/down to default speed (negated) and to 0 in stop
    • move: calc and set new position and clamp to game height - player height
    • step: move with speed
CodePieces:
  • IPTPlayer/up
up
°°°°°
2
2
°°
	"Set the speed to negated defaultSpeed."
°°
	self speed: self defaultSpeed negated.
  • IPTPlayer/down
down
°°°°°
2
2
°°
	"Set the speed to defaultSpeed."
°°
	self speed: self defaultSpeed.
  • IPTPlayer/stop
stop
°°°°°
2
2
°°
	"Set the speed to 0."
°°
	self speed: 0.
  • IPTPlayer/defaultSpeed
defaultSpeed
°°°°°
2
2
°°
°°
	^ 10.
  • IPTPlayer/speed
speed
°°°°°
2
2
°°
°°
	^ speed.
  • IPTPlayer/speed:
speed: aNumber
°°°°°
2
2
°°
°°
	speed := aNumber.
  • IPTPlayer/step
step
°°°°°
2
2
°°
	"Call move with the speed."
°°
	self move: self speed.
  • IPTPlayer/move
move: aNumber
°°°°°
2
2
°°
	"Add the number to our center. Clamp the new center y component to the game border by using this offset."
	| offset |
	offset := self height * 0.5.
°°
	| offset |
	offset := self height * 0.5.
	self center: self center 
		+ (0 @ (aNumber min: offset max: (self game height - offset))).
  • IPTGame/initializePlayers

Level 03: Keyboard input

  • newly visible classes and methods:
    • Game
      • (mouseEnter: / Leave:) leave out for now
      • keyUp: / Down:
      • handlesKeyboad:
  • Initial state:
    • all empty
  • Explain/Task:
    • keyDown: move respective player up/down depending on key
      • left with w/s, right with arrows
      • explain how to get the key e.g. $W, $S, Character arrowUp
      • ifTrue if not explained yet
    • keyUp: stop respective Player
      • use "or" or collection comparison like anySatisfy
      • ifTrue