繁体   English   中英

C# 旋转pictureBox,我想垂直移动总线我不会,你能帮我编码吗?

[英]C# Rotate pictureBox , I want to move the bus vertically and I cant, can you help me of coding?

using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form {

    public bool move_right, move_left, move_up, move_down;
    public int speed = 10;
    public int score = 0;

    public Form1() { InitializeComponent(); }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (move_left == true && pictureBox2.Left > 0) {
            pictureBox2.Left -= speed;
        }

        if (move_right == true && pictureBox2.Left < 665) {
            pictureBox2.Left += speed;
        }

        if (move_up == true && pictureBox2.Top > 0) {
            pictureBox2.Top -= speed;
        }

        if (move_down == true && pictureBox2.Top < 366) {
            pictureBox2.Top += speed;
        }
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left) {
            move_left = true;
        }

        if (e.KeyCode == Keys.Right) {
            move_right = true;
        }

        if (e.KeyCode == Keys.Up) {
            move_up = true;
        }

        if (e.KeyCode == Keys.Down) {
            move_down = true;
        }
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left) {
            move_left = false;
        }

        if (e.KeyCode == Keys.Right) {
            move_right = false;
        }

        if (e.KeyCode == Keys.Up) {
            move_up = false;
        }

        if (e.KeyCode == Keys.Down) {
            move_down = false;
        }
    }
}

一种可能的解决方案(不是唯一的)是创建 3 个具有所需方向的图像版本。 然后,您可以根据需要将图片分配给图片框。

也就是说,您最好直接在 PictureBox 上绘图。 绘图允许您以任意角度旋转图像,并为您提供更大的灵活性。

理想情况下,您希望使用 2D 游戏引擎而不是手动完成所有操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM