-1
\$\begingroup\$

I'm a student studying Games Development. I want a sprite to speed up on it's second passing of the screen. How do I do this?

Code I wrote so far:

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

    int carwrap;
    int carspeed;
    //structure for 2D graphics
    struct graphic2D
    {
        public Texture2D image;
        public Rectangle rect;
    }
    //structure for moving 2D graphics

    struct sprite2D
    {
        public Texture2D image; //texture to hold image
        public Vector3 position; //position on screen
        public Rectangle rect;  //size and position
        public Vector2 origin;  //centre point
        public float size;      //size ratio
        public Vector3 velocity; // sprite speed and direction 
        public BoundingSphere bsphere; //bounding sphere

    }
    sprite2D car;
    sprite2D car2;
    graphic2D background;
    Boolean gameover;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        // set game resolution to current resolution
        //this.graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
        //this.graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
    }

    /// <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
        //store height and width of screen

        displaywidth = graphics.GraphicsDevice.Viewport.Width;
        displayheight = graphics.GraphicsDevice.Viewport.Height;

        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);
        //load background image
        background.image = Content.Load<Texture2D>("challenge1");

        //work out ratio for image depending on screen size
        float ratio = ((float)displaywidth / background.image.Width);

        background.rect.Width = displaywidth;
        background.rect.Height = (int)(background.image.Height * ratio);

        background.rect.X = 0;
        background.rect.Y = (displayheight - background.rect.Height) / 2;

        // Loads sprites to car
        car.image = Content.Load<Texture2D>("sub1");
        car2.image = Content.Load<Texture2D>("sub2");


        reset();
        // TODO: use this.Content to load your game content here
    }
    // use the reset to initialise at start of game and call again when game is restarted
    void reset()
    {
        //set initial position car 1
        car.position = new Vector3(displaywidth / 10, displayheight / 2, 0);
        car.size = 0.5f; //set size of car
        car.origin.X = car.image.Width / 2;
        car.origin.Y = car.image.Height / 2;

        car.rect.Width = (int)(car.image.Width * car.size);
        car.rect.Height = (int)(car.image.Height * car.size);
        car.velocity = new Vector3(4, 0, 0); 
        carspeed = 0;

        //car 2
        car2.position = new Vector3(displaywidth / 10, displayheight / 1.2f, 0);
        car2.size = 0.5f; //set size of car
        car2.origin.X = car2.image.Width / 2;
        car2.origin.Y = car2.image.Height / 2;

        car2.rect.Width = (int)(car2.image.Width * car.size);
        car2.rect.Height = (int)(car2.image.Height * car.size);
        car2.velocity = new Vector3(8, 0, 0); // Velocity of (X,Y,Z)
        gameover = false;
    }
    /// <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)
    {

        KeyboardState keys = Keyboard.GetState(); //reads keyboard


        if (!gameover)
        {
            //  code for when game is running
            car.position += car.velocity; //add current velocity to pos of thr

            // check if car hits any of the four sides and bounce it off them

            if ((car.position.X + car.rect.Width / 2) >= displaywidth) //RHS

                car.position.X = 0;
                carwrap++;

                if (carwrap % 2 == 0)

                    carspeed++;


                car.rect.X = (int)car.position.X;
                car.rect.Y = (int)car.position.Y;

            //set bounding sphere
            car.bsphere = new BoundingSphere(car.position, car.rect.Width / 2);

            // car 2
            car2.position += car2.velocity; //add current velocity to pos of thr

            // check if car hits any of the four sides and bounce it off them

            if
                ((car2.position.X + car2.rect.Width / 4) >= displaywidth) //RHS
                car2.position.X = 0;
            //other way
            //if ((car2.position.X - car2.rect.Width / 2) <= 0)//LHS
            //car2.position.X = (car2.rect.Width / 2);

            car2.rect.X = (int)car2.position.X;
            car2.rect.Y = (int)car2.position.Y;

            //set bounding sphere
            car2.bsphere = new BoundingSphere(car2.position, car2.rect.Width / 2);


        }

        else
        {
                reset();
        }
        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
        spriteBatch.Begin();
        spriteBatch.Draw(background.image, background.rect, Color.White);


        //draw car
        spriteBatch.Draw(car.image, car.rect, null, Color.White, 0,car.origin, SpriteEffects.None, 0);
        spriteBatch.Draw(car2.image, car2.rect, null, Color.White, 0, car2.origin, SpriteEffects.None, 0);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Which parts of that code are relevant to the question? \$\endgroup\$
    – Anko
    Sep 16, 2014 at 16:04
  • \$\begingroup\$ I think that it's within this part if ((car.position.X + car.rect.Width / 2) >= displaywidth) //RHS car.position.X = 0; carwrap++; if (carwrap % 2 == 0) carspeed++; \$\endgroup\$
    – Cerberus
    Sep 16, 2014 at 21:29
  • \$\begingroup\$ You could clarify the question by editing it to remove the irrelevant parts. \$\endgroup\$
    – Anko
    Sep 16, 2014 at 21:41

2 Answers 2

0
\$\begingroup\$

You are doing most of it already.

When you wrap the car around the screen, you are incrementing carspeed. But you never use the carspeed variable.

You are using the velocity vector to control the speed of the car. You could, for instance, increase the velocity instead of carspeed.

\$\endgroup\$
1
  • \$\begingroup\$ Sorry to seem a noob but how could i increase the velocity \$\endgroup\$
    – Cerberus
    Sep 16, 2014 at 17:17
0
\$\begingroup\$

This looks like XNA/Monogame. If you really wanted to base it on the draw rate, change the code in the Draw() method to toggle a bool and increase the Car object position property whenever the bool toggles true.

Ideally you really don't want to do this on a draw - the car's position should be handled by update as everyone gets different refresh rates and hits the draw method a different number of time per second. You should instead look at your code in the Update() method and put your toggle in there. That way the car will consistently hop ahead a little bit equally for everyone.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .