Jian's profile星舞PhotosBlogListsMore Tools Help

Blog


    12/18/2008

    无语。。

    最近这个空间通过搜索引擎连过来的访问量增长不少。。。一看,大部分都是连到我N年前随便写的一个学期总结上。。感慨啊。。。现在基本上是国内要求写学期总结的时间,可怎么现在国内的学生堕落到连写个学期总结也要上网找的程度?当年读高中的时候,一个星期要求交一篇随笔,一个学期后,基本上人手一本厚厚的硬皮笔记本,算起来好歹也2多万字了,那会儿大家都还不知道上网去搜,写的都是自己原创,实打实的东西,如果换成现在?search->print,一学期200万字也轻松给你搞定了!
    12/11/2008

    写在学期结束后

    本学期有点累。。。但是,还是很有价值的,积累了些经验,对今后很有帮助~
     
    对我而言,08年基本上算是过去了。。。真是多灾多难的一年,新车提到的第一天就被个死阿X追尾!课业上也一直心惊胆颤的。。。本命年,一切过的都很小心。。。车买来2个月才开了不到2000公里,在加拿大这个鬼地方算是很少的了。。。
     
    哎。过去的事就不去想了,明天开始,可以做自己喜欢做的事情了~去年我在这个空间说过要在24岁生日前要完成一款游戏!不容易。。。我真的是在生日的前几个小时完成了它,算是一种对自己说过的话负责的另类表现吧。。。游戏虽然很简陋。。但是好歹算是我的第一款游戏,纪念意义远远大于实际的效果。。。而且通过了那次经验,我已经完全有信心完成一款复杂的游戏~
     
    所以,和去年一样,我计划在明年生日前完成一款真正意义上的游戏,所有的一切都是由自己独立开发完成的,包括美工等内容~为了实现这个“远大”的目标,明天就开坑~

    C++&openGL制作的简单射击游戏源代码

    游戏特色:

    1. 飞船是由物理引擎控制的,不同的状态显示不同的图片效果(图片自己画的,时间紧所以有点恶心。。。)

    2. 实现了射击和手工填弹功能

    3. 小行星全方位出现,随机角度随机速度

    4. 增加了道具系统,在飞行过程中可以“吃”各种道具

    5. 小行星碰撞飞船后,会依照角度改变方向

    5. 实现了生命值显示,活动背景

    6. 小行星全灭后出现个BOSS,是由Keyframe system控制轨迹的,但是因为时间关系,BOSS除了会动和挨打外啥也干不了,我也懒得去完善它,以后自己做的小游戏中会增加更多功能

    点此下载源代码

    XNA制作的简单飞行闪避小行星游戏

    通用的东西就不写了,节约空间

    游戏规则:

    小行星会源源不断的从屏幕上方出现,每个小行星会有随机的速度和角度,玩家操控飞机闪避这些小行星,每出现一个行星,分数+1

    class Meteor
        {
            public Texture2D sprite; //used to store the picture of meteor
            public Vector2 position; //the position of the meteor
            public double angle; //the angle of the meteor
            public Vector2 center; //the center of the meteor picture
            public Vector2 velocity; //the velocity of the meteor
            public bool alive; //used to determine the meteor state
            public Rectangle rect; //the rectangle of meteor

            //initial the meteor class
            public Meteor(Texture2D loadedTexture)
            {
                angle = 0.0f;
                position = Vector2.Zero;
                sprite = loadedTexture;
                center = new Vector2(sprite.Width / 2, sprite.Height / 2);
                velocity = Vector2.Zero;
                alive = false;
            }
        }

    class SpaceShip
    {
        public Texture2D sprite; //used to store the picture of space ship
        public Vector2 position; //the space ship's position
        public Vector2 center; //the center of the space ship
        public Vector2 velocity; //the space ship's velocity
        public bool alive; //used to determine the space ship's state
        public Rectangle rect; //the rectangle of space ship

       //initial the space ship class
        public SpaceShip(Texture2D loadedTexture)
        {
            position = Vector2.Zero;
            sprite = loadedTexture;
            center = new Vector2(sprite.Width / 2, sprite.Height / 2);
            velocity = Vector2.Zero;
            alive = false;
        }
    }

    /// <summary>
    /// This is the main type for your game
    /// </summary>

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

            Texture2D background; //used to store the backgroud picture
            Rectangle viewportRect; //viewport's rectangle
            SpaceShip spaceship; //the space ship
            const int maxMeteors = 10; //the max number of meteors
            Meteor[] meteorList; //a meteor list to hold all meteors
            const float maxMeteorVelocity = 8.0f; //max velocity of meteor
            const float minMeteorVelocity = 3.0f; //min velocity of meteor
            Random random = new Random(); //used to get random value

            int score = 0; //used to store score, initial is 0
            int highestScore = 0; //the highest score
            SpriteFont font;
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }

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

                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 picture
                background = Content.Load<Texture2D>("Sprites\\SpaceBackground");
                //load the space ship
                spaceship = new SpaceShip(Content.Load<Texture2D>("Sprites\\spaceShip"));
                spaceship.position = new Vector2(graphics.GraphicsDevice.Viewport.Width/2,graphics.GraphicsDevice.Viewport.Height-75);//set up the position
                spaceship.rect = new Rectangle((int)spaceship.position.X, (int)spaceship.position.Y, 30, 30);
                //load meteors
                meteorList = new Meteor[maxMeteors];
                for (int i=0; i<maxMeteors; i++)
                {
                    meteorList[i] = new Meteor(Content.Load<Texture2D>("Sprites\\meteor"));
                }
                //load game font
                font = Content.Load<SpriteFont>("Fonts\\GameFont");
                //drawable area of the game screen.
                viewportRect = new Rectangle(0, 0,
                    graphics.GraphicsDevice.Viewport.Width,
                    graphics.GraphicsDevice.Viewport.Height);

            }

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

            //update the meteor position
            public void UpdateMeteors()
            {
                foreach (Meteor meteor in meteorList)
                {

                    if (meteor.alive) //if meteor.alive is true
                    {
                        meteor.position += meteor.velocity; //update meteor's position
                        meteor.rect.X = (int)meteor.position.X;
                        meteor.rect.Y = (int)meteor.position.Y;
                        //if the meteor out of the screen
                        if (!viewportRect.Contains(new Point(
                            (int)meteor.position.X,
                            (int)meteor.position.Y)))
                        {
                            meteor.alive = false;
                        }
                    }
                    else
                    {
                        meteor.alive = true;
                        //position (x,y), x is a random value between screen left and screen right, y is the screen top
                        meteor.position = new Vector2(
                            MathHelper.Lerp(
                            (float)viewportRect.Left,
                            (float)viewportRect.Right,
                            (float)random.NextDouble()),
                            viewportRect.Top);
                        meteor.rect = new Rectangle((int)meteor.position.X,(int)meteor.position.Y, 45, 45);
                       //the angle of the meteor's velocity
                        meteor.angle = MathHelper.Lerp(MathHelper.ToRadians(-15), MathHelper.ToRadians(15), (float)random.NextDouble());
                        //the velocity of the meteor
                        meteor.velocity = new Vector2(
                            //x velocity
                            MathHelper.Lerp(
                            minMeteorVelocity,
                            maxMeteorVelocity,
                            (float)random.NextDouble())*(float)Math.Sin(meteor.angle),
                           //y velocity
                            MathHelper.Lerp(
                            minMeteorVelocity,
                            maxMeteorVelocity,
                            (float)random.NextDouble())*(float)Math.Cos(meteor.angle)
                            );
                        score++; //each alive meteor increase score by 1
                        if (highestScore < score)
                            highestScore = score; //get the highest score
                    }
                    //if meteor hits the space ship
                    if (meteor.rect.Intersects(spaceship.rect))
                    {
                        //initial the space ship's position
                        spaceship.position = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height - 75);
                        //kill all meteors in the meteor list therefore initial meteors
                        foreach (Meteor meteors in meteorList)
                        {
                            meteors.alive = false;
                        }
                       //initial score
                        score = 0;
                    }
                }
            }

            //update the position of space ship
            public void UpdateSpaceShip()
            {
                spaceship.rect.X = (int)spaceship.position.X;
                spaceship.rect.Y = (int)spaceship.position.Y;
            }

            /// <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 (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();

    #if !XBOX
                KeyboardState keyboardState = Keyboard.GetState();
                //'Q' exit the game
                if (keyboardState.IsKeyDown(Keys.Q))
                    this.Exit();
                if (keyboardState.IsKeyDown(Keys.Left))
                {
                    //make sure the space ship doesn't move out of screen
                    if (spaceship.position.X > viewportRect.Left - 3)
                        spaceship.position.X -= 3;
                }
                if (keyboardState.IsKeyDown(Keys.Right))
                {
                    //make sure the space ship doesn't move out of screen
                    if (spaceship.position.X < viewportRect.Right - 3)
                        spaceship.position.X += 3;
                }
                if (keyboardState.IsKeyDown(Keys.Down))
                {
                    //make sure the space ship doesn't move out of screen
                    if (spaceship.position.Y < graphics.GraphicsDevice.Viewport.Height - 3)
                        spaceship.position.Y += 3;
                }
                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    //make sure the space ship doesn't move out of screen
                    if (spaceship.position.Y > 3)
                        spaceship.position.Y -= 3;
                }
    #endif
                UpdateSpaceShip();
                UpdateMeteors();
                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)
            {
                graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

                spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

                //Draw the background sized to the width and height of the screen.
                spriteBatch.Draw(background, viewportRect,
                    Color.White);
                spriteBatch.Draw(spaceship.sprite, spaceship.rect, Color.White);
                //Draw meteors
                foreach (Meteor meteor in meteorList)
                {
                    if (meteor.alive)
                    {
                        spriteBatch.Draw(meteor.sprite,
                            meteor.rect, Color.White);
                    }
                }
               //draw score
                spriteBatch.DrawString(font,
                    "Score: " + score.ToString(),
                    new Vector2(20,20),
                    Color.Yellow);

                spriteBatch.DrawString(font,
                    "Highest Score: " + highestScore.ToString(),
                    new Vector2(160,20),
                    Color.Red);

                spriteBatch.End();

                base.Draw(gameTime);
            }
        }