首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c#需要通过向两个方向移动对象来获得帮助

c#需要通过向两个方向移动对象来获得帮助
EN

Stack Overflow用户
提问于 2016-07-12 05:10:13
回答 0查看 51关注 0票数 0

我需要帮助,在两个不同的方向移动面板上的对象。例如在向右和向下方向上通过按下右和左按钮。c#有没有类似的方法?

代码语言:javascript
运行
复制
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 runningRectangle
{
  enum Position {Left, Right, Down, Up, LeftUp, LeftDown, RightUp, RightDown }
    public partial class Form1 : Form
    {
        private int x;
        private int y; 
        private Position objPos;
        private int speed = 10;
        private bool stopPressed = false;

    public Form1()
    {
    InitializeComponent();
        x = 50;
        y = 50;


    }

private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.Red, x, y, 50, 50);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if(objPos == Position.Right)
        {
            x += speed;

        }else if(objPos == Position.Left)
        {
            x -= speed;
        }
        else if (objPos == Position.Down)
        {
            y += speed;
        }
        else if (objPos == Position.Up)
        {
            y -= speed;
        } else if (objPos == Position.RightUp)
        {
            x += speed;
            y -= speed;
        }
        else if (objPos == Position.RightDown)
        {
            x += speed;
            y += speed;
        }
        else if (objPos == Position.LeftUp)
        {
            x -= speed;
            y += speed;
        }
        else if (objPos == Position.LeftDown)
        {
            x -= speed;
            y -= speed;
        }

        Invalidate();
    }
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
            if (e.KeyCode == Keys.Right)
            {
            objPos = Position.Right;

            }

            else if (e.KeyCode == Keys.Left)
            {
            objPos = Position.Left;

        }
            else if (e.KeyCode == Keys.Down)
            {
            objPos = Position.Down;

        }
            else if (e.KeyCode == Keys.Up)
            {
            objPos = Position.Up;
        }
        else if(e.KeyCode == Keys.Right && e.KeyCode == Keys.Down)
        {
            objPos = Position.RightDown; 
        }
        else if (e.KeyCode == Keys.Right && e.KeyCode == Keys.Up)
        {
            objPos = Position.RightUp;
        }
        else if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Down)
        {
            objPos = Position.LeftDown;
        }
        else if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Up)
        {
            objPos = Position.LeftUp;

        }

        else if(e.KeyCode == Keys.Space)
        {
            if (!stopPressed)
            {
                speed = 0;
                stopPressed = true;
            }
            else if(stopPressed)
            {
                speed += 10;
                stopPressed = false;
            }
        }        
        }
}
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38316286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档