Almost There

February 9, 2010 by admin
Filed under: Xbox360 News 

Hey Guys,

I have almost have a basic prototype of the gameplay I want for this game. However, I have ran into some problems. I have got the blocks to fall from the screen and stop at the bottom of the playing field. However, the color of the blocks should appear of different colors randomly, all the blocks are yellow. Also, How can I get the blocks to stay inside of the selector? And one last step, How would I do it, so that when the user presses a key, the blocks switch sides? I know how to program input from a controller, I just need the code or an example of how I would switch the two block positions. Here is the code. Thanks for any help.

1 using System; 
2 using System.Collections.Generic; 
3 using System.Linq; 
4 using Microsoft.Xna.Framework; 
5 using Microsoft.Xna.Framework.Audio; 
6 using Microsoft.Xna.Framework.Content; 
7 using Microsoft.Xna.Framework.GamerServices; 
8 using Microsoft.Xna.Framework.Graphics; 
9 using Microsoft.Xna.Framework.Input; 
10 using Microsoft.Xna.Framework.Media; 
11 using Microsoft.Xna.Framework.Net; 
12 using Microsoft.Xna.Framework.Storage; 
13  
14 namespace Puzzle_Battles 
15
16     /// <summary> 
17     /// This is the main type for your game 
18     /// </summary> 
19     public class Game1 : Microsoft.Xna.Framework.Game 
20     { 
21         GraphicsDeviceManager graphics; 
22         SpriteBatch spriteBatch; 
23         SpriteFont gameFont; 
24  
25         Texture2D LogoGfx; 
26         Rectangle LogoRect; 
27  
28         Texture2D[] BlockGfx; 
29         Rectangle[] BlockRect; 
30  
31         Texture2D TitleGfx; 
32         Rectangle TitleRect; 
33  
34  
35         Texture2D[] PlayFieldGfx; 
36         Rectangle[] PlayFieldRect; 
37  
38         Texture2D[] CharGfx; 
39         Rectangle[] CharRect; 
40  
41         Texture2D[] SelectorGfx; 
42         Rectangle[] SelectorRect; 
43  
44         private KeyboardState lastKey = Keyboard.GetState(); 
45         private GamePadState lastButton = GamePad.GetState(PlayerIndex.One); 
46         private GamePadState lastButton2 = GamePad.GetState(PlayerIndex.Two); 
47  
48         long lastTick = 0; 
49         float FramesPerSec = 0.0f; 
50         float deltaTime = 0.0f; 
51         int lastFrame = 0; 
52         int currentFrame = 0; 
53         int frameCounter = 0; 
54         string FPS = ""
55  
56         string[] GameSelection = new string[] { 
57             "Adventure Mode"//0 
58             "Gaunlet Mode"//1 
59             "Tournament Mode"//2 
60             "Tutorial Mode"//3 
61             "Local Match"//4 
62             "Online Match"//5 
63             "Game Options"//6 
64             "Exit Game"//7 
65         }; 
66  
67         int Selected = 0; 
68  
69         enum ScreenState 
70         { 
71             LogoScreen, 
72             TitleScreen, 
73             CharacterSelectScreen, 
74             AdventureScreen, 
75             TournamentScreen, 
76             LocalMatchScreen, 
77             TutorialScreen, 
78             GaunletScreen, 
79             OnlineMatchScreen, 
80             OptionScreen 
81         } 
82  
83         enum GameState 
84         { 
85             Running, 
86             Paused 
87         } 
88  
89         Color gamePausedColor = Color.TransparentWhite; 
90         bool gamePaused = false
91  
92         ScreenState gScreen = new ScreenState(); 
93         GameState gGameState = new GameState(); 
94  
95         public const int Field_Width = 334; 
96         public const int Field_Height = 697; 
97  
98         public const int Block_Width = 32; 
99         public const int Block_Height = 32; 
100  
101         public const int Selector_Width = 128; 
102         public const int Selector_Height = 128; 
103  
104         public const float BlockFallSpeed = 0.05f; 
105         public const float BlockAppearRate = 0.10f; 
106  
107         enum BlockType 
108         { 
109             BlackBlock, 
110             FireBlock, 
111             EarthBlock, 
112             WaterBlock, 
113             WhiteBlock, 
114             WindBlock 
115         } 
116  
117         Color BlackBlockColor; 
118         Color FireBlockColor; 
119         Color EarthBlockColor; 
120         Color WaterBlockColor; 
121         Color WhiteBlockColor; 
122         Color WindBlockColor; 
123  
124         BlockType gBlockType = new BlockType(); 
125  
126         List<Vector2> BlockPos = new List<Vector2>(); 
127  
128         Random gNxtBlock = new Random(); 
129         Random gNxtFallBlock = new Random(); 
130  
131         public const int MAX_ROWS = 60; 
132         public const int MAX_COLMNS = 30; 
133  
134         public int Selector_X = 20; 
135         public int Selector_Y = 50; 
136         public int Selector_X2 = 690; 
137         public int Selector_Y2 = 50; 
138  
139         public Game1() 
140         { 
141             graphics = new GraphicsDeviceManager(this); 
142             Content.RootDirectory = "Content"
143  
144             this.graphics.PreferredBackBufferWidth = 1024; 
145             this.graphics.PreferredBackBufferHeight = 768; 
146  
147             this.IsMouseVisible = true
148             this.IsFixedTimeStep = false
149  
150             gScreen = ScreenState.LogoScreen; 
151             gGameState = GameState.Running; 
152         } 
153  
154         /// <summary> 
155         /// Allows the game to perform any initialization it needs to before starting to run. 
156         /// This is where it can query for any required services and load any non-graphic 
157         /// related content.  Calling base.Initialize will enumerate through any components 
158         /// and initialize them as well. 
159         /// </summary> 
160         protected override void Initialize() 
161         { 
162             // TODO: Add your initialization logic here 
163  
164             base.Initialize(); 
165         } 
166  
167         /// <summary> 
168         /// LoadContent will be called once per game and is the place to load 
169         /// all of your content. 
170         /// </summary> 
171         protected override void LoadContent() 
172         { 
173             // Create a new SpriteBatch, which can be used to draw textures. 
174             spriteBatch = new SpriteBatch(GraphicsDevice); 
175  
176             // TODO: use this.Content to load your game content here 
177             gameFont = Content.Load<SpriteFont>("mainFont"); 
178  
179             PlayFieldGfx = new Texture2D[2]; 
180             PlayFieldGfx[0] = Content.Load<Texture2D>("play_field"); 
181             PlayFieldGfx[1] = Content.Load<Texture2D>("play_field"); 
182  
183             int BlockW = PlayFieldGfx[0].Width / Field_Width; 
184             int BlockH = PlayFieldGfx[0].Height / Field_Height; 
185             int BlockW2 = PlayFieldGfx[1].Width / Field_Width; 
186             int BlockH2 = PlayFieldGfx[1].Height / Field_Height; 
187  
188  
189             SelectorGfx = new Texture2D[2]; 
190             SelectorGfx[0] = Content.Load<Texture2D>("selector"); 
191             SelectorGfx[1] = Content.Load<Texture2D>("selector"); 
192  
193             CharGfx = new Texture2D[9]; 
194             CharGfx[0] = Content.Load<Texture2D>("Cynthia puzzle 2"); 
195             CharGfx[1] = Content.Load<Texture2D>("Cynthia puzzle"); 
196             CharGfx[2] = Content.Load<Texture2D>("Cynthia thumb"); 
197             CharGfx[3] = Content.Load<Texture2D>("Cynthia thumb2"); 
198             CharGfx[4] = Content.Load<Texture2D>("Epic Puzzle"); 
199             CharGfx[5] = Content.Load<Texture2D>("Epic Puzzle2"); 
200             CharGfx[6] = Content.Load<Texture2D>("Epic thumb 2"); 
201             CharGfx[7] = Content.Load<Texture2D>("Epic thumb"); 
202             CharGfx[8] = Content.Load<Texture2D>("trufa heart"); 
203  
204             LogoGfx = Content.Load<Texture2D>("Logo"); 
205  
206             CharRect = new Rectangle[3]; 
207             CharRect[0] = new Rectangle(250, 200, 128, 128); 
208             CharRect[1] = new Rectangle(380, 200, 128, 128); 
209             CharRect[2] = new Rectangle(500, 200, 128, 128); 
210  
211             BlockGfx = new Texture2D[6]; 
212             BlockGfx[0] = Content.Load<Texture2D>("black_block"); 
213             BlockGfx[1] = Content.Load<Texture2D>("earth_block"); 
214             BlockGfx[2] = Content.Load<Texture2D>("fire_block"); 
215             BlockGfx[3] = Content.Load<Texture2D>("water_block"); 
216             BlockGfx[4] = Content.Load<Texture2D>("white_block"); 
217             BlockGfx[5] = Content.Load<Texture2D>("wind_block"); 
218  
219             BlockRect = new Rectangle[6]; 
220             BlockRect[0] = new Rectangle(15, 32, 32, 32); 
221             BlockRect[1] = new Rectangle(45, 32, 32, 32); 
222             BlockRect[2] = new Rectangle(15, 32, 32, 32); 
223             BlockRect[3] = new Rectangle(15, 32, 32, 32); 
224             BlockRect[4] = new Rectangle(15, 32, 32, 32); 
225             BlockRect[5] = new Rectangle(15, 32, 32, 32); 
226  
227             SelectorRect = new Rectangle[2]; 
228             SelectorRect[0] = new Rectangle(Selector_X, Selector_Y, Selector_Width, Selector_Height); 
229             SelectorRect[1] = new Rectangle(Selector_X2, Selector_Y2, Selector_Width, Selector_Height); 
230  
231             PlayFieldRect = new Rectangle[2]; 
232             PlayFieldRect[0] = new Rectangle(10, 30, Field_Width, Field_Height); 
233             PlayFieldRect[1] = new Rectangle(680, 30, Field_Width, Field_Height); 
234  
235             LogoRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, 
236                 graphics.GraphicsDevice.Viewport.Height); 
237  
238             TitleRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, 
239                             graphics.GraphicsDevice.Viewport.Height); 
240  
241  
242         } 
243  
244         /// <summary> 
245         /// UnloadContent will be called once per game and is the place to unload 
246         /// all content. 
247         /// </summary> 
248         protected override void UnloadContent() 
249         { 
250             // TODO: Unload any non ContentManager content here 
251             spriteBatch = null
252             gameFont = null
253             LogoGfx = null
254             BlockGfx = null
255             CharGfx = null
256             PlayFieldGfx = null
257             SelectorGfx = null
258         } 
259  
260         /// <summary> 
261         /// Allows the game to run logic such as updating the world, 
262         /// checking for collisions, gathering input, and playing audio. 
263         /// </summary> 
264         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
265         protected override void Update(GameTime gameTime) 
266         { 
267             // Allows the game to exit 
268             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
269                 this.Exit(); 
270  
271             // TODO: Add your update logic here 
272             lastTick = gameTime.TotalGameTime.Milliseconds; 
273  
274             if (lastTick > gameTime.TotalGameTime.Milliseconds) 
275             { 
276                 deltaTime = 1000 - lastTick + gameTime.TotalGameTime.Milliseconds; 
277                 FPS = Convert.ToString(frameCounter); 
278  
279                 frameCounter = 0; 
280             } 
281             else 
282             { 
283                 deltaTime = gameTime.TotalGameTime.Milliseconds - lastTick; 
284             } 
285  
286             UpdateInput(gameTime); 
287  
288             base.Update(gameTime); 
289         } 
290  
291         private void UpdateInput(GameTime gameTime) 
292         { 
293             KeyboardState currentKey = Keyboard.GetState(); 
294             GamePadState currentButton = GamePad.GetState(PlayerIndex.One); 
295             GamePadState currentButton2 = GamePad.GetState(PlayerIndex.Two); 
296  
297             if (currentKey.IsKeyDown(Keys.Escape)) 
298             { 
299                 this.Exit(); 
300             } 
301  
302             switch (gScreen) 
303             { 
304                 case ScreenState.LogoScreen: 
305                     { 
306                         if ((currentKey.IsKeyDown(Keys.Enter) || currentButton.Buttons.Start == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Enter) && lastButton.Buttons.Start == ButtonState.Released) 
307                         { 
308                             gScreen = ScreenState.TitleScreen; 
309                         } 
310                         break
311                     } 
312                 case ScreenState.TitleScreen: 
313                     { 
314                         if ((currentKey.IsKeyDown(Keys.Up) || currentButton.DPad.Up == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Up) && lastButton.DPad.Up == ButtonState.Released) 
315                         { 
316                             if (Selected > 0) 
317                                 –Selected; 
318                         } 
319                         else if ((currentKey.IsKeyDown(Keys.Down) || currentButton.DPad.Down == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Down) && lastButton.DPad.Down == ButtonState.Released) 
320                         { 
321                             if (Selected < GameSelection.Length - 1) 
322                                 ++Selected; 
323                         } 
324                         else if (((currentKey.IsKeyDown(Keys.Enter) || currentButton.Buttons.Start == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Enter) && lastButton.Buttons.Start == ButtonState.Released) || currentButton.Buttons.A == ButtonState.Pressed && lastButton.Buttons.A == ButtonState.Released && Selected == 0) 
325                         { 
326                             gScreen = ScreenState.CharacterSelectScreen; 
327                         } 
328                         else if (((currentKey.IsKeyDown(Keys.Enter) || currentButton.Buttons.Start == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Enter) && lastButton.Buttons.Start == ButtonState.Released) || currentButton.Buttons.A == ButtonState.Pressed && lastButton.Buttons.A == ButtonState.Released && Selected == 1) 
329                         { 
330                             gScreen = ScreenState.CharacterSelectScreen; 
331                         } 
332                         else if (((currentKey.IsKeyDown(Keys.Enter) || currentButton.Buttons.Start == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Enter) && lastButton.Buttons.Start == ButtonState.Released) || currentButton.Buttons.A == ButtonState.Pressed && lastButton.Buttons.A == ButtonState.Released && Selected == 2) 
333                         { 
334                             gScreen = ScreenState.CharacterSelectScreen; 
335                         } 
336                         else if (((currentKey.IsKeyDown(Keys.Enter) || currentButton.Buttons.Start == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Enter) && lastButton.Buttons.Start == ButtonState.Released) || currentButton.Buttons.A == ButtonState.Pressed && lastButton.Buttons.A == ButtonState.Released && Selected == 3) 
337                         { 
338                             gScreen = ScreenState.TutorialScreen; 
339                         } 
340                         else if (((currentKey.IsKeyDown(Keys.Enter) || currentButton.Buttons.Start == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Enter) && lastButton.Buttons.Start == ButtonState.Released) || currentButton.Buttons.A == ButtonState.Pressed && lastButton.Buttons.A == ButtonState.Released && Selected == 4) 
341                         { 
342                             gScreen = ScreenState.CharacterSelectScreen; 
343                         } 
344                         else if (((currentKey.IsKeyDown(Keys.Enter) || currentButton.Buttons.Start == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Enter) && lastButton.Buttons.Start == ButtonState.Released) || currentButton.Buttons.A == ButtonState.Pressed && lastButton.Buttons.A == ButtonState.Released && Selected == 5) 
345                         { 
346                             gScreen = ScreenState.CharacterSelectScreen; 
347                         } 
348                         else if (((currentKey.IsKeyDown(Keys.Enter) || currentButton.Buttons.Start == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Enter) && lastButton.Buttons.Start == ButtonState.Released) || currentButton.Buttons.A == ButtonState.Pressed && lastButton.Buttons.A == ButtonState.Released && Selected == 6) 
349                         { 
350                             gScreen = ScreenState.OptionScreen; 
351                         } 
352                         else if (((currentKey.IsKeyDown(Keys.Enter) || currentButton.Buttons.Start == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Enter) && lastButton.Buttons.Start == ButtonState.Released) || currentButton.Buttons.A == ButtonState.Pressed && lastButton.Buttons.A == ButtonState.Released && Selected == 7) 
353                         { 
354                             this.Exit(); 
355                         } 
356                         break
357                     } 
358  
359                 case ScreenState.CharacterSelectScreen: 
360                     { 
361                         if (currentKey.IsKeyDown(Keys.Enter)) 
362                         { 
363                             gScreen = ScreenState.AdventureScreen; 
364                         } 
365                         break
366                     } 
367  
368                 case ScreenState.AdventureScreen: 
369                     { 
370                         for (int b = 0 + gNxtBlock.Next(1,6); b < BlockRect.Length; ++b) 
371                         { 
372                             BlockRect[b].Y = BlockRect[b].Y + 1; 
373  
374                             if (BlockRect[b].Y >= 686) 
375                             { 
376                                 BlockRect[b].Y = 686; 
377                             } 
378                         } 
379  
380                         if ((currentKey.IsKeyDown(Keys.Left) || currentButton.DPad.Left == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Left) && lastButton.DPad.Left == ButtonState.Released) 
381                         { 
382                             SelectorRect[0].X = SelectorRect[0].X - 5; 
383                         } 
384                         else if ((currentKey.IsKeyDown(Keys.Right) || currentButton.DPad.Right == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Right) && lastButton.DPad.Right == ButtonState.Released) 
385                         { 
386                             SelectorRect[0].X = SelectorRect[0].X + 5; 
387                         } 
388                         else if ((currentKey.IsKeyDown(Keys.Up) || currentButton.DPad.Up == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Up) && lastButton.DPad.Up == ButtonState.Released) 
389                         { 
390                             SelectorRect[0].Y = SelectorRect[0].Y - 5; 
391                         } 
392                         else if ((currentKey.IsKeyDown(Keys.Down) || currentButton.DPad.Down == ButtonState.Pressed) && !lastKey.IsKeyDown(Keys.Down) && lastButton.DPad.Down == ButtonState.Released) 
393                         { 
394                             SelectorRect[0].Y = SelectorRect[0].Y + 5; 
395                         } 
396  
397                         if (SelectorRect[0].X <= 15) 
398                         { 
399                             SelectorRect[0].X++; 
400                         } 
401                         else if (SelectorRect[0].X >= 206) 
402                         { 
403                             SelectorRect[0].X–; 
404                         } 
405                         else if (SelectorRect[0].Y <= 35) 
406                         { 
407                             SelectorRect[0].Y++; 
408                         } 
409                         else if (SelectorRect[0].Y >= 591) 
410                         { 
411                             SelectorRect[0].Y–; 
412                         } 
413  
414                         break
415                     } 
416             } 
417  
418             lastButton = currentButton; 
419             lastButton2 = currentButton2; 
420             lastKey = currentKey; 
421  
422         } 
423  
424         /// <summary> 
425         /// This is called when the game should draw itself. 
426         /// </summary> 
427         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
428         protected override void Draw(GameTime gameTime) 
429         { 
430             GraphicsDevice.Clear(Color.CornflowerBlue); 
431  
432             // TODO: Add your drawing code here 
433             spriteBatch.Begin(SpriteBlendMode.AlphaBlend); 
434  
435             switch (gScreen) 
436             { 
437                 case ScreenState.LogoScreen: 
438                     { 
439                         spriteBatch.Draw(LogoGfx, LogoRect, Color.White); 
440                         break
441                     } 
442                 case ScreenState.TitleScreen: 
443                     { 
444                         for (int i = 0, n = GameSelection.Length; i != n; ++i) 
445                         { 
446                             spriteBatch.DrawString(gameFont, GameSelection[i], new Vector2(430, 190 + 50 * i), (i == Selected) ? Color.Yellow : Color.White); 
447                         } 
448                         break
449                     } 
450                 case ScreenState.CharacterSelectScreen: 
451                     { 
452                         spriteBatch.Draw(CharGfx[4], CharRect[0], Color.White); 
453                         spriteBatch.Draw(CharGfx[1], CharRect[1], Color.White); 
454                         spriteBatch.Draw(CharGfx[8], CharRect[2], Color.White); 
455                         break
456                     } 
457  
458                 case ScreenState.AdventureScreen: 
459                     { 
460                         spriteBatch.Draw(PlayFieldGfx[0], PlayFieldRect[0], Color.White); 
461                         spriteBatch.Draw(PlayFieldGfx[1], PlayFieldRect[1], Color.White); 
462                         // 
463                         spriteBatch.Draw(SelectorGfx[0], SelectorRect[0], Color.White); 
464                         spriteBatch.Draw(SelectorGfx[1], SelectorRect[1], Color.White); 
465                         // 
466                         foreach (Texture2D BlockGf in BlockGfx) 
467                         { 
468                             foreach (Rectangle BlockRct in BlockRect) 
469                             { 
470                                 spriteBatch.Draw(BlockGf, BlockRct, Color.White); 
471                             } 
472                         } 
473                         break
474                     } 
475  
476             } 
477  
478             spriteBatch.DrawString(gameFont, "Selector X:" + SelectorRect[0].X, new Vector2(1, 1), Color.Yellow); 
479             spriteBatch.DrawString(gameFont, "Selector Y:" + SelectorRect[0].Y, new Vector2(1, 15), Color.Yellow); 
480  
481             spriteBatch.End(); 
482  
483             frameCounter++; 
484             base.Draw(gameTime); 
485         } 
486     } 
487
488  

Comments

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!