如何在C#visual studio中创建碰撞系统 [英] How do I make a collision system in C# visual studio

查看:125
本文介绍了如何在C#visual studio中创建碰撞系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我制作的游戏就像一只飞扬的小鸟但是我无法制作一个合适的碰撞系统。这只鸟是一个圆形椭圆,而墙是矩形。



在我目前的系统中,碰撞是随机而无意义的。基本上它坏了。



视觉表示

https://youtu.be/1RElwfCVNZg



Gametimer是我的更新框架功能的一部分,基本上每隔.1秒出现一个新框架



我尝试了什么:



我如何制作Bird和Rectanglular墙

So i was making a game like flappy bird however i am unable to make a proper collison system. The bird is a circular Ellipse while the walls are rectangles.

In my current system the collision works randomly and pointlessly. Basically it is broken.

Visual representation
https://youtu.be/1RElwfCVNZg

Gametimer is part of my update frame function basically every .1 sec a new frame comes up

What I have tried:

How i made the Bird and Rectanglular wall

//Bird
               new Settings();
               //Bird
               //Set Colour Bird
               SolidBrush Yellow = new SolidBrush(Color.Yellow);
               Graphics Canvas = e.Graphics;
               //Make Bird
               Canvas.FillEllipse(Yellow, Bird.X, Bird.Y, Settings.WidthBird, Settings.HeightBird);

               SolidBrush Black = new SolidBrush(Color.Black);

               //Max Height
               int MAxHeight = PbCanvas.Size.Height;
               MH = MAxHeight - 80;
               if (i == 0 || i % j == 1)
               {
                   HeightD = random.Next(10, MAxHeight - 80);
               }
               HeightU = MH - HeightD;
               Bottom = PbCanvas.Bottom;
               //Walls
               Canvas.FillRectangle(Black, X, 0, 50, HeightU);
               Canvas.FillRectangle(Black, X, Bottom - HeightD, 50, HeightD);



我和J意味着2件事。我指的是墙壁产生了多少次,j是产生的每个墙壁之间的秒数量



我的碰撞想法是


I and J means 2 things. I means how many times a wall has spawned and j is the amount of secodns between each wall being spawned

My idea for collision is

if (Bird.X == X && Bird.Y > HeightD && Bird.Y < HeightU - 80) 
            {
                GameOver = true;
                label2.Visible = true;
                MessageBox.Show("You Died");
                X = 300; //Defauly X value of wall
                GameTimer.Stop();
            }



这里有2面墙。如果它超过顶壁的高度,它将通过,如果底壁的高度小于它将通过的高度。我遇到的问题是鸟的值是X和Y坐标,而墙值是高度。可能是什么颜色


Here is 2 walls are present. If its more than the height of the top wall it will pass and if the height of bottom wall is less than the it will pass. THe problems i am encountering are that the bird values are in X and Y coords while the wall values are in height. What can be the possible colutions to this

推荐答案

这是一个使用 .IntersectsWith()的例子:

C#.Net&中的碰撞检测Java示例|代码的比特 [ ^ ]
Here is an example using .IntersectsWith():
Collision detection in C#.Net & Java example | Bits and Pieces of Code[^]


这篇关于如何在C#visual studio中创建碰撞系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