I broke my game need help

Soldato
Joined
30 Aug 2009
Posts
8,099
Location
one nation under sony
Code:
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 cw
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    /// 
    

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        const int WINDOW_WIDTH = 400;
        const int WINDOW_HEIGHT = 400;

        Texture2D paddle;
        Rectangle paddleRect;

        Texture2D ball;
        Rectangle ballRect;

  
       
       
        HUD hud;

        int paddleshift = 10;

        const int Lives = 3;
        int START_SPEED = 5;
        Random rand = new Random();
        double randHolder;
        Vector2 velocity = new Vector2(1,1);
        double angle;

        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
            graphics.PreferredBackBufferWidth = WINDOW_HEIGHT;
            IsMouseVisible = true;
        }


        /// <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
            
        }

        /// <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);
            Texture2D paddle = Content.Load<Texture2D>("Paddle");
            Rectangle paddleRect = new Rectangle(WINDOW_WIDTH / 2 - paddle.Width / 2, WINDOW_HEIGHT - paddle.Height, paddle.Width, paddle.Height);


            spriteBatch = new SpriteBatch(GraphicsDevice);
            Texture2D ball = Content.Load<Texture2D>("ball2");
            Rectangle ballRect = new Rectangle(WINDOW_WIDTH / 2 - ball.Width / 2, WINDOW_HEIGHT / 2 - ball.Width / 2, ball.Width, ball.Height);

            angle = 2 * Math.PI * 30.0 / 360;

            velocity.X = (float)(Math.Cos(angle) * START_SPEED);
            velocity.Y = (float)(-1 * Math.Sin(angle) * START_SPEED);

            hud = new HUD();
            hud.Font = Content.Load<SpriteFont>("Arial");

            // 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)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            do
                randHolder = rand.NextDouble();
            while (randHolder < 30.0 / 360 || (randHolder > 60.0 / 360 && randHolder < 120.0 / 360) || randHolder > 150.0 / 360);

            // TODO: Add your update logic here
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
                if (paddleRect.Left > 0)
                    paddleRect.X -= paddleshift;

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
                if (paddleRect.Right < WINDOW_WIDTH)
                    paddleRect.X += paddleshift;



            ballRect.X += (int)velocity.X;
            ballRect.Y += (int)velocity.Y;

            if (ballRect.Y < 0)
            {
                ballRect.Y = 0;
                velocity.Y *= -1;
            }

            if (ballRect.X < 0)
            {
                ballRect.X = 0;
                velocity.X *= -1;
            }
        


            if (ballRect.Y > WINDOW_HEIGHT - ball.Height)
            {
                ballRect.Y = 0;
                velocity.Y *= -1;
                hud.Lives -= 1;
                START_SPEED = 5;

                velocity.X *= 0.9f;
                velocity.Y *= 0.9f;
            }
 

            if (hud.Lives <= -3)
            {
                velocity.X *= 0f;
                velocity.Y *= 0f;
            }

           
           
           if (ballRect.Right > WINDOW_WIDTH)
            {
                ballRect.X = WINDOW_WIDTH - ballRect.Width;
                velocity.X *= -1;
            }

            if (paddleRect.Intersects(ballRect))
            {
                ballRect.Y = WINDOW_HEIGHT - ballRect.Height - paddleRect.Height;
                velocity.Y *= -1;

                velocity.X *= 1.10f;
                velocity.Y *= 1.10f;

                hud.Score += 10;
            }

                //paddleShift = (int)(paddleShift * 1.15);

            if (Keyboard.GetState().IsKeyDown(Keys.Down))
                LoadContent();
            
            }
        

        
        /// <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.DarkSeaGreen);
            // TODO: Add your drawing code here
            {
                spriteBatch.Begin();
            spriteBatch.Draw(paddle, paddleRect, Color.White);
            spriteBatch.Draw(ball, ballRect, Color.White);
            hud.Draw(spriteBatch);
            spriteBatch.End();
            }
            
            
            base.Draw(gameTime);
        
        }
    }

}

SKhwf1C.png

it worked before then I tried to add a intro screen didn't work took the code out and now the game won't work
 
Last edited:
Associate
Joined
24 May 2011
Posts
262
The ball object hasn't been initialised. I'd imagine when you removed the intro screen code it removed the code that initialised the ball object, but obviously I can't be certain as that code no longer exists.
 

AGD

AGD

Soldato
Joined
23 Nov 2007
Posts
5,048
Just to add another point, please, please use some source control system (github is very easy to set up). This will mean if you ever get into the same situation, you can just rewind time to your last correct version.
 
Soldato
OP
Joined
30 Aug 2009
Posts
8,099
Location
one nation under sony
The ball object hasn't been initialised. I'd imagine when you removed the intro screen code it removed the code that initialised the ball object, but obviously I can't be certain as that code no longer exists.

I'm trying to compare older code




http://ericlippert.com/2014/03/05/how-to-debug-small-programs/

Read the stuff in this link and apply it to the problem you have.
Seriously, if you do the things suggested it will help you become a better developer.


thanks for the link

Just to add another point, please, please use some source control system (github is very easy to set up). This will mean if you ever get into the same situation, you can just rewind time to your last correct version.

will do
 
Back
Top Bottom