public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; menuSprite = new Sprite(); pointerSprite = new Sprite(); creditSprite = new Sprite(); gameOverSprite = new Sprite(); gameball = new Ball(285, 285, 30, 30, 5, 5); paddle1 = new Paddle(1); paddle2 = new Paddle(2); paddle3 = new Paddle(3); paddle4 = new Paddle(4); ballsprite = new Sprite(gameball.getx(), gameball.gety()); paddlesprite1 = new Sprite(paddle1.getX(), paddle1.getY()); paddlesprite2 = new Sprite(paddle2.getX(), paddle2.getY()); paddlesprite3 = new Sprite(paddle3.getX(), paddle3.getY()); paddlesprite4 = new Sprite(paddle4.getX(), paddle4.getY()); menu = new Menu(); }
public void collision(Paddle pad) { //checks if a bullet has collided with another bullet int padx = pad.getX(); int pady = pad.getY(); int padw = pad.getWidth(); int padh = pad.getHeight(); // Console.WriteLine(padx + " " + pady + " " + ballx + " " + bally + " " + (padx <= ballx) + " " + (ballx <= padx + padw) + " " + (pady <= bally) + " " + (bally <= pady + padh)); var bx = ballx + ballw / 2f; var by = bally + ballh / 2f; if (padx <= bx && bx <= padx + padw && pady <= by && by <= pady + padh) { dx *= -1; dy *= -1; /*if (pad.getPlayer() == 1) { dx *= -1; dy *= randomneg(); } if (pad.getPlayer() == 2) { dx *= -1; dy *= randomneg(); } if (pad.getPlayer() == 3) { dx *= randomneg(); dy *= -1; } if (pad.getPlayer() == 4) { dx *= randomneg(); dy *= -1; }*/ //dx += 1; //dy += 1; */ } }
private void MainLoop(object sender, EventArgs e) { /* Clears the Screen */ CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600); pongBall.Draw(this.CreateGraphics()); right.Draw(this.CreateGraphics()); left.Draw(CreateGraphics()); pongBall.calculate(); if (pongBall.getX() <= 0) { left.setPoints(left.getPoints() + 1); if (left.getPoints() >= numberOfPointsToWin) { CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600); CreateGraphics().DrawString("Right WINS!", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(100, 100)); t.Enabled = false; } right.setX(770); right.setY(250); left.setX(30); left.setY(250); pongBall.setX(400); pongBall.setY(300); pongBall.setXVelocity((int)(randomObject.Next(4, pongBall.getMaxSpeed() * 2) - pongBall.getMaxSpeed())); pongBall.setYVelocity((int)(randomObject.Next(4, pongBall.getMaxSpeed() * 2) - pongBall.getMaxSpeed())); } if (pongBall.getX() >= 800) { right.setPoints(right.getPoints() + 1); if (right.getPoints() >= numberOfPointsToWin) { CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600); CreateGraphics().DrawString("LEFT WINS!", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(90, 100)); t.Enabled = false; return; } right.setX(770); right.setY(250); left.setX(30); left.setY(250); pongBall.setX(400); pongBall.setY(300); pongBall.setXVelocity((int)(randomObject.Next(4, pongBall.getMaxSpeed() * 2) - pongBall.getMaxSpeed())); pongBall.setYVelocity((int)(randomObject.Next(4, pongBall.getMaxSpeed() * 2) - pongBall.getMaxSpeed())); } if (pongBall.getY() >= 600) { pongBall.setYVelocity(-pongBall.getYVelocity()); } if (pongBall.getY() <= 0) { pongBall.setYVelocity(-pongBall.getYVelocity()); } if (pongBall.getX() <= left.getX() + left.getWidth() && pongBall.getX() >= left.getX()) { if (pongBall.getY() <= left.getY() + left.getHeight() && pongBall.getY() >= left.getY()) { pongBall.setXVelocity(-pongBall.getXVelocity()); } } if (pongBall.getX() <= right.getX() + right.getWidth() && pongBall.getX() >= right.getX()) { if (pongBall.getY() <= right.getY() + right.getHeight() && pongBall.getY() >= right.getY()) { pongBall.setXVelocity(-pongBall.getXVelocity()); } } CreateGraphics().DrawString(right.getPoints() + "", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(100, 100)); CreateGraphics().DrawString(left.getPoints() + "", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(600, 100)); Console.WriteLine("Pong Ball Position - X: " + pongBall.getX() + " Y: " + pongBall.getY()); Console.WriteLine("Paddle Left Position - X: " + left.getX() + " Y: " + left.getY()); if (right.getPoints() >= numberOfPointsToWin) { CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600); CreateGraphics().DrawString("LEFT WINS!", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(90, 100)); t.Enabled = false; return; } if (left.getPoints() >= numberOfPointsToWin) { CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600); CreateGraphics().DrawString("Right WINS!", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(100, 100)); t.Enabled = false; return; } }