Array of Graphics Objects
Array of Graphics Objects
Array of Graphics Objects
Array of Graphics Objects
Array of Graphics Objects
Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects
Array of Graphics Objects Array of Graphics Objects
Array of Graphics Objects
Go Back  Xtreme Visual Basic Talk > > > Array of Graphics Objects


Reply
 
Thread Tools Display Modes
  #1  
Old 08-19-2012, 04:40 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default Array of Graphics Objects


I'm working on a game based off of the space invaders
game and i need to create an array of invaders that are
images of a space ship.

I've done it but nothing seems to be displaying, so i need
to show you what i have done to see what i did wrong.

First i had to dimension the array
Code:
' Number of invaders (ships) used 
Dim NumOfInvaders As Integer = 20

' Create array of invader graphics objects
Dim InvaderGrap(NumOfInvaders) As Graphics
Then later on in the code, i load the image
into the array
Code:
        ' Start moving the invaders
        For Me.X = 0 To NumOfInvaders

            ' Invaders are at the near left side of form, so drop
            ' invaders down by value of InvaderDrop. (Height of ship = 27)
            If dstInvaderRect(X).X < Me.ClientRectangle.Left And IRight(X) = False Then
                IRight(X) = True
                dstInvaderRect(X).Y += InvaderDrop
            End If

            ' Load ship image into graphics objects
            InvaderGrap(X) = Graphics.FromImage(bmpInvader)

            ' Draw the invader images in new location
            InvaderGrap(X).DrawImage(bmpInvader, dstInvaderRect(X), srcInvaderRect, GraphicsUnit.Pixel)

            ' Dispose of invader objects
            InvaderGrap(X).Dispose()
        Next
I have the form, which i'm draw on, invalidating in a timer

My shooter and laser beam show up just fine, so
i must be doing something wrong with my invader
graphics objects.
Reply With Quote
  #2  
Old 08-19-2012, 06:48 PM
passel's Avatar
passelArray of Graphics Objects passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

It looks like you're doing things backwards.
You get a graphics object from bmpInvader.
This will allow you to draw on bmpInvader.
I assume you don't want to draw on bmpInvader, but use it to draw on your game field.
In fact, it looks like your intent is to draw on the form.

You really should be doing your drawing in the paint event of the form.
Be sure to set the doubleBuffered option if you're going to be drawing on the form so that you don't get the flashing. By default, the form is designed for hosting controls, not a drawing surface for games.
But, setting the doubleBuffer property will at least give one of the basic needs for implementing a game.

When you want to draw a new frame of your game, do a Me.Refresh to trigger the paint event. In the paint event, a graphics object will be passed to you in e, e.Graphics.
Use this graphics object to do all your drawing on the form, and it will be drawing in the backbuffer set up by using the doubleBuffered option.
When you leave the paint event, the visible form is updated from the backbuffer in one shot.

Another thing to note, is that by default, when the paint event occurs the drawing area has already been cleared so you don't have to spend time clearing away old graphics, but you do have to redraw everything since it has been erased.

You may want to take some time and read through this thread.
It concerns trying to implement space invaders, although it wasn't finished.
There are other threads as well, but independent of writing the game, it would seem developing more knowledge of the tools you need to use to implement the game, such as how drawing works in VB.Net would be a prerequisite to writing any game.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 08-19-2012 at 07:01 PM.
Reply With Quote
  #3  
Old 08-19-2012, 07:48 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default Space Invaders VB.Net code

Have you looked at Reed Kimble's working VB.Net space invaders example
available via the "XarcadeProject-GDI+ Version-735KB"" download link
on this page: "Sprites in VB.Net"

I think you will find the Sprite and Render classes inside that project
very useful to study (if you can hopefully understand them).
Reply With Quote
  #4  
Old 08-19-2012, 11:20 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Quote:
It looks like you're doing things backwards.
You get a graphics object from bmpInvader.
This will allow you to draw on bmpInvader.
I don't follow you.
bmpInvader is the memory bitmap and not
the graphics object.

You first have to create the bitmap before you
create the graphics object. Then you load the
bitmap into the graphics object like this

Code:
 ' Load ship image into graphics objects
            InvaderGrap(X) = Graphics.FromImage(bmpInvader)
This works fine for my single bitmaps but i'm having
trouble using arrayed graphics objects. None of them
will display.

I was just wondering if anyone has done this before
and if so, how. Am i doing it correctly or not?

Of course, i also had to create an array of rectangles to.

