
-----------------------------------
Zasalamel
Sun Dec 20, 2009 2:50 pm

Realistic Movement &amp; 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.

-----------------------------------
Zasalamel
Sun Dec 20, 2009 4:11 pm

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...




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...


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


loop
    Input.KeyDown (chars)

This gives a value of the keys being pressed to chars

    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:

        yv += 15

this will make the player jump by adding to the y velocity

    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:

        xv -= 1

This subtracts 1 from the players y velocity (which will make the player move left)

    elsif chars ('d') and x2 < maxx then

if key pressed equals (d on keyboard) then

        xv += 1

add 1 to x velocity to move the player right

    end if
    if y1 > 0 then

if the bottom of the box not touching the ground then

        yv -= g

subtract gravity from the velocity until the player moves down and touches the ground

    elsif y1 < 0 then

if the bottom of the box is below the screen then

        yv := 0

stop the box

        y1 := 0

move the box to the bottom of the screen

    end if
    if xv > 0 then

if the box is moving then

        xv -= 0.5
    elsif xv < 0 then

slow it down

        xv += 0.5
    end if
    if x1 < 0 then

if the box has gone off the left side of the screen then

        x1 := 0

move the box to the edge of the screen

        xv := 0

stop the box

    elsif x2 > maxx then

if the box has passed the right side of the screen then

        x1 := maxx - 10

move the box to the edge of the screen

        xv := 0

and stop the box

    end if
    if y2 > maxy then

if the box has passed the top of the screen then

        y1 := maxy - 10

move it to the top of the screen

        yv := 0

stop the box

    end if
    if y1 = 0 then

if the box is touching the bottom of the screen

        ground := true

ground = true (thus allowing the player to jump

    else

otherwise

        ground := false

box cannot jump

    end if



    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

    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


    x2 := x1 + 10

the right side of the box = the left side plus 10 pixels
This keeps the box the same size

    y2 := y1 + 10

the top of the box = the bottom of the box plus 10 pixels
*keeps the box the same size

    Draw.FillBox (x1 - 1, y1 - 1, x2 + 1, y2 + 1, grey)
    Draw.FillBox (x1, y1, x2, y2, black)

This Draws our player

    View.Update

brings the drawn images to the screen

    delay (10)

makes the controls less responsive and easier to control

    cls

erases everything on the screen
* so only the currently drawn box will show

end loop

More will be posted on the subject later

Zasalamel

-----------------------------------
SNIPERDUDE
Sun Dec 20, 2009 6:05 pm

RE:Realistic Movement &amp; Player Control
-----------------------------------
For those who want the code all in one place (for a nice copy and paste):

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.

-----------------------------------
Zasalamel
Sun Dec 20, 2009 6:17 pm

RE:Realistic Movement &amp; 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:

if chars (KEY_ESC) then
exit
Window.Close(win)
end if

it looks neater and more professional

-----------------------------------
Tony
Sun Dec 20, 2009 6:55 pm

Re: RE:Realistic Movement &amp; Player Control
-----------------------------------


if chars (KEY_ESC) then
exit
Window.Close(win)
end if


Window.Close(win) will never execute.

-----------------------------------
Zasalamel
Sun Dec 20, 2009 8:58 pm

RE:Realistic Movement &amp; Player Control
-----------------------------------
oops i was typing to fast i guess

-----------------------------------
andrew.
Sun Dec 20, 2009 11:06 pm

RE:Realistic Movement &amp; Player Control
-----------------------------------
Here's what it should be:
if chars (KEY_ESC) then 
    Window.Close(win)
    exit
end if

-----------------------------------
TheGuardian001
Sun Dec 20, 2009 11:40 pm

Re: First the basic movement controls!!!
-----------------------------------

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.

-----------------------------------
Zasalamel
Mon Dec 21, 2009 3:01 pm

RE:Realistic Movement &amp; Player Control
-----------------------------------
Thank you TheGuardian001 i was not sure. ( rows and columns do make more sense)

-----------------------------------
livingheaven
Sat Jan 09, 2010 10:02 pm

RE:Realistic Movement &amp; Player Control
-----------------------------------
Great tutorial =D best so far

-----------------------------------
blizzard4800
Sun Jan 17, 2010 10:57 am

RE:Realistic Movement &amp; Player Control
-----------------------------------
How do u make more than one platform

-----------------------------------
Turing_Gamer
Sun Jan 17, 2010 3:31 pm

Re: Realistic Movement &amp; Player Control
-----------------------------------
Here you would have to array the platform pictures, like so...
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...

-----------------------------------
SNIPERDUDE
Sun Jan 17, 2010 4:02 pm

Re: Realistic Movement &amp; 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. (