Computer Science Canada

mario movement

Author:  coldmine16 [ Wed Aug 09, 2006 5:27 pm ]
Post subject:  mario movement

Hey im having trouble moving mario, i cant get it to be a complete motion it just shoots him froom one side of the screen to the other, im doing it in a procedure as you can see but somehow to me i think im doing it wrong i know the movement to the left is not yet complet because i have to rotate the pic so it faces to the left but if you could help me move mario so its not shooting him across the screen that would be great thanks.

Author:  richcash [ Wed Aug 09, 2006 6:03 pm ]
Post subject: 

Umm... I don't see your code anywhere. If you could provide some of your code someone could help you further.

If your character is moving too fast across the screen, slow down your counter or add a delay.
I assume that you have your character imported with a picture like this :
code:
var picID := Pic.FileNew ("whatever_file_name.bmp")
Pic.Draw (picID, x, y, picCopy)


And then in your procedure you have a counter to make mario move like this :
code:
proc moveMario
     x := x + 10                         %10 is just an example value
end moveMario


so modify it like this :
code:
proc moveMario
     x := x + 1           %adjust the 1 and the
     delay (10)           %10 to make it the right speed
end moveMario

Obviously, the value you add to x each time should be smaller to make it go slower and the value in the delay () should be larger to make it go even slower.

I need the code to help you more specifically.

Author:  coldmine16 [ Wed Aug 09, 2006 6:12 pm ]
Post subject: 

heres some of the code

code:

View.Set ("graphics:400;400")
var dead : int := Pic.FileNew ("Dead.bmp")
var jump : int := Pic.FileNew ("Jump.bmp")
var stand : int := Pic.FileNew ("Stand.bmp")
var walk1 : int := Pic.FileNew ("Walk1.bmp")
var walk2 : int := Pic.FileNew ("Walk2.bmp")
var walk3 : int := Pic.FileNew ("Walk3.bmp")
var chars : array char of boolean
var x : int := -1
var y : int := 5
procedure mariodraw ()
    Pic.Draw (walk1, x, y, picMerge)
    Pic.Draw (walk2, x, y, picMerge)
    Pic.Draw (walk3, x, y, picMerge)
end mariodraw

loop
    Pic.Draw (stand, x, y, picMerge)
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        x := x + 1
        mariodraw ()
    end if
    if chars (KEY_LEFT_ARROW) then
        x := x - 1
        mariodraw ()
    end if
end loop

Author:  Cervantes [ Wed Aug 09, 2006 6:16 pm ]
Post subject: 

The code is within the rar.

To solve the problem, add a delay to the main portion of your loop.

You should not call that mariodraw procedure within your if statements. Aside from just not making sense (you'll be drawing him more often if he's moving), it will cause all sorts of troubles.

Rather, create some boolean variables to remember Mario's state: is he walking? standing still? jumping? facing right? facing left? You will only have one spot in your loop where Mario is drawn, and which picture to draw will depend on Mario's state: those variables.

Author:  coldmine16 [ Wed Aug 09, 2006 6:21 pm ]
Post subject: 

isnt that quite a bit of extra code there has to be some easier and faster method to do it

Author:  Cervantes [ Wed Aug 09, 2006 6:28 pm ]
Post subject: 

It may seem like more work, but it's work that you need to do. You'll need to keep track of Mario's state for purposes other than simply drawing.

Besides, this way works. Wink

Author:  TheOneTrueGod [ Wed Aug 09, 2006 6:47 pm ]
Post subject: 

I actually prefer having an integer keep track of his state. I.E. 0 = walking left, 1 = walking right, 2 = jumping left, 3 = jumping right, 4 = dying, etc...

This way, it works REALLY nicely with a 2-D array when you're trying to draw him from a character map.

Author:  coldmine16 [ Wed Aug 09, 2006 7:13 pm ]
Post subject: 

ahhh its buggy well i got it to move well like it isnt speeding across the screen anymore but stil however the animation of mario moving left and right is terrible plus there is a fliker and i know u put view.set ofscreenonly thing but then that makes him disapear sry i forgot what you had to do so that the object stays on the screen its been a while since i programmed but anyway here is the code so far

code:

var moveright1 : int := Pic.FileNew ("Moveright1.bmp")
var moveright2 : int := Pic.FileNew ("Moveright2.bmp")
var jumpright : int := Pic.FileNew ("Jumpright.bmp")
var down : int := Pic.FileNew ("Down.bmp")
var stand : int := Pic.FileNew ("Stand.bmp")
var jumpleft : int := Pic.FileNew ("Jumpleft")
var moveleft1 : int := Pic.FileNew ("Moveleft1.bmp")
var moveleft2 : int := Pic.FileNew ("Moveleft2.bmp")
var a1 : boolean := false%move left%
var a2 : boolean := false%move right%
var a3 : boolean := true%standing%
var a4 : boolean := false%Jump right%
var a5 : boolean := false%jump left%
var chars : array char of boolean
var x : int := -1
var y : int := 5
loop
    a3 := true
    if a3 = true then
        Pic.Draw (stand, x, y, picMerge)
    end if
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        x := x + 1
        a1 := true
        a3 := false
    end if
    if chars (KEY_LEFT_ARROW) then
        x := x - 1
        a2 := true
        a3 := false
    end if
    if a1 = true then
        Pic.Draw (moveright1, x, y, picMerge)
        delay (10)
        Pic.Draw (moveright2, x, y, picMerge)
    end if
    if a2 = true then
        Pic.Draw (moveleft1, x, y, picMerge)
        delay (10)
        Pic.Draw (moveleft2, x, y, picMerge)
    end if
    delay (30)
    cls
    a1 := false
    a2 := false
    a3 := false
    a4 := false
    a5 := false
end loop

Author:  TheOneTrueGod [ Wed Aug 09, 2006 7:21 pm ]
Post subject: 

Theres a View.Update tutorial, but i'll summarize it here in pseudocode

code:

View.Set('offscreenonly')
%Variable Declarations
loop
   %Stuff happens
   %Draw things
   View.Update
   %delay for however long using whatever method (Use Time.DelaySinceLast)
   cls
end loop

Author:  coldmine16 [ Wed Aug 09, 2006 7:23 pm ]
Post subject: 

o ya the view.update things thanx

and can anyone help on the other stuff

Author:  TokenHerbz [ Thu Aug 10, 2006 11:19 am ]
Post subject: 

your calling your mariodraw too many times, i think...

When Cervantes mentioned the use of booleans i believe he wanted you to do do somthing like have it all in your proc.. here lemmy ex. you.


code:

%%get pic vars
var a1,a2,a3,a4 : boolean := false %%id use better variables myself...

%%now we make your draw proc

proc draw_mario
    %%i dont think theres anything wrong with accepting input from a     proc, i could be misstaken... cervantes should know :)

    Input.KeyDown(keys)
    if keys (right) then
        draw your picture right 1
        a1 := true
        a2 := false
        a3 := false
        a4 := false
     elseif keys (down) then
         draw you pic down 1
         a1 := false
         a2 := true
        etc:
      end if

end draw_mario

Setting the booleans after you draw him allow you to know which way he is facing so you can optomise your code.  You can have 2 variations of you picture to make walking "more realistic" and have another boolean to show you which of the variation pics you should draw.   You can even have your drawing check to see if your char will be out of bounds, and not draw him ahead of time :)

Anyways with this set up, your main code would look like:


%%variables
%%proc

loop
Input.KeyDown(keys)
cls
draw_mario
View.Update
delay(20)
end loop
[/code]


Cervantes, i would like to know what you think of useing Input.KeyDown inside your procs?? Is it bad?

Author:  Cervantes [ Thu Aug 10, 2006 11:24 am ]
Post subject: 

It means using global variables. (In this case, keys is your global variable.)

So yes, it's bad. But not overly so.

If you wanted a subroutine to get your input, use a function that returns an 'array char of boolean' (which is the data type for your keys variable).

Of course, there's no getting away from letting procedures manipulate the state of your variables. Input.KeyDown is itself a procedure that changes the variable you pass it. This is one of those ways that Turing fails: so much use of procedures, not enough use of functions.

Author:  coldmine16 [ Thu Aug 10, 2006 12:54 pm ]
Post subject: 

one second the way u explained it i dont really need boolean variables

code:

procedure draw_mario ()
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        x := x + 1
        Pic.Draw (moveright1, x, y, picMerge)
        delay (20)
        cls
        Pic.Draw (moveright2, x, y, picMerge)
        delay(20)
        cls
    end if
end draw_mario

i have mario walking perfectly with this procedure like it draws the pictures however it draws them at such a speed like hes on steroids or something anything i can do about that

Author:  Cervantes [ Thu Aug 10, 2006 4:22 pm ]
Post subject: 

No. No no no no no!

If you do that, every other part of your program will stall for 40ms while mario takes one step to the right.

Do you know why that might be?

Author:  coldmine16 [ Thu Aug 10, 2006 4:50 pm ]
Post subject: 

o yea ur right because i have 2 20 ms delays in my draw mario, i understand how to get him to move its just that 2 picture thing you know to get it to look like he's actually moving

Author:  TokenHerbz [ Fri Aug 11, 2006 3:27 am ]
Post subject: 

you dont need delays to slow a program down

Author:  coldmine16 [ Fri Aug 11, 2006 1:10 pm ]
Post subject: 

well is there some way of slowing the transition of pic1 to pic2 of movement for mario?

Author:  Clayton [ Fri Aug 11, 2006 2:56 pm ]
Post subject: 

you dont need to slow down the program to get him to be drawn nicely, all you have to do is limit how many times he is draw per second, or in other words how many frames will be drawn to the screen, so just make use of the Time. commands to create a fps limiter and draw mario whenever you want to, using if constructs Very Happy

Author:  [Gandalf] [ Fri Aug 11, 2006 3:32 pm ]
Post subject: 

SuperFreak82, that would be the exact same thing as using delays, albeit in a more controlled way.

The simplest way to control the execution speed of your program, as you would have seen my looking at TheOneTrueGod's pseudocode, is to use Time.DelaySinceLast(). It works exactly the same as delay() (which is really Time.Delay()), except it tries to make the speed equal on any computer.

Author:  Clayton [ Fri Aug 11, 2006 4:33 pm ]
Post subject: 

actually [Gandalf] ill have to disagree with you on that one, a fps limiter is not slowing down your program, all it is doing is telling the program when to draw your frames Wink

Author:  coldmine16 [ Fri Aug 11, 2006 6:00 pm ]
Post subject: 

alrite lol i still cant get mario to move around ive never tried to do a moving animation b4 in turing so im having lots of probs heres wut hte code looks like so far

code:

var moveright1 : int := Pic.FileNew ("Moveright1.bmp")
var moveright2 : int := Pic.FileNew ("Moveright2.bmp")
var jumpright : int := Pic.FileNew ("Jumpright.bmp")
var down : int := Pic.FileNew ("Down.bmp")
var stand : int := Pic.FileNew ("Stand.bmp")
%var jumpleft : int := Pic.FileNew ("Jumpleft")
var moveleft1 : int := Pic.FileNew ("Moveleft1.bmp")
var moveleft2 : int := Pic.FileNew ("Moveleft2.bmp")
var a1 : boolean := false %move left%
var aa1 : boolean := false
var a2 : boolean := false %move right%
var a3 : boolean := true %standing%
var a4 : boolean := false %Jump right%
var a5 : boolean := false %jump left%
var chars : array char of boolean
var x : int := -1
var y : int := 5
procedure draw_mario ()
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        x := x + 3
        Pic.Draw (moveright1, x, y, picMerge)
        View.Update
        Pic.Draw (moveright2, x, y, picMerge)
        View.Update
        cls
    end if
    if chars (KEY_LEFT_ARROW) then
        x := x - 3
        Pic.Draw (moveleft1, x, y, picMerge)
        View.Update
        Pic.Draw (moveleft2, x, y, picMerge)
        View.Update
        cls
    end if
end draw_mario
View.Set ("offscreenonly")
loop
    Input.KeyDown (chars)
    cls
    draw_mario ()
    View.Update
    delay (20)
end loop


c im jsut having problems drawing the pictures to move left or right so that it actually looks like a regular moving mario i tried looking at other peoples movements but i couldnt quite understand how to do it i know crevantes said with the boolean variables im not sure exactly how to do it so if u guys can elaborate on it thatd be great thanx

Author:  Clayton [ Fri Aug 11, 2006 6:07 pm ]
Post subject: 

okay for the left and right running animations you have two pictures right? now, do you understand how booleans work? if you do you know that there are two choices, true or false, so just assign a picture to either of the choices (eg. marioleft1 when your boolean variable is true and marioleft2 when your boolean variable is false) and try it out Very Happy

Author:  [Gandalf] [ Fri Aug 11, 2006 7:56 pm ]
Post subject: 

SuperFreak, a controlled delay also tells the program when to draw the frames (ie. after 15 milliseconds have passed).

Yeah, alright, it's not the exact same thing. Smile


: