如何在c#中构造一个图片框

本文关键字:一个 | 更新日期: 2023-09-27 18:06:52

我创建了一个flappy bird程序,我想做的就是调用这个方法在屏幕上创建一个管道。我很难把它表现出来。我如何调用我的方法create pipe,我的方法会创建一个管道吗?

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FlappyBird
{
    public partial class Form1 : Form
    {
        int yspeed = 0;
        int xspeed = 1;
        PictureBox pipe = new PictureBox();

        private void CreatePipe()
        {
            this.pipe = new PictureBox();
            this.pipe.Location = new Point(2, 2);
            this.pipe.Name = "pipe";
            this.pipe.Size = new Size(200, 200);
            this.pipe.TabIndex = 0;
            this.pipe.TabStop = false;
            this.pipe.BackColor = Color.Red;
            this.pipe.Visible = true;

        }


        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Press okay to start.");
            timer1.Enabled = true;
            CreatePipe();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            yspeed += xspeed;
            bird.Top += yspeed;
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Space) {
                yspeed = -15;
            }
        }


        private void fileToolStripMenuItem_Click(object sender, EventArgs e)//new game
        {
        }
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)//about
        {
        }

    }
    }

如何在c#中构造一个图片框

您需要将PictureBox放在表单上,否则它将留在内存中,但由于它没有父调用,因此无法可视化:

this.Controls.Add(pipe);
相关文章: