Parallax Effect, background not visible
Blitz3D Forums/Blitz3D Programming/Parallax Effect, background not visible
| ||
Hi. I have a bit of a dilemma with parallax sky programming. I have two different tile images that I have put into this program. The format of images I'm using is in the .PNG format. I have set the foreground and background images for the program, and set both of them to scroll. However, only the foreground image is visible. I believe the intent was to see both images simultaneously one behind the other. Here's the code, and verify how this code was REALLY supposed to work. Somehow I can't seem to see both images at once.; Sky.bb - This program provides a parallaxed sky for an airplane Graphics3D 800,600 ; Set the default AutoMidHandle and BackBuffer() properties AutoMidHandle True SetBuffer BackBuffer() ; This is the segment with the images for the program ; This is the first part of the background that is closer to the player backgroundimageclose = LoadImage("sky.png") ; There is also a more distant image in the background backgroundimagefar = LoadImage("skyfarther.png") ; Create the variable that tracks the scrolling scrolly = 0 ; This starts the main loop of the program While Not KeyDown(1) ; Make the screen as clear as possible to start Cls Text 0,0, "Press <Esc> to Quit. " ; Make both the background tiles properly sped TileImage backgroundimagefar,0,scrolly TileImage backgroundimageclose,0,scrolly*2 ; Increase the scrolly variable value scrolly = scrolly + 1 ; Return the variable for tracking to the original value if it exceeds the max value If scrolly >= ImageHeight(backgroundimageclose) scrolly = 0 EndIf Flip Wend ; This will conclude the main loop for the program |
| ||
Does the foreground image have transparency? By the way, if you want your text to be readable, then you need to draw it last (after the background images). |
| ||
Okay, I'll verify where the transparency effect comes into play. When I think of this sky effect, I think of the original Quake with the sky that featured this technology with scroll-parallaxing. This seems to work to have Paint Shop Pro or MS Paint even, there may be a way to make the picture a little on the half-transparent side. |
| ||
The 2D commands (like TileImage) don't support semi-transparency, only masking. |
| ||
Ah, yeah, yeah, I see that. Now that I'm thinking about it, I was looking at an example similar to this, and he seemed to use a black background for the foreground, but would that look weird with the other graphic having the sky-blue background? |
| ||
Oh yeah, that works, we have updated the code. I also added an airplane onto the program to show it off even more. You have the clouds in the foreground, then I added the ground below in the background, and then the plane is the object that you can rotate around, and it looks like you're really flying. It's an awesome combination of code, here's what it looks like. Now all I need is for the plane to shoot pellets, and to make some enemy planes that come into view. Does a game like that sound familiar to you? Here's the code:; Sky.bb - This program provides a parallaxed sky for an airplane Graphics 800,600 ; Set the default AutoMidHandle and BackBuffer() properties AutoMidHandle True SetBuffer BackBuffer() ; This is the segment with the images for the program ; This is the first part of the background that is closer to the player backgroundimageclose = LoadImage("sky.png") ; There is also a more distant image in the background backgroundimagefar = LoadImage("ground.png") ; This is the image segment of the program ; The first steps load the image into the program planeimage = LoadImage ("plane.png") ; These load the constants in the program ; The following line determines the total amount of rotations Const rotations = 16 ; This will create the array of rotations Dim imagearray(rotations) ; The following will take that rotation variable and copy that image for rotating to the specific degree amounts For frame = 0 To rotations - 1 imagearray(frame) = CopyImage (planeimage) RotateImage imagearray(frame), frame*360/rotations Next ; Begin at the first frame (0) and face north frame = 0 ; Create the variable that tracks the scrolling scrolly = 0 ; This starts the main loop of the program While Not KeyDown(1) ; Make the screen as clear as possible to start Cls ; Make both the background tiles properly sped TileImage backgroundimagefar,0,scrolly TileImage backgroundimageclose,0,scrolly*2 ; Increase the scrolly variable value scrolly = scrolly + 1 ; Return the variable for tracking to the original value if it exceeds the max value If scrolly >= ImageHeight(backgroundimageclose) scrolly = 0 EndIf ; Add some text to the program Text 0,0, "Press <Left> to turn left, press <Right> to turn right." Text 0,12, "Press <Esc> to Quit: " ; IF player chooses to move left, turn the plane to the left If KeyDown (203) ; Decrease the frame by 1 to rotate the image frame = frame - 1 ; Once the frame goes lower than 0, reset the maximum value for that array If frame <= 0 frame = rotations - 1 EndIf ; If player chooses to move right, turn the plane to the right ElseIf KeyDown (205) ; Increase the value by 1, and turn the plane frame = frame + 1 ; If the frame gets to be higher than the maximum value, return to the original frame If frame >= rotations frame = 0 EndIf EndIf ; Take the present frame, and draw it DrawImage imagearray(frame), 400,300 Flip ; Standby for a brief delay Delay 50 Text 0,0, "Press <ESC> to Quit: " Wend ; This will conclude the main loop for the program Looks like it'll be an interesting turn of events. I guess that we'll see some good stuff coming out of this. |
| ||
Well, here's where I am at this point. I'm trying to incorporate true stereo sound to this, so when you rotate your airplane right, the engine'll play out of the right speaker. However, with the left speaker, the sound doesn't seem to want to switch to the left speaker, and I'm quite sure I did the code right, here it is: ; b-17.bb - This is the source code for the game project, B-17: 1942 ; Set the graphics resolution Graphics3D 800,600 ; Load the background in backimage = LoadImage ("background.png") ; Start playing the background music thememusic = PlayMusic ("b17.mp3") ; Draw in the background to match the program DrawImage backimage,0,0 ; Load the airplane picture foreimage = LoadImage ("b17g.png") ; Put the B-17 in the middle MidHandle foreimage ; Mask the B-17 image MaskImage foreimage,0,0,10 ; Draw the airplane in the middle of the screen DrawImage foreimage,320,240 Flip ; Wait for a keypress from the user WaitKey ; Make sure the screen is clear before proceeding Cls ; Set the initial AutoMidHandle and BackBuffer() AutoMidHandle True SetBuffer BackBuffer() ; This is the part where the media is loaded ; First, load in the airplane image that will have the rotation planeimage = LoadImage ("airplane.png") ; First, load the clouds image in, since it's closer to the airplane cloudsimage = LoadImage("clouds.png") ; Then, load the ground below into the program which is further away groundimage = LoadImage("ground.png") ; Then, load the background music for this game warmusic = PlayMusic("b17.mp3") ; Next, create a variable to track the scrolling scrolly = 0 ; Now, load the sound effect of the airplane flying planesound = LoadSound ("b17.wav") propsound = LoadSound ("prop.wav") ; Load the constants for this program Const rotations = 16 ; Now, create a variable that rotates this array Dim imagearray (rotations) ; With all the rotations specified, copy and rotate the airplane image with the respective amount of rotations For frame = 0 To rotations - 1 imagearray(frame) = CopyImage (planeimage) RotateImage imagearray (frame), frame * 360 / rotations Next ; Start the airplane at the first (North) frame frame = 0 ; This begins the main loop of the program While Not KeyDown(1) ; Make sure the screen is clear Cls ; Next, set the images into place at their respective speeds TileImage groundimage,0,scrolly TileImage cloudsimage,0,scrolly * 2 ; Increase the scrolly image value scrolly = scrolly + 1 ; Now, reset the scrolling tracking variable when it exceeds the maximum value If scrolly >= ImageHeight(cloudsimage) scrolly = 0 EndIf ; Play the sounds of the airplane PlaySound planesound PlaySound propsound ; Now, add the text for the user's instructions Text 0,0, "Press <Left> to Turn Left. Press <Right> to Turn Right." Text 0,12, "Press <Esc> to Quit. " ; Set the plane to rotate left if the user chooses left If KeyDown (203) ; Decrease the frame by 1 respectively frame = frame - 1 ; Play the plane sound out of the left speaker pan# = -1.000 ; If the frame is below the minimum level, reset it to the maximum value of the array If frame <= 0 frame = rotations - 1 EndIf ; Set the plane to rotate right if the user chooses right ElseIf KeyDown (205) ; Increase the frame by 1 respectively frame = frame + 1 ; Play the plane sound of of the right speaker pan# = 1.000 ; If the frame goes above the maximum level, reset the plane to the first frame If frame >= rotations frame = 0 EndIf SoundPan planesound, pan# SoundPan propsound, pan# EndIf ; Take the current frame, and draw it DrawImage imagearray(frame),400,300 Flip ; Standby for a brief delay Delay 50 Wend Not too sure how this is going to work. Also, it would be nice to still learn how to incorporate enemy plane targets, and to be able to shoot pellets at them, and make them explode. Still have to go through all that mess. However, to get the sound panned right between the right and left channels, that's my primary concern for right now. |
| ||
Because your SoundPan commands are within the bounds of your If KeyHit(205) command. Try indenting your code. It helps A LOT to uncover mistakes like this. Heres what you've got (indented): If KeyDown (203) ;Set the plane to rotate left if the user chooses left ; Decrease the frame by 1 respectively frame = frame - 1 ; Play the plane sound out of the left speaker pan# = -1.000 ; If the frame is below the minimum level, reset it to the maximum value of the array If frame <= 0 frame = rotations - 1 EndIf ElseIf KeyDown (205) ; Set the plane to rotate right if the user chooses right ; Increase the frame by 1 respectively frame = frame + 1 ; Play the plane sound of of the right speaker pan# = 1.000 ; If the frame goes above the maximum level, reset the plane to the first frame If frame >= rotations frame = 0 EndIf SoundPan planesound, pan# SoundPan propsound, pan# EndIfand heres what you should have (indented): If KeyDown (203) Set the plane to rotate left if the user chooses left ; Decrease the frame by 1 respectively frame = frame - 1 ; Play the plane sound out of the left speaker pan# = -1.000 ; If the frame is below the minimum level, reset it to the maximum value of the array If frame <= 0 frame = rotations - 1 EndIf ElseIf KeyDown (205); Set the plane to rotate right if the user chooses right ; Increase the frame by 1 respectively frame = frame + 1 ; Play the plane sound of of the right speaker pan# = 1.000 ; If the frame goes above the maximum level, reset the plane to the first frame If frame >= rotations frame = 0 EndIf EndIf SoundPan planesound, pan# SoundPan propsound, pan# For some tips, check out my programming tutorial website. Heres some indenting tipe on the site: http://home.cmit.net/rwolbeck/programmingtutorial/reference/goodhabits.htm#indentation . |
| ||
Okay, thank you. Now, as far as shooting pellets, I'm going to have to find some code on how to press the space bar, and shoot pellets out at your enemy, like a real war shooter flight game. Also, I'm debating whether to keep the WWII theme, or whether to go to something more current, like Iraq or Afghanistan, and use an F-117A or B-2. I mean, the B-17's okay, but there's so many WWII games being made, it's ridiculous anymore. I usually like to influence game development around current events if I can. I dunno how many flight games there are not geared around the Middle East. Not as many as FPS army-type games, I bet. |
| ||
Heres some code snippets from a simple game I made with a stationary 'ship' that fires bullets upwards from the bottom of the screen. I'm sure you could modify it to fit your needs. Type bullet Field image Field bulletx# Field bullety# Field xangle# Field yangle# End Type bulletimage = LoadImage("bullet.png") MidHandle bulletimage shoot = KeyDown(57) ;space If shoot And MilliSecs() > lastshot thisBullet.bullet = New bullet thisBullet\image = CopyImage(bulletimage) RotateImage thisBullet\image, rotation thisBullet\bulletx# = 400 thisBullet\bullety# = 550 thisBullet\xangle# = Sin(rotation) thisBullet\yangle# = -(Cos(rotation)) lastshot = MilliSecs() + 1000 EndIf ;bullet advance For thisBullet.bullet = Each bullet thisBullet\bulletx# = thisBullet\bulletx# + thisBullet\xangle# * 5 thisBullet\bullety# = thisBullet\bullety# + thisBullet\yangle# * 5 If thisBullet\bulletx# < -40 Or thisBullet\bulletx# > 840 Or thisBullet\bullety# < -40 FreeImage thisBullet\image Delete thisBullet EndIf Next ;draw bullets For thisBullet.bullet = Each bullet DrawImage thisBullet\image, thisBullet\bulletx#, thisBullet\bullety# If ImagesCollide(thisBullet\image, thisBullet\bulletx#, thisBullet\bullety#, 0, enemy, (enemyx-1)*40, (enemyy-1)*40, 0) KillEnemy(enemy) EndIf Next The rotation variable in this code: RotateImage thisBullet\image, rotationis equal to the 'ships' rotation. Set the bullets starting x,y values here: thisBullet\bulletx# = 400 thisBullet\bullety# = 550equal to the 'ships' location in your game. |
| ||
Okay, I shall use that. I did dump the B-17, BTW. World War II gaming's just been too popular. However, I am using this code towards another air combat game, called "Nighthawk Strike." You can probably imagine which fighter aircraft's gonna be used in this game. It'll be taking place in the Middle East, so you know that this game's as current as I can make it. |