I had to declare them like this:
Code:
  ' Create an array of invader rectangles
 Dim dstInvaderRect(NumOfInvaders) As Rectangle
Later on i create all the rectangles i need
Code:
 
 For Me.X = 0 To NumOfInvaders ' NumOfInvaders = 21
            IRight(X) = True
            dstInvaderRect(X) = New Rectangle(0, 0, 42, 27)
            dstInvaderRect(X).Y = 0
 Next

Is this right?
Reply With Quote
  #5  
Old 08-20-2012, 12:11 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

OK. Passel words kept bounding around in my head
and i knew he saw something wrong that i didn't and
then it dawn on me. I was using the wrong bitmap as
Passel said
. Boy am i slow tonight.

Here was the offending code:
Code:
' Load ship image into graphics objects
InvaderGrap(X) = Graphics.FromImage(bmpInvader)
It should have been:
Code:
' Load ship image into graphics objects
InvaderGrap(X) = Graphics.FromImage(Me.BackgroundImage)
I'm drawing on the form, not the invader bitmap, just as passel said.

Thanks for pointing that out Passel.

Right now though, i'm only seeing one ship so i know
i need to rework some of my coordinates.
Reply With Quote
  #6  
Old 08-20-2012, 01:21 AM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default arrayed bitmap graphics

Quote:
This works fine for my single bitmaps but I'm having
trouble using arrayed graphics objects. None of them will display.
At the risk of being ignored again (but others who find this thread after it dies may still benefit..)
Display sprites from spritesheet using 2D rect clipping from bitmap, (screenshot)
Reply With Quote
  #7  
Old 08-20-2012, 02:25 AM
PlausiblyDamp's Avatar
PlausiblyDampArray of Graphics Objects PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

A Graphics object is something you draw onto, there is no need to maintain a separate graphics object for each image. You should try to keep the number of Graphics objects in use to a minimum as they are quite an expensive resource.

The only graphics object you should need in this case is the one provided in the form's Paint event.

I would strongly recommend you follow the links surfR2911 has posted has they are going to be very useful to you.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #8  
Old 08-20-2012, 03:31 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Sorry surf. I wasn't ignoring you. I checked out
the links you provided but nothing seemed to be
what i was searching for. I do value you input though.

By the way, that _bitmapClone_clipRect project
will come in handy. I was going to make a program
next, that cuts tiles out of sprite sheets. You just
saved me a lot of work and taught me a new way
of doing this. Thank you Thank you......

PlausiblyDamp:

Maybe my terminology is wrong. If a graphics object
is something you draw on, like a picturebox or a memory
bitmap then what is it called when you dimension something
as graphics.

Dim mypic as Graphics <-- What is that called then?


Anyway, i got my game working but with one slight problem.

As you know by now, i have an array of images serving as the
invaders in my game. The problem right now is, if i have 5 images
on the screen and i shoot the one in the middle, the image on the
end will disappear.

I have a sub that checks to see if an image was hit with a laser beam
or if you want to call it a bullet, that fine. The sub works fine. It detects
the hit, right away but it removes the wrong image.

Code:
 Private Sub CheckHit()

        For Me.X = 0 To NumOfInvaders

            ' Check to see if the shot fired, actually hit the invader
            If (dstShotRect.Y + dstShotRect.Height >= dstInvaderRect(X).Y) And (dstShotRect.Y <= dstInvaderRect(X).Y + dstInvaderRect(X).Height) And (dstShotRect.X + dstShotRect.Width >= dstInvaderRect(X).X) And (dstShotRect.X <= dstInvaderRect(X).X + dstInvaderRect(X).Width) Then

                ' Create a graphics object for the shot image
                Dim ShotGrap As Graphics = Graphics.FromImage(Me.BackgroundImage)

                ShotGrap.Clear(Color.Black)

                ShotGrap.Dispose()

                ' We hit something so stop shooting
                ShotFired = False

                ' Reset the shot top location
                dstShotRect.Y = dstShooterRect.Y - dstShooterRect.Height

                ' Load ship image into graphics objects
                  InvaderGrap(X) = Graphics.FromImage(Me.BackgroundImage)

                 InvaderGrap(X).Clear(Color.Black)

                ' Dispose of invader objects
                InvaderGrap(X).Dispose()

                ' An invader was shot, so increment
                ' ShotDown by 1
                ShotDown += 1

                ' Decrease the number of invaders by 1
                NumOfInvaders -=1
                ' ================================
                ' This stuff isn't being used yet
                ' Increment intSound by 1
                intSound += 1

                ' Invader was shot, so play the hit sound
                '   With Hit
                '.Name = "Sound" & intSound
                ' .Play(Application.StartupPath & "\Hit.wav", False)
                '  End With
                ' ================================
            End If
        Next
       
        Me.Text = "Invaders shot down = " & ShotDown
    End Sub
Reply With Quote
  #9  
Old 08-20-2012, 05:56 AM
PlausiblyDamp's Avatar
PlausiblyDampArray of Graphics Objects PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you are holding an image that you want to draw onto something (such as a bitmap) then declare it as an Image.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #10  
Old 08-20-2012, 10:18 AM
Iceplug's Avatar
IceplugArray of Graphics Objects Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Quote:
Maybe my terminology is wrong. If a graphics object
is something you draw on, like a picturebox or a memory
bitmap then what is it called when you dimension something
as graphics.
Perhaps this will help:
If you were to consider an artist drawing a picture, the Graphics object is the artist and the Bitmap/Image is the picture that he has drawn.

Thus, you make an image first and then get an artist (Graphics object) to draw on the image... you also don't need 64 artists to draw invaders on your form - only one artist can do all of that work (you wouldn't hire 64 artists to draw on one piece of art, would you), plus, as PlausiblyDamp said:
Quote:
You should try to keep the number of Graphics objects in use to a minimum as they are quite an expensive resource.
- just as artists are.

So:
Code:
' Create a graphics object for the shot image
                Dim ShotGrap As Graphics = Graphics.FromImage(Me.BackgroundImage)
...
 InvaderGrap(X) = Graphics.FromImage(Me.BackgroundImage)
This guy (as it should only be one guy who draws on this image) should be declared at the beginning of the program rather than getting a bunch because you're assigning one in a loop.

Hopefully, this helps you conceptualize what you are doing.

Also, if you want to cover a specific invader (assuming that you aren't continuously drawing to the form), I would just draw a black rectangle over him using .DrawRectangle or something similar, vice clearing a picture. I don't see how clearing the picture is doing anything besides getting rid of all of your invaders.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #11  
Old 08-20-2012, 02:33 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default Clearing - the best way possible..

Quote:
Originally Posted by VB_Alien
Sorry surf. I wasn't ignoring you.
Good to know.

Quote:
Originally Posted by VB_Alien
By the way, that _bitmapClone_clipRect project
will come in handy. I was going to make a program
next, that cuts tiles out of sprite sheets. You just
saved me a lot of work and taught me a new way
of doing this. Thank you
Also good to know.
A lot of times I've already been down the path you are treading for the first time and
I have sort of a felling about coding "mileposts" you are due to pass in your journey.
Thus, much of the code I offer or link to are anticipatory,
and not always deal with the specific coding issue you are dealing with at the moment
(I'm usually impatient for you to get past a things that should be simple to undertsnad
and get to stuff that's a really interesting coding (or design) issue..

Quote:
Originally Posted by Iceplug
I would just draw a black rectangle over him using .DrawRectangle or something similar, vice clearing a picture. I don't see how clearing the picture is doing anything besides getting rid of all of your invaders.
Iceplug knows game programming..and he is right.

You are calling Clear twice:
Code:
ShotGrap.Clear(Color.Black)
InvaderGrap(X).Clear(Color.Black)
You are also calling Graphics.FromImage(Me.BackgroundImage) twice:
Code:
' Create a graphics object for the shot image
Dim ShotGrap As Graphics = Graphics.FromImage(Me.BackgroundImage)

' Load ship image into graphics objects
InvaderGrap(X) = Graphics.FromImage(Me.BackgroundImage)
Both of these represent code optimization opportunities.

You might look at the section on this page called:
"Intelligent Invalidation, i.e. Think Before You Paint".
I describes using using clipping along with FillRectangle to specific target the areas you need clear or refreshed or invalidated.

The Invalidate call causes the Paint Event to repaint.
However, how it does it's invalidate painting is up to you.
You don't have to re-draw the whole e.Graphics.

You can code a specific sub to erase just
the section that contains the previously drawn
space invader, (or any others graphics that have moved..like bullets),
at its old position, before drawing it at its new position.

Worth noting - controls, like pictureboxes,
are always double buffered in .Net
(and a Form can be doublebuffered if
its DoubleBuffered property is set to true).

By default a clipped invalidation draws from the background graphic that is doublebuffered (hiding in memory to be used for window-on-window refreshes).
This a good and useful thing..especially in you are game programming.

Last edited by surfR2911; 08-20-2012 at 02:39 PM.
Reply With Quote
  #12  
Old 08-20-2012, 02:34 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Iceplug:

Then i was right by your description of the graphics object.

When i Dim mypic as Graphics then mypic becomes
the graphics object or artist that is doing the drawing.

I do agree the i am using way to many graphic objects. I knew it
didn't make any sense, even to me, when i coded it that way but
i needed a way to keep track of each individual invader.

I also have an array of rectangles for my invaders. Should i be using
those to keep track of them instead?

I don't know how else to do it.

Maybe using a structure (UDT) would be better.
Reply With Quote
  #13  
Old 08-20-2012, 02:42 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default sprite storage structures.

Quote:
I also have an array of rectangles for my invaders. Should i be using
those to keep track of them instead?

I don't know how else to do it.

Maybe using a structure (UDT) would be better.
This issue is at the heart of another thread,
(which by shear good timing "co-wink-idence" happens to be going on at the same time as this thread..)

Note: UDTs are a VB6 thing, not really a .Net thing.

Also by the way - the solution to that thread could be
also be offer a great addition to
your tile map editor..specifically being able to separate out your
"place-ables" sprtiesheet into clipped out sprite objects
plopped into a gridded panel with checkerboard-as transparent background, then allow dragging them on top of your tile map tiles in the main editor area.

Some interesting to code to think about the possibilities:
Code:
Dim imgPictures(8) As Image
imgPictures(0) = My.Resources.ResourceManager.GetString("myResourceName")
' For assigning a resource to a image control based on your array 
' of strings use something  like this:
Me.picture1.Image = My.Resources.ResourceManager.GetObject(arr(i))
I prefer it this way (using graphics as resources internally "tucked" into a .Net project).

However it should also be possible to load a bunch of graphic files into a directory/folder and have them sucked up into an image array using something like:
Code:
Dim dir = New IO.DirectoryInfo("C:\Users\turcotd\Desktop\ITLPers")
Dim images = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories).ToList
Have you read through this MSDN Structure Design article?

Last edited by surfR2911; 08-20-2012 at 03:06 PM.
Reply With Quote
  #14  
Old 08-20-2012, 02:56 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

surfR2911:

Quote:
You are calling Clear twice:
I have to be honest. I'm not sure how that works.
In my thinking, calling clear from a different graphics
object, only clears the image in that object.

So i called it once to clear out the laser beam(bullet)
and from a different graphics object, i called clear
again to remove the bitmap in that object.

Of course, now i'm being told that i have to many
graphics object in play, so i'm going to change that.

About UDT's, i know that is vb6 but the structure type
replaced that, as far as i know. I've used it and it does work.
Reply With Quote
  #15  
Old 08-21-2012, 01:32 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

surfR2911

I thought the purpose of this sight was to help others. It looks like
your here to make to people like me look bad and shame them.

If that's how your going to help people then GO TO HELL.

I think the mods should lock you out of here.

MODS:

You can ban me out of here to. I'm not coming back
to a place that belittles people....
Reply With Quote
  #16  
Old 08-21-2012, 02:34 AM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default belittle - no!

My purpose was not to belittle you at all but to help you.
However since it was taken the wrong way I apologize
and will delete the message.
Reply With Quote
  #17  
Old 08-21-2012, 03:10 AM
passel's Avatar
passelArray of Graphics Objects passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Well, perhaps this is for nought (or naught?), but I did put together a little example using the old two state animation shapes for the classic Space Invaders game, as an example of doing the drawing in the Paint event of the form.
It is just a few hours of planning and another few hours coding, with another hour or so adding all the comments before posting.
Didn't try to implement the game, so no defender movement, shooting, or obstacles.
It just sets up the sprites and draws the 5 x 11 array of aliens and moves them back and forth and down the screen. Once they reach the defender or you remove them all, the game is over and nothing else will happen. You have to exit the program.

"Remove them all", you say, but shooting is not implemented.

To allow testing of the speeding up of the motion as the aliens are reduced in number, I implemented a simple click on them to remove them code.
That's about it.

p.s. Future expanded versions of this example may be found in this thread.
Attached Files
File Type: zip QuickSpaceInvaderExample.zip (22.8 KB, 14 views)
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 08-22-2012 at 12:04 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Array of Graphics Objects
Array of Graphics Objects
Array of Graphics Objects Array of Graphics Objects
Array of Graphics Objects
Array of Graphics Objects
Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects Array of Graphics Objects
Array of Graphics Objects
Array of Graphics Objects
 
Array of Graphics Objects
Array of Graphics Objects
 
-->