Similar presentations

Copyright © 2005, Richard A. DeVenezia. All rights reserved. Topics to be Covered n Invention n Pieces n Game Play n Movement Keys n Layout n Timing Loop

Copyright © 2005, Richard A. DeVenezia. All rights reserved. Inventor n Alexey Pajitnov n 1985 n World wide craze More dangerous than a Rubik’s Cube n Still popular

Copyright © 2005, Richard A. DeVenezia. All rights reserved. Drawing a block n DATA Step - DSGI n 45 degree lines left side, then bottom side color varies by linear interpolation

Copyright © 2005, Richard A. DeVenezia. All rights reserved. Saving a block n goptions device=GIF xpixels=30 ypixels=30; n rc = graph (‘CLEAR’, “E”);...... rc = gset ('COLREP', cindex, acolor); rc = gset ('LINCOLOR', cindex); rc = gdraw ('LINE', 2, x1,x2, y1,y2);... rc = graph (‘UPDATE’); * creates E.GIF; n PIECE.CLASS uses 4 block images

Copyright © 2005, Richard A. DeVenezia. All rights reserved. Game Play n Pieces drop by gravity n Player can move, rotate or drop pieces Within the grid n Can’t move? Locked piece Check for completed rows n Game over? Any part of locked piece outside the grid

Copyright © 2005, Richard A. DeVenezia. All rights reserved. Piece Class n Adapts to piece type A,B,C,... arrays define geometry private num D [ 4, 3, 3 ] / ( initialValue = {.,.,., 1, 2, 3,.,., 4,., 1,.,., 2,., 4, 3,., 4,.,., 3, 2, 1,.,.,.,., 3, 4,., 2,.,., 1,. }); n One array per piece 4 states of rotation 4 blocks per piece values define block layout 1, 2, 3 4 1, 2, 3, 4, 3, 2, 1 4, 4 3, 2, 1,

Copyright © 2005, Richard A. DeVenezia. All rights reserved. Piece Class - attributes Track state block[4]- image viewers field- grid (locked blocks) piece- A,B,C... state- rotation state 1..4 side- dimension of side (px) gravity- how much to drop (px) timeout- when gravity next occurs baseSleep - event checking interval x,y,row,col - position of piece on field n Where, when, how, what is going on

Copyright © 2005, Richard A. DeVenezia. All rights reserved. Piece Class - methods Attribute driven methods setcam* - x,y,row,col Behaviors right - move blocks of piece left down drop rotate lock Misc blockRowOf(y), topOfRow(n) - support geometry tests

Copyright © 2005, Richard A. DeVenezia. All rights reserved. HOLD ON, WAIT a SECOND! n Piece.wait() Important and essential Wait for player to do something n Does what needs doing while the player does nothing Drop a little (gravity) Play a blip sound

Copyright © 2005, Richard A. DeVenezia. All rights reserved. The Waiting Game wait: method return=num dt = datetime(); do until EVENT(); do while (dt =timeout) then do;... move down, play sound, timeout+X... return 1 if locked, -1 if game over end; end; return 0; * event needs servicing;

Copyright © 2005, Richard A. DeVenezia. All rights reserved. What caused the event? n A Text Entry Control Field where user types in 4 5 6 left rotate right 2 drop n Has overrides kbd.keyFeedback = ‘Yes’ kbd. _setInstanceMethod ('_onKey', 'Playmethods', 'onkey');

Copyright © 2005, Richard A. DeVenezia. All rights reserved. Playmethods.scl:onKey n Dispatches keypress handlers piece = _self_.piece; select (upcase(_self_.text)); when ('4') piece.left(); when ('5') piece.rotate(); when ('6') piece.right(); when ('2') piece.drop(); when ('D') piece.drop(); when ('Q') field.gameover = 1; otherwise ; end; n AND...

Copyright © 2005, Richard A. DeVenezia. All rights reserved. AND... Re-enters the wait method wait: rc = piece.wait(); if rc > 0 then do;... is game over ?... if not, lock the piece and randomly select a new one _self_.piece = _new_ Piece (field, byte(41x + ranuni(0)*7); goto wait; end; * code reaches here if EVENT() is pending; endmethod;

Copyright © 2005, Richard A. DeVenezia. All rights reserved. BUT... n Pending event (by design) is a keyPress n SAS/AF Executor, event handling Invokes onKey of Text Entry SAS/AF event supervisor Text Entry onKey() piece.wait() for event() Text Entry key pressed Control Loop