Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / I need help fixing this program

Author
Message
Shadow666
17
Years of Service
User Offline
Joined: 29th Jan 2007
Location:
Posted: 13th Feb 2007 01:21
Ok the object of this program is for the ball to hit the brick and the brick deletes and the ball resets back to the middle. I can get it to do that but what I want is when the ball hits the brick and it goes away I want it to say good job at the top middle of the screen. I can get the good job to come up but it just flashes by and when CLS runs it turns to a blue screen I don't want that blue screen. Please help me with this.
CLS

SYNC ON
SYNC RATE 35
HIDE MOUSE

` Hit will be a boolean value = true if collision has occured
hit AS INTEGER = 0

`Set background color
INK 0, RGB(0,0,0)

`Load picture
LOAD IMAGE "ball.bmp",1
LOAD IMAGE "brick.bmp",2


`Where to put the sprites
x = 100
y = 100
SPRITE 1,X,Y,1

`First brick
x1 = 0
y1 = 0
SPRITE 2,X1,Y1,2

`Second Brick
x2 = 0
y2 = 405
SPRITE 3,x2,y2,2

`Third brick
x3 = 540
y3 = 0
SPRITE 4,x3,y3,2

`Fourth brick
x4 = 540
y4 = 405
SPRITE 5,x4,y4,2

SPRITE 1, x, y, 1
CLS
REPEAT

`Controls
IF (RIGHTKEY())
x = x + 10
ENDIF

IF (LEFTKEY())
x = x - 10
ENDIF

IF (UPKEY())
y = y - 10
ENDIF

IF (DOWNKEY())
y = y + 10
ENDIF

`Top left brick deletes after ball hits
IF (SPRITE COLLISION(1, 2) > 0)
DELETE SPRITE 2
INK RGB(255, 0, 0),0
TEXT 310, 0, "Good Job"
SYNC
hit = 1
x = 320
y = 240
ENDIF

`Bottom left brick deletes after ball hits
IF (SPRITE COLLISION(1, 3) > 0)
DELETE SPRITE 3
INK RGB(255, 0, 0,),0
TEXT 320, 0, "Good Job"
SYNC
hit = 1
x = 320
y = 240
ENDIF

`Top right brick deletes after ball hits
IF (SPRITE COLLISION(1, 4) > 0)
DELETE SPRITE 4
INK RGB(255, 0, 0),0
TEXT 310,0, "Good Job"
SYNC
hit = 1
x = 320
y = 240
ENDIF

`Bottom right brick deletes after ball hits
IF (SPRITE COLLISION(1, 5) > 0)
DELETE SPRITE 5
INK RGB(255, 0, 0),0
TEXT 310, 0, "Good Job"
SYNC
hit = 1
x = 320
y = 240
ENDIF

SPRITE 1, x, y, 1

SYNC
CLS

IF (hit = 1)
Wait 3000
hit = 0
endif

UNTIL (ESCAPEKEY())
end
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 13th Feb 2007 02:57 Edited at: 13th Feb 2007 03:04
I'm no 2D master but maybe this will work? I'm not entirely sure though... It's untested.



I just erased a bunch of SYNC commands, the CLS you had, and added the SPRITE EXIST command before all of your SPRITE COLLISION checks, that way it will only check for collision if the brick is still there.



Also, when you post code here on the forum try putting it in a code box by typing [ code ] before your code and then [ /code ] after your code (without the spaces). So, this:

[ code ] CODE YAY! [ /code ]

Would turn out like this:



Shadow666
17
Years of Service
User Offline
Joined: 29th Jan 2007
Location:
Posted: 13th Feb 2007 03:20
yea that worked but the screen is blue now and isnt black the way i wanted it to be
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 13th Feb 2007 05:04 Edited at: 13th Feb 2007 05:16
OK, whinges out of the way first...

Please check the stickies at the top of the forum. These tell you how to post without getting whinged at - eg: Please state the version of DB you are using!

As Sixty Squares says, use code boxes - highlighting it and then clicking on the code button does the same thing as manually typing tags in.

The main thing this will do is maintain any indentation your code has. Don't use them and your indentation disappears.

So, if you don't already, indent your code. For me, length-wise your snippet was right at the limit of what I will bother looking at when not indented.

Experienced programmers indent their code and often won't even bother looking at non-indented code. You need to keep them on side.

With the code boxes and indentation, your code snippet looks like this:



OK, that said, your program is very linear and when it grows will quickly become unmanageable. You need to structure your programs at a very early stage to make them modular. This makes them much easier to add to and maintain.

You need to put your code into procedures and/or functions and call them from a main loop. This will also prevent you having some of the problems you are currently experiencing

An example general purpose skeleton layout can be found in my Program Layout tutorial (link in the stickies).

As for your issues (in no particular order):

1.


is better as:



though admittedly the result is exactly the same!

2. In a 3D game, the background colour blue is the default Backdrop colour. Use the Color Backdrop 0 command to change it to black. In 2D CLS 0 clears the screen to black. INK 0, RGB(0,0,0) as you have used sets the TEXT background colour - not the screen.

3. Under normal circumstances, you should only have one Sync in your program - inside the main program loop at the end. You shouldn't need a Sync after a text command, but if you do require one, then you often have to put the Sync before the Text command.

4. You have 4 sprite collision blocks checking for collision between sprite 1 and 2, 3, 4 and 5. These 40 or so lines could be replaced with:



I know many of these things come with coding experience, so I hope you don't mind me pointing them out now.

TDK_Man

Pillarofire
20
Years of Service
User Offline
Joined: 31st Dec 2003
Location: Good Question, <looks around.>
Posted: 13th Feb 2007 05:11 Edited at: 13th Feb 2007 05:15
Put CLS immediately after the REPEAT for a black screen.

EDIT: TDK has some very good advice. Better to take his word.
Shadow666
17
Years of Service
User Offline
Joined: 29th Jan 2007
Location:
Posted: 13th Feb 2007 05:44
thanx guys for the advice it works great now ty

Login to post a reply

Server time is: 2024-11-26 01:00:20
Your offset time is: 2024-11-26 01:00:20