Write a C sharp code to draw a pumpkin or happy face using the graphics commands
ID: 3602450 • Letter: W
Question
Write a C sharp code to draw a pumpkin or happy face using the graphics commands. Have the image to move across the screen. When it reaches the right hand side of the screen, have the image to reappear on the left hand side of the screen and move to the right side again and repeat above process. Write a C sharp code to draw a pumpkin or happy face using the graphics commands. Have the image to move across the screen. When it reaches the right hand side of the screen, have the image to reappear on the left hand side of the screen and move to the right side again and repeat above process.Explanation / Answer
private int rSpeed = 15; private void Form1_Paint(object sender, PaintEventArgs e) { if (backBuffer == null) { backBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height); } using (Graphics g = Graphics.FromImage(backBuffer)) { g.Clear(Color.Gray); g.DrawImage(ballImage, ballRectangle.X, ballRectangle.Y); g.DrawImage(blackImage, blackRectangle.X, blackRectangle.Y); ballRectangle.X = Math.Max(0, Math.Min(ClientSize.Width - ballImage.Width, ballRectangle.X)); ballRectangle.Y = Math.Max(0, Math.Min(ClientSize.Height - ballImage.Height, ballRectangle.Y)); //blackRectangle.X = Math.Max(0, Math.Min(ClientSize.Width - blackImage.Width, blackRectangle.X)); blackRectangle.X += rSpeed; e.Graphics.DrawImage(backBuffer, 0, 0); } } private int xSpeed = 10; private int ySpeed = 10; private void updatePositions() { if (keyArgs != null) { switch (keyArgs.KeyCode) { case Keys.Up: ballRectangle.Y -= ySpeed; break; case Keys.Down: ballRectangle.Y += ySpeed; break; case Keys.Right: ballRectangle.X += xSpeed; break; case Keys.Left: ballRectangle.X -= xSpeed; break; default: return; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.