Beispiel #1
0
        /// <summary>
        /// Initialises the game elements
        /// </summary>
        internal void init()
        {
            ball        = new Ball(new Point(430, 130), new Size(50, 50));
            leftRacket  = new Racket(new Point(Racket.xDistance, 20), new Size(10, 170));
            rightRacket = new Racket(new Point(gameRenderSize.Width - Racket.xDistance, 225), new Size(10, 170));


            drawList.Add(ball);
            drawList.Add(leftRacket);
            drawList.Add(rightRacket);
        }
Beispiel #2
0
        public Form1()
        {
            InitializeComponent();
            //Hard-coded locations for fix form size
            racket1 = new Racket(START_X, this.Size.Height / 2 - 50);
            racket2 = new Racket(this.Size.Width - START_X * 2, this.Size.Height / 2 - 50);

            upFrame   = new Racket(0, 0, this.Size.Width, 5);
            downFrame = new Racket(0, this.Size.Height - 45, this.Size.Width, 5);

            ball           = new Ball(this.Size.Width / 2, this.Size.Height / 2 - 35);
            timer          = new Timer();
            timer.Tick    += Timer_Tick;
            timer.Interval = 1;
            timer.Start();

            this.DoubleBuffered = true;
        }
Beispiel #3
0
        /// <summary>
        /// Calc the next Racket colliding position and move the Racket to this pos
        /// </summary>
        /// <param name="g">GameObject</param>
        /// <param name="b">Ball to check</param>
        /// <param name="r">Racket to check</param>
        /// <param name="dir">Racket direction</param>
        public static void calc(PongGame g, Ball b, Racket r, Racket.Direction dir)
        {
            Ball imag = new Ball(new Point(b.Position.X, b.Position.Y), new Size(50, 50));

            imag.dx = int.Parse(b.dx.ToString());
            imag.dy = int.Parse(b.dy.ToString());


            while (imag.isCollidingWallEX(g.gameRenderSize) != dir)
            {
                imag.Move(0.2d, 1);
            }

            // Console.WriteLine("while ended colliding: " + imag.Position.ToString());
            _ii.Add(imag.Position.Y);
            int i = imag.Position.Y - (int)(imag.Size.Height);  ///0.5f

            // Console.WriteLine("moving to: " + i);
            r.MoveTo(i, g.gameRenderSize, g.speed);
        }
Beispiel #4
0
        protected override void LoadContent()
        {
            // Initializing the font that will be used for the score
            font = Content.Load <SpriteFont>("font");

            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Creating the ball
            ball = new Ball(GraphicsDevice, spriteBatch, this, ballSize);

            // Creating the player rackets
            Player1 = new Racket(GraphicsDevice, spriteBatch, this, racketWidth, racketHeight, 10, GraphicsDevice.Viewport.Height / 2 - racketHeight / 2);
            Player2 = new Racket(GraphicsDevice, spriteBatch, this, racketWidth, racketHeight, GraphicsDevice.Viewport.Width - 10 - racketWidth, GraphicsDevice.Viewport.Height / 2 - racketHeight / 2);

            // Adding the rackets for the players and the ball to the game
            Components.Add(Player1);
            Components.Add(Player2);
            Components.Add(ball);

            ball.ResetBall();
        }
Beispiel #5
0
        /// <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);

            var wallTexture = Content.Load<Texture2D>("wall");
            _topWall = new GameObject(wallTexture,Vector2.Zero);
            _bottomWall = new GameObject(wallTexture, new Vector2(0, Window.ClientBounds.Height - wallTexture.Height));

            var paddleTexture = Content.Load<Texture2D>("paddle");
            Vector2 position;

            position = new Vector2(0, (Window.ClientBounds.Height - paddleTexture.Height)/2);
            _playerOne = new Racket(paddleTexture, position);

            position = new Vector2((Window.ClientBounds.Width - paddleTexture.Width),
                                   (Window.ClientBounds.Height - paddleTexture.Height)/2);
            _playerTwo  = new Racket(paddleTexture, position);

            var ballTexture = Content.Load<Texture2D>("ball");
            position = new Vector2(_playerOne.BoundingBox.Right + 1, (Window.ClientBounds.Height - ballTexture.Height)/2);
            _ball = new GameObject(ballTexture, position, new Vector2(8f, -8f));
        }
Beispiel #6
0
        /// <summary>
        /// Checks collisions with Racket
        /// </summary>
        /// <param name="b">used ball</param>
        /// <param name="racket">Racket to check</param>
        /// <param name="dir">RacketDirection</param>
        /// <param name="calc">Auto play second racket</param>
        public void checkColision(Ball b, Racket racket, Racket.Direction dir, bool calc = false)
        {
            int rackTop = racket.Position.Y - b.Size.Height / 2;
            int rackBot = racket.Position.Y + racket.Size.Height + b.Size.Height / 2;

            //Console.WriteLine("{0}/{1}", rackTop, rackBot);

            if (dir == Racket.Direction.Right)
            {
                if (b.Position.X >= (racket.Position.X - b.Size.Width))
                {
                    //  Console.WriteLine("LOOG  {0}>={1}", b.Position.X, racket.Position.X - (b.Size.Width / 2) - racket.Size.Width);
                    if (b.Position.Y >= rackTop && b.Position.Y <= rackBot)   //evntl. toleranzzone berechnen
                    {
                        b.dx        *= -1;
                        b.Position.X = b.Position.X - 5;
                        Console.WriteLine("Collided right:" + b.Position.ToString());


                        if (calc)
                        {
                            var dispatcherTimer = new Timer {
                                Interval = 500
                            };
                            dispatcherTimer.Tick += (sender, args) =>
                            {
                                var timer = sender as Timer;
                                if (timer != null)
                                {
                                    timer.Stop();
                                }

                                Console.WriteLine("Calc pos");
                                Ball.calc(this, ball.Clone(), leftRacket, Racket.Direction.Left);
                            };
                            dispatcherTimer.Start();
                        }
                    }
                }
            }
            else if (dir == Racket.Direction.Left)
            {
                if (b.Position.X <= (racket.Position.X + b.Size.Width / 12))
                {
                    if (b.Position.Y >= rackTop && b.Position.Y <= rackBot)
                    {
                        b.dx        *= -1;
                        b.Position.X = b.Position.X + 5;
                        Console.WriteLine("Collided left:" + b.Position.ToString());
                        this.calc = false;
                        if (calc)
                        {
                            var dispatcherTimer = new Timer {
                                Interval = 500
                            };
                            dispatcherTimer.Tick += (sender, args) =>
                            {
                                var timer = sender as Timer;
                                if (timer != null)
                                {
                                    timer.Stop();
                                }

                                Console.WriteLine("Calc pos");
                                Ball.calc(this, ball.Clone(), rightRacket, Racket.Direction.Right);
                            };
                            dispatcherTimer.Start();
                        }
                    }
                }
            }

            if (racket.Position.Y <= 0)
            {
                racket.Position.Y = 0;
            }


            if (racket.Position.Y >= (gameRenderSize.Height - racket.Size.Height))
            {
                racket.Position.Y = gameRenderSize.Height - racket.Size.Height;
            }
        }