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 / help to add background

Author
Message
reuben360
15
Years of Service
User Offline
Joined: 1st Oct 2009
Location: USA (IN)
Posted: 7th Oct 2009 09:04
I having troubles adding a background to my pong demo. Everything was working until I added the image and the paste image 1, 0, 0 and sync commands in the game loop.

Rem ***** Included Source File *****
REM September 06, 2009

Rem ***** Main Source File *****
rem make some variables
ballx as integer = 320
bally as integer = 240
speedx as integer = -2
speedy as integer = 1
px1 as integer = 600
pyl as integer = 240
px2 as integer = 20
py2 as integer = 240
score1 as integer = 0
score2 as integer = 0

rem slow down the game
sync on
sync rate 60
backdrop on
color backdrop 1
hide mouse

load image "background.png", 1

rem the game runs in as loop
do
paste image 1, 0, 0
sync
cls

rem chec shift/ctrl keys to move the left paddle
if shiftkey()
dec py2, 3.5
if py2 < 30 then py2 = 30
endif
if controlkey()
inc py2, 3.5
if py2 > 410 then py2 = 410
endif

rem check up/down to move the right paddle
if upkey()
dec py1, 3.5
if py1 < 30 then py1 = 30
endif
if downkey()
inc py1, 3.5
if py1 > 410 then py1 = 410
endif

rem move the ball
inc ballx, speedx
inc bally, speedy

rem bounce ball off the top/bottom of the screen
if bally < 0 or bally > 470 then speedy = speedy* -1.5

rem see if left player missed the ball
rem if so, player 2 scores, reset the ball
if ballx < 0
inc score2, 1
ballx = 320
bally = 240
speedx = 2
sleep 700
endif

rem see if left player missed the ball
rem if so, player 1 scores, reset the ball
if ballx > 630
inc score1, 1
ballx = 320
bally = 240
speedx = -2
sleep 700
endif

rem draw the ball
circle ballx, bally, 10

rem draw the paddles
box px1, py1, px1+16, py1+64, RGB(255,0,0), RGB(255,0,0), RGB(255,0,0), RGB(255,0,0)
box px2, py2, px2+16, py2+64, RGB(0,75,192), RGB(0,75,192), RGB(0,75,192), RGB(0,75,192)

rem collision detection
if point(ballx+5,bally+5) > 0
speedx = speedx* -1
endif

rem print the score
text 10, 10, "Player 1 SCORE: " + str$(score1)
text 498, 10, "Player 2 SCORE: " + str$(score2)

rem draw the screen
sync

loop
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 7th Oct 2009 15:40
You are pasting the image, synchronising the screen, then you clear the screen again, draw everything else and synchronise again.
This won't work well, as you'll probably see when reading this

So, in order to fix it, paste the image before any other drawing operations (like you are doing now) to draw it to the back, but remove the extraneous sync and cls commands from the beginning of the loop and you should be fine.

reuben360
15
Years of Service
User Offline
Joined: 1st Oct 2009
Location: USA (IN)
Posted: 8th Oct 2009 07:18
Hey thanks Rudolpho. I manage to get my image in the background of the pong game, but the paddles and ball go crazy when I remove the cls commmand. The coding still needs to be worked on.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 31st Oct 2009 05:52
Try putting the 'cls' command before you paste the image and after the 'do'.

A clever person solves a problem, a wise person avoids it - Albert Einstein

Login to post a reply

Server time is: 2024-11-24 01:49:17
Your offset time is: 2024-11-24 01:49:17