Download presentation
Presentation is loading. Please wait.
Published byMelinda Bond Modified over 9 years ago
1
Teaching Loops Facilitated by introducing simple threads Few interesting loop examples before arrays –Boring calculations –Interaction –Drawing –Animation Bad idea to postpone loops until arrays
2
Simple While Loops Repetition in drawing –Grass, bricks –Laundry basket grassX = 0; while ( grassX < SCREEN_WIDTH ){ new Line(grassX, GR_BASE, grassX, GR_TOP, canvas).setColor(GRASSGREEN);grassX = grassX + GR_SPACING; }
3
Relate Loops to Familiar Concepts int grassX = 0; public void onMousePress (Location pt){ if ( grassX < SCREEN_WIDTH ){ new Line(grassX, GR_BASE, grassX, GR_TOP, canvas).setColor(GRASSGREEN);grassX = grassX + GR_SPACING; }
4
More Simple While Loops Animation Balls (leaves, snow) fall down Cars move left to right (or vice versa) Balls bounce Exercise: other examples that don’t require arrays?
5
Animations Natural way to introduce looping Interesting to students Requires introduction of threads –Introduction of threads at this stage sounds crazy! –But a natural way to get sophisticated behaviors with fairly simple programs.
6
Active Objects Students extend ActiveObject Syntax simplified ActiveObjects (Threads) registered for appropriate clean-up.
7
ActiveObject Methods –public void run() - overridden by students –public void start() - called by students –public void pause(double) - exceptionless “start()” at end of constructor
8
A Simple ActiveObject public FallingBall(DrawingCanvas canvas) { ball = new FilledOval(SCREENWIDTH/2,TOP, BALLSIZE, BALLSIZE, canvas); start(); } public void run() { while (ball.getY() < BOTTOM ) { ball.move(0, Y_SPEED); pause(DELAY_TIME); } ball.hide(); }
9
Interactions Interactions between ActiveObjects and other objects yield more interesting loop conditions. With single non-active object Between different active objects –Active objects as generators of others –More complex interactions
10
Interactions Pong Falling snow (or leaves) Hungry ball
11
Boxball: a Simple First Exercise Falling ball is familiar Interaction with a single non-active object (the box) Opportunity for thinking about parameters
12
Frogger: a Second Exercise Purpose of lab: to consider a variety of simple interactions that appear quite sophisticated. Lanes create cars Cars know about frog Frog knows whether its dead or alive Most difficult component of the exercise is management of parameters.
13
Additional topics Smooth animation –Interesting –Necessary for Frogger Visible Images –Many more interesting examples –Necessary for Frogger –Good time with respect to our syllabus, but could be done earlier
14
A Gentle Reminder Don’t compare threads to “the old way” Many simple but interesting examples… but some simple examples aren’t so simple
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.