Answered by:
I can't seem to get DrawImage to work with events and Shape Graphics

Question
-
I am working on a game where a projectile is launched at targets on the screen. The program detects a hit when the projectile overlaps the target. It works fine if the GraphicsWindow is just given a screen color. But when I try to use GraphicsWindow.DrawImage, the image appears but none of the other game graphics...the launcher, the projectiles nor the targets. IOW, the game doesn't seem to work at all. Or, is the "game" somehow going on "in back of" the image I placed in the GraphicsWindow?
This is the code that draws the image to the screen and the creates the screen.
imagepath="C:\Small Basic\DSC04276.jpg"
image=ImageList.LoadImage (imagepath)
w= ImageList.GetWidthOfImage (image)
h=ImageList.GetHeightOfImage (image)GraphicsWindow.Top = 0
GraphicsWindow.Left = 0
GraphicsWindow.Height = h
GraphicsWindow.Width = w
GraphicsWindow.DrawImage (image,0,0)The program goes on to use:
GraphicsWindow.KeyDown = keydown
GraphicsWindow.KeyUp = keyupto detect keys pressed and then creates and moves shapes about the screen.
Thanks Dan
Thursday, October 18, 2018 8:58 AM
Answers
-
If so and it works with setting by GW.BackgroundColor = ..., then perhaps it's because your GW dimensions
depend on w,h (image size) and your shapes for launcher, projectiles, target are outside of the GW now.
Please use for 'w' and 'h' the same values, you used before (NOT image dimensions) like so
imagepath="C:\Small Basic\DSC04276.jpg" ' w, h = ??? image=ImageList.LoadImage(imagepath) 'w = ImageList.GetWidthOfImage(image) 'h = ImageList.GetHeightOfImage(image) w = width you used before (when using .BackgroundColor) h = height you used before (when using .BackgroundColor) GraphicsWindow.Top = 0 GraphicsWindow.Left = 0 GraphicsWindow.Height = h GraphicsWindow.Width = w 'GraphicsWindow.DrawImage(image,0,0) GraphicsWindow.DrawResizedImage(image, 0,0, w,h) ' will be wrong scaled, but only for testing
...
...
or provide the full code (together with image w,h), e.g. as an ID.
Code for the 2 Subs you show above leaves to many unknowns like: how big or small are w,h, launchx, launchy, opacity, etc ???
- Edited by Pappa LapubEditor Thursday, October 18, 2018 6:42 PM
- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, October 18, 2018 9:37 PM
- Marked as answer by Rizdek Friday, October 19, 2018 9:15 AM
Thursday, October 18, 2018 6:41 PMAnswerer
All replies
-
I guess, that you're using GraphicsWindow.Fill... or GraphicsWindow.Draw... commands for the launcher, the projectiles and the targets + later on you use GraphicsWindow.DrawImage again in a loop or so.
If that's the case, then use Shapes (see in the 'Shapes' class) for launcher, the projectiles and the targets.
Shapes will stay on top of the background image and -drawings.
because see:
GraphicsWindow.FillRectangle(10,10, 100,100) '' same with .DrawRectangle etc. 'GraphicsWindow.DrawImage(image, 30,30) '' drawing is overwritten, BUT.... GraphicsWindow.BackgroundColor = "Red" '' drawing stays visible
- Edited by Pappa LapubEditor Thursday, October 18, 2018 10:31 AM
- Proposed as answer by litdev Thursday, October 18, 2018 5:27 PM
Thursday, October 18, 2018 10:27 AMAnswerer -
I am using Shapes commands for the launcher, projectiles and targets. Here is the code to launch the projectiles which happen to be white circles which get smaller and more opaque (fade) as the go down range to simulate a 3-D effect. I use an array variable to store up to 10 "circles" in the p[n] variable and create them at launchx and launchy.
Sub launch
For n = 1 To projNum
If pOn [n] = "False" Then
Shapes.ShowShape(p[n])
Shapes.Move(p[n],launchx,launchy)
Shapes.SetOpacity (p[n],30)
leftBarrel [n]="False"
pOn[n] = "True"
pCount[n] =1
px[n]=launchx
py[n]=launchy
'trajectoryxch and ych depend on the aim of the gun
pxch[n] = aimxch
pych[n] = trajectoryych
Shapes.ShowShape (p[n])
'this makes it drop out of the for loop since a projectile has fired so no more should fire
n=projNum 'this forces it out of the loop if a projectile is created
EndIf
EndFor
fire="False"
justFired="True" ' keeps it from repeat firing
EndsubThe projectiles are managed with a subroutine:
Sub manageProjectiles
For n=1 To projNum
If pOn[n] = "True" Then
'projectileRangeCount starts at 1 and counts up to a hundred
'when it gets to 100 it hits and program tests if it hits a target or not
pCount [n]= pCount [n] + 1
'the following will show the projectile getting smaller as the rangecount increases
Shapes.Zoom (p[n],((2-(pCount[n]/(maxpCount/2)))+1),((2-(pCount[n]/(maxpCount/2)))+1))
Shapes.Move(p[n],px[n],py[n])
If leftBarrel[n]="True" Then'this causes the projectile to fade as it "goes down range"
Shapes.SetOpacity (p[n],(opacity-((pCount[n]/5)+10))+100-pCount[n])
EndIf
px[n]=px[n]+pxch[n]
py[n]=py[n]+pych[n]
pxch[n]=pxch[n]+wind
pych[n]=pych[n]+grav
EndIf'following routine makes the projectile invisible until it "clears" the end of the barrel of the gun
If pCount[n]=turnProjectileOpaque Then
leftBarrel[n] = "True" 'the projectile has left the barrel so will be visible
Shapes.SetOpacity (p[n],opacity)
EndIf
If pCount[n]=maxpCount Then
whichProj=n 'used to test if it hit the target
landed="True" 'tells the game routine that a projectile has landed and it should test for a hit
pOn[n]="False" 'turns that projectile off
Shapes.HideShape (p[n]) 'hides that projectile
pCount[n]=0
EndIf
EndFor
EndSubThis all works if I just make the graphics window green or whatever color. But as soon as I try to draw an image as a background, I can't see the launcher (just a red rectangle), any projectiles, or the target.
Thanks for your response. Dan
Thursday, October 18, 2018 3:01 PM -
If so and it works with setting by GW.BackgroundColor = ..., then perhaps it's because your GW dimensions
depend on w,h (image size) and your shapes for launcher, projectiles, target are outside of the GW now.
Please use for 'w' and 'h' the same values, you used before (NOT image dimensions) like so
imagepath="C:\Small Basic\DSC04276.jpg" ' w, h = ??? image=ImageList.LoadImage(imagepath) 'w = ImageList.GetWidthOfImage(image) 'h = ImageList.GetHeightOfImage(image) w = width you used before (when using .BackgroundColor) h = height you used before (when using .BackgroundColor) GraphicsWindow.Top = 0 GraphicsWindow.Left = 0 GraphicsWindow.Height = h GraphicsWindow.Width = w 'GraphicsWindow.DrawImage(image,0,0) GraphicsWindow.DrawResizedImage(image, 0,0, w,h) ' will be wrong scaled, but only for testing
...
...
or provide the full code (together with image w,h), e.g. as an ID.
Code for the 2 Subs you show above leaves to many unknowns like: how big or small are w,h, launchx, launchy, opacity, etc ???
- Edited by Pappa LapubEditor Thursday, October 18, 2018 6:42 PM
- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, October 18, 2018 9:37 PM
- Marked as answer by Rizdek Friday, October 19, 2018 9:15 AM
Thursday, October 18, 2018 6:41 PMAnswerer -
Wow, thanks!!
You were exactly right. I thought, that because the size of the image was greater than the width and height dimensions I used to make the graphics window that at least the shapes would show up and I'd have to reposition them. But as soon as I resized the image with the command you provided, everything worked with the dimensions I used originally.
I'll mark this as answered.
Friday, October 19, 2018 9:15 AM