forums

TNT Basic Forums > Programming Q&A
Using sprites and images... how?
< Last Thread     Next Thread >
Author
Thread        Post A Reply

01-24-2007 18:09

Posted by:
ProfessorApe

Location:
Pittsburgh, USA

Find more posts by ProfessorApe

I have been trying to learn TNT Basic as an introduction to programming games and general programming.

I have read and re-read the documentation, tutorials and looked at other project files but I can't figure out how to use sprites. I have been trying to modify the Splats project and I can replace the background image and access the first sprite in the image bank but I can't animate it or draw a different sprite.

Could someone give a little clearer explanation of how to use sprites and images?

I want to know how to:
1) set an background image anywhere besides the upper left corner, like centered maybe?
2) pull a sprite (not the first one) from the image bank, start it on the left side of the screen and animate it across the screen horizontally.
3) pull 2+ sprites from the bank, place them and make one user controlled (arrow keys).

Here's a link to a really basic project file I am using for experimentation -> FILE
Sorry if it seems like I am asking for a lot, I like the clean syntax and features of TNT and want to try and master it before moving to something more complex, like C.


-Prof

01-24-2007 23:34

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

hi, first instead of modifying a sample i suggest starting with a blank new project - it might be my personal preference, but i think this is easier because every code that ends up there, you know what it's there for

for drawing an image to the background, i suggest you look at the command reference for 'paste sprite', 'draw image', 'paste image' (don't have the docs in front of me so i don't know for sure)

Now to the sprites

first thing to do is to put a bunch of images in bank 128, then load them with "load images 128". (after entering graphics mode or window mode)

After this, as a first test, you can just show a single sprite.

int id=0
int x=100
int y=100
int image=0
sprite id,x,y,image
draw frame
wait mouse click

draw frame is crucial, without it nothing will appear on screen.

Then, if you want a moving sprite, you will need a loop (to repeatedly execute the code that creates an image, to create multiple images in row, i.e. an animation)

int id=0
int x=100
int y=100
int image=0
int executing=true

while executing

x=x+1

sprite id,x,y,image
draw frame

wend

hope it helps =) don't hesistate to ask more questions

01-24-2007 23:44

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Hi Professor...

To answer in order:

1) Try 'Paste Image x, y, image' - this lets you place the image at the specified coordinates, any where you like really...

2) The 'Sprite' command lets you place the sprite and takes four values (or parameters), the first is the number of the sprite (1 if you only have one sprite, if you have more then it's one for the first one, 2 for the second and so on...), next comes the X coordinate of the image, X coordinates run from zero (laft edge of the screen) to the width of the screen minus one (remaining visible). The greater the X the further right the image. Y coordinates are similar but run vertically, from zero (top of the screen) to screen height minus one, the greater the Y the further down the screen the image. Lastly is the image number, this is the number of the image in the bank, change this number to change the picture. To move the sprite, change the X and / or Y value(s), so to make it go right you make the X bigger...

3) To add more sprites you use another Sprite call, make this one number 2 (see above) for example. Give it a position (with X and Y) and an image. To male one sprite user controlled you will need to read the keyboard, look for up, down, left and right. When these keys are pressed you will need to change the values of X and Y accordingly, then re-display the sprite (with another sprite call) and call 'Draw Frame'[/] to show the changes.

I hope this helps with your project, any more problems just drop us a line...

Danny (nods)

01-25-2007 03:12

Posted by:
ProfessorApe

Location:
Pittsburgh, USA

Find more posts by ProfessorApe

Thanks for the tips, I managed to get a sprite on screen and moving as well as learning how to put the background up at varying coordinates.

I was trying to figure out how to make the sprite "bounce" off the edge of the screen. I had it set up to move horizontally toward the right and wanted to have it reverse direction once it reaches the screen edge. How can this be done? Modifying loops and if statements showed me how to vary speed and direction with mouse/key presses. I guess collision detection would be something to learn about also.

01-25-2007 08:58

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Bouncing on screen edges.

I'm assuming you've set up some displacements for the sprite to make it move every frame. If this is the case you just need to check both coordinates against the relevant screen edges and if the coordinate is out of bounds (that is, off screen) then subtract the corresponding coordinate from zero, which effectively reverses it. for example:

if x<0 or x>myScreenWidth then xOffset=0-xOffset

This is a very simple example but shows the basics. If you are not holding offsets then you will simply need to clip the coordinates to the screen, basically if they go out of bounds you just put them back in a again:

if x<0 then x=0[/i[

Have fun...

Danny (nods)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.