John Fraser Computer Contest Club

Forums

Post Reply
Forum Home > Turing > Movement via velocity changes

Stanley
Moderator
Posts: 5

This form of movement changes the velocity. It will appear as if you have no control over the trajectory of the ball but you do. This is an example of what happens if you do not have someone to slow down the character or if you don't ensure that movement only occurs upon keystroke.


Why is this useful? 

At first this would seem unreasonable as the character can not stop. For some games (like snake) this may be true. However, the reasoning behind this can be used for functions in which cannot have a zero velocity (or no change in cords, such as the ribbon game where you float up and down to avoid obstacles, if there were an option to reach zero velocity and remain stable that would be pointless)


Here is the code:


 

setscreen ("graphics")

setscreen ("offscreenonly")

var x : int := 100

var y : int := 100

var x_velocity : int := 0

var y_velocity : int := 0

var keys : array char of boolean

 

loop

cls

Input.KeyDown (keys)

if keys (KEY_UP_ARROW) and y_velocity < 6 then %makes sure the character won't go past the top of the screen

y_velocity += 1 %See here, we are changing the velocity instead of the coordinates

end if

 

if keys (KEY_DOWN_ARROW) and y_velocity > -5 then % we also set the max speed here (5)

y_velocity -= 1

end if

 

if keys (KEY_LEFT_ARROW) and x_velocity > -5 then

x_velocity -= 1

end if

 

if keys (KEY_RIGHT_ARROW) and x_velocity < 5 then

x_velocity += 1

end if

 

if y >= maxy or y <= 0 then

y_velocity *= -1 %If the ball hits a wall, the velocity will reverse, 'bouncing' the ball the other way

end if

if x >= maxx or x <= 0 then

x_velocity *= -1

end if

y += y_velocity % this is where the ball's X and Y values are actually changed.

x += x_velocity

 

 

Draw.FillOval (x, y, 10, 10, red)

delay (10)

View.Update

end loop

 


--

- Stanley

October 11, 2010 at 9:32 PM Flag Quote & Reply

You must login to post.