D-Bug & Automation Forum | |
D-Bug & Automation Forum >> Coding >> Sauron's homework! (assignment #1 - bouncy)
http://d-bug.mooo.com/dbugforums/cgi-bin/yabb2/YaBB.pl?num=1304238634 Message started by ggn on 01.05.11 at 08:30:33 |
Title: Re: Sauron's homework! (assignment #1 - bouncy) Post by ggn on 07.05.11 at 16:49:35
Well, it took long for someone to reply here :).
Of course it's all very good and fine. There is one small potential bug there, that is if the ball touches 2 borders at once it'll fail to flip both directions. In any case, I'll just add some variations to this to make some other points. First in line is, how to make it run faster. Now, of course it's a bit moot to speed up such a small routine, but it's mostly to give some ideas. So here's a faster version: [code]ball_x dc.l 160 ; ball x position ball_y dc.l 120 ; ball y position x_direction dc.l 1 ; vertical direction of ball y_direction dc.l 1 ; horizontal direction of ball topborder dc.l 0 ; sets top border bottomborder dc.l 240 ; sets bottom border leftborder dc.l 0 ; sets left border rightborder dc.l 320 ; sets right border start: movem.l ball_x(pc),d0-d3 ;load ball_x in d0, ball_y in d1, x_direction in d2 and y_direction in d3 sub.l d2,d0 ; moves ball left by one pixel add.l d3,d1 ; moves ball down by one pixel moveball: move.l d0,ball_x ; updates ball_x variable move.l d1,ball_y ; updates ball_y variable cmp.l leftborder,d0 ; compares left border to x position bne.s .check_right ; branches if not equal to left border moveq #1,d2 ; then starts moving the ball right bra.s .check_top ; now go check for top/bottom .check_right: cmp.l rightborder,d0 ; compares right border to x position bne.s .check_top ; branches if not equal to right border moveq #-1,d2 ; then starts moving the ball left .check_top: cmp.l topborder,d1 ; compares top border to y position bne.s .check_bottom ; branches if not equal to top border moveq #1,d3 ; then moves the ball down bra.s .check_done .check_bottom: cmp.l bottomborder,d1 ; compares bottom border to y position beq.s .move_up ; branches if equal to bottom border moveq #-1,d3 ; then moves the ball up .check_done: movem.l d0-d3,ball_x rts ;all done, thank you, drive thru![/code] A bit different, isn't it? :) Now, let me comment my changes a bit.
|
D-Bug & Automation Forum » Powered by YaBB 2.6.0! YaBB Forum Software © 2000-2024. All Rights Reserved. |