News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

ABOUT SPRITES

Started by servogod85, October 12, 2005, 12:50:55 PM

Previous topic - Next topic

servogod85

:huh: My first language was Turbo Pascal 7. My 2d games consisted in drawing an image in a certain area, update its coordinates, delete the previous image and finally redrawing the image in its new location. If I load my images as sprites and I want to move them around, will I have to delete the old image and draw the new one or will the compiler delete the old image automatically??? If the compiler does not do it automatically, can you explain to me how to make sprites move? (the same way I did with TP 7?)

kevin

#1
Sprites don't have a 'Back save' styled mode.  It's generally too much trouble.


Your update loop will look a bit like this.  Each frame you draw your backdrop / move your sprites & then draw the sprites..

PlayBASIC Code: [Select]
;Start of DO/loop
Do

; Clear the screen
Cls Rgb(0,0,0)

; Move your sprites here

MoveSprite 1,2,2



; Draw The Entire Sprite List tot eh current surface (in case it'll be the screen)

DrawALLSprites


; Swap the Screens back buffer to the front buffer, so the user can see it
Sync

; loop back to the DO, so the loop continues
loop




servogod85

:blink: ...Ok you lost me as soon as you said backdrop. From what I uderstand, you are proposing that I keep two seperate images of the screen, one that is currently drawn in the monitor and one that functions as a "pre-screen" in which i work on. I do all my updating on that "pre-screen" image and then i load that to the screen thus making that the current screen and creating a new "pre-screen" image? Of course the details would be swapped from the two screen images (prescreen-> current screen). Is this what you meant? I know I am not an expert so could you break it down literally to me. Explain me the method and the list the details maybe? Thanks I appreciate it :unsure:

Draco9898

#3
All you have to do is move sprites around and draw them, playbasic does the rest. Pretty simple (and fast).

You do not have to manually update the sprites drawing yourself, all pixels that we're behind the sprite are just written over.

You won't see the image moving unless you draw it, (of course...)

A loop would look like this(with psuedo code):


DO

Draw your backdrops,
Move the sprites around using movesprite commands or positionsprite
Then you can choose to draw all the sprites with DrawAllSprites or DrawSprite.

SYNC
LOOP
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

servogod85

#4
:) Ok I'll try it out and see what happens, if all good, Thanks!!! I owe you one and if something will go wrong I'lljust post the code and you can help me out on it. Lol I guess it's the best thing to do right now. As soon as I get home from work I'll do it. :) I downloaded some tiles that Calypson(is that how you spell it? I'm not sure) posted. It will save me time and make my experience a little nicer(imagine if it works!?!?) Lol

kevin

#5
QuoteFrom what I uderstand, you are proposing that I keep two seperate images of the screen, one that is currently drawn in the monitor and one that functions as a "pre-screen" in which i work on. I do all my updating on that "pre-screen" image and then i load that to the screen thus making that the current screen and creating a new "pre-screen" image?

Well you obviously understand the concept.    But PB actually does this automatically for you.  So the Screen is actually two images.   The front image (also know as the front buffer) is the one we see, while the back image (back buffer) is the one drawing operations take place on.

 The SYNC command in PB swaps these buffers for over for us.    

 So logically what our programs do, is draw everything that we need to draw on the back buffer (which is where PB draws to by default), and when were finished drawing, we use SYNC to swap buffers and show this to the user.


 For example

PlayBASIC Code: [Select]
; Crear the screen to a GREEN colour
Cls Rgb(0,155,0)


; Draws a bunch of circle randomly on the screen
For lp=0 to 50
Circlec rnd(800),rnd(600),rnd(100),1,rndrgb()
next


; Display this message to the Screens Back Buffer
Print "Hello World"



; Tell PB to swap the back buffer for the front buffer. (so we can see what was drawn)
Sync

;wait until a key is pressed
waitkey

;wait until NO keys are pressed (otherwise it would fall through the next wait
; as the code is executed so quickly )
WaitNokey




; Crear the screen to a Blue colour
Cls Rgb(0,0,200)


; Draws a bunch of lines randomly on the screen
For lp=0 to 50
linec rnd(800),rnd(600),rnd(800),rnd(600),rndrgb()
next

; Display this message to the Screens Back Buffer
Print "Hello Again"

; Tell PB to swap the back buffer for the front buffer. (so we can see what was drawn)
Sync

; Wait until a key is pressed.
Waitkey



; End the program
End







NOTE: if there any command you don't understand .. Move the mouse pointer over this command name (the ones in red) click the command name and  press the F1 key.  This will take you to this commands 'dizzy' page.


- Sprite Tutorials In PlayBASIC Documentation


servogod85

#6
Thanks alot Kevin i tried it out and it's working fine. So now I understand the real power of the sprite concept. My next problem deals with controls. All I need to do is just verify if any arrow key has been pressed and if so modify the respective coordinates and then reposition the sprites. As for the waitnokey..that is great. In TP that function didnt exist lol..i had to assign the variable that help the ascci value to 0 or a value that i was sure i didn't use.Here are my question in a more structured manner:

1)What is the ascii code for backspaceykey? There is no command for that key  in PB;

2)When moving sprites, should I keep track of their position with variables(example  Xpos=200..later: Xpos= Xpos+2 and Ypos=150...Ypos= Ypos-2) or will the computer keep track of that for me? I ask this question becuase i was using themovespritex and movespritey commands and i became curious of how they worked.


Salvatore

kevin

Quote1)What is the ascii code for backspaceykey? There is no command for that key in PB;

If there's not command for reading the key, then you'll need to use scancode() function (which returns Scan Code of a pressed key - note it doesn't support multiple keys)  To read  particular key you

  The IDE has scancode map build into it.  It's located under the Help Menu as   Scancode Map


Or if you want yuo can to work them out yourself, by using a bit of code like this.


Do
Cls rgb(0,0,0)
KeyPressed=Scancode()
If KeyPressed >0
Print "This Key is Scancode:"+str$(KeyPRessed)
else
Print "No Key Was pressed"
endif
Sync
Loop




Quote2)When moving sprites, should I keep track of their positio with variable(example Xpos=200 and Ypos=150) or will the computer keep track of that for me? I ask this question becuase i was using the movespritex and movespritey commands and i became curious of how they worked.

 Internally every sprite has a list of it's properties.  From stuff like,  It's position (XYZ), type of collision, rotation etc etc.

 You can either keep track of the sprites positions yourself.  Then simply position the sprite each update, feeding it(them) your coordinates.

 or, you can move the sprite.    They both basically achieve the same result.