Results 1 to 10 of 10

Thread: two layers of graphics

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    4

    two layers of graphics

    I am trying to do a game application similar to chess, where i need to move the game peaces(graphics) over another graphics which is the game board. using the mouse. How can I do multiple layers of graphics and control their movement?

    i hope am not bothering u guys, please in details, I know basic but new to visual

    Note: I have visual basic 2010 express, windows xp home

    thanks in advance

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: two layers of graphics

    DirectX view-spaces have a ZOrder for sprites drawn on them. You can separate layers by using ZOrder range adders. Layer 0 adds 0, Layer 1 adds 100 to the ZOrder, Layer 2 adds 200... etc.

    By storing all sprites in a certain layer in a sprite collection of some sort, you can easily organize them since each collection would represent a layer.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    4

    Re: two layers of graphics

    thank you very much Jenner, but isnt their anyway simpler than that? I am barely good in VB, so how about direct x !!!
    I thought there is a way to move two images over each others without going that deep, its not an action game where timeing is an issue, its as simple as moving chess stones over the board

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: two layers of graphics

    DirectX is far simpler to use than trying to slide pictureboxes. Don't let "THE GREAT AND MIGHTY DIRECTX!!!" scare you. It requires a little more setup time on your part and some secondary downloads from Microsoft (XNA Game Studio) but after that, you have the basic tools you need to make most any game.

    If you try to animate pictureboxes, you'll spend hours trying to time their movements so they look even halfway consistent, days trying to hack in transparency into the system so you can utilize things other than square graphics, and weeks trying to figure out why everything is flickering... all because you're trying to make the business winforms graphics system (GDI+) do what it was never, ever intended to do.

    On the other hand, you can download XNA Game Studio, create a new project, make a "Game" object which does all your drawing, rendering and timing control for you. Load in all your graphics as "Texture2D" objects, and slide them around your render window all day in silky-smooth, alpha-blended 60fps ease... all in about an hour's time.

    EDIT: I'm going to upload a simple, game-starter example to here and/or the codebank just to prove how simple creating the beginnings of a basic 2D game can be.
    Last edited by Jenner; Apr 8th, 2011 at 12:17 PM.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    4

    Re: two layers of graphics

    than u very much, but in the example; try to be as detailed as u can handle, i am totaly new to this

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: two layers of graphics

    Quote Originally Posted by Jenner View Post
    EDIT: I'm going to upload a simple, game-starter example to here and/or the codebank just to prove how simple creating the beginnings of a basic 2D game can be.
    I would love to see that.


    Edit#1:
    I found a basic 2D shooter game tutorial done in XNA and C# : http://create.msdn.com/en-US/educati...etting_started



    Edit#2
    I found another thing that, XNA is gonna support VB soon. Check this blog entry: http://blogs.msdn.com/b/lisa/archive...ual-basic.aspx
    Last edited by akhileshbc; Apr 8th, 2011 at 01:27 PM. Reason: found a tutorial; found another info

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #7
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: two layers of graphics

    Step 1. Google "XNA Game Studio". Go to Microsoft's site, find it, and install it. You may need Visual C# Express installed to even get the installer to work, though you don't need it to use XNA. For this example, I'll demonstrate using Visual Basic 2008 and XNA Game Studio 3.1.

    Step 2. Make a new WinForms project. Go into the project references and add "Microsoft.Xna.Framework" and "Microsoft.Xna.Framework.Game".

    Name:  AddReferences.png
Views: 2416
Size:  64.0 KB

    Next, all games need graphics. So I get three of them. Two are animation frames for my movable character that I'll call the "bee".
    Name:  beeboth.png
Views: 2088
Size:  1.5 KB
    The third is a background image the bee will travel on top of.
    Name:  back.gif
