Results 1 to 11 of 11

Thread: Writing enemies for game

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Writing enemies for game

    Hello, I am writing a 2D augmented reality game for now maybe I will make it 3D soon,but I want to experiment on 2D first.
    So I am new with writing games so my question is what is the principle of creating enemy attacking at some coordinates.

    Lets say that my user is now on Level.50 , how am I suppose to create 50 enemies attack at x,y coordinates with code,and how to make that when I click on them to destroy them ?
    The only idea that comes to my mind is to create a class Enemy and then to write the logic in it, and when the level increase the objects in the class enemy will also increase,but what the logic will be to attack at x,y coordinates and destroy each enemy just by click on them,and how to show then on screen (please don't say picturebox)

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Writing enemies for game

    Moved From The C# section To Games Programming

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Writing enemies for game

    Whoa are you asking how to write an entire engine ? You should be a little more specific on what you need at this given point in time. What you are asking is way too broad.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: Writing enemies for game

    Quote Originally Posted by Niya View Post
    Whoa are you asking how to write an entire engine ? You should be a little more specific on what you need at this given point in time. What you are asking is way too broad.
    No , I am just asking people with more experience than me in computer gaming how they will do it.
    I need to know how you will draw 50 enemies to user screen and make them attack at some location (x,y) will you use OOP and create objects or you will make a array of PictureBox and draw some code in it.
    I hope you understand me , because I don't how to explain it other way

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

    Re: Writing enemies for game

    My simple answer would be I'd make 50 "bad guy" objects, have each one reference a sprite, give each one an X/Y starting position and R starting rotation, and add them to an object collection that would get picked up by the rendering loop and drawn to the DirectX screen region at a smooth 60 times a second.



    Whether that makes sense or not though depends on how well you understand typical game design structure... i.e. how a simple "game" application is designed. I can go into details of you wish.
    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: Writing enemies for game

    Yeah it will be great to use xna or directX to draw the enemy and then to control it,but my project is inside form and there is picturebox I load the camera image and when the camera see a special marker, replace it with a tower just like the game here : http://www.ardefender.com/.
    and in order to add enemy which attack the tower(marker) i need to draw all the enemies on camera image to create augmented reality game. (the point is that i dont want to create 50 picturebox on camera image and control them.)
    Is there a way to implement my form code inside xna , I mean to implement the part with the camera image and then my code for recognizing special marker and replaced it again with a 2D model ?

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

    Re: Writing enemies for game

    You can pipe your DirectX view into a viewport, and place it on a WinForm. I made a couple of examples on here for some business-type applications that needed very fancy, higher speed, graphical displays (I believe I used a massive 10000x10000 graphic littered with 1000 sprites that could be mouse scrolled like a map and controlled via a WinForm panel on the screen). You lose some efficiency and speed, but if your game isn't very action oriented, it's very effective. Ideally, you want to write your own game controls and design the graphics for them yourself. I know there are plenty of examples of making XNA button, textbox, combobox and checkbox controls over on the Microsoft XNA forums.
    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
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Writing enemies for game

    Quote Originally Posted by mitko29 View Post
    No , I am just asking people with more experience than me in computer gaming how they will do it.
    I need to know how you will draw 50 enemies to user screen and make them attack at some location (x,y) will you use OOP and create objects or you will make a array of PictureBox and draw some code in it.
    I hope you understand me , because I don't how to explain it other way
    Me personally I don't use objects like pictureboxes to make games cause they look generic and lame. Instead I use DirectX. If you really wanna have a nice structure on enemies, the simplest way to do it is to create a structure:

    c# Code:
    1. public struct Sprite
    2.         {
    3.             int x, y;
    4.             bool Dead;
    5.         }
    6.  
    7.         Sprite[] Enemy = new Sprite[50];

    Although Sprites tend to have a lot more information, this is just to give you a general idea. And I use structures all the time in my DirectX games to keep track of sprite data.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: Writing enemies for game

    ok but is there a way to transform camera bitmap image in texture in xna or directX,this way I will be able to create augmented reality ?

  10. #10
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Writing enemies for game

    Ok to setup for DirectX9:
    Code:
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    -------------------------------
    Visual C# 2008
    -------------------------------
    Setting up Project:
    - Start a new project and create a Windows Forms Application.
    
    Setting up for DirectX9:
    - Go up in the menu to Project > Add Reference... 
    - Next goto .NET tab. 
    - Scroll down to Microsoft.DirectX and click it. 
    - Hold control and also add Microsoft.DirectX.Direct3D. You are welcome to adding more DirectX references if you choose. 
    - Continue to hold control and also add Microsot.DirectX.Direct3DX . This is used for texture mapping.
    - Click Ok. Now you are ready to code in DirectX9. 
                
    Note: If any of the DirectX stuff isn't located in your .NET tab, you may need to manually add it using your Browse tab under (your harddrive letter):\Windows\Microsoft.NET\DirectX For Managed Code\1.0.2902.0 and add your necessary .dll files such as Microsoft.DirectX.dll and Microsoft.DirectX.Direct3D.dll, etc. For any other new projects afterwards, if it's still not there, look under the Recent Tab.
    
    Project must be in X86 for DirectX9 apps to run. And must be done for every new DirectX project. Here's how to set it up.
    - Go up in the menu to Build > Configuration Manager... 
    - If Configuration Manager is not shown, you only need to do this one part once ever. On the Visual Studio Tools menu, click Options, expand Projects and Solutions, and then click General. Click to check Show advanced build configurations and Click Ok. Now it should show up in the menu bar within Build. Go up in the menu to Build > Configuration Manager... 
    - Go where it says Platform (and NOT Active Solution Platform). Select <New...>. A new window should pop up.
    - Where it says New Platform, select x86.
    - Click Ok.
    - Both Platform and Active Solution Platform should have changed to x86. Click Close.
    - You are now ready to code with DirectX
    
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    -------------------------------
    Visual C# 2010
    -------------------------------
    Setting up Project:
    - Start a new project and create a Windows Forms Application.
    
    Setting up for DirectX9:
    - Go up in the menu to Project > Add Reference... 
    - Next goto .NET tab. 
    - Scroll down to Microsoft.DirectX and click it. 
    - Hold control and also add Microsoft.DirectX.Direct3D. You are welcome to adding more DirectX references if you choose. 
    - Continue to hold control and also add Microsot.DirectX.Direct3DX . This is used for texture mapping.
    - Click Ok.
                
    Note: If any of the DirectX stuff isn't located in your .NET tab, you may need to manually add it using your Browse tab under (your harddrive letter):\Windows\Microsoft.NET\DirectX For Managed Code\1.0.2902.0 and add your necessary .dll files such as Microsoft.DirectX.dll and Microsoft.DirectX.Direct3D.dll, etc. For any other new projects afterwards, if it's still not there, look under the Recent Tab.
    
    Project must be in X86 for DirectX9 apps to run. And thankfully it's x86 by default. However the .Net Framework is 4.0 and needs changed to 3.5. Here's how to set it up:
    - Go towards the right of the screen where it says Solution Explorer.
    - Right click your project name and goto Properties.
    - On the left side, select Application if it's not there already.
    - Where it says Target Framework, select .NET Framework 3.5. Be sure to save your project prior to this or you will receive an error.
    - You are now ready to code with DirectX
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    And here is a texture mapping example. Be sure your image is within the \bin\x86\debug\ directory of your project:

    c# Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using Microsoft.DirectX;
    10. using Microsoft.DirectX.Direct3D;
    11.  
    12. namespace DX_Tut
    13. {
    14.     public partial class frmMain : Form
    15.     {
    16.         Device Device;
    17.         DisplayMode Display_Mode;
    18.         PresentParameters Screen;
    19.  
    20.         bool Fullscreen_Enabled;
    21.         bool Running;
    22.         CustomVertex.TransformedColoredTextured[] Vertex_List = new CustomVertex.TransformedColoredTextured[4];
    23.  
    24.         Texture Texture;
    25.  
    26.         public void DirectX_Initialize()
    27.         {
    28.             Screen = new PresentParameters();
    29.             if (Fullscreen_Enabled == true)
    30.             {
    31.                 Display_Mode.Width = 800;
    32.                 Display_Mode.Height = 600;
    33.                 Display_Mode.Format = Format.R5G6B5;
    34.                 Screen.Windowed = false;
    35.                 Screen.BackBufferCount = 1;
    36.                 Screen.BackBufferWidth = Display_Mode.Width;
    37.                 Screen.BackBufferHeight = Display_Mode.Height;
    38.             }
    39.             else
    40.             {
    41.                 Screen.Windowed = true;
    42.             }
    43.             Screen.SwapEffect = SwapEffect.Copy;
    44.             Screen.BackBufferFormat = Display_Mode.Format;
    45.             Device = new Device(0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, Screen);
    46.         }
    47.  
    48.         CustomVertex.TransformedColoredTextured Create_Custom_Vertex(float X, float Y, float Z, float RHW, int Color, float TU, float TV)
    49.         {
    50.             CustomVertex.TransformedColoredTextured Vertex = new CustomVertex.TransformedColoredTextured();
    51.             Vertex.Position = new Vector4(X, Y, Z, 1);
    52.             Vertex.Rhw = RHW;
    53.             Vertex.Color = Color;
    54.             Vertex.Tu = TU;
    55.             Vertex.Tv = TV;
    56.  
    57.             return Vertex;
    58.         }
    59.  
    60.         void Create_Polygon()
    61.         {
    62.             Vertex_List[0] = Create_Custom_Vertex(0, 0, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 0, 0);
    63.             Vertex_List[1] = Create_Custom_Vertex(100, 0, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 1, 0);
    64.             Vertex_List[2] = Create_Custom_Vertex(0, 100, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 0, 1);
    65.             Vertex_List[3] = Create_Custom_Vertex(100, 100, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 1, 1);
    66.         }
    67.  
    68.         void Draw_Polygon()
    69.         {
    70.             Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;
    71.             Device.SetTexture(0, Texture);
    72.             Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List);
    73.         }
    74.  
    75.         public void Load_Texture(string Filepath, int Transparency_Color)
    76.         {
    77.             Texture = TextureLoader.FromFile(Device, Filepath, 512, 512, 1, Usage.None, Format.A8B8G8R8, Pool.Managed, Filter.Point, Filter.Point, Transparency_Color);
    78.         }
    79.  
    80.         public void Render()
    81.         {
    82.             Device.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 0), 1.0f, 0);
    83.             Device.BeginScene();
    84.             Create_Polygon();
    85.             Draw_Polygon();
    86.             Device.EndScene();
    87.             Device.Present();
    88.         }
    89.  
    90.         public void Game_Loop()
    91.         {
    92.             do
    93.             {
    94.                 Render();
    95.                 Application.DoEvents();
    96.             } while (Running == true);
    97.         }
    98.  
    99.         public void Main()
    100.         {
    101.             if (MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    102.                 Fullscreen_Enabled = true;
    103.  
    104.             this.Show();
    105.             this.KeyPreview = true;
    106.             this.Width = 330;
    107.             this.Height = 250;
    108.             this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
    109.             this.Text = "DirectX Tutorial";
    110.             if (Fullscreen_Enabled == true)
    111.                 this.FormBorderStyle = FormBorderStyle.None;
    112.             DirectX_Initialize();
    113.             Load_Texture(Application.StartupPath + @"\Texture.jpg", 0);
    114.            
    115.             Running = true;
    116.  
    117.         }
    118.  
    119.         public void Shutdown()
    120.         {
    121.             Running = false;
    122.             Device = null;
    123.             Application.Exit();
    124.         }
    125.  
    126.         public frmMain()
    127.         {
    128.             InitializeComponent();
    129.         }
    130.  
    131.         private void frmMain_Load(object sender, EventArgs e)
    132.         {
    133.             Main();
    134.         }
    135.  
    136.         private void frmMain_KeyDown(object sender, KeyEventArgs e)
    137.         {
    138.             if (e.KeyCode == Keys.Escape)
    139.                 Shutdown();
    140.         }
    141.  
    142.         private void frmMain_Paint(object sender, PaintEventArgs e)
    143.         {
    144.             Game_Loop();
    145.         }
    146.  
    147.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    148.         {
    149.             Shutdown();
    150.         }
    151.  
    152.         private void frmMain_Resize(object sender, EventArgs e)
    153.         {
    154.             if (Fullscreen_Enabled == false)
    155.             {
    156.                 Running = false;
    157.                 Device.Reset(Screen);
    158.                 Render();
    159.                 Application.DoEvents();
    160.                 Running = true;
    161.             }
    162.         }
    163.     }
    164. }

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: Writing enemies for game

    Thanks for the explanation and the directX cod,but I decided to use XNA instead ,but I have a little problem:
    So first i wrote a application which take the camera image inside bitmap variable.
    Then in game1 class I load that video cam bitmap into another bitmap in game1 class , and after that I have transformed bitmap to 2DTexture.
    And heres the code:
    Code:
    using System;
    using System.Linq;
    using System.Drawing;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    
    namespace ilg
    {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        public class Game1 : Microsoft.Xna.Framework.Game
        {
           
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            Bitmap video;
            private Texture2D texture = null;
            private object sync = new object();
            public Game1(Form1 m)
            {
                video = m.mitko;            
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";            
            }        
            /// <summary>
            /// Allows the game to perform any initialization it needs to before starting to run.
            /// This is where it can query for any required services and load any non-graphic
            /// related content.  Calling base.Initialize will enumerate through any components
            /// and initialize them as well.
            /// </summary>
            protected override void Initialize()
            {
                // TODO: Add your initialization logic here
    
                base.Initialize();
            }
    
            /// <summary>
            /// LoadContent will be called once per game and is the place to load
            /// all of your content.
            /// </summary>
            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                
    
                // TODO: use this.Content to load your game content here
            }
    
            /// <summary>
            /// UnloadContent will be called once per game and is the place to unload
            /// all content.
            /// </summary>
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }
    
            /// <summary>
            /// Allows the game to run logic such as updating the world,
            /// checking for collisions, gathering input, and playing audio.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Update(GameTime gameTime)
            {
                // Allows the game to exit
               // if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
               // {
               //     this.Exit();
              //  }
    
                // TODO: Add your update logic here
                if (video != null)
                {
                    texture = Tools.XNATextureFromBitmap(video, GraphicsDevice);
                }
                base.Update(gameTime);
            }
    
            /// <summary>
            /// This is called when the game should draw itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);
                lock (sync)
                {
                    if (texture != null)
                    {
                        spriteBatch.Begin(0, BlendState.Opaque);
                        spriteBatch.Draw(texture, new Vector2(0, 0), Microsoft.Xna.Framework.Color.White);
                        spriteBatch.End();
                        GraphicsDevice.BlendState = BlendState.Opaque;
                        GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                        GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
                        GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
                    }
                    // TODO: Add your drawing code here
    
                    base.Draw(gameTime);
                }
            }
        }
    }
    The point is that the game1 class have to show me the camera image,but it seems that I can initialize the texture in draw method.

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