如何使用箭头键移动图片框?

时间:2015-03-25 17:55:45

标签: c# animation picturebox keydown

我在Visual Studio中使用C#创建了一个动画,在一个使用pacman的多个图像的pictureBox中。我现在试图通过使用箭头键让pictureBox在表单(向上,向下,向右和向左)之间移动,但我似乎无法让它工作。任何人都可以帮我弄清楚为什么pictureBox不会移动? 谢谢 到目前为止,这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Week9
{
public partial class Pacman : Form
{
    private Image[] pacmanImage = new Image[4];
    private int currentMouthPosition = 0;
    private int xPosition = 0;
    private int yPosition = 0;

    // The index of the current frame.
    private int FrameNum = 0;

    public Pacman()
    {
        InitializeComponent();
        this.KeyDown +=newSystem.Windows.Forms.KeyEventHandler(this.Pacman_KeyDown);
    }


    private void picFrame_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawImage(pacmanImage[currentMouthPosition], xPosition, yPosition, 32, 32);

    }

    // Load the images.
    private void Pacman_Load(object sender, EventArgs e)
    {
        pacmanImage[0] = Image.FromFile("..\\..\\pac32_left_close.png");
        pacmanImage[1] = Image.FromFile("..\\..\\pac32_left_open.png");
        pacmanImage[2] = Image.FromFile("..\\..\\pac32_left_wide.png");
        pacmanImage[3] = Image.FromFile("..\\..\\pac32_left_widest.png");
        // Display the first frame.
        picFrame.Image = pacmanImage[FrameNum];
    }

    // This is where I have tried to get the pictureBox to move
    private void Pacman_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Left:
                picFrame.Left -= 5;
                break;
            case Keys.Right:
                picFrame.Left += 5;
                break;
            case Keys.Up:
                picFrame.Top -= 5;
                break;
            case Keys.Down:
                picFrame.Top += 5;
                break;
        }
    }


    // Display the next image.
    private void tmrNextFrame_Tick(object sender, EventArgs e)
    {
        FrameNum = ++FrameNum % pacmanImage.Length;
        picFrame.Image = pacmanImage[FrameNum];
    }

    private void btnStartStop_Click_1(object sender, EventArgs e)
    {
        tmrNextFrame.Enabled = !tmrNextFrame.Enabled;
        if (tmrNextFrame.Enabled) btnStartStop.Text = "Stop";
        else btnStartStop.Text = "Start";
    }

  }
}

3 个答案:

答案 0 :(得分:0)

pictureBox1.Location = new Point(x, y);

你可以在表单按键事件或keydown或keyup事件中写一些这样的代码。获取当前图片框位置并加起来或缩小位置。 您可以使用此代码获取当前位置:

pictureBox1.Location.X; //to get X
pictureBox1.Location.Y; // To get Y

答案 1 :(得分:0)

private void Pacman_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Left:
            mouthcurrentposition = 0;
            picFrame.yPosition -= 10;
            picFrame.Left(); //This should be a method that will draw your pacman
            break;
        case Keys.Right:
            mouthcurrentposition = 1;
            picFrame.yPosition += 10;
            picFrame.Right();
            break;
        case Keys.Up:
            mouthcurrentposition = 2;
            picFrame.yPosition -= 10;
            picFrame.Up();
            break;
        case Keys.Down:
            mouthcurrentposition = 3;
            picFrame.yPosition += 10;
            picFrame.Down();
            break;
    }
}

答案 2 :(得分:0)

我知道我迟到了,但是您错过的是主表单上的this.KeyDown += Pacman_KeyDown