private void MouseTimer_Tick(object sender, EventArgs e) { Point curPos; if (_canvas.GetLastMouseLeftClick(out curPos) && _drawFlag == false) { _drawFlag = true; _lineStack.Push(new LinkedList <LineSeg>()); lastMousePos = curPos; } if (_canvas.GetLastMousePosition(out curPos) && _drawFlag == true) { LineSeg line = new LineSeg(lastMousePos, curPos, TrB_LineThick.Value, _color); totalLines += 1; _lineStack.Peek().AddLast(line); lastMousePos = curPos; line.Render(_canvas); } if (_canvas.GetLastMouseRightClick(out curPos) && _drawFlag == true) { _drawFlag = false; } UpdateInfo(); }
//when user picks the ball at specific location private int Pick(Ball[,] balls, CDrawer _canvas) { int x = 0; int y = 0; int killed = 0; Point pos = new Point(); //when user presses the left mouse if (_canvas.GetLastMousePosition(out pos)) { //getting the x and y index of the position clicked x = pos.X / ballSize; y = pos.Y / ballSize; //getting value of how many balls are killed killed = CheckBalls(x, y, balls[x, y].ballColor); } //drops all the balls that are alive FallDown(balls, _canvas); //if number of balls killed is more than 1 then bouns score if (killed > 1) { killed += 2; } return(50 * killed); }
static void Main(string[] args) { Random rng = new Random(); int xVelocity = 1; //the rate at which the ball moves on the X plane int yVelocity = 1; //the rate at which the ball moves on the Y plane int xBall = 80; int yBall = 60; int wallTop = 0; //top boundry of screen/game int wallRight = 0; //right boundry of screen/game int wallBottom = 0; //bottom boundry of screen/game bool playAgain = true; Point playerPosition; //location of mouse so paddle can be appropriately set on the Y plane const int playerPositionX = 0; //the constant postion of the paddle against the left side of screen CDrawer gdi = new CDrawer(); gdi.Scale = 5; gdi.ContinuousUpdate = false; do { while (true) { //sets the boundry for the ball with as a visual for (wallTop = 0; wallTop < 160; wallTop = ++wallTop) { gdi.SetBBScaledPixel(wallTop, 0, Color.SkyBlue); } for (wallRight = 0; wallRight < 120; wallRight = ++wallRight) { gdi.SetBBScaledPixel(wallTop - 1, wallRight, Color.SkyBlue); } for (wallBottom = 0; wallBottom < 160; wallBottom = ++wallBottom) { gdi.SetBBScaledPixel(wallBottom, wallRight - 1, Color.SkyBlue); } //the ball while (xBall < wallRight && yBall < wallTop && yBall > wallBottom) { gdi.AddRectangle(xBall, yBall, 2, 2, Color.Gainsboro); //the paddle gdi.GetLastMousePosition(out playerPosition); //playerPositionYStart = playerPositionYStart + playerPosition; gdi.AddLine(playerPositionX, (playerPosition.Y + 25) / 5, playerPositionX, (playerPosition.Y - 25) / 5, Color.DarkRed, 10); //the delay, new images then removal of old images to create "animation" gdi.Render(); System.Threading.Thread.Sleep(20); gdi.Clear(); xBall += xVelocity; yBall += yVelocity; } } playAgain = false; } while (playAgain); }
static void Main(string[] args) { bool gameOver = false; // Exit the Game bool gameExit = false; // Exit the Program bool start = false; // Start the Game // Instance of random. Random rand = new Random(); //x ball position int iX = 3; //y ball position int iY = rand.Next(2, 115); //amount ball moves in x direction for every loop int iXVelocity = 3; //amount ball moves in y direction for every loop int iYVelocity = 3; // Game score int score = 0; // Ball speed delay int ballSpeed = 120; //create a drawer window CDrawer Canvas = new CDrawer(); Canvas.Scale = 5; // coordinates. Point coord; // Mouse position in Canvas int mouseYPos = 70; //loop until the ball leaves the visible window while (((iX < 160) || (iX > 0)) && !gameExit) { while (gameOver == false) { // Erase the old ball and paddle. Canvas.Clear(); // If left mouse click start the game. if (Canvas.GetLastMouseLeftClick(out coord)) { start = true; } // if start is true start the game. if (start) { // Get the mouse position in the canva and scale it to 5. if (Canvas.GetLastMousePosition(out coord)) { mouseYPos = (coord.Y / 5) - 5; } // Add the paddle. Canvas.AddRectangle(0, mouseYPos, 1, 10, Color.Red); //draw the new ball Canvas.AddEllipse(iX, iY, 2, 2); //time delay to slow down the ball System.Threading.Thread.Sleep(ballSpeed); // If mouse is on paddle position bounce back. if ((iY - 9) <= mouseYPos && (iY + 1) >= mouseYPos && iX <= 2) { // Reverse speed position (goes opposite way). iXVelocity = -iXVelocity; iYVelocity = -iYVelocity; // increment the ball speed. ballSpeed = ballSpeed <= 20 ? 1 : ballSpeed - 20; // increment score. score++; } // Increment the position with the velocity. iX += iXVelocity; iY += iYVelocity; //check for bouncing off of the lower edge of the window if ((iY > 118) || (iY < 0)) { //reverse the y velocity (ball goes up) iYVelocity = -iYVelocity; } // If ball position is greater than 160 reverse the X velocity (ball goes left). if (iX > 160) { iXVelocity = -iXVelocity; } // If ball position is less than 0 game is over. if (iX < 0) { gameOver = true; } } } // Erase the old ball and paddle. Canvas.Clear(); // Display the final score and option buttons to play again and quit the game. Canvas.AddText($"Final Score: {score}", 36, Color.Gray); Canvas.AddText("Play Again", 12, 85, 84, 30, 45, Color.Green); Canvas.AddText("Quit", 12, 120, 84, 30, 45, Color.Gray); Canvas.AddRectangle(87, 103, 25, 8, Color.Empty, 1, Color.Green); Canvas.AddRectangle(122, 103, 25, 8, Color.Empty, 1, Color.Gray); while (gameOver == true) { if (Canvas.GetLastMouseLeftRelease(out coord)) { Console.WriteLine("Click!"); } // If play again button is click start the game. if (Canvas.GetLastMouseLeftClick(out coord) && (coord.X >= 435 && coord.X <= 560) && (coord.Y >= 515 && coord.Y <= 555)) { // Initialize ball position and speed. iX = 3; iY = rand.Next(2, 115); iXVelocity = 3; iYVelocity = 3; ballSpeed = 120; score = 0; // Start the game. gameOver = false; start = true; } // If quit button is click quit the game. if (Canvas.GetLastMouseLeftClick(out coord) && (coord.X >= 610 && coord.X <= 735) && (coord.Y >= 515 && coord.Y <= 555)) { // Exit the game. gameExit = true; gameOver = false; } } } }