[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

04/12/2009 at 02:38PM PDT, ID: 24316113
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.5

How to create solid object in my game.

Asked by KG1973 in Game Programming, Miscellaneous Games, Other Strategy Games

Hi,
I am new with game development. Currently I am creating simple game (VS 2008 C++ and XML). The game should be simple. The following code actually contain :
1. Background (garden)
2. Tree
3. Boy character, walking around the garden.

The boy can walk around including passig through the Tree.

What I want to do is,
1. to create a new object/sprite called "Fence". And the boy can walk anywhere around the garden, but he should not be allowed to pass through the fence. I assumed that the fence is solid object. At this stage I don't border so much the location of the fence.

2. create another obeject, let say "Tunnel" or "Cave". When the boy go into the tunnel/Cave, then new screen will be displayed, as if new level of the game with new background but with the same character (Boy).

I'd marked the code //================= Help me here ============================, which I think where the changes should be, or state otherwise.

Thanks.
          
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace WindowsGame1
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
 
        //Global Variable Declarations
        Texture2D backgroundTexture;
        Texture2D sprite; 
        Texture2D sprite1;
        Point frameCounter = new Point(0,0);  
        Point frameCounter1 = new Point(0, 0);
        Vector2 position = Vector2.Zero; 
        Vector2 speed = Vector2.Zero; 
        SpriteEffects se =SpriteEffects.FlipHorizontally; 
 
        
        double frameDelay = 0.1; 
        double frameDelay1 = 0.5;
        Level level; //xml level code that will be serialized into this object
        SpriteFont spriteFont;
 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
 
        protected override void Initialize()
        {
            XmlSerializer s = new XmlSerializer(typeof(Level));
            TextReader r = new StreamReader("Content/Level.xml");
 
            level = (Level)s.Deserialize(r);
 
            r.Close();
 
            
            foreach (Level.character c in level.allCharacters)
            {
                c.sprite = Content.Load<Texture2D>(c.textureAsset);
                c.bound = new BoundingSphere(new Vector3(c.position , 0), 50);
            }
 
            base.Initialize();
        }
 
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteFont = Content.Load<SpriteFont>("Arial");
 
            backgroundTexture = Content.Load<Texture2D>("Background");
            sprite = Content.Load<Texture2D>("Boy");
            sprite1 = Content.Load<Texture2D>("Tree");
 
 
	    //================= Help me here ============================
	    //
            //create new sprite here called Fence or wall
            //png file will be added into the content called Fence.png
 
 
 
        }
 
        protected override void UnloadContent()
        {
        }
 
        protected override void Update(GameTime gameTime)
        {
 
            KeyboardState kb = new KeyboardState();
            kb = Keyboard.GetState();   
            speed = Vector2.Zero;       
            bool moving = false;         
 
            if (kb.IsKeyDown(Keys.Right))
            {
                
                speed.X = 2;
                moving = true;
                
                se = SpriteEffects.FlipHorizontally;
                frameCounter.Y = 0;
            }
            if (kb.IsKeyDown(Keys.Up))
            {
                speed.Y = -2;
                moving = true;
                frameCounter.Y = sprite.Height * 2 / 3;
            }
            if (kb.IsKeyDown(Keys.Left))
            {
                speed.X = -2;
                moving = true;
                
                se = SpriteEffects.None;
                frameCounter.Y = 0;
            }
            if (kb.IsKeyDown(Keys.Down))
            {
                speed.Y = 2;
                moving = true;
                frameCounter.Y = sprite.Height / 3;
            }
            
            position += speed;
 
            if (moving)  
            {
                
                frameDelay -= gameTime.ElapsedGameTime.TotalSeconds;
                if (frameDelay < 0)
                {
                    
                    frameDelay = 0.1;
                    frameCounter.X += sprite.Width / 8; 
                    if (frameCounter.X >= sprite.Width) frameCounter.X = 0; 
                }
            }
            frameDelay1 -= gameTime.ElapsedGameTime.TotalSeconds;
            if (frameDelay1 < 0)
            {
                
                frameDelay1 = 0.5;
                frameCounter1.X += sprite1.Width / 5; 
                if (frameCounter1.X >= sprite1.Width) frameCounter1.X = 0; 
            }
            base.Update(gameTime);
        }
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Green);
            spriteBatch.Begin();
 
            //Draw Background (Background with trees, rock, garden etc...)
            spriteBatch.Draw(backgroundTexture, new Rectangle(0,0,Window.ClientBounds.Width, Window.ClientBounds.Height), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
            
            //Draw Sprite1 (Tree)
            spriteBatch.Draw(sprite1, new Vector2(300, 500), new Rectangle(frameCounter1.X, frameCounter1.Y, sprite1.Width / 6, sprite1.Height), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            
            //Draw Sprite (Boy character)
            spriteBatch.Draw(sprite, position, new Rectangle(frameCounter.X, frameCounter.Y, sprite.Width/8, sprite.Height/3), Color.White,0,Vector2.Zero,1,se,0);
 
	    
 
	    //================= Help me here ============================
	    //
            //Draw new sprite (Fence)
            //The boy cannot past through the fence
 
 
            //create a bounding sphere at the players current position
            BoundingSphere playerSphere = new BoundingSphere(new Vector3(position,0),1);
            
            //In this loop we draw each Male/Female character...
            foreach (Level.character c in level.allCharacters)
            {
                spriteBatch.Draw(c.sprite, c.position, new Rectangle(0, 0, c.sprite.Height, c.sprite.Height), Color.White, 0, Vector2.Zero,c.scale, SpriteEffects.None, 0);
                
                if (c.bound.Intersects(playerSphere))
                {
                    
                    spriteBatch.DrawString(spriteFont, c.greeting, (c.position + new Vector2(10, -10)), Color.White);
                }
 
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
Attachments:
 
source code game1.cs
 
[+][-]04/13/09 02:28 PM, ID: 24133088

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/13/09 04:25 PM, ID: 24133767

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/14/09 05:43 AM, ID: 24137499

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/14/09 05:45 AM, ID: 24137515

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/14/09 11:53 AM, ID: 24141291

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/14/09 05:01 PM, ID: 24143775

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/14/09 11:31 PM, ID: 24145184

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/15/09 05:58 AM, ID: 24147408

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/15/09 08:22 AM, ID: 24149057

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/15/09 09:13 AM, ID: 24149743

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]04/15/09 02:50 PM, ID: 24152925

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/15/09 03:06 PM, ID: 24153040

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/16/09 01:55 AM, ID: 24155592

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/16/09 02:02 AM, ID: 24155634

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/16/09 10:57 AM, ID: 24160439

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Game Programming, Miscellaneous Games, Other Strategy Games
Sign Up Now!
Solution Provided By: Xcone
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04/16/09 01:41 PM, ID: 24162364

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/17/09 07:55 AM, ID: 24168436

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/17/09 08:22 AM, ID: 24168713

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/22/09 05:24 PM, ID: 24210848

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/22/09 11:23 PM, ID: 24212192

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 - Hierarchy / EE_QW_3_20080625