News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Basic timed movement

Started by Bustaballs, February 28, 2006, 01:54:19 PM

Previous topic - Next topic

Bustaballs

After many hours of trying to make something so simple and everything failing, I finally create something that works.  This is the start of my little project and since it finally works, I had to show it off.  This code is very basic timed movement.




PlayBASIC Code: [Select]
; PROJECT : BasicTimedMove
; AUTHOR : Bustaballs
; CREATED : 2/28/06
; EDITED : 2/28/06
; ---------------------------------------------------------------------

LoadImage CurrentDir$()+"b.bmp",1
ImageMaskColour 1,RGB(255,0,255)
OpenScreen 640, 483, 16, 1
playerx = 320
playery = 256
Time = Timer()

Do
If Timer() - Time >= 100
Cls RGB(200,200,200)
DrawImage 1, playerx, playery, 1
Gosub Move
Sync
Time = Timer()
EndIf
Loop

Move:
If LeftKey() = True
playerx = playerx - 32
EndIf

If RightKey() = True
playerx = playerx + 32
EndIf

If DownKey() = True
playery = playery + 32
EndIf

If UpKey() = True
playery = playery - 32
EndIf
Return



QuoteOh please

Bustaballs

oh yeah!  I wanted to ask a question.  Whenever you press multiple directional keys (ie: up and right) the character will move diagonally.  How do I prevent this?
QuoteOh please

Digital Awakening

#2
To answer your question I would recommend the new elseif command, if it's woring in the version you are using, can't remember when Kevin added that.

you could do something like this:

Move:
If LeftKey()
 playerx = playerx - 32
ElseIf RightKey()
 playerx = playerx + 32
ElseIf DownKey()
 playery = playery + 32
ElseIf UpKey()
 playery = playery - 32
EndIf
Return


Oh, and you don't need to use True.

If that doesn't work you could always put it within a loop like this:

Move:
For x = 1 to 1
If LeftKey() = True
 playerx = playerx - 32
 ExitFor
EndIf

If RightKey() = True
 playerx = playerx + 32
 ExitFor
EndIf

If DownKey() = True
 playery = playery + 32
 ExitFor
EndIf

If UpKey() = True
 playery = playery - 32
 ExitFor
EndIf
next x
Return
Wisit my site at: DigitalAwakening.net

Bustaballs

Thanks.. the ElseIf makes it work fine =).
QuoteOh please

thaaks

Hi Bustaballs,

have a look at the source code of my asteroids remake - it's completely time based.

I calculate the passed milliseconds using FPS() function and a little division.
With this delta I call every other update function and can deal with milliseconds everywhere.
I don't need Timer(), just the one FPS() call in the main loop.

You find the stuff in my asteroids remake thread in the competition forum.

Feel free to ask questions regarding my code.

Have fun,
Tommy

Bustaballs

good god you did a lot of work on that...

Even though I've been programing for years, I'm still a beginner because I haven't been dedicated enough and I haven't had the right help and I've never had money.

I don't understand most of your code.  The amount of code is completely overwhelming to me.  To think you did that by yourself in only 6 weeks.  I've done some simple projects in VB but that's it.  You can find them on my site.  www.bigbagofthings.bravehost.com

I'll continue my attempts at reading your code.  I'm also downloading the game as I type.
QuoteOh please

Bustaballs

#6
This is getting to be interesting.  After some more work I've managed to add sprite facing the direction you move and basic screen collision detection.  With exception of the full collision detection and the abilility to load my maps, this is what my engine was capable of... pretty sad, huh?  Anyways, I'm gonna take a crack at movement animation next.  Here's the current progress.  Tell me what you think.


PlayBASIC Code: [Select]
; PROJECT : BasicTimedMove
; AUTHOR : Bustaballs
; CREATED : 2/28/06
; EDITED : 3/2/06
; ---------------------------------------------------------------------


LoadImage CurrentDir$()+"front.bmp",1
LoadImage CurrentDir$()+"back.bmp",2
LoadImage CurrentDir$()+"left.bmp",3
LoadImage CurrentDir$()+"right.bmp",4

For i = 1 To 4
ImageMaskColour i,RGB(255,0,255)
Next

OpenScreen 640, 483, 16, 1
playerx = 320
playery = 256
Time = Timer()

Do
If Timer() - Time >= 100
Cls RGB(200,200,200)
Gosub Move
Time = Timer()
EndIf

If dx = 0
DrawImage 1, playerx, playery, 1
EndIf

Sync

Loop

Move:
If LeftKey() = True
If playerx <> 0
playerx = playerx - 32
EndIf
dx = 3

ElseIf RightKey() = True
If playerx <> 640-32
playerx = playerx + 32
EndIf
dx = 4

ElseIf DownKey() = True
If playery <> 480-32
playery = playery + 32
EndIf
dx = 1

ElseIf UpKey() = True
If playery <> 0
playery = playery - 32
EndIf
dx = 2

EndIf


If dx = 1
DrawImage 1, playerx, playery, 1
ElseIf dx = 2
DrawImage 2, playerx, playery, 1
ElseIf dx = 3
DrawImage 3, playerx, playery, 1
ElseIf dx = 4
DrawImage 4, playerx, playery, 1
EndIf
Return



QuoteOh please

Ian Price

#7
Seems like it's coming along nicely, however the bit where you are drawing your images can be reduced by just using -


DrawImage dx,playerx,playery,1


That gets rid of all those IF... ElseIF's :)


Also at the start, before "Do" just use "dx=1", you can then get rid of the "If dx=0...."

If you make dx a Global value, then you can get rid of the last bit entirely and just use my code above in the main loop.
I came. I saw. I played some Nintendo.

Bustaballs

I already tried that...






I try the easiest things and they never work.. I keep trying all kinds of things until it works and continue from there.
QuoteOh please

Ian Price

Did you ensure that dx was Global and that you had given it a value?, otherwise it's trying to use graphic 0 (zero) as an image and graphic 0 doesn't exist. That would probably explain that error.
I came. I saw. I played some Nintendo.

Bustaballs

ah hell.. my trial broke again and I installed the older version and my ElseIfs don't work... I won't be able to mess with my code again until monday or tuesday when I'm back in Oklahoma... I'll have to check then.  God that trial braking like that upsets me quite a bit.. oh well.  I'll let you know on Monday or Tuesday.
QuoteOh please

Bustaballs

well since Kevin released another update, it reset my trial and I got to try it out.  It works fine.  Thanks.  I'm having a hard time figured out how to make a simple animation though =/.
QuoteOh please

kevin

Important !

This  clip of code bellow from the original example contains an error.    

ie.

LoadImage CurrentDir$()+"b.bmp",1
ImageMaskColour 1,RGB(255,0,255)
OpenScreen 640, 483, 16, 1



In PB (at this time) you must first open the screen mode you require, then load your media.   Changing the screen mode after loading images can destroy them.  If not on your machine, then certainly on somebody else's



So it should be


OpenScreen 640, 483, 16, 1

LoadImage CurrentDir$()+"b.bmp",1
ImageMaskColour 1,RGB(255,0,255)

Ian Price

#13
There are several ways of doing animation, so I'm going to give you some code that will allow very simple animation (but you are going to have to do a little work!)

Firstly create 8 bitmap images all the same size, and name them "1.bmp", "2.bmp", "3.bmp", "4.bmp" etc.

Ensure that the first four bitmaps are for right facing images and the next four for left facing images.

Make sure ALL of these bitmap images are different (just use colour blocks if you want)

PlayBASIC Code: [Select]
OpenScreen 640,480,16,2

Global anim,anim_delay,dir,x,y

SetFPS 30

; Right facing animations
LoadImage "1.bmp",1
LoadImage "2.bmp",2
LoadImage "3.bmp",3
LoadImage "4.bmp",4

; Left facing animations
LoadImage "5.bmp",5
LoadImage "6.bmp",6
LoadImage "7.bmp",7
LoadImage "8.bmp",8

; anim=animation frame number
; anim_delay=speed of animation
; dir=direction of player

anim=1
x=320
y=240

; Repeat until ESC key pressed
While Not EscKey()

; Move right
If RightKey()
x=x+2; Move right
anim_delay=anim_delay+1; Increase animation speed value
dir=0; Set direction to 0
EndIf

; Move left
If LeftKey()
x=x-2; Move left
anim_delay=anim_delay+1; Increase animation speed value
dir=1; Set direction to 1
EndIf

; Change animation frame number if animation speed at maximum
If anim_delay>5; Maximum speed of animation
If dir=0 Then anim=anim+1; Increase animation frame
If dir=1 Then anim=anim-1; Decrease animation frame
anim_delay=0; Reset the animation speed value
EndIf

; Ensure that the animation stays within legal range
If anim<1 Then anim=4
If anim>4 Then anim=1

; Draw our animated image
DrawImage anim+(dir*4),x,y,1

Sync

Cls RGB(0,0,0)

Text 10,10,"PRESS LEFT/RIGHT TO MOVE"

EndWhile



What this does is gives the user full control over the animation of a character and introduces left/right animation.

The speed of the animation can be altered by changing the maximum value of anim_delay. Eg "If anim_delay>20" would slow the animation down, but "If anim_delay>1" would speed the animation up.

Using the above info, you could then add up and down animations by adding 8 more bitmaps (4 for up, 4 for down), then copying the keypress routines using dir=2 for UP and dir=3 for DOWN (or vice versa), and Y=Y-/+ instead of X-/+.
I came. I saw. I played some Nintendo.

Bustaballs

much thanks.. I was having too much trouble figuring out how to animate with only the Timer() command.  I also couldn't figure out how to have different speeds for different things with only using FPS.  I'll slip you into my special thanks part of the credits =P.
QuoteOh please