Beispiel #1
0
        // this procedure is called when a key is pressed or released
        static void onKeyboard(object sender, SdlDotNet.Input.KeyboardEventArgs args)
        {
            if (args.Down) {

                switch (args.Key) {
                    case SdlDotNet.Input.Key.RightArrow :
                        ship.setRotation(-12);
                        break;
                    case SdlDotNet.Input.Key.LeftArrow :
                        ship.setRotation(+12);
                        break;
                    case SdlDotNet.Input.Key.UpArrow :
                        ship.thrust(2);
                        break;
                    case SdlDotNet.Input.Key.Space :
                        if (laser == null) {
                            laser = new Laser(3,ship.getX(),ship.getY(),12.0,ship.getDirection()+90);
                        }
                        break;

                    case SdlDotNet.Input.Key.Escape :
                        Events.QuitApplication();
                        break;
                }

            } else {

                switch (args.Key) {
                    case SdlDotNet.Input.Key.RightArrow :
                    case SdlDotNet.Input.Key.LeftArrow :
                        ship.setRotation(0);
                        break;
                }

            }
        }
Beispiel #2
0
        // This procedure is called (invoked) for each window refresh
        static void onTick(object sender, TickEventArgs args)
        {
            // STEP
            // Update the automagic elements and enforce the rules of the game here.

            ship.move();

            if (ship.getX() < 0) {
                ship.setX(FRAME_WIDTH);
            } else if (ship.getX() > FRAME_WIDTH) {
                ship.setX(0);
            }

            if (ship.getY() < 0) {
                ship.setY(FRAME_HEIGHT);
            } else if (ship.getY() > FRAME_HEIGHT) {
                ship.setY(0);
            }

            for (int i = 0; i < asteroids.Length; ++i) {
                asteroids[i].move();

                if (asteroids[i].getX() < 0) {
                    asteroids[i].setX(FRAME_WIDTH);
                } else if (asteroids[i].getX() > FRAME_WIDTH) {
                    asteroids[i].setX(0);
                }

                if (asteroids[i].getY() < 0) {
                    asteroids[i].setY(FRAME_HEIGHT);
                } else if (asteroids[i].getY() > FRAME_HEIGHT) {
                    asteroids[i].setY(0);
                }
            }

            if (laser != null) {
                Beam beam = laser.getBeam();
                Newtonian.move(beam.getPosition(),beam.getMotion());
                if (   (beam.getPosition().getX() < 0)
                    || (beam.getPosition().getX() > FRAME_WIDTH)
                    || (beam.getPosition().getY() < 0)
                    || (beam.getPosition().getY() > FRAME_HEIGHT)) {
                    laser = null;
                }
            }

            // DRAW
            // Draw the new view of the game based on the state of the elements here.

            drawBackground();

            if (laser != null) {
                drawSprite(laser.getSprite(),(int)laser.getBeam().getPosition().getX(), (int)laser.getBeam().getPosition().getY(), (int)laser.getBeam().getMotion().getDirection()+90);
            }

            drawSprite(ship.getSprite(),ship.getX(),ship.getY(),ship.getDirection());

            for (int i = 0; i < asteroids.Length; ++i) {
                drawSprite(asteroids[i].getSprite(), asteroids[i].getX(), asteroids[i].getY(), asteroids[i].getDirection());
            }

            // ANIMATE
            // Step the animation frames ready for the next tick
            // ...

            // REFRESH
            // Tranfer the new view to the screen for the user to see.
            video.Update();
        }
        // This procedure is called (invoked) for each window refresh
        static void onTick(object sender, TickEventArgs args)
        {
            // STEP
            // Update the automagic elements and enforce the rules of the game here.

            duck.move();

            step++;

            if (step > 60)
            {
                for (int i = 0; i < asteroids.Length; ++i)
                {
                    if (asteroids[i].getSprite() == 2)
                    {
                        asteroids[i].setSprite(1);
                    }
                    else
                    {
                        asteroids[i].setSprite(2);
                    }
                    step = 0;
                }
            }

            if (duck.getX() < 0)
            {
                duck.setX(FRAME_WIDTH);
            }
            else if (duck.getX() > FRAME_WIDTH)
            {
                duck.setX(0);
            }

            if (duck.getY() < 0)
            {
                duck.setY(FRAME_HEIGHT);
            }
            else if (duck.getY() > FRAME_HEIGHT)
            {
                duck.setY(0);
            }

            for (int i = 0; i < asteroids.Length; ++i)
            {
                asteroids[i].move();

                if (asteroids[i].getX() < 0)
                {
                    asteroids[i].setX(FRAME_WIDTH);
                }
                else if (asteroids[i].getX() > FRAME_WIDTH)
                {
                    asteroids[i].setX(0);
                }

                if (asteroids[i].getY() < 0)
                {
                    asteroids[i].setY(FRAME_HEIGHT);
                }
                else if (asteroids[i].getY() > FRAME_HEIGHT)
                {
                    asteroids[i].setY(0);
                }
            }

            if (laser != null)
            {
                Beam beam = laser.getBeam();
                Newtonian.move(beam.getPosition(), beam.getMotion());
                if ((beam.getPosition().getX() < 0) ||
                    (beam.getPosition().getX() > FRAME_WIDTH) ||
                    (beam.getPosition().getY() < 0) ||
                    (beam.getPosition().getY() > FRAME_HEIGHT))
                {
                    laser = null;
                }
            }


            // DRAW
            // Draw the new view of the game based on the state of the elements here.

            drawBackground();

            if (laser != null)
            {
                drawSprite(laser.getSprite(), (int)laser.getBeam().getPosition().getX(), (int)laser.getBeam().getPosition().getY(), (int)laser.getBeam().getMotion().getDirection() + 90);
            }

            drawSprite(duck.getSprite(), duck.getX(), duck.getY(), duck.getDirection());

            for (int i = 0; i < asteroids.Length; ++i)
            {
                drawSprite(asteroids[i].getSprite(), asteroids[i].getX(), asteroids[i].getY(), asteroids[i].getDirection());
            }


            // ANIMATE
            // Step the animation frames ready for the next tick
            // ...

            // REFRESH
            // Tranfer the new view to the screen for the user to see.
            video.Update();
        }