Re: Chock full o' bugs
- Posted by Brian Broker <bkb at CNW.COM> Sep 22, 2000
- 479 views
On Thu, 21 Sep 2000 21:02:14 -0700, Thomas wrote: >I've written a program I'd like someone too look over. At the moment, it's >a hairy mess.The objective was to have a little tank that fired a beam >that came from the tip of the turret. The user could move the turret too. >Could you tell me what went wrong or if it's less work re-write the >program so I can see what a 'clean' version looks like? Thanks OK Thomas, this is what I've come up with. I started with your original program so it's a bit hacked together and mostly redone. There is much room for improvement and features IMO. I'll leave that to you... Your first assignment is figuring out how to control the bullet because I think there are times where you can't hit the target (you'll be either too high or too low). I apologize for lack of comments but I try to name my variables informatively and do things such that I hope it's self-explanatory. So without further ado... -- Sha-boom.ex -- started by Thomas -- tinkered with by Brian include graphics.e include image.e include get.e constant X = 1, Y = 2, UPPER = 1, LOWER = 2, TRUE = 1, FALSE = 0, MAX = {320,200} -- screen size for mode 19 integer gmode, inkey gmode = graphics_mode(19) sequence turret_p1, turret_p2, turret_limit, target -------------------- procedure wait( atom pinterval ) atom endtime endtime= time()+pinterval while endtime > time() do end while end procedure -------------------- procedure draw_target() -- initialize target target = {0,0} -- set target target[X] = MAX[X] - 20 target[Y] = 10 + rand( MAX[Y] - 20 ) -- draw target ellipse( RED, 1, target-{6,10}, target+{6,10} ) ellipse( WHITE, 1, target-{3,5}, target+{3,5} ) end procedure -------------------- function target_hit( atom y ) if y > target[Y]-10 and y < target[Y]+10 then return TRUE else return FALSE end if end function -------------------- procedure draw_tank( integer x, integer y ) -- draw_tank ellipse( GREEN, 1, {x+2,y}, {x+12,y+10} ) polygon( GREEN, 1, {{x,y+5},{x+20,y+5},{x+20,y+10}, {x+15,y+15},{x+5,y+15},{x,y+10}} ) polygon( BROWN, 0, {{x,y+10},{x+20,y+10},{x+15,y+15},{x+5,y+15}} ) -- set turret_limit = { upper limit, lower limit } turret_limit = {y-8,y+3} -- draw turret turret_p1 = {x+11, y+1} turret_p2 = {x+20, y+1} draw_line(2,{turret_p1,turret_p2}) end procedure -------------------- function get_slope() return (turret_p2[Y] - turret_p1[Y]) / (turret_p2[X] - turret_p1[X]) end function -------------------- procedure explode() integer pix ellipse( BLACK, 1, target-{6,10}, target+{6,10} ) for y = -15 to 15 do for x = -15 to 15 do pix = rand(10)-8 if pix > 0 then if pix = 1 then pixel( RED, target + {x,y} ) else pixel( WHITE, target + {x,y} ) end if end if end for end for end procedure -------------------- procedure fire() integer lcolr sequence laser_p1, laser_p2, last_p1, last_p2 -- initialize last points to off-screen last_p1 = {-1,-1} last_p2 = {-1,-1} -- initialize start of laser to end of turret laser_p1 = turret_p2 while 1 do inkey = get_key() if inkey = 'q' then -- quit -- gmode = graphics_mode(-1) abort(0) end if wait( 0.005 ) lcolr = rand(15) laser_p2 = laser_p1 + {5, 5*get_slope()} draw_line( lcolr, {laser_p1,laser_p2} ) draw_line( BLACK, {last_p1, last_p2} ) last_p1 = laser_p1 last_p2 = laser_p2 laser_p1 = laser_p2 if laser_p2[X] > target[X] then if target_hit( laser_p2[Y] ) then puts(1,"\n\n SHA-BOOM!" ) explode() else puts(1,"\n\n MISSED!" ) end if return end if end while end procedure -------------------- -- MAIN PROGRAM LOOP -- while 1 do clear_screen() draw_target() draw_tank( 10, 135 + rand( MAX[Y]-155 ) ) while 1 do inkey = wait_key() if inkey = 'q' then -- quit -- gmode = graphics_mode(-1) abort(0) elsif inkey = 'u' then -- move turret up -- if turret_p2[Y] > turret_limit[UPPER] then draw_line(0,{turret_p1,turret_p2}) turret_p2[Y] -= 1 draw_line( GREEN,{turret_p1,turret_p2}) end if elsif inkey = 'd' then -- move turret down -- if turret_p2[Y] < turret_limit[LOWER] then draw_line(0,{turret_p1,turret_p2}) turret_p2[Y] += 1 draw_line( GREEN,{turret_p1,turret_p2}) end if elsif inkey = 'f' then -- fire! -- fire() -- play again? -- puts( 1, "\n Play Again? (y/n)" ) if wait_key() = 'y' then exit -- exit inner while loop else abort(0) -- exit program end if end if end while end while -- have fun!!! -- Brian