Jump to content

  • Log In with Google      Sign In   
  • Create Account


--- Finished Breakout ---


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 mistervirtue   Members   -  Reputation: 590

Like
4Likes
Like

Posted 06 November 2012 - 05:09 PM

I finsihed my breakout project. I feel like things are coming together, my next step is going to snake. I feel like am making some right moves, I am really starting like programming.

MisterVirtue's Breakout Game

Tell me what you think.

Edited by mistervirtue, 07 November 2012 - 11:34 AM.


Sponsor:

#2 mistervirtue   Members   -  Reputation: 590

Like
0Likes
Like

Posted 07 November 2012 - 11:35 AM

I fixed the link sorry about that.

Edited by mistervirtue, 07 November 2012 - 11:52 AM.


#3 Josh Petrie   Moderators   -  Reputation: 3104

Like
1Likes
Like

Posted 07 November 2012 - 11:40 AM

Why don't you just paste the code here? It's far more likely that somebody will take a look at it.

Josh Petrie | Core Tools Engineer, 343i | Microsoft C++ MVP


#4 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Like
1Likes
Like

Posted 07 November 2012 - 11:43 AM

Really? You need the source code to try out a game...?
Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#5 Josh Petrie   Moderators   -  Reputation: 3104

Like
1Likes
Like

Posted 07 November 2012 - 12:00 PM

I have no intention of playing it (and the OP did not specifically ask for that), I want to look at the code and critique that.

I'm also not in the habit of downloading and running random executable from forums anyhow, especially this one. It only takes one such instance for us to have a repeat of fongerchat.

Edited by Josh Petrie, 07 November 2012 - 12:01 PM.

Josh Petrie | Core Tools Engineer, 343i | Microsoft C++ MVP


#6 mistervirtue   Members   -  Reputation: 590

Like
4Likes
Like

Posted 07 November 2012 - 12:02 PM

Sure, Posting the source code wouldn't hurt.
Source Code HOOOOO!

Sprite Manager / Game Compentent
[source lang="csharp"]using System;using System.Collections.Generic;using System.Linq;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;namespace Jreakout_BreakOutClone_ { /// <summary> /// This is a game component that implements IUpdateable. /// </summary> public class SpriteManager : Microsoft.Xna.Framework.DrawableGameComponent { SpriteBatch spriteBatch; PlayerPaddle playerPaddle; Rectangle screenRectangle; Ball ball; //Columns and rows for brick int bricksWide = 10; int bricksHigh = 5; //Texture for the brick Texture2D brickImage; //Array for the placement for the bricks Brick[,] bricks; public SpriteManager(Game game) : base(game) { // TODO: Construct any child components here } protected override void LoadContent() { spriteBatch = new SpriteBatch(Game.GraphicsDevice); screenRectangle = new Rectangle(0, 0, Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height); Texture2D playerPaddleTexture = Game.Content.Load<Texture2D>("paddle"); playerPaddle = new PlayerPaddle(playerPaddleTexture, screenRectangle); Texture2D ballTexture = Game.Content.Load<Texture2D>("ball"); ball = new Ball(ballTexture, screenRectangle); brickImage = Game.Content.Load<Texture2D>("brick"); StartGame(); base.LoadContent(); } /// <summary> /// Allows the game component to perform any initialization it needs to before starting /// to run. This is where it can query for any required services and load content. /// </summary> public override void Initialize() { // TODO: Add your initialization code here base.Initialize(); } /// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(GameTime gameTime) { playerPaddle.Update(); ball.Update(); //Check For Collisions ball.CheckForWallCollision(); ball.CheckForPlayerCollision(playerPaddle.GetBounds()); foreach (Brick brick in bricks) { brick.CheckCollision(ball); } if (ball.CheckForBottom()) { StartGame(); } base.Update(gameTime); } public override void Draw(GameTime gameTime) { spriteBatch.Begin(); foreach (Brick brick in bricks) { brick.Draw(spriteBatch); } ball.Draw(spriteBatch); playerPaddle.Draw(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } public void StartGame() { playerPaddle.SetInStartPosition(); ball.SetInStartPosition(); //When the game starts Place the bricks //Layer them by color //Create an array to hold the bricks bricks = new Brick[bricksWide, bricksHigh]; //Loop thr for (int y = 0; y < bricksHigh; y++) { Color tint = Color.White; switch (y) { case 0: tint = Color.Blue; break; case 1: tint = Color.Red; break; case 2: tint = Color.Green; break; case 3: tint = Color.Yellow; break; case 4: tint = Color.Purple; break; } for (int x = 0; x < bricksWide; x++) { bricks[x, y] = new Brick( brickImage, new Rectangle( x * brickImage.Width, y * brickImage.Height, brickImage.Width, brickImage.Height), tint); } } } } }[/source]
Brick Class
[source lang="csharp"]using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Media;namespace Jreakout_BreakOutClone_ { class Brick { //Holds the texture for the brick Texture2D texture; //Holds the position of the brick Vector2 position; //Holds the location of the brick Rectangle location; //Allows to change the color of the bricks Color tint; //A boolean value to see if the brick is alive or not. bool alive; public Rectangle Location { get { return location; } } //Constructor for the brick class public Brick(Texture2D texture, Rectangle location, Color tint) { this.texture = texture; this.location = location; this.tint = tint; this.alive = true; } //Check to see if anything has collided with the brick public void CheckCollision(Ball ball) { if (alive && ball.Bounds.Intersects(location)) { alive = false; ball.Deflection(this); } } public void Draw(SpriteBatch spriteBatch) { if (alive) { spriteBatch.Draw(texture,location,tint); } } } }[/source]
Ball Class
[source lang="csharp"]using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Media;namespace Jreakout_BreakOutClone_ { class Ball { //Holds for the direction the ball is travelling Vector2 motion; //Holds for the the current position of the ball Vector2 position; //Holds for the speed of the ball float ballSpeed = 5; //Holds for the texture Image of ball; Texture2D texture; //Holds for the bounds of the screen Rectangle screenBounds; //Rectangle Bounds Rectangle bounds; public Rectangle Bounds { get { bounds.X = (int)position.X; bounds.Y = (int)position.Y; return bounds; } } //Consturtor for the ball class //This allows for the creation of instances of ball. Pass texture and screenbounds as parameters public Ball(Texture2D texture, Rectangle screenBounds) { this.texture = texture; this.screenBounds = screenBounds; } //Upate for the ball class //Check for Ccollisions with the wall and the player public void Update() { position += motion * ballSpeed; CheckForWallCollision(); } //Check to see if the ball is hitting the window bounds on the left and right and top //Reverse it's direction upon collision public void CheckForWallCollision() { //Top of the Screen if (position.Y < 0) { position.Y = 0; motion.Y *= -1; } //Left Bound if (position.X < 0) { position.X = 0; motion.X *= -1; } //Right Bound if (position.X + texture.Width > screenBounds.Width) { position.X = screenBounds.Width - texture.Width; motion.X *= -1; } } //Check to see if the ball is hitting the player paddle //Reverese it's direction upon collision public void CheckForPlayerCollision(Rectangle paddleLocation) { Rectangle ballLocation = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); if (paddleLocation.Intersects(ballLocation)) { position.Y = paddleLocation.Y - texture.Height; motion.Y = -1; } } //Check to see if the ball has gone of the bottom of the screen //If so return true public bool CheckForBottom() { if (position.Y + texture.Height > screenBounds.Height) { return true; } return false; } public void SetInStartPosition() { motion = new Vector2(-1,1); position.X = (screenBounds.Width - texture.Width) / 2; position.Y = (screenBounds.Height - texture.Height) / 2; } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture, position, Color.White); } public void Deflection(Brick brick) { motion.Y *= -1; } } }[/source]

Game1.cs

[source lang="csharp"]using System;using System.Collections.Generic;using System.Linq;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;namespace Jreakout_BreakOutClone_ { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteManager spriteManager; Rectangle screenRectangle; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferHeight = 600 ; graphics.PreferredBackBufferWidth = 750; screenRectangle = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); } /// <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() { //Instantiate the spriteManager spriteManager = new SpriteManager(this); Components.Add(spriteManager); 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(Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } // TODO: Add your update logic here 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(Color.CornflowerBlue); // TODO: Add your drawing code here base.Draw(gameTime); } } }[/source]

PlayerPaddle Class
[source lang="csharp"]using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Media;namespace Jreakout_BreakOutClone_ { class PlayerPaddle { //Variable Declaration// //Holds the Position of the PlayerPaddle Vector2 position; //Holds the Direction the PlayerPaddle is move Vector2 direction; //Holds the speed of the player paddle float paddleSpeed =8; //Holds the texture Image for the ball Texture2D texture; //Allow for player Input KeyboardState keyboardState; //Holds the Rectangle for the GameWindow Rectangle screenBounds; //End of Variable Declaration// //Consturctor for the PlayerPaddle // Used to create an instance of PlayerPaddle (Pass texture,screenbounds as parameters) public PlayerPaddle(Texture2D texture, Rectangle screenBounds) { this.texture = texture; this.screenBounds = screenBounds; } //Update for Paddle //Use Keyboard to allow for Movment only in the X direction public void Update() { direction = Vector2.Zero; keyboardState = Keyboard.GetState(); if(keyboardState.IsKeyDown(Keys.Left)) { direction.X = -1; } if (keyboardState.IsKeyDown(Keys.Right)) { direction.X = 1; } direction.X *= paddleSpeed; position += direction; CheckScreenBounds(); } //Draw for Paddle //Draw the Paddle Yellow public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture, position, Color.Purple); } //CheckScreenBounds for PlayerPaddle //Do not Allow the Paddle to leave the clientBounds public void CheckScreenBounds() { if (position.X < 0) { position.X = 0; } if (position.X + texture.Width > screenBounds.Width) { position.X = screenBounds.Width - texture.Width; } } //GetBounds for PlayerPaddle //Create A Rectangle around the texture image of playerPaddle public Rectangle GetBounds() { return new Rectangle ((int)position.X, (int)position.Y, texture.Width, texture.Height); } public void SetInStartPosition() { position.X = (screenBounds.Width - texture.Width) / 2; position.Y = screenBounds.Height - texture.Height - 5; } } }[/source]

There you go.

Edited by mistervirtue, 07 November 2012 - 12:09 PM.


#7 mistervirtue   Members   -  Reputation: 590

Like
0Likes
Like

Posted 07 November 2012 - 12:10 PM

I'm also not in the habit of downloading and running random executable from forums anyhow.


Probably a smart habit.

#8 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Like
1Likes
Like

Posted 07 November 2012 - 12:12 PM

I have no intention of playing it (and the OP did not specifically ask for that), I want to look at the code and critique that.

I read that as "try out my game" as in play it. But critiquing code is always good too.

I'm also not in the habit of downloading and running random executable from forums anyhow, especially this one. It only takes one such instance for us to have a repeat of fongerchat.

Well ok. This is true. But I don't hold that blemish against GD.Net. Plus, that's what VMs are for :)
Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#9 Josh Petrie   Moderators   -  Reputation: 3104

Like
5Likes
Like

Posted 07 November 2012 - 12:21 PM

You have one of the more unusual indentation styles I've ever seen (or was that the forum's useless "helpful" reformatting?). In any case, I will look over this when I get a chance and give you some specifics, but from a quick glance I can suggest two avenues you might want to look at:
  • Consider looking at a system by which you wouldn't need to hard-code collision detection mechanisms between "the ball" and "the wall" -- how could you generalize your collision representation and handling so you could add new things without modifying the ball code?
  • Your sprite manager is rather coupled to your game logic in a few key ways -- for example, "StartGame." How could you factor that dependency out so you can reuse this sprite management code in your next project?

Josh Petrie | Core Tools Engineer, 343i | Microsoft C++ MVP


#10 mistervirtue   Members   -  Reputation: 590

Like
0Likes
Like

Posted 07 November 2012 - 12:41 PM

Thanks Josh, you the man. Seriously I appreciate a Engineering Lead taking time out of their day to look am some scrub/noob code.
  • To answer your question about ball/wall collision. I am not really sure how i would do that, but your question has certainly given me something to think about. I don't know if I know a more efficient way to handle that collision.
  • To answer your other question about my Sprite Manager. I suppose I could could have the brick laying in my StartGame() be a method specific to brick rather than it being in the StartGame(). Or maybe i could have my StartGame stuff in the loadcontent
I will really think about how to improve my code. thanks for the head-ups.

#11 Pash   Members   -  Reputation: 233

Like
1Likes
Like

Posted 08 November 2012 - 07:38 AM

Thanks Josh, you the man. Seriously I appreciate a Engineering Lead taking time out of their day to look am some scrub/noob code.

  • To answer your question about ball/wall collision. I am not really sure how i would do that, but your question has certainly given me something to think about. I don't know if I know a more efficient way to handle that collision.
  • To answer your other question about my Sprite Manager. I suppose I could could have the brick laying in my StartGame() be a method specific to brick rather than it being in the StartGame(). Or maybe i could have my StartGame stuff in the loadcontent
I will really think about how to improve my code. thanks for the head-ups.


Simply put and to cover Josh's pointers "code refactoring". Take this scenario in a breakout as your chosen game:-

- you decide to add falling power ups that are spawned when bricks break.

Think about what you need now to catch those falling power ups with your paddle and or the ball maybe? Where would your code need editing?

The same applies to sprite manager combined with game logic in this case. Never be afraid to break your code down and make it useable for later projects. Remember your next game might need collision detection but there are no balls and bricks!

You need to check this principle out http://en.wikipedia.org/wiki/Single_responsibility_principle

Good work, keep it up!
Please Add Rep if I helped // Working on untitled 2D platformer (more to come soon)
Twitter - @MarkPashby

#12 xINFINITELIGHTx   Members   -  Reputation: 89

Like
-3Likes
Like

Posted 08 November 2012 - 02:45 PM

Hey everybody I'm new here was searching google and it's weird because I'm currently working on the breakout project too in XNA game studois 4.0. Except I have the neurosky mindset http://www.neurosky.com/Products/MindSet.aspx and I am trying to design a game where the paddle gets bigger and smaller depending on your brainwave frequency and how relaxed you are. (To be very brief the neurosky mindset reads you brianwaves and that's how relaxed, alert, asleep, in the middle of repaxed alert, or super alert you are)

Good job getting the project done man. Congratulations. I for one am working through this online tutorial on youtube to get a break out game done and I am using both my art (I'm an aritst) and my programming skills to get it done.

It's great though that you got you game done i'm goin to play it first thing when I get home and tell you how awesome it is (if it's breakout, then it's awesome)


This literally serves as an inspiration for me to get my game done. Thanks for even allowing this to exist.

#13 Josh Petrie   Moderators   -  Reputation: 3104

Like
6Likes
Like

Posted 15 November 2012 - 11:12 AM

There are two basic ideas that I'm trying to drive you towards: the idea that a given class or interface should serve a single function ("single responsibility principle") and, to that end, the idea that while object-oriented design begins with the conceptualization of types as real world analogues, it doesn't (and shouldn't end there) -- you can have classes that don't correspond to real-world objects.

Your sprite manager code has some game-specific constructs, such as a specific texture variable for the player and the ball. It also handles game-specific initialization and update logic, such as StartGame and the collision checks (even the fact that it handles collision at all is problematic).

At a high level, I would instead expect to see this:
  • A type called "SpriteManager" should only understand how to manage a list of (generic) sprite objects and send them to the graphics card at render time. While these sprites may include position, width and height as data, that data is not necessarily what is used to perform collision. The idea that collision geometry and render geometry are distinct is a common practice and becomes very important with 3D games or even in 2D games when you have complicated shapes.
  • You should have a distinct entity for representing collision geometry; perhaps a "CollisionShape" interface contains collision bounds information. These collision entities should be managed by a "CollisionWorld" or similar high-level interface that understands the relative positions of all collision entities and can raise events or invoke callbacks when any set of entities enters or leaves collision (or remains in collision across more than one frame, that can be useful as well).

Your top-level "game" class should be the one that contains all the game specific logic and types. The game class can maintain a set of logical game objects -- either using a single common class or, if their functionality differs wildly enough, many classes. Since we're already attempting to generalize quite a lot, I will leave further generalization of the game logic entities to you and, for the purposes of this example, say we have a Ball class and a Player class.

The game creates a ball instance, and asks the sprite manager to create a corresponding sprite. That sprite is stored in the ball object. Similarly, the game asks the collision world to construct a collision entity with the ball's dimensions and initial position in the world. A reference to that entity is also stored in the ball. Similarly for the player.

Now the game loop consists of, approximately: collecting player input, applying those inputs to the player object's collision entity (to move it), then asking the collision world to update. This will cause the collision world to fire any callbacks registered concerning inter-object collision -- the game would have installed a callback to handle ball-ball collision, ball-wall collision, and ball-player collision, possibly. In those callbacks, appropriate action can be taken by the game class -- perhaps to destroy one or both entities, or reflect the path of one to cause a "bounce" effect, et cetera.

Finally, the game looks at every game entity it knows about (balls, player, et cetera) and transfers the appropriate logical properties -- such as position, or damage state, or whatever -- to the object's renderable representation (the sprite in this case). Then your game asks the sprite manager to render all your frames.

With only this first level of generalization and refactoring (and there's much more you can do on top of this), you should have two systems -- the collision handling stuff and the sprite handling stuff -- that are entirely game-agnostic and can be reused and extended without having to couple them to the idea of a breakout (or any other) kind of game. It's only at the highest level of abstraction where your game-specific logic and means of tying these two systems together exist.

Edited by Josh Petrie, 15 November 2012 - 11:13 AM.

Josh Petrie | Core Tools Engineer, 343i | Microsoft C++ MVP


#14 mistervirtue   Members   -  Reputation: 590

Like
0Likes
Like

Posted 16 November 2012 - 11:57 PM

That is a lot for me to take in, but i will strive to understand and apply this knowledge. Seeing as I am a beginner i want to try and do what the pros do, so I am going to attempt to generalize and refactor my code before I move on to the next project. I want to develop smart and efficient habits early on in my programming career, that way I can have a strong foundation. I will do more research on this "single responsibility principle" and see what that is all about, sounds pretty cool. Thanks Josh.

#15 jbadams   Senior Staff   -  Reputation: 17753

Like
1Likes
Like

Posted 17 November 2012 - 12:08 AM

I will do more research on this "single responsibility principle" and see what that is all about, sounds pretty cool

SOLID (Wikipedia)
Object mentor has some good articles on the topic, although some of it may be difficult to follow until you gain the practical experience to go with the knowledge. Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS