制作突围游戏 [英] Making breakout game
问题描述
我的朋友们我参与了这个游戏的一些部分,但是我在游戏中遇到了两个问题。
1)将障碍与障碍区别开来的问题
1-1)第一个问题与复杂的指导方针有关,我将它们用于模拟与球板碰撞或球碰撞的球碰撞。这些指导方针并不准确,特别是当球遇到挡块角落或底部时。如果它不是更好的碰撞区分我使用更好的代码。我写了这段代码。你认为那里有更好的方法吗?
2)如果球撞到任何障碍物,障碍物会报告碰撞,我怎能做一项工作。 />
我的目标是使用事件。你是否认为我作为运行时制造障碍?如果我作为运行时制作,我怎么能为它们制作碰撞事件?
最好的问候
My friends I made some parts of this game but I have two problems in this game.
1)Problem about distinguishing ball collision with obstacles
1-1)A first problem is related to complex guidelines that I used them for simulation of ball collision with board or ball collision with blocks. These guidelines are not accurate especially when the ball meets with block corner or bottom. If it is not better for collision distinguish I use a better code. I wrote this code. Are you think that are there better way?
2)How I can do a work that if the ball collision with any obstacle, the obstacle report the collision.
My aim is about using of events. Are you thinking that I make obstacles as runtime? If I make as a runtime, how I can make collision event for them?
With best regards
private Point MouseDownLocation;
int step = 2;
int stepleft = 2;
bool flagBottom;
bool flagTop;
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 10;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
ball.Top += step;
ball.Left += stepleft;
//board simulate collision
bool collisonX = ball.Location.X + ball.Width > board.Location.X && ball.Location.X < board.Location.X + board.Width;
bool collisonY = ball.Top + ball.Height == board.Location.Y || ball.Top + ball.Height - 1 == board.Location.Y;
//board2(button1) simulate collision
bool collisonX2 = ball.Location.X + ball.Width > board2.Location.X && ball.Location.X < board2.Location.X + board2.Width;
bool collisonY2 = ball.Top + ball.Height == board2.Location.Y || ball.Top + ball.Height - 1 == board2.Location.Y;
//Collision the ball with under buttons
bool collsionButtonY = ball.Top - ball.Height == board2.Location.Y || ball.Top - ball.Height == board2.Location.Y - 1;
//collision leftwall
bool leftWall = ball.Left == 0 || ball.Left == -1 || ball.Left == 1;
//collision rightwall
bool topWall = ball.Top == 0 || ball.Top == -1 || ball.Top == 1;
bool bottomWall = collisonX && collisonY;
bool toppWall = collisonX2 && collisonY2;
//collision
bool barrier = collisonX2 && collsionButtonY;
//rightwall
bool rightWall = ball.Left + ball.Width == this.ClientSize.Width || ball.Left + ball.Width == this.ClientSize.Width - 1;
// sidewall = collision rightwall or leftwall
bool sideWall = leftWall || rightWall;
//Check the ball hit the ground
bool check = ball.Top + ball.Height < this.ClientSize.Height;
//if topWall true,This means that the ball is hit to the topwall
if (topWall)
{
flagBottom = false;
flagTop = true;
if (stepleft > 0)
{
step = 2;
}
else if (stepleft < 0)
{
step = 2;
}
}
//if bottomWall true,This means that the ball is hit to the board
else if (bottomWall)
{
flagBottom = true;
flagTop = false;
if (stepleft > 0)
{
step = step * -1;
}
else if (stepleft < 0)
{
step = step * -1;
}
}
//if barrier true and flagbottom true,This means that the ball is hit to the board2(button1)
else if (barrier && flagBottom)
{
if (stepleft > 0)
{
step = step * -1;
}
else if (stepleft < 0)
{
step = step * -1;
}
}
//if toppWall true and flagTop true,This means that the ball is hit to The top button is hit
else if (toppWall && flagTop)
{
if (stepleft > 0)
{
step = step * -1;
}
else if (stepleft < 0)
{
step = step * -1;
}
}
else if (sideWall)
{
//if leftwall true,This means that the ball is hit to the left side wall
if (leftWall)
{
if (flagTop)
{
stepleft = 2;
}
else if (flagBottom)
{
stepleft = 2;
}
}
//if rightWall true,This means that the ball is hit to the left side wall
else if (rightWall)
{
if (flagTop)
{
stepleft = -2;
}
else if (flagBottom)
{
stepleft = -2;
}
}
}
//check if ckeck==ture,this mean the ball is hit the ground
else if (!check)
{
timer1.Enabled = false;
}
}
private void board_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void board_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
board.Left = e.X + board.Left - MouseDownLocation.X;
}
}
推荐答案
1)有许多不同的方法可以检测和响应碰撞,使用哪种方法取决于你当前的情况。
对于突破游戏,我会尝试模拟划桨和阻挡多边形(由线组成),球形为圆形。
然后使用矢量数学,你可以找出两条线(多边形的一侧和球的轨迹)Intersect [ ^ ]。
2.我认为你不想要个别障碍报告碰撞。
我知道你想要使用事件,但是对于诸如突破之类的游戏你可能最好只是迭代可碰撞物品列表并将它们传递给碰撞检查器。
我写了关于游戏编程的文章 [ ^ ]我实施了一个突破游戏,它在java中,但碰撞计算可能对你有用。
希望这会有所帮助,
Fredrik
1) There are many different ways of detecting and responding to collisions and which one to use depends on your current scenario.
For a breakout game I would try to model the paddle and blocks as polygons (made up from lines) and the ball as a circle.
Then using vector math you can figure out where two lines (the side of the polygon and the trajectory of the ball) intersect[^].
2. I don't think you want the individual obstacle to report the collision.
I understand you want to use events, but for a game such as breakout you're probably better off just iterating over the list of collidable items and pass them to a collision checker.
I wrote an article on game programming[^] where I implemented a breakout game, it's in java but the collision calculations might still be useful to you.
Hope this helps,
Fredrik
这篇关于制作突围游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!