Computer Science Canada

Realistic Movement & Player Control

Author:  Zasalamel [ Sun Dec 20, 2009 2:50 pm ]
Post subject:  Realistic Movement & Player Control

Most People know how to create moveable characters on the screen but may not know how to make it more realistic.
But if you do not know how to create a movable character then that is here too.

Author:  Zasalamel [ Sun Dec 20, 2009 4:11 pm ]
Post subject:  First the basic movement controls!!!



Here is how the tutorial will be posted:
- Below will be step by step code with comments in between the blocks of code
- At the end i will add attachments of uncommented and commented completed code files
- Also if you have any semblance of coding knowledge bear with me I go into some detail
- If you have any questions please post them...

First the screen...


Turing:


setscreen ("screen:max;max,offscreenonly")
             /* OR */
View.Set("graphics:500;500,offscreenonly")
            /* OR */
var win:= Window.Open ("graphics:max;max,offscreenonly")





These are the three most common window customizing commands
View.Set and setscreen are basically the same but will not let you control the window
var win := Window.Open will allow you to open and close the window
screen is setting the screen in inches ( screen:10;20 )
graphics sets the screen in pixels
max will automayically set the screen or graphics to fit the size of the monitor used.

Next you will need your variables

More Posted Later on the subject...

Turing:

var chars : array char of boolean
var x1 : int := 1
var x2 : int := x1 + 10
var y1 : int := 1
var y2 : int := y1 + 10
var xv : real := 0
var yv : real := 0
var g : real := 1
var ground : boolean := false

[size=16]
chars is the variable that will store the keys pressed
the x's are the x positions of your player ( we will be using a box as the player)
the y's are the y position of the player
the xv and yv are the velocities (speeds) of you player
g is the gravity this is just for convenience so you can change it at the top of the screen
ground is a boolean (true or false) that will be used to indicate that the player is touching a platform (this will be used only for multi-platform games)

Next is the main loop:

Turing:

loop
    Input.KeyDown (chars)

This gives a value of the keys being pressed to chars
Turing:

    if chars ('w') and y2 < maxy and ground = true then

this basically says if keys being pressed equals (w key on keyboard) and and the player is on the screen and touching the ground then do this:
Turing:

        yv += 15

this will make the player jump by adding to the y velocity
Turing:

    elsif chars ('a') and x1 > 0 then

else if key being pressed equals (a key on keyboard) and the box is on the screen then do this:
Turing:

        xv -= 1

This subtracts 1 from the players y velocity (which will make the player move left)
Turing:

    elsif chars ('d') and x2 < maxx then

if key pressed equals (d on keyboard) then
Turing:

        xv += 1

add 1 to x velocity to move the player right
Turing:

    end if
    if y1 > 0 then

if the bottom of the box not touching the ground then
Turing:

        yv -= g

subtract gravity from the velocity until the player moves down and touches the ground
Turing:

    elsif y1 < 0 then

if the bottom of the box is below the screen then
Turing:

        yv := 0

stop the box
Turing:

        y1 := 0

move the box to the bottom of the screen
Turing:

    end if
    if xv > 0 then

if the box is moving then
Turing:

        xv -= 0.5
    elsif xv < 0 then

slow it down
Turing:

        xv += 0.5
    end if
    if x1 < 0 then

if the box has gone off the left side of the screen then
Turing:

        x1 := 0

move the box to the edge of the screen
Turing:

        xv := 0

stop the box
Turing:

    elsif x2 > maxx then

if the box has passed the right side of the screen then
Turing:

        x1 := maxx - 10

move the box to the edge of the screen
Turing:

        xv := 0

and stop the box
Turing:

    end if
    if y2 > maxy then

if the box has passed the top of the screen then
Turing:

        y1 := maxy - 10

move it to the top of the screen
Turing:

        yv := 0

stop the box
Turing:

    end if
    if y1 = 0 then

if the box is touching the bottom of the screen
Turing:

        ground := true

ground = true (thus allowing the player to jump
Turing:

    else

otherwise
Turing:

        ground := false

box cannot jump
Turing:

    end if


Turing:

    x1 += round (xv)

adds the velocity to the x position to create movement
*side note - xv needs to be rounded because it is not an integer but a real( or a decimal) round will convert xv to integer
Turing:

    y1 += round (yv)

adds the velocity to the y position to create movement
*side note - yv needs to be rounded because it is not an integer but a real( or a decimal) round will convert yv to integer

Turing:

    x2 := x1 + 10

the right side of the box = the left side plus 10 pixels
This keeps the box the same size
Turing:

    y2 := y1 + 10

the top of the box = the bottom of the box plus 10 pixels
*keeps the box the same size
Turing:

    Draw.FillBox (x1 - 1, y1 - 1, x2 + 1, y2 + 1, grey)
    Draw.FillBox (x1, y1, x2, y2, black)

This Draws our player
Turing:

    View.Update

brings the drawn images to the screen
Turing:

    delay (10)

makes the controls less responsive and easier to control
Turing:

    cls

erases everything on the screen
* so only the currently drawn box will show
Turing:

end loop

More will be posted on the subject later

    
Zasalamel

Author:  SNIPERDUDE [ Sun Dec 20, 2009 6:05 pm ]
Post subject:  RE:Realistic Movement & Player Control

For those who want the code all in one place (for a nice copy and paste):

Turing:
var chars : array char of boolean
var x1 : int := 1
var x2 : int := x1 + 10
var y1 : int := 1
var y2 : int := y1 + 10
var xv : real := 0
var yv : real := 0
var g : real := 1
var ground : boolean := true

setscreen ("graphics:450;450,offscreenonly,nobuttonbar,title:Player Movement Tutorial")

loop
    Input.KeyDown (chars)
   
    if chars ('w') and y2 < maxy and ground = true then
        yv += 8
    end if
    if chars ('a') and x1 > 0 then
        xv -= 1
    elsif chars ('d') and x2 < maxx then
        xv += 1
    elsif chars (KEY_ESC) then
        exit
    end if
   
    if y1 > 0 then
        yv -= g
    elsif y1 < 0 then
        yv := 0
        y1 := 0
    end if
   
    if xv > 0 then
        xv -= 0.5
    elsif xv < 0 then
        xv += 0.5
    end if
   
    if x1 < 0 then
        x1 := 0
        xv := 0
    elsif x2 > maxx then
        x1 := maxx - 10
        xv := 0
    end if
   
    if y2 > maxy then
        y1 := maxy - 10
        yv := 0
    end if
   
    if y1 = 0 then
        ground := true
    else
        ground := false
    end if
   
    x1 += round(xv)
    y1 += round(yv)
    x2 := x1 + 10
    y2 := y1 + 10
   
    Draw.FillBox (x1, y1, x2, y2, black)
    Draw.Box (x1 - 1, y1 - 1, x2 + 1, y2 + 1, grey)
    locate(1, 1)
   
    View.Update
    delay (10)
    cls
end loop


A couple things I changed:
- There was a bug with jumping, the character kept getting stuck under 0 y, and thus couldn't jump (the velocity was automatically set back down to zero without moving the character)
- I split the if statements for 'w' and the rest so you can both jump and move at the same time
- I added an end using the ESC key.
- I minimized the jump height so it looks about average for a side-scrolling game
- I changed the setscreen and just added a title (and got rid of the buttons on top, since you can exit with the ESC key now). Also, I don't see any need to go into windows for this tutorial, just using the default will work fine for now (unless you get into multi-window games [ex: one window for gameplay, one for a map. Or two different windows for two different players {same keyboard}, one for a minimap]).

Good post, I'm sure this will be useful for some.

Author:  Zasalamel [ Sun Dec 20, 2009 6:17 pm ]
Post subject:  RE:Realistic Movement & Player Control

Keep in mind that this was just the very basic version of the tutorial With Improvements Later On.
oh and the Window.Open is useful for the ESC key (ie:
Turing:

if chars (KEY_ESC) then
exit
Window.Close(win)
end if

it looks neater and more professional

Author:  Tony [ Sun Dec 20, 2009 6:55 pm ]
Post subject:  Re: RE:Realistic Movement & Player Control

Zasalamel @ Sun Dec 20, 2009 6:17 pm wrote:

Turing:

if chars (KEY_ESC) then
exit
Window.Close(win)
end if


Window.Close(win) will never execute.

Author:  Zasalamel [ Sun Dec 20, 2009 8:58 pm ]
Post subject:  RE:Realistic Movement & Player Control

oops i was typing to fast i guess

Author:  andrew. [ Sun Dec 20, 2009 11:06 pm ]
Post subject:  RE:Realistic Movement & Player Control

Here's what it should be:
Turing:
if chars (KEY_ESC) then
    Window.Close(win)
    exit
end if

Author:  TheGuardian001 [ Sun Dec 20, 2009 11:40 pm ]
Post subject:  Re: First the basic movement controls!!!

Zasalamel @ Sun Dec 20, 2009 4:11 pm wrote:

screen is setting the screen in inches ( screen:10;20 )

No it isn't.

screen and text modes both set the window in terms of rows and columns, based on the character sizes used by the put command.

Just thought I'd point that out.

Author:  Zasalamel [ Mon Dec 21, 2009 3:01 pm ]
Post subject:  RE:Realistic Movement & Player Control

Thank you TheGuardian001 i was not sure. ( rows and columns do make more sense)

Author:  livingheaven [ Sat Jan 09, 2010 10:02 pm ]
Post subject:  RE:Realistic Movement & Player Control

Great tutorial =D best so far

Author:  blizzard4800 [ Sun Jan 17, 2010 10:57 am ]
Post subject:  RE:Realistic Movement & Player Control

How do u make more than one platform

Author:  Turing_Gamer [ Sun Jan 17, 2010 3:31 pm ]
Post subject:  Re: Realistic Movement & Player Control

Here you would have to array the platform pictures, like so...
Turing:
var platform := Pic.FileNew ("Platform Picture.jpg")
Pic.SetTransparentColor (platform, brightgreen)
var xcoord, ycoord : array 1 .. 5 of int
xcoord (1) := 100
xcoord (1) := 100
xcoord (2) := 100
xcoord (2) := 100
% etc...

Pic.Draw (platform, xcoord (1), ycoord (1), picMerge)
Pic.Draw (platform, xcoord (2), ycoord (2), picMerge)
% etc...

Author:  SNIPERDUDE [ Sun Jan 17, 2010 4:02 pm ]
Post subject:  Re: Realistic Movement & Player Control

I'm sure the question was regarding collision detection regarding platforms as opposed to drawing multiple platforms.

I'd look into the whatdotcolour and the collision detection tutorials on the Turing Walkthrough. (<- Link provided)

Author:  evildaddy911 [ Thu Dec 08, 2011 11:43 am ]
Post subject:  RE:Realistic Movement & Player Control

looking through, I think that you should add a velocity cap, also, if you are using real variables for more realistic movement, make the x and y coords real too, the round() should be at the draw command

Author:  Alex C. [ Wed Jan 11, 2012 10:15 pm ]
Post subject:  RE:Realistic Movement & Player Control

thats great, but if you could add wall jumping into your tutorial i feel that it would be better Wink

Author:  evildaddy911 [ Sun May 06, 2012 10:37 am ]
Post subject:  Re: Realistic Movement & Player Control

Turing:
if chars('w') then
     if x1=0 then
          yv+=5
          xv+=2
     elsif x2=maxx then
          yv+=5
          xv-=2
     end if
end if

add this in if you want wall jumping
not sure how long constitutes necroing... sorry if 4 months counts

Author:  Raknarg [ Sun May 06, 2012 2:40 pm ]
Post subject:  RE:Realistic Movement & Player Control

It's only necroing if what you're putting up isn't useful, like how some people might post "Oh thats really cool" on a two year old post.

Author:  eddie2222 [ Thu Jun 07, 2012 11:42 am ]
Post subject:  RE:Realistic Movement & Player Control

hey can you show me how to make a platform using the drawfill option so the box can actually stand and jump off of it thanks Very Happy

Author:  Raknarg [ Thu Jun 07, 2012 1:06 pm ]
Post subject:  RE:Realistic Movement & Player Control

1. Make your own topic for this sortof this please
2. HAve you looked into whatdotcolour before?


: