为什么我的图像不会移动? [英] Why will my image not move?

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

问题描述

我正在制作一个 2d 自上而下的游戏,玩家控制一只猫.为此,该人使用 WASD 键移动.我有 Form1、GameManager、Cat 和 Moveable 类.Form1 向 GameManager 发送 cat 图像列表和 e.graphics(用于图片框).GameManager 有一个计时器,每个滴答声都会检查猫是否移动了.Cat 处理移动逻辑.当我运行程序时,猫精灵出现在它的初始位置,但在按下某个键时不会移动.我无法弄清楚我的问题,有人可以帮忙吗?

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?

这是我的课程:

表格 1:

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);
        }
    }
}

游戏管理器:

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();
        }
    }
}

猫:

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)
        {

        }
    }
}

可移动:

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);
    }
}

所以,我没有任何东西可以调用 KeyDown().如果需要 KeyEventArgs e,如何调用 KeyDown()?

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

Picturebox1 没有 keydown 事件,form1 有.我还需要在 cat 类中使用 keydown 事件,以便它知道它面向的方向,因此它知道要移动的方向.

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. 您的代码中没有键盘事件.可能你把它遗漏了(已经有太多的代码了),然后说点什么.

  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.

在每个 move() 之后,您需要 Invalidate() 相关的控件,在本例中为图片框.

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

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

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