forums

TNT Basic Forums > Programming Q&A
Something I really can't figure out...
< Last Thread     Next Thread >
Author
Thread        Post A Reply

02-14-2006 12:33

Posted by:
lothar250

Location:
Switzerland

Click Here to Email lothar250   Find more posts by lothar250

Hello there!

I'm making a game, where you're a ball and you have to jump around to reach the finish.
Now today, I wanted to make it scroll, so I used:

Open Canvas 1,2048,768
Open Viewport 1,0,0 to 1024,768
Set Viewport Offset 1,0,0


and in the repeat part i put:

ViewportX=nowX-512

if not ViewportX+Viewport Width (1) > 2048 and not ViewportX < 0
Set Viewport Offset 1,ViewportX,0
end if


but it just wont scroll... I've tried taking the

Set Viewport Offset 1,ViewportX,0

out of the if-build, and then it worked, until the Viewport lied outside of the canvas, then it crashed.

To prevent that, I use the If-build, but it just won't scroll then...

If anybody can help I'd be really grateful!

02-14-2006 20:48

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

I like variables...

...because; what if you wanted to change the screen size?

Try this:

Global Int screenWidth=1024, screenHeight=768
Global Int canvasWidth=2048, canvasHeight=768
Global Int viewportWidth=2048, viewportHeight=768

Open canvas 1, canvasWidth, canvasHeight
Open Viewport 1, 0, 0 to screenWidth,768

And in the repeat part:


'Scroll the viewport so that the ball is centered
int viewportX=playerX-viewportWidth/2, viewportY=playerY-viewportHeight/2

if viewportX<0 then viewportX=0
if viewportX>canvasWidth-viewportWidth then viewportX=canvasWidth-viewportWidth

if viewportY<0 then viewportY=0
if viewportY>canvasHeight-viewportHeight then viewportY=canvasHeight-viewportHeight


And make sure you have a 'draw frame' in your loop.

Hope this helps,
Jacob

02-15-2006 11:22

Posted by:
lothar250

Location:
Switzerland

Click Here to Email lothar250   Find more posts by lothar250

Hi there and thanks for your reply!

That unfortunately didn't do anything... Actually the only thing you did was adding variables for screenwidth, screenheight and so on, it still doesn't work...

And yes, I do have a "draw frame" :P, the game works perfectly with all the physics, but it just wont scroll, that's the only problem.

I've also tried putting

Draw Text 200,200,"if not "+(ViewportX+Viewport Width (1))+" > 2048 and not "+ViewportX+" < 0"

in the repeat part, so that it always shows me if the conditions are true, and they were true until my ball went too far to the left and ViewportX got below zero, but even when the conditions were true it just didn't scroll...

What's going on?

02-15-2006 12:23

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Oops!

Yeah I've just noticed that.

I forgot this in the repeat part:
Set Viewport Offset 1,viewportX, viewportY

And with that it works for me...

02-15-2006 12:34

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Oops! (Again?)

Make this look like this:
Global Int viewportWidth=1024, viewportHeight=768


And this:
Open Viewport 1, 0, 0 to viewportWidth,viewportHeight


Now I've tested this one and it works!

02-15-2006 12:44

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

And one more thing:

Centering viewports

This became useful to me at times.
What it does is makes it so the viewport is centerd on the screen, well, just if you make a small level or something:

int mapWidth=?, mapHeight=?
int viewportHeight=mapHeight
int viewportWidth=mapWitdth
int maxWidth=screenWidth, maxHeight=screenHeight

if viewportWidth>screenWidth then viewportWidth=screenWidth
if viewportHeight>screenHeight then viewportHeight=screenHeight

Open Viewport 1, screenWidth/2-viewportWidth/2, screenHeight/2-viewportHeight/2 to screenWidth/2+viewportWidth/2, screenHeight/2+viewportHeight/2


If you want to use this, replace the current 'Open Viewport' with this code.

More ideas for your game...

02-15-2006 14:16

Posted by:
lothar250

Location:
Switzerland

Click Here to Email lothar250   Find more posts by lothar250

Warning, spam ahead :)

well yeah i have noticed these 2 errors in your code, and I have corrected them... It doesn't work with any of your advices... I'll just post the whole source here now, so you can test it (You will probably see some ugly structures, but that's because I have started programming like 3 days ago :P):

quote:
'VARIABLES
int nowX,nowY,x,yVelocity,n
int xVelocity,yVelocityadded
int numobstacles,numimages,quit
int oldX,oldY,ViewportX,ViewportY
int imagenumber[10]
int spriteimage[10]
global int imagewidth[10]
global int imageheight[10]
global int spriterightborder[10]
global int spriteleftborder[10]
global int spritetopborder[10]
global int spritebottomborder[10]
global int hitobstacle[10]
global int screenwidth=1024,screenheight=768
global int canvaswidth=2048,canvasheight=768
global int viewportwidth=1024,viewportheight=768
'END VARIABLES

graphics mode hardware screenwidth,screenheight
hide mouse
Open Canvas 1,canvaswidth,canvasheight
Open Viewport 1,0,0 to viewportwidth,viewportheight
Set Viewport Offset 1,0,0
Canvas 1
set pen colour white
paint canvas

'FUNCTIONS
procedure CreateRect(int width,int height,int colour,int imagenum)
set pen colour white
paint canvas
set pen colour colour
fill rect 0,0 to width,height
copy image 0,0,width,height,white,false to imagenum
set pen colour white
paint canvas
end proc

procedure SetSprites(int leftborder,int topborder,int image,int spritenumber)
spriterightborder[spritenumber]=leftborder+imagewidth[image]
spritebottomborder[spritenumber]=topborder+imageheight[image]
sprite spritenumber,leftborder,topborder,image
end proc
'END FUNCTIONS

'IMAGE VARIABLES
imagewidth[1]= 1024 'Top and Bottom Wall
imageheight[1]= 100

imagewidth[2]= 100 'Side Wall
imageheight[2]= 768

imagewidth[3]= 512
imageheight[3]= 100

numimages=3
'END IMAGE VARIABLES

'SPRITE VARIABLES
spriteleftborder[1]= 0 'Floor
spritetopborder[1]= 668
imagenumber[1]= 1

spriteleftborder[2]= 0 'Ceiling
spritetopborder[2]= 0
imagenumber[2]= 1

spriteleftborder[3]= 0 'Left Wall
spritetopborder[3]= 0
imagenumber[3]= 2

spriteleftborder[4]= 1948 'Right Wall
spritetopborder[4]= 0
imagenumber[4]= 2

spriteleftborder[5]= 0
spritetopborder[5]= 450
imagenumber[5]= 3

numobstacles=5
'END SPRITE VARIABLES

'DRAW IMAGES
set pen colour white 'Draw Ball
paint canvas
set pen colour black
fill oval 0,0 to 50,50
copy image 0,0,51,50,white,false to 0
set pen colour white
paint canvas
set image offsets 0,25,25

For x=1 to numimages step 1
CreateRect(imagewidth[x],imageheight[x],black,x) 'Draw Obstacles
Next x
'END DRAW IMAGES

'DRAW LEVEL SPRITES
for n=1 to numobstacles step 1
SetSprites(spriteleftborder[n],spritetopborder[n],imagenumber[n],n)
next n

'END DRAW LEVEL SPRITES

'MAIN FUNCTION
nowX=600
nowY=200



repeat
if pressed(12)
quit=true
end if
Yvelocityadded=false



ViewportX=nowX-(viewportwidth/2)

if ViewportX<0 then ViewportX=0
if ViewportX>canvaswidth-viewportwidth then ViewportX=canvaswidth-viewportwidth



if not ViewportX+Viewport Width (1) > 2048 and not ViewportX < 0
Set Viewport Offset 1,ViewportX,0
end if

oldX=nowX

oldY=nowY

nowY=nowY+Yvelocity

nowX=nowX+Xvelocity


sprite 0,nowX,nowY,0




for n=1 to numobstacles step 1
hitobstacle[n]=sprite col(0,n)
next n

for x=1 to numobstacles step 1
if hitobstacle[x]=x
if oldX<=spriteleftborder[x] 'If ball touches left border of sprite
if Xvelocity>0
nowX=nowX-Xvelocity
Xvelocity=-Xvelocity
end if
end if
if oldX>=spriterightborder[x] 'If ball touches right border of sprite
if Xvelocity<0
nowX=nowX-Xvelocity
Xvelocity=-Xvelocity
end if
end if




if oldY<spritetopborder[x] 'If ball touches top border of sprite
if Yvelocity>3
nowY=spritetopborder[x]-24
Yvelocity=-(Yvelocity/1.5)
else
nowY=spritetopborder[x]-24
Yvelocity=0
end if
end if




if oldY>=spritebottomborder[x] 'If ball touches bottom border of sprite
if Yvelocity>0
nowY=nowY+Yvelocity
else
nowY=nowY-Yvelocity
Yvelocity=-Yvelocity
end if
end if
else if Yvelocityadded=false
Yvelocity=Yvelocity+1
Yvelocityadded=true
end if
next x

if pressed(0) or pressed(123)
Xvelocity=Xvelocity-1
else if pressed(2) or pressed(124)
Xvelocity=Xvelocity+1
end if

if Xvelocity<0 'Rotate Ball
int xveltemp
xveltemp=-Xvelocity
Rotate Sprite 0,Xvelocity,xveltemp
else
Rotate Sprite 0,Xvelocity,Xvelocity
end if

if pressed(13)
Rotate Sprite 0,-10,10
else if pressed(1)
Rotate Sprite 0,10,10
end if

for x=1 to numobstacles step 1
if hitobstacle[x]=x and nowY<spritetopborder[x]
if pressed(49) or pressed(126) then Yvelocity=-30
end if
next x

if Xvelocity>25
Xvelocity=25
else if Xvelocity<-25
Xvelocity=-25
end if

if Yvelocity>25
Yvelocity=25
else if Yvelocity<-25
Yvelocity=-25
end if

draw frame
until quit=true

02-15-2006 16:34

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

This code make the viewport scroll:

Make a new project (keep your old one :)), put this code in it:

Global Int screenWidth=1024, screenHeight=768
Global Int canvasWidth=2048, canvasHeight=768
Global Int viewportWidth=1024, viewportHeight=768
Global Int playerX=50, playerY=50

window mode screenWidth, screenHeight
Open canvas 1, canvasWidth, canvasHeight
Open Viewport 1, 0, 0 to viewportWidth,viewportHeight
canvas 1
int i
for i=0 to 1000
set pen color make color (random(0,255),random(0,255),random(0,255))
frame rect random(0,canvasWidth), random(0,canvasheight) to random(0,canvasWidth), random(0,canvasheight)
next
repeat
draw text playerX, playerY, "Player"
if left then playerX=playerX-10
if right then playerX=playerX+10
'Scroll the viewport so that the ball is centered
int viewportX=playerX-viewportWidth/2, viewportY=playerY-viewportHeight/2

if viewportX<0 then viewportX=0
if viewportX>canvasWidth-viewportWidth then viewportX=canvasWidth-viewportWidth

if viewportY<0 then viewportY=0
if viewportY>canvasHeight-viewportHeight then viewportY=canvasHeight-viewportHeight
set viewport offset 1, viewportX, viewportY

draw frame
forever


Use the left and right keys to move the 'player'.
Please! Let me know if the viewport scrolls.

02-15-2006 16:40

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Your viewport scrolls

Just replace this:

if not ViewportX+Viewport Width (1) > 2048 and not ViewportX < 0
Set Viewport Offset 1,ViewportX,0
end if

With this:

Set Viewport Offset 1,ViewportX,0

...I mean, just get rid of the 'if' around the Set viewpot Offset.

02-15-2006 17:16

Posted by:
lothar250

Location:
Switzerland

Click Here to Email lothar250   Find more posts by lothar250

As I said in my first post:

quote:
I've tried taking the

Set Viewport Offset 1,ViewportX,0

out of the if-build, and then it worked, until the Viewport lied outside of the canvas, then it crashed.

To prevent that, I use the If-build, but it just won't scroll then...



if i just leave away the "if"-part, it will crash as soon as nowX-512 is lower than zero...

02-15-2006 17:21

Posted by:
lothar250

Location:
Switzerland

Click Here to Email lothar250   Find more posts by lothar250

And from that code you've posted 3 posts above, i've got this:



Why did you tell me to try that code for you? Don't you have TNT Basic, or are you just at work? :P

02-15-2006 18:26

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

I'm advanced with TNT Basic and a novice Objective-C programmer.

About the code I posted 4 posts above:
You can replace all those messy boxes with whatever you want, they were just to show you that it scrolls (you can' tell if a blank background is scrolling).

And with the code you've posted of your game above, if you get rid of the if statement around where it says 'Set Viewport Offset 1, viewportX, 0' it works just fine and won't go out of the canvas because you have the code I gave you right above it.

02-15-2006 18:50

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Now now ladies... ;)

The code is working fine, but the if block around the scroll call is not necessary. If you remove this, or just comment it out, it scrolls.
There will not be a situation when your display goes out of bounds as the lines which precede this block 'clip' the values:

ViewportX=nowX-(viewportwidth/2)

if ViewportX<0 then ViewportX=0 'Can't be less than zero here...
if ViewportX>canvaswidth-viewportwidth then ViewportX=canvaswidth-viewportwidth 'Can't overflow the other way here...

'if not ViewportX+Viewport Width (1) > 2048 and not ViewportX < 0 'so this check is not needed... just comment it out.
Set Viewport Offset 1,ViewportX,0
'end if

I pasted your code it hieroglyph and it ran fine with the above adjustments... no crashing. I also tested the following 'if' block, and this worked fine too:

if ViewportX+Viewport Width (1) < 2048 and ViewportX > 0 '[Set Viewport Offset 1,ViewportX,0
end if

Basically I just swapped the conditions round and lost the references to 'not'. This would suggest a logic error, probably with the precedence of the operators. So I tried this:

if not (ViewportX+Viewport Width (1) > 2048) and not (ViewportX < 0)
Set Viewport Offset 1,ViewportX,0
end if

and again it worked... Hope this 'finally' solves the problem!

Danny (nods)

- TNT Basic developer and 15 year+ programming veteran -

02-15-2006 19:14

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Try this code (different looks):

'VARIABLES
int nowX,nowY,x,yVelocity,n
int xVelocity,yVelocityadded
int numobstacles,numimages,quit
int oldX,oldY,ViewportX,ViewportY
int imagenumber[10]
int spriteimage[10]
global int imagewidth[10]
global int imageheight[10]
global int spriterightborder[10]
global int spriteleftborder[10]
global int spritetopborder[10]
global int spritebottomborder[10]
global int hitobstacle[10]
global int screenwidth=1024,screenheight=768
global int canvaswidth=2048,canvasheight=768
global int viewportwidth=1024,viewportheight=768
'END VARIABLES

window mode hardware screenwidth,screenheight
hide mouse
Open Canvas 1,canvaswidth,canvasheight
Open Viewport 1,0,0 to viewportwidth,viewportheight
Set Viewport Offset 1,0,0
Canvas 1
set pen colour white
paint canvas

'FUNCTIONS
procedure CreateRect(int width,int height,int colour,int imagenum)
set pen colour white
paint canvas
set pen colour colour
fill rect 0,0 to width,height

int depth=6
set pen transparency 75
set pen color black
fill poly 0,height to width, height to width-depth, height-depth to 0+depth, height-depth'bottom
fill poly width, height to width, 0 to width-depth, 0+depth to width-depth, height-depth'right
set pen color white
fill poly 0,0 to width, 0 to width-depth, 0+depth to 0+depth, 0+depth'top
fill poly 0,0 to 0,height to 0+depth, height-depth to 0+depth, 0+depth'left

set pen transparency 0
copy image 0,0,width,height,white,false to imagenum
set pen colour white
paint canvas
end proc

procedure SetSprites(int leftborder,int topborder,int image,int spritenumber)
spriterightborder[spritenumber]=leftborder+imagewidth[image]
spritebottomborder[spritenumber]=topborder+imageheight[image]
sprite spritenumber,leftborder,topborder,image
end proc
'END FUNCTIONS

'IMAGE VARIABLES
imagewidth[1]= 1024 'Top and Bottom Wall
imageheight[1]= 100

imagewidth[2]= 100 'Side Wall
imageheight[2]= 768

imagewidth[3]= 512
imageheight[3]= 100

numimages=3
'END IMAGE VARIABLES

'SPRITE VARIABLES
spriteleftborder[1]= 0 'Floor
spritetopborder[1]= 668
imagenumber[1]= 1

spriteleftborder[2]= 0 'Ceiling
spritetopborder[2]= 0
imagenumber[2]= 1

spriteleftborder[3]= 0 'Left Wall
spritetopborder[3]= 0
imagenumber[3]= 2

spriteleftborder[4]= 1948 'Right Wall
spritetopborder[4]= 0
imagenumber[4]= 2

spriteleftborder[5]= 0
spritetopborder[5]= 450
imagenumber[5]= 3

numobstacles=5
'END SPRITE VARIABLES

'DRAW IMAGES
set pen colour white 'Draw Ball
paint canvas
set pen colour black
fill oval 0,0 to 50,50
set pen colour make color (75,75,75)
fill oval 3,3 to 47,47
set pen colour make color (150,150,150)
fill oval 6,6 to 44,44
set pen colour yellow
fill oval 0,20 to 50,30
fill oval 20,0 to 30,50
set pen colour make color (255,100,0)
fill oval 2,22 to 48,28
fill oval 22,2 to 28,48
set pen colour red
fill oval 20,20 to 30,30
copy image 0,0,51,50,white,false to 0
set pen colour white
paint canvas
set image offsets 0,25,25

For x=1 to numimages step 1
CreateRect(imagewidth[x],imageheight[x],red,x) 'Draw Obstacles
Next x
'END DRAW IMAGES

'DRAW LEVEL SPRITES
for n=1 to numobstacles step 1
SetSprites(spriteleftborder[n],spritetopborder[n],imagenumber[n],n)
next n

'END DRAW LEVEL SPRITES

'MAIN FUNCTION
nowX=600
nowY=200



repeat
if pressed(12)
quit=true
end if
Yvelocityadded=false



ViewportX=nowX-(viewportwidth/2)

if ViewportX<0 then ViewportX=0
if ViewportX>canvaswidth-viewportwidth then ViewportX=canvaswidth-viewportwidth

Set Viewport Offset 1,ViewportX,0

oldX=nowX

oldY=nowY

nowY=nowY+Yvelocity

nowX=nowX+Xvelocity


sprite 0,nowX,nowY,0




for n=1 to numobstacles step 1
hitobstacle[n]=sprite col(0,n)
next n

for x=1 to numobstacles step 1
if hitobstacle[x]=x
if oldX<=spriteleftborder[x] 'If ball touches left border of sprite
if Xvelocity>0
nowX=nowX-Xvelocity
Xvelocity=-Xvelocity
end if
end if
if oldX>=spriterightborder[x] 'If ball touches right border of sprite
if Xvelocity<0
nowX=nowX-Xvelocity
Xvelocity=-Xvelocity
end if
end if




if oldY<spritetopborder[x] 'If ball touches top border of sprite
if Yvelocity>3
nowY=spritetopborder[x]-24
Yvelocity=-(Yvelocity/1.5)
else
nowY=spritetopborder[x]-24
Yvelocity=0
end if
end if




if oldY>=spritebottomborder[x] 'If ball touches bottom border of sprite
if Yvelocity>0
nowY=nowY+Yvelocity
else
nowY=nowY-Yvelocity
Yvelocity=-Yvelocity
end if
end if
else if Yvelocityadded=false
Yvelocity=Yvelocity+1
Yvelocityadded=true
end if
next x

if pressed(0) or pressed(123)
Xvelocity=Xvelocity-1
else if pressed(2) or pressed(124)
Xvelocity=Xvelocity+1
end if

if Xvelocity<0 'Rotate Ball
int xveltemp
xveltemp=-Xvelocity
Rotate Sprite 0,Xvelocity,xveltemp
else
Rotate Sprite 0,Xvelocity,Xvelocity
end if

if pressed(13)
Rotate Sprite 0,-10,10
else if pressed(1)
Rotate Sprite 0,10,10
end if

for x=1 to numobstacles step 1
if hitobstacle[x]=x and nowY<spritetopborder[x]
if pressed(49) or pressed(126) then Yvelocity=-30
end if
next x

if Xvelocity>25
Xvelocity=25
else if Xvelocity<-25
Xvelocity=-25
end if

if Yvelocity>25
Yvelocity=25
else if Yvelocity<-25
Yvelocity=-25
end if

draw frame
until quit=true

02-15-2006 19:43

Posted by:
lothar250

Location:
Switzerland

Click Here to Email lothar250   Find more posts by lothar250

Oh right, sorry I didn't realize that, Jacob!

Thanks to you two for helping a newbie!

02-16-2006 00:14

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

: )

No problem at all!
Email me your project after you finish :).


P.S. did you like the wall and ball graphics?

02-16-2006 09:49

Posted by:
lothar250

Location:
Switzerland

Click Here to Email lothar250   Find more posts by lothar250

Yeah, I'm using your ball graphics now, they're pretty cool, thanks :).
The wall gfx don't really fit imho, but thanks for making them tho :D

Greets

02-26-2006 21:32

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Any Progress?

Email me your project if you made any progress! :)

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.