C# game over arcanoid brickout game
Мне нужно, когда мяч проходит от доски, то показать поражение ' игра окончена.Я не знаю, как написать этот код.Пожалуйста, помогите мне.
Спасибо.
Что я уже пробовал:
class Ball { public int x; public int y; public int vx = 3; public int vy = 3; public int r; public Graphics gf; public Game game; public SolidBrush myColor; public Ball(int x,int y,int vx,int vy,int r,Graphics gf,Game game) { this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.r = r; this.gf = gf; this.game = game; myColor = new SolidBrush(Color.IndianRed); } public void Draw() { this.gf.FillEllipse(myColor, x, y, r, r); } public void Move() { this.x += this.vx; this.y += this.vy; if(this.x <= 0 || this.x > game.ClientSize.Width - r) { Random rnd = new Random(); int r = rnd.Next(0, 256); int g = rnd.Next(0, 256); int b = rnd.Next(0, 256); SolidBrush guyn = new SolidBrush(Color.FromArgb(255, r, g, b)); this.myColor = guyn; vx *= -1; } if(this.y <= 0) { Random rnd = new Random(); int r = rnd.Next(0, 256); int g = rnd.Next(0, 256); int b = rnd.Next(0, 256); SolidBrush guyn = new SolidBrush(Color.FromArgb(255, r, g, b)); this.myColor = guyn; this.vy *= -1; } this.Draw(); } public bool hits(Board board) { int x1 = this.x + this.r / 2; int x2 = board.x + board.w / 2; int y1 = this.y; int y2 = board.y; int y_dist = Math.Abs(y1 - y2); int x_dist = Math.Abs(x1 - x2); if(y_dist <= r/2 + board.h/2 && x_dist <= board.w/2 + r / 2) { return true; } else { return false; } } } class Board { public int x; public int y; public int w; public int h; public Graphics gf; public Game game; public SolidBrush MyColor; public Board(int x, int y, int w, int h, Graphics gf, Game game) { this.x = x; this.y = y; this.w = w; this.h = h; this.gf = gf; this.game = game; MyColor = new SolidBrush(Color.Black); } public void draw() { gf.FillRectangle(MyColor, x, y, w, h); } public void gna(string v) { if (v == "left" && x > 0) { this.x -= 10; } else if (v == "right" && x < this.game.ClientSize.Width - 85) { this.x += 10; } this.draw(); } } public partial class Game : Form { Graphics gf; Ball ball; Board board; public Game() { InitializeComponent(); this.gf = this.CreateGraphics(); this.board = new Board(this.ClientSize.Width / 2 - 40, this.ClientSize.Height - 60, 80, 20,gf,this); this.ball = new Ball(this.ClientSize.Width / 2 - 10, this.ClientSize.Height/2 - 10, 3, 3, 20, gf, this); } private void Game_Paint(object sender, PaintEventArgs e) { this.board.draw(); this.ball.Draw(); } private void Game_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Left: case Keys.A: this.board.gna("left"); break; case Keys.Right: case Keys.D: this.board.gna("right"); break; } } private void timer1_Tick(object sender, EventArgs e) { this.gf.FillRectangle(Brushes.White, 0, 0, this.ClientSize.Width, this.ClientSize.Height); this.board.draw(); this.ball.Draw(); this.ball.Move(); if (this.ball.hits(this.board)) { Random rnd = new Random(); int r = rnd.Next(0, 256); int g = rnd.Next(0, 256); int b = rnd.Next(0, 256); SolidBrush guyn = new SolidBrush(Color.FromArgb(255, r, g, b)); this.ball.myColor = guyn; this.board.MyColor = guyn; this.ball.vy *= -1; } } }