Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.

Paste

Pasted as C# by registered user Brunaldo ( 14 years ago )
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 MiniRun
{

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    /// private Vector2 yourPosition;
    public class Game1 : Microsoft.Xna.Framework.Game
        {         
            Texture2D background;
            Rectangle mainFrame;
            GraphicsDeviceManager graphics;

            SpriteBatch spriteBatch;
          
            Texture2D car1blueTexture;
            Vector2 car1bluePosition = new Vector2(120f, 520f);
            int car1blueSpeed = 3;

            Texture2D car2redTexture;
            Vector2 car2redPosition = new Vector2(75f, 520f);
            int car2redSpeed = 3;

            Song song;
            bool songstart = false;

            Texture2D greyBumper;
            Texture2D lowerRedBumper;
            Texture2D upperRedBumper;
            Texture2D innerGrass;
            Texture2D outerGrass;
            Texture2D box;
            Texture2D startingLine;
            Vector2 startingLinePosition = new Vector2(0f, 0f);
            Vector2 innerGrassPosition = new Vector2(0f, 0f);
            Vector2 outerGrassPosition = new Vector2(0f, 0f);
            Vector2 greyBumperPosition = new Vector2(0f, 0f);     
            Vector2 upperRedBumperPosition = new Vector2(0f, 0f);
            Vector2 lowerRedBumperPosition = new Vector2(0f, 0f);
            Vector2 boxPosition = new Vector2(0f, 0f);

            Texture2D chequeredFlagTexture;
            Vector2 checkeredFlagPosition = new Vector2(0f, 0f);

        
            Rectangle sourceRect;
            Rectangle destinationRedRect;
            Rectangle destinationBlueRect;
            int car1blueWidth = 25;
            int car1blueHeight = 50;
            int car2redWidth = 25;
            int car2redHeight = 50;

          

            Color[] car1blueTextureData;
            Color[] car2redTextureData;
            Color[] greyBumperTextureData;
            Color[] lowerRedBumperTextureData;
            Color[] upperRedBumperTextureData;
            Color[] innerGrassTextureData;
            Color[] outerGrassTextureData;
            Color[] boxTextureData;
            Color[] chequeredFlagTextureData;
            Color[] startingLineTextureData;
            int direction = 0;
            bool blueHit = false;
            bool redHit = false;

            


            //Set origin and roation angle for car
            Vector2 car1blueOrigin;
            private float car1blueRotationAngle = 4.7f;

            Vector2 car2redOrigin;
            private float car2redRotationAngle = 4.7f;

            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }
       
            protected override void Initialize()
            {
            
                //Change the resolution to 1000x800          
                graphics.PreferredBackBufferWidth = 1000;
                graphics.PreferredBackBufferHeight = 800;
                graphics.ApplyChanges();
               
                base.Initialize();
                base.Initialize();
            }

       
            protected override void LoadContent()
            {
            // Create a new SpriteBatch, which can be used to draw textures
            spriteBatch = new SpriteBatch(GraphicsDevice);
            // Load the background content.
            background = Content.Load<Texture2D>("roadscan");
            mainFrame = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            car1blueTexture = Content.Load<Texture2D>("car1blue");
            car1blueOrigin = new Vector2(car1blueTexture.Width / 2, car1blueTexture.Height / 2);
            destinationBlueRect = new Rectangle(25, 50, car1blueWidth,car1blueHeight);
            car1blueTextureData = new Color[car1blueTexture.Width * car1blueTexture.Height];
            car1blueTexture.GetData(car1blueTextureData);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            car2redTexture = Content.Load<Texture2D>("car2red");
            car2redOrigin = new Vector2(car2redTexture.Width / 2, car2redTexture.Height / 2);
            destinationRedRect = new Rectangle(25, 50, car2redWidth,car2redHeight);
            car2redTextureData = new Color[car2redTexture.Width * car2redTexture.Height];
            car2redTexture.GetData(car2redTextureData);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            greyBumper = Content.Load<Texture2D>("greyBumper");
            greyBumperTextureData = new Color[greyBumper.Width * greyBumper.Height];
            greyBumper.GetData(greyBumperTextureData);

            song = Content.Load<Song>("song");
            MediaPlayer.IsRepeating = true;

            spriteBatch = new SpriteBatch(GraphicsDevice);
            upperRedBumper = Content.Load<Texture2D>("upperRedBumper");
            upperRedBumperTextureData = new Color[upperRedBumper.Width * upperRedBumper.Height];
            upperRedBumper.GetData(upperRedBumperTextureData);


            spriteBatch = new SpriteBatch(GraphicsDevice);
            lowerRedBumper = Content.Load<Texture2D>("lowerRedBumper");
            lowerRedBumperTextureData = new Color[lowerRedBumper.Width * lowerRedBumper.Height];
            lowerRedBumper.GetData(lowerRedBumperTextureData);


            spriteBatch = new SpriteBatch(GraphicsDevice);
            chequeredFlagTexture = Content.Load<Texture2D>("chequeredFlag");

            spriteBatch = new SpriteBatch(GraphicsDevice);
            box = Content.Load<Texture2D>("box");
            boxTextureData = new Color[box.Width * box.Height];
            box.GetData(boxTextureData);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            innerGrass = Content.Load<Texture2D>("innerGrass");
            innerGrassTextureData = new Color[innerGrass.Width * innerGrass.Height];
            innerGrass.GetData(innerGrassTextureData);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            outerGrass = Content.Load<Texture2D>("outerGrass");
            outerGrassTextureData = new Color[outerGrass.Width * outerGrass.Height];
            outerGrass.GetData(outerGrassTextureData);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            startingLine = Content.Load<Texture2D>("startingLine");
            startingLineTextureData = new Color[startingLine.Width * startingLine.Height];
            startingLine.GetData(startingLineTextureData);
            }


        
        
            protected override void UnloadContent()
            {
           
            }


            protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            

            KeyboardState keyboard = Keyboard.GetState();
            GamePadState gamePad = GamePad.GetState(PlayerIndex.One);


            GamePad.SetVibration(PlayerIndex.One, 0, 0);

            // Move the sprites around.

            direction = 0;
            blueHit = false;
            redHit = false;

            destinationBlueRect.X = (int)car1bluePosition.X;
            destinationBlueRect.Y = (int)car1bluePosition.Y;

            destinationRedRect.X = (int)car2redPosition.X;
            destinationRedRect.Y = (int)car2redPosition.Y;


            Rectangle greyBumperRectangle = new Rectangle(0, 0, greyBumper.Width, greyBumper.Height);
            Rectangle lowerRedBumperRectangle = new Rectangle(0, 0, lowerRedBumper.Width, lowerRedBumper.Height);
            Rectangle upperRedBumperRectangle = new Rectangle(0, 0, upperRedBumper.Width, upperRedBumper.Height);
            Rectangle innerGrassRectangle = new Rectangle(0, 0, innerGrass.Width, innerGrass.Height);
            Rectangle outerGrassRectangle = new Rectangle(0, 0, outerGrass.Width, outerGrass.Height);
            Rectangle boxRectangle = new Rectangle(0, 0, box.Width, box.Height);

            if (!songstart)
            {
                MediaPlayer.Play(song);
                songstart = true;
            }
            ///Player 1 Controlls (car1blue)

            if (keyboard.IsKeyDown(Keys.Left) || gamePad.DPad.Left == ButtonState.Pressed)
            {
                car1blueRotationAngle -= 0.05f;
                
            
                direction = 1;
            }

            if (keyboard.IsKeyDown(Keys.Right) || gamePad.DPad.Right == ButtonState.Pressed)
            {
                   car1blueRotationAngle += 0.05f;
                    direction = 2;
            }

               
                if (keyboard.IsKeyDown(Keys.Up) || gamePad.DPad.Right == ButtonState.Pressed)
                {

                    car1bluePosition.X += car1blueSpeed * (float)Math.Sin(car1blueRotationAngle + Math.PI / 2);
                    car1bluePosition.Y += car1blueSpeed * -(float)Math.Cos(car1blueRotationAngle + Math.PI / 2);
                    direction = 3;
                }

                ///Player 2 Controls (car2red)                                      
                  
                    
                if (keyboard.IsKeyDown(Keys.A) || gamePad.DPad.Left == ButtonState.Pressed)
                 {
                 car2redRotationAngle -= 0.05f;
                 direction = 1;
                 }

                 if (keyboard.IsKeyDown(Keys.D) || gamePad.DPad.Right == ButtonState.Pressed)
                 {
     
                 car2redRotationAngle += 0.05f;     
                 direction = 2;
                 }

                  if (keyboard.IsKeyDown(Keys.W) || gamePad.DPad.Right == ButtonState.Pressed)
                     {
      
                     car2redPosition.X += car2redSpeed * (float)Math.Sin(car2redRotationAngle + Math.PI / 2);
                     car2redPosition.Y += car2redSpeed * -(float)Math.Cos(car2redRotationAngle + Math.PI / 2);                          
                     direction = 3;
                     }

          if (direction == 0)
             {
             sourceRect = new Rectangle(0, 0, 50, 25);
             }

             ////////////////////////////////////////////////////////////////////////////////

             if (IntersectPixels(destinationBlueRect, car1blueTextureData, greyBumperRectangle,
                 greyBumperTextureData))
                            
             {
                blueHit = true;
        
             }

             if (IntersectPixels(destinationBlueRect, car1blueTextureData, lowerRedBumperRectangle,
          lowerRedBumperTextureData))
             {
                 blueHit = true;

             }
             if (IntersectPixels(destinationBlueRect, car1blueTextureData, upperRedBumperRectangle,
              upperRedBumperTextureData))
             {
                 blueHit = true;

             }
             if (IntersectPixels(destinationBlueRect, car1blueTextureData, boxRectangle, boxTextureData))
             {
                 blueHit = true;
             }

             if (IntersectPixels(destinationBlueRect, car1blueTextureData, innerGrassRectangle,
                   innerGrassTextureData))
             {
                 car1blueSpeed = 1;

             }
             else
             {
                 car1blueSpeed = 3;
             }

             if (IntersectPixels(destinationBlueRect, car1blueTextureData, outerGrassRectangle,
            outerGrassTextureData))
             {
                 car1blueSpeed = 1;
             }
             else
             {
                 car1blueSpeed = 3;
             }
     
             /////////////////////////////////////////////////////////////////////////////////////
             if (IntersectPixels(destinationRedRect, car2redTextureData, greyBumperRectangle,
                  greyBumperTextureData))
             {
                 redHit = true;
             }
             if (IntersectPixels(destinationRedRect, car2redTextureData, lowerRedBumperRectangle,
           lowerRedBumperTextureData))
             {
                 redHit = true;
             }

             if (IntersectPixels(destinationRedRect, car2redTextureData, upperRedBumperRectangle,
        upperRedBumperTextureData))

             {
                 redHit = true;
             }
             if (IntersectPixels(destinationRedRect, car2redTextureData, boxRectangle, boxTextureData))
             {
                 redHit = true;
             }
                       if (IntersectPixels(destinationRedRect, car2redTextureData, outerGrassRectangle,
            outerGrassTextureData))
                    {
                        car2redSpeed = 1;
                    }
                    else
                    {
                        car2redSpeed = 3;
                    }
                    if (IntersectPixels(destinationRedRect, car2redTextureData, innerGrassRectangle,
            innerGrassTextureData))
                    {
                        car2redSpeed = 1;
                    }
                    else
                    {
                        car2redSpeed = 3;
                    }    
     

                      
             base.Update(gameTime);
                                
             }
       
            protected override void Draw(GameTime gameTime)
            {
             GraphicsDevice.Clear(Color.CornflowerBlue);

            

             if (blueHit)
             {
                graphics.GraphicsDevice.Clear(Color.Red);
                GamePad.SetVibration(PlayerIndex.One, 0, 1);

                 car1bluePosition.X = 120.0f;
                 car1bluePosition.Y = 420.0f;
                 car1blueRotationAngle = 4.7f;

             }
             if (redHit)
             {
                 graphics.GraphicsDevice.Clear(Color.Red);
                 GamePad.SetVibration(PlayerIndex.Two, 0, 1);

                 car2redPosition.X = 75.0f;
                 car2redPosition.Y = 420.0f;
                 car2redRotationAngle = 4.7f;
             }
             else
             {
                 graphics.GraphicsDevice.Clear(Color.White);
                 GamePad.SetVibration(PlayerIndex.One, 0, 0);

                 graphics.GraphicsDevice.Clear(Color.White);
                 GamePad.SetVibration(PlayerIndex.Two, 0, 0);
             }
             

            spriteBatch.Begin();
            spriteBatch.Draw(background, mainFrame, Color.White);                    
       
            spriteBatch.Draw(greyBumper, greyBumperPosition, Color.White);        
            spriteBatch.Draw(upperRedBumper, upperRedBumperPosition, Color.White);       
            spriteBatch.Draw(lowerRedBumper, lowerRedBumperPosition, Color.White);        
    
            spriteBatch.Draw(innerGrass, innerGrassPosition, Color.White);
            spriteBatch.Draw(outerGrass, outerGrassPosition, Color.White);
            spriteBatch.Draw(car1blueTexture, car1bluePosition, null, Color.White, car1blueRotationAngle, car1blueOrigin, 1.0f,
            SpriteEffects.None, 0f);
            spriteBatch.Draw(car2redTexture, car2redPosition, null, Color.White, car2redRotationAngle, car2redOrigin, 1.0f,
            SpriteEffects.None, 0f);
            spriteBatch.Draw(chequeredFlagTexture, checkeredFlagPosition, Color.White);
            spriteBatch.Draw(startingLine, startingLinePosition, Color.White);
            spriteBatch.End();                       
        

            

            base.Draw(gameTime);
            }

          
            static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
                                        Rectangle rectangleB, Color[] dataB)
            {
           // Find the bounds of the rectangle intersection
            int top = Math.Max(rectangleA.Top, rectangleB.Top);
            int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
            int left = Math.Max(rectangleA.Left, rectangleB.Left);
            int right = Math.Min(rectangleA.Right, rectangleB.Right);

           // Check every point within the intersection bounds
            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
           // Get the color of both pixels at this point
                    Color colorA = dataA[(x - rectangleA.Left) +
                                  (y - rectangleA.Top) * rectangleA.Width];
                    Color colorB = dataB[(x - rectangleB.Left) +
                                  (y - rectangleB.Top) * rectangleB.Width];

           // If both pixels are not completely transparent,
                    if (colorA.A != 0 && colorB.A != 0)
                    {
           // then an intersection has been found
                        return true;
                    }
                }
            }

            // No intersection found
            return false;
        }

    }
}

 

Revise this Paste

Your Name: Code Language: