forums

TNT Basic Forums > Programming Q&A
(noob) deleting drawn text from the canvas.
< Last Thread     Next Thread >
Author
Thread        Post A Reply

01-08-2007 21:32

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

i have been working on the pong example to make it more os x compatible (and to help me learn tnt basic syntax) and i want to draw the current score for player 1 and player two. first of all how would i get that text to update everytime someone scores? and after a player reaches ten i want the scores text deleted and new text to come in saying who won? how might I accomplish this? TIA
-Hendo

01-08-2007 22:45

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Hi Hendo, drawing the score involves many steps. Please tell us more about which step you don't know how to do for a less general answer =)

Usually you can just write a drawScore() procedure and call it whenever a player scores. The procedure can then erase the previously drawn score by drawing on top of it, with fill rect if background is plain color, or white some draw image command (don't remember which one by heart) if background is a picture. Then you can draw the new score with draw string (or something similar - look at the TNT help)

01-09-2007 09:34

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

But remember there are stars in the background... if you fill rect... then you lose some of the stars.

01-09-2007 14:09

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

then either you keep a plain color area for the score, either you redraw the stars every time (if there's any random in the drawing, it will have to be taken out though otherwise the stars will move when score changes)

01-10-2007 04:08

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

or you can keep a copy of the canvas... and paste the score area everytime the score changes.

01-12-2007 21:23

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

basically i just need to know how to clear all drawn text on the canvas but not the background (which was the starfield procedure)

01-12-2007 22:09

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

sorry for the double post...i was just wondering why the game quits everytime i score. i have a reset method that resets the ball but it quits right after. I'm just going to post the code right now.



' go into graphics mode and hide the mouse
graphics mode 640,480
hide mouse
load sounds 128
load music 128
set music volume 50
play music
music loop on


' make some simple sprite images using tnt basic's rectangle and oval routines.
' pick the images up and store them in the images bank.
' image 0 - blue bat
' image 1 - red bat
' image 2 - ball

' blue bat
set pen colour white
fill rect 5,0 to 10,50
set pen colour white
fill rect 0,0 to 5,50
copy image 0,0,10,50,-1,true to 0

' red bat
set pen colour white
fill rect 0,0 to 5,50
set pen colour white
fill rect 5,0 to 10,50
copy image 0,0,10,50,-1,true to 1

' ball
set pen colour white
fill oval 50,50 to 60,60
copy image 50,50,60,60,black,true to 2
set image offsets 2,5,5

' now erase the background with black and draw a starfield on it as decoration
' this could be replaced with a load picture command to load a backdrop picture instead
set pen colour black
paint canvas
DrawStarfield()
set pen colour white
frame rect 0,0 to 640,480



float ballx=320,bally=240 ' the x and y position of the ball
float ballxvel,ballyvel=random(-4,4) ' the x and y velocities of the ball
int p1score=0 ' player 1's score
int p2score=0 ' player 2's score
int winner=0
float player1y=320,player2y=320 ' the y coords of the bats, 320 is middle of y axis

' pick a random direction for the ball and set it off at 4 pixels per frame
' you can change this to a faster speed for a more challenging game!
if random (0,1) = 1
ballxvel=4
else
ballxvel=-4
end if

' main loop
repeat
' draw sprites
sprite 0,630,player2y,0
sprite 1,0,player1y,1
sprite 2,ballx,bally,2
draw frame

' bounce the ball off the floor and ceiling

if bally<=5 or bally>=475

'play sound
play sound 0
' move the ball away from what it just hit
bally=bally-ballyvel
' then reverse the direction
ballyvel=-ballyvel
end if

' bounce the ball off the bats
int hitbat=sprite col (2,0 to 1)

' if hitbat>-1 then it hit a bat, reverse its direction
if hitbat>-1
play sound 0
' move the ball away from the bat it just hit
ballx=ballx-ballxvel
' reverse the balls direction
ballxvel=-ballxvel

' change the yvelocity of the ball according to where on the bat the ball hit
' if it hit near the top of the bat then make the ball move upwards faster, if
' it hit near the bottom of the bat then make the ball move downwards faster,
' and if it hits in the middle of the bat simply reflect it without changing the
' yvelocity
float batcentre
if hitbat=0 then batcentre=player2y+25 ' bat height is 50 so + 25 gives the middle of the bat
if hitbat=1 then batcentre=player1y+25

ballyvel=ballyvel+(bally-batcentre)*0.1
end if

' see if it's gone off the side of the screen yet, if so, add to score.
if ballx<=5
Reset()
p2score=p2score+1
else if ballx>=635
Reset()
p1score=p1score+1
end if

if pressed(126) ' up=up
player1y=player1y-15
else if pressed (125) 'down=down
player1y=player1y+15
end if
if player1y>=445
player1y=player1y-15
else if player1y<=-15
player1y=player1y+15
end if

if pressed(91) ' pad 8=up
player2y=player2y-15
else if pressed (87) 'pad 5=down
player2y=player2y+15
end if
if player2y>=445
player2y=player2y-15
else if player2y<=-15
player2y=player2y+15
end if

' move the ball
ballx=ballx+ballxvel
bally=bally+ballyvel

until p1score=5 or p2score=5
draw frame


' 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧
' 腹DrawStarField
' 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧
' plots random coloured pixels all over the screen, looks like a starfield and is an easy way to generate a
' backdrop without using pictures
procedure DrawStarfield()
int star

for star=0 to 100
set pixel colour random(0,640),random(0,480),random(black,white)
next star
end proc
'This procedure is for when the a point is scored the ball is reset in the middle and fired off in a random direction.
procedure Reset()
float ballx=320,bally=240 ' the x and y position of the ball
float ballxvel,ballyvel=random(-4,4) ' the x and y velocities of the ball
if random (0,1) = 1
ballxvel=4
else
ballxvel=-4
end if
sprite 2,ballx,bally,2
draw frame
ballx=ballx+ballxvel
bally=bally+ballyvel
end proc

01-12-2007 23:10

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Because Reset just displays the ball in the middle, it does not change the location of ballx and bally.

Reset creates its own ballx and bally variables and changes them locally, but without affecting the global ballx and bally variables. You will need to declare ballx and bally as global and not re-create them in Reset. Same thing for ballxvel and ballyvel

01-13-2007 01:15

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

awesome, works perfectly! thanks!

01-14-2007 17:31

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

what would my procedure look like for updating the score? Would I have to convert the p1 and p2score variables which are integers to strings first? if so i am assuming that my original p1 and p2 scores have to be global variables.

This is how I thought I could convert number to strings:
p1score=as string(p1score, 1)

but it didn't work. I tried looking in the tutorials but couldn't find anything.

01-14-2007 17:40

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT


you need to define a global string variable as the string to be displayed i.e.

global str = p1scorestr
p1scorestr = as string(p1score)

and display it as such like:
-------------------------------------------
set pen colour white
draw text 100,100,"PLAYER 1 SCORE: "+p1scorestr
draw frame
-------------------------------------------

cheers

barry

01-14-2007 18:46

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

nice, it almost works.
the thing is everytime a playerscores the string is just pasted over the old one.
this is my procedure

procedure drawScore()

int p1score=p1score
int p2score=p2score
p1scorestr = as string(p1score)
p2scorestr = as string(p2score)


set pen colour white
draw text 35,50,"PLAYER 1 SCORE: "+p1scorestr
draw text 500,50,"PLAYER 2 SCORE: "+p2scorestr
draw frame
end proc

this is the piece of code that checks to see if the ball hits one of the walls and scores:

if ballx<=5
p2score=p2score+1
drawScore()
Reset()
else if ballx>=635
p1score=p1score+1
drawScore()
Reset()
end if

these are my general starting variables:

global float ballx=320,bally=240 ' the x and y position of the ball
global float ballxvel,ballyvel=random(-4,4) ' the x and y velocities of the ball
global int p1score=0 ' player 1's score
global int p2score=0 ' player 2's score
global str p1scorestr=""
global str p2scorestr=""
float player1y=320,player2y=320 ' the y coords of the bats, 320 is middle of y axis

01-14-2007 18:51

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

before drawing the new score you will need to erase the old one.

How to do this depends on how you draw the background. Easiest way would just to show score in a black/color rectangle.

01-14-2007 18:53

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT



procedure drawScore()

int p1score=p1score
int p2score=p2score
p1scorestr = as string(p1score)
p2scorestr = as string(p2score)

------------------------added this to draw black to erase background
------------------------assuming the background is black?
set pen colour black
fill rect 20,30 to 100,80------------player one
fill rect 480,30 to 560,80------------player two
------------------------this draws over the previous number
------------------------and lets you draw a new number
------------you will need to adjust the x,y to x,y to place it properly
------------ if you change the set pen colour 'black' to yellow or something
------------ you will readily see where it is drawing in relation to the scores

set pen colour white
draw text 35,50,"PLAYER 1 SCORE: "+p1scorestr
draw text 500,50,"PLAYER 2 SCORE: "+p2scorestr
draw frame
end proc

hope that helps
cheers

01-14-2007 19:00

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

its still not erasing the old score.
this is what I have:

procedure drawScore()

int p1score=p1score
int p2score=p2score
p1scorestr = as string(p1score)
p2scorestr = as string(p2score)


set pen colour black
fill rect 20,30 to 100,80
fill rect 480,30 to 560,80


set pen colour white
draw text 35,50,"PLAYER 1 SCORE: "+p1scorestr
draw text 500,50,"PLAYER 2 SCORE: "+p2scorestr
draw frame
end proc

01-14-2007 19:09

Posted by:
swagIT

Location:
Great Wet North (Vancouver)

Find more posts by swagIT

change the set pen colour 'black' to 'yellow' or something that contrasts with the background - so you can see where the rectangle is drawing.
It may not be drawing over the actual score depending on your type size - you may need to adjust the coordinates

01-14-2007 19:12

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

ah, found it, and fixed it! thanks a ton for all your help guys!

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.