簡體   English   中英

在使用ref方法的簡單2D游戲中C#缺乏大腸菌病

[英]C# lack of colision in simple 2D game using ref in method

我嘗試創建2D馬里奧風格的游戲。 一切正常,直到我開始使其更具“客觀性”。Mario不會在窗口結尾處停下來-停下來,但是要經過一會兒。 如果我沒有參考方法,馬里奧就不會停下來。

馬里奧課

class Mario: System.Windows.Forms.PictureBox
{
    public Mario(int x, int y)
    {
        Image = Image.FromFile("Mario.png");
        Location = new Point(x, y);
        Size = new Size(16, 32);
        SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
        TabIndex = 0;
        TabStop = false;
    }
public void colision( System.Windows.Forms.Panel s,ref bool l, ref bool r)
    {
        if (this.Right > s.Right) { r = false; }

        if (this.Left < s.Left) {  l = false;  }
    }

}

主類-Form1

 public partial class Form1 : Form
{

    bool right=false,left=false;

public Form1()
    {
        InitializeComponent();
        player.Top = screen.Height - player.Height;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if(right== true) { player.Left += 1; }
        if (left == true) { player.Left -=1; }

       player.colision(screen, ref left, ref right);
}

屏幕是System.Windows.Forms.Panel,播放器是Mario Type,它們已在form1.Designer.cs中初始化

我刪除了無關的字段和方法。

控件的位置是相對於其父控件的-如果將Mario放在(0, 0) ,則它不會出現在屏幕窗體的左上角,而是出現在Panel左上角-它的直接父代。

同樣,該Panel的位置也相對於其父Panel

假設您的Panel在窗體中的位置為(100, 100) ,其大小為(400, 300) -這意味着其Left屬性為100Right屬性為500對於這100個像素, Mario不可見。

因此,您的支票應為:

if (this.Right > s.Width) { r = false; }
if (this.Left < 0) {  l = false;  }

您已經對垂直放置做了正確的事情(通過使用Height而不是Bottom ):

player.Top = screen.Height - player.Height;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM