C#游戏结束了arcanoid砖砌游戏 [英] C# game over arcanoid brickout game
本文介绍了C#游戏结束了arcanoid砖砌游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要当球从棋盘中传球然后显示失败。游戏结束。我不知道如何编写该代码。请帮助我。
谢谢。
我尝试过:
I need when the ball passes from the board then show a defeat`Game Over.I don't know how to write that code.Please help me.
Thanks.
What I have tried:
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;
}
}
}
推荐答案
这看起来有点陈旧,但是也许它对你有用:如何建立一个具有动画和声音的突破风格游戏。 [ ^ ]
另请参阅此处的答案:制作突破游戏 [ ^ ]
你知道board.w
和board.h
,所以只需检查球的位置是否大于这些值。还要检查球的位置是否小于board.x
和board.y
。
如果你想要一个'游戏结束'屏幕,那么只需创建一个新表格并将背景图像属性设置为图片,这可以是.jpg,.gif或.png图像。
请参阅: Control.BackgroundImage属性(System.Windows.Forms) [ ^ ]
This looks a bit dated, but maybe it will be useful to you: How to build a breakout style game with animation and sound.[^]
Also see the answer here: Making breakout game[^]
You know theboard.w
andboard.h
, so simply check if the ball position is greater than these values. Also check if the ball position is less thanboard.x
andboard.y
.
If you want a 'Game Over' screen, then just create a new Form and set the background image property to a picture, this can be a .jpg, .gif or .png image.
See: Control.BackgroundImage Property (System.Windows.Forms)[^]
这篇关于C#游戏结束了arcanoid砖砌游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文