为什么我的形象不动? [英] Why will my image not move?

查看:129
本文介绍了为什么我的形象不动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个2D自上而下的游​​戏,玩家控制一只猫。要做到这一点,人使用WASD键移动。我有Form1中,游戏管理器,猫,和可移动的类。 Form1上发送游戏管理的猫图像列表和e.graphics(用于图片框)。游戏管理具有定时器和每个刻度检查是否猫已经移动。猫处理,此举的逻辑。当我运行该程序,猫精灵展示了在它的初始位置,但在pressing的关键不动。我想不通我的问题,可以请人帮忙吗?

下面是我的类:

Form1的:

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;

命名空间CatAndMouse
{
    公共部分类Form1中:形态
    {
        游戏管理myGM =新游戏管理();
        公共Form1中()
        {
            的InitializeComponent();
            新游戏();
        }

        私人无效pictureBox1_Paint(对象发件人,PaintEventArgs的E)
        {
            如果(this.myGM!= NULL)
                this.myGM.paint(e.Graphics);
        }

        公共无效newGame()
        {
            myGM.newGame(imgCat);
        }
    }
}
 

游戏管理:

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;

命名空间CatAndMouse
{
    类游戏管理
    {
        猫CA1 =新的猫();
        INT金额= 5;
        定时时间=新的Timer();
        公众的ImageList imgCat =新的ImageList();

        公共无效newGame(ImageList中的猫)
        {
            imgCat =猫;
            time.Start();
        }

        公共无效移动()
        {
            ca1.Move(金额);
        }

        公共无效漆(图形G)
        {
            g.DrawImage(imgCat.Images [0],ca1.getLocation());
        }

        私人无效time_Tick(对象发件人,EventArgs的)
        {
            移动();
        }
    }
}
 

猫:

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;
命名空间CatAndMouse
{
    类猫:可移动
    {
        随机myCLoc =新的随机();
        私人可移动myCatMove;
        公共点P =新的点(100,100);
        INT DIR = 0;

        公共无效移动(INT N)
        {
            如果(DIR == 0)
            {
                p.Y = p.Y  -  N;
            }
            如果(DIR == 1)
            {
                p.X = p.X + N;
            }
            如果(DIR == 2)
            {
                p.Y = p.Y + N;
            }
            如果(DIR == 3)
            {
                p.X = p.X  -  N;
            }
        }
        私人无效的KeyDown(KeyEventArgs E)
        {
            如果(e.Key code == Keys.Up)
            {
                DIR = 0;
            }
            如果(e.Key code == Keys.Right)
            {
                DIR = 1;
            }
            如果(e.Key code == Keys.Down)
            {
                DIR = 2;
            }
            如果(e.Key code == Keys.Left)
            {
                DIR = 3;
            }
        }
        公共无效changeDirection()
        {

        }

        公共点的getLocation()
        {
            返回磷;
        }

        公共无效漆(PaintEventArgs的E)
        {

        }
    }
}
 

可移动的:

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;

命名空间CatAndMouse
{
    公共接口可移动
    {
        无效移动(INT N);
        无效changeDirection();
        //点​​的getLocation();
        无效漆(PaintEventArgs的E);
    }
}
 

所以,我没有什么要求的KeyDown()。如何让我的东西调用的KeyDown(),如果它需要KeyEventArgsè?

Picturebox1没有一个keydown事件,form1中一样。我还需要使用keydown事件在猫类,所以它知道什么方向是朝上,所以它知道什么方向移动。

解决方案
  1. 。在你的code无键盘事件。可能是你(已经太多了code)留出来,但然​​后说一些事情。

  2. 在每个移动()需要的Invalidate()相关控制,在这种情况下,在PictureBox。

I am making a 2d top-down game where the player controls a cat. To do this, the person uses the WASD keys to move. I have Form1, GameManager, Cat, and Moveable classes. Form1 sends GameManager the cat imagelist and e.graphics (for the picturebox). GameManager has a timer and each tick checks to see if the cat has moved. Cat handles the move logic. When I run the program, the cat sprite shows up at its initial position, but does not move upon pressing a key. I can't figure out my issue, could somebody please help?

Here are my classes:

Form1:

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

namespace CatAndMouse
{
    public partial class Form1 : Form
    {
        GameManager myGM = new GameManager();
        public Form1()
        {
            InitializeComponent();
            newGame();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (this.myGM != null)
                this.myGM.paint(e.Graphics);
        }

        public void newGame()
        {
            myGM.newGame(imgCat);
        }
    }
}

GameManager:

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

namespace CatAndMouse
{
    class GameManager
    {
        Cat ca1 = new Cat();
        int amount = 5;
        Timer time = new Timer();
        public ImageList imgCat = new ImageList();

        public void newGame(ImageList cat)
        {
            imgCat = cat;
            time.Start();
        }

        public void move()
        {
            ca1.Move(amount);
        }

        public void paint(Graphics g)
        {
            g.DrawImage(imgCat.Images[0], ca1.getLocation());
        }

        private void time_Tick(object sender, EventArgs e)
        {
            move();
        }
    }
}

Cat:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CatAndMouse
{
    class Cat: Moveable
    {
        Random myCLoc = new Random();
        private Moveable myCatMove;
        public Point p = new Point(100, 100);
        int dir = 0;

        public void Move(int n)
        {
            if (dir == 0)
            {
                p.Y = p.Y - n;
            }
            if (dir == 1)
            {
                p.X = p.X + n;
            }
            if (dir == 2)
            {
                p.Y = p.Y + n;
            }
            if (dir == 3)
            {
                p.X = p.X - n;
            }
        }
        private void KeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                dir = 0;
            }
            if (e.KeyCode == Keys.Right)
            {
                dir = 1;
            }
            if (e.KeyCode == Keys.Down)
            {
                dir = 2;
            }
            if (e.KeyCode == Keys.Left)
            {
                dir = 3;
            }
        }
        public void changeDirection()
        {

        }

        public Point getLocation()
        {
            return p;
        }

        public void paint(PaintEventArgs e)
        {

        }
    }
}

Moveable:

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

namespace CatAndMouse
{
    public interface Moveable
    {
        void Move(int n);
        void changeDirection();
        //Point getLocation();
        void paint(PaintEventArgs e);
    }
}

So, I don't have anything calling KeyDown(). How do I make something call KeyDown() if it needs KeyEventArgs e?

Picturebox1 does not have a keydown event, form1 does. I also need to use the keydown event in the cat class so it knows what direction it is facing, so it knows what direction to move.

解决方案

  1. There is no Keyboard event in your code. Could be you left it out (too much code already) but then say something about it.

  2. After each move() you need to Invalidate() the relevant Control, in this case the PictureBox.

这篇关于为什么我的形象不动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