Re: Bouncing Ball?

new topic     » goto parent     » topic index » view thread      » older message » newer message

David Roach wrote:


> Could someone give me an idea as to how to get a ball
> to bounce around the screen in Windows. Everything I
> try just doesn't seem to work.

I wasn't sure which you wanted, so here are two: one that bounces in a
window, and the other that bounces in the window screen. Note that the first
on really should use double buffering.

Hope this helps!

-- David Cuny

-- VERSION 1: BOUNCES IN WINDOW --

include win32lib.ew

constant
Win = create( Window, "Bounce Demo", 0, Default, Default, 200, 100, 0 ),
BallSize = 20,      -- size of ball
BallRate = 2        -- smoothness of motion

integer x, y, dx, dy

-- ball initial position
x = 0
y = 0

-- ball initial vector
dx = rand(3)*BallRate
dy = rand(3)*BallRate

-- tick early and often
setTimer( Win, 1, 1 )

procedure Tick( integer timer )
    sequence size

    -- move ball
    x += dx
    y += dy

    -- get window size
    size = getClientRect( Win )

    -- hit top or bottom?
    if x < size[1] then
        -- don't go past top
        x = size[1]
        -- change direction
        dx = rand(3)*BallRate
    elsif x+BallSize > size[3] then
        -- don't go past bottom
        x = size[3]-BallSize
        -- change direction
        dx = -rand(3)*BallRate
    end if

    -- hit left or right?
    if y < size[2] then
        -- don't go past left
        y = size[2]
        -- change direction
        dy = rand(3)*BallRate
    elsif y+BallSize > size[4] then
        -- don't go past right
        y = size[4]-BallSize
        -- change direction
        dy = -rand(3)*BallRate
    end if

    -- erase window
    repaintWindow( Win )

    -- draw ball
    drawEllipse( Win, True, x, y, x+BallSize, y+BallSize )

end procedure
onTimer[Win] = routine_id("Tick")

WinMain( Win, Normal )

-- VERSION 2: BOUNCES IN SCREEN

include win32lib.ew

constant
Win = create( Window, "Bounce Demo", 0, Default, Default, 200, 100, 0 ),
Pix = create( Pixmap, "", 0, 0, 0, BallSize, BallSize, 0 ),
BallSize = 20,      -- size of ball
BallRate = 2        -- smoothness of motion

integer x, y, dx, dy

-- ball initial position
x = 0
y = 0

-- copy area behind ball
bitBlt( Pix, 0, 0, Screen, x, y, BallSize, BallSize, SRCCOPY )

-- ball initial vector
dx = rand(3)*BallRate
dy = rand(3)*BallRate

-- tick early and often
setTimer( Win, 1, 1 )

procedure Tick( integer timer )
    sequence size

    -- restore area behind ball
    copyBlt( Screen, x, y, Pix )

    -- move ball
    x += dx
    y += dy

    -- get window size
    size = getSize( Screen )

    -- hit top or bottom?
    if x < size[1] then
        -- don't go past top
        x = size[1]
        -- change direction
        dx = rand(3)*BallRate
    elsif x+BallSize > size[3] then
        -- don't go past bottom
        x = size[3]-BallSize
        -- change direction
        dx = -rand(3)*BallRate
    end if

    -- hit left or right?
    if y < size[2] then
        -- don't go past left
        y = size[2]
        -- change direction
        dy = rand(3)*BallRate
    elsif y+BallSize > size[4] then
        -- don't go past right
        y = size[4]-BallSize
        -- change direction
        dy = -rand(3)*BallRate
    end if

    -- copy area ball will cover
    bitBlt( Pix, 0, 0, Screen, x, y, BallSize, BallSize, SRCCOPY )

    -- draw ball
    drawEllipse( Screen, True, x, y, x+BallSize, y+BallSize )

end procedure
onTimer[Win] = routine_id("Tick")

WinMain( Win, Normal )

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu