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.