Views: 2479
Size:  138.9 KB

    I add them into my project.

    Next, I delete the default "Form1" object. I don't need it. "Forms" are for business app programmers, not game programmers

    Make a Module and create a Sub Main in it:

    Code:
    Module Module1
        'This exists only to serve as a starting point for the program and launch the game object.
        Public Sub Main()
            Dim g As New Game1
            g.Run()
        End Sub
    
    End Module

    Then, make a new object called "Game1" and Inherit an XNA game object to it. The complete code is below:
    Code:
    Imports Microsoft.Xna.Framework
    Imports Microsoft.Xna.Framework.Graphics
    Imports Microsoft.Xna.Framework.Input
    
    Public Class Game1
        'This is not a form!  This is an XNA Game object!
        Inherits Game
    
        'This sets up the DirectX rendering
        Private gDevMgr As New GraphicsDeviceManager(Me)
        Private gDevice As GraphicsDevice
    
        'The player location, represented by a 2D vector
        Private playerPosition As New Vector2(0, 0)
    
        'Timing values for the player animation
        Private timer As Double = 0
        Private interval As Double = 70
    
        'The game sprites it will draw
        Private mySprites As SpriteBatch
    
        'Info for our bee.   How many frames of animation does it have?  What is the current frame? 
        Private currentFrame As Integer = 0
        Private frameCount As Integer = 2
    
        'The direction our bee is facing.  I'm keeping it simple and only using left and right since I don't have the
        'graphics for front and back.
        Private facingLeft As Boolean = True
    
        'The bee's graphics
        Private beeSprite(1) As Texture2D
    
        'The background graphic
        Private backgrnd As Texture2D
    
        Protected Overrides Sub Initialize()
            'This is called when the game first starts.
    
            'Set my graphics options and set up the renderer.
            gDevMgr.PreferredBackBufferWidth = 500
            gDevMgr.PreferredBackBufferHeight = 500
            gDevMgr.ApplyChanges()
            gDevice = gDevMgr.GraphicsDevice
            AddHandler gDevice.DeviceReset, AddressOf OnResetDevice
    
            MyBase.Initialize()
        End Sub
    
        Protected Overrides Sub LoadContent()
            'This part runs after Initialize.  It loads in all your content.  
    
            'Setup my spritebatch and load my bee images as textures. 
            mySprites = New SpriteBatch(gDevice)
            beeSprite(0) = Texture2D.FromFile(gDevice, "bee1.png")
            beeSprite(1) = Texture2D.FromFile(gDevice, "bee2.png")
    
            backgrnd = Texture2D.FromFile(gDevice, "back.png")
    
            MyBase.LoadContent()
        End Sub
    
        Public Sub OnResetDevice(ByVal sender As Object, ByVal e As EventArgs)
            'This is just here in case the DirectX device gets reset.   If it does, re-set it up.
            If gDevice Is Nothing Then
                Me.Initialize()
            End If
        End Sub
    
        Protected Overrides Sub Update(ByVal gameTime As GameTime)
            'This is my game loop.  I need no timer.  
    
            'Get the keyboard status.  XNA has keyboard handling built into it.  
            Dim kState As KeyboardState = Keyboard.GetState
    
            If kState.IsKeyDown(Input.Keys.Right) Then
                playerPosition.X += 2.0F
                facingLeft = False
            End If
            If kState.IsKeyDown(Input.Keys.Left) Then
                playerPosition.X -= 2.0F
                facingLeft = True
            End If
            If kState.IsKeyDown(Input.Keys.Up) Then
                playerPosition.Y -= 2.0F
            End If
            If kState.IsKeyDown(Input.Keys.Down) Then
                playerPosition.Y += 2.0F
            End If
    
            'Add the current game time to the timer variable to see if we should flip to the next animation frame.
            timer += gameTime.ElapsedGameTime.TotalMilliseconds
    
            'Determine which animation frame to show.
            If timer > interval Then
                currentFrame += 1
                If currentFrame > frameCount - 1 Then
                    currentFrame = 0
                End If
                timer = 0
            End If
    
            MyBase.Update(gameTime)
        End Sub
    
        Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
            'This is the render loop.  It asynchronously draws to screen.
    
            'Dray a green background
            gDevice.Clear(Color.ForestGreen)
    
            'Render all the sprites in the spritebatch.  Since there's only two, this is easy.
    
            'Begin the render, set some parameters for this batch.
            mySprites.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None)
    
            'Draw the bee.
            mySprites.Draw(beeSprite(currentFrame), playerPosition, Nothing, Color.White, 0, New Vector2(0, 0), 1, If(facingLeft, SpriteEffects.None, SpriteEffects.FlipHorizontally), 0)
            'Draw the background.
            mySprites.Draw(backgrnd, New Vector2(0, 0), Nothing, Color.White, 0, New Vector2(0, 0), 1, SpriteEffects.None, 1)
            'Done!
            mySprites.End()
    
            MyBase.Draw(gameTime)
        End Sub
    
        'Using DirectX, we get smooth, flicker free animation.   
        'Game and render loops are standard operating procedure in game programming.   Learning 
        'how to time animations with these loops though takes practice.   Here, I’m counting the 
        'gametime  elapsed and checking if 70ms has gone by.  If so, go to the next frame.  
    
        'If I had to do this for multiple objects, I’d make a class to handle all the repetitive 
        'animation code and instance one per object, otherwise, the game and render loops would 
        'start getting bulky with animation timing routines. 
    End Class
    That's it! Run it, the effect looks like the picture below. Use the arrow keys to move the animated bee around the forest scene. Look how smoothly it moves!
    Name:  example.gif
Views: 4362
Size:  117.6 KB

    The final source code is below.
    VBNET and XNA Example.zip

    Now, you have to admit. That's EASY. That's DirectX, animation, movement in about a single page of extremely basic code. I'm not using anything more complex than an array in here.
    Last edited by Jenner; Apr 8th, 2011 at 05:06 PM.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    4

    Re: two layers of graphics

    I tried to do it with 2010 express, its diffirent, i managed to do it until this:


    Then, make a new object called "Game1" and Inherit an XNA game object to it. The complete code is below:
    how do u make an "object", and where i can write the game intellegance(the code in visual basic)?, how to use the mouse instead of the arrows to control the graphical objects?

  9. #9
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: two layers of graphics

    VB.NET is an "object oriented" language. Every component of it, is a generic "object". Think of an "object" like a generic box. Forms are objects. Controls are objects. You can even whip up your own custom objects out of thin air. In VB.NET, a generic, empty object is called a "Class".

    If I want to make a new, empty object, I just tell Visual Studio to add a new Class to my project.

    One of the aspects of any object oriented language is "inheritance". This allows me to take an already existing, fully fleshed out object, and copy all it's properties and methods to a new object. You do this unknowingly every time you make a new Windows Form for example. This allows you to "customize" that new object with specific data. If you look at the designer file of any Windows Form, you'll see the line:
    Code:
     Inherits System.Windows.Forms.Form
    This tells VB.NET to make this object inherited from a generic Form object found at: System.Windows.Forms.Form

    In my example, I'm inheriting a Game object to my generic Class. More specifically, I'm inheriting a:
    Code:
    Inherits Microsoft.Xna.Framework.Game
    My example only shows:
    Code:
    Inherits Game
    since I'm using some shorthand notation called "Imports". "Imports" tells VB.NET "If I tell you to look for an object, and you can't find it, also look in these locations."

    Looking at the top of my code, you'll see:
    Code:
    Imports Microsoft.Xna.Framework
    Imports Microsoft.Xna.Framework.Graphics
    Imports Microsoft.Xna.Framework.Input
    Once you make your "Game" object, you have all the tools to start writing your game logic.
    The generic Game object contains several Protected Overrides Subs that you can use for various tasks:

    Protected Overrides Sub Initialize() is called when the game first starts.
    Protected Overrides Sub LoadContent() is called to load in all your content.
    Protected Overrides Sub Update(ByVal gameTime As GameTime) is your game-loop. It's called several times a second as fast as possible to update your game-state.
    Protected Overrides Sub Draw(ByVal gameTime As GameTime) is your render loop. It's synchronized with your monitor's refresh and handles drawing the game screen.

    In addition to this, the Xna libraries contain oodles of helpful game-designing tools classes and methods.

    Keep working at getting my example running. VB2010 and XNA 4 aren't that different from VB2008 and XNA 3.1; not for what I'm showing you how to do. This is like game design 101 stuff. If you add the libraries as a reference to your project, and then make a new class and inherit the game object to it, you'll be good to go.
    Last edited by Jenner; Apr 11th, 2011 at 08:12 AM.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  10. #10
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: two layers of graphics

    Jenner

    Love your background image ..

    Spoo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width