检查吃豆子运动
Check pacman movement
我正在制作吃豆人游戏,但我不知道该怎么做:
当吃豆人碰到食物时,食物应该消失。我的代码不工作。这是初始化食物世界的函数:
namespace pacman
{
public partial class Form1 : Form
{
Timer timer;
Pacman pacman;
static readonly int TIMER_INTERVAL = 250;
static readonly int WORLD_WIDTH = 15;
static readonly int WORLD_HEIGHT = 10;
Image foodImage;
bool[][] foodWorld;
public Form1()
{
InitializeComponent();
foodImage = Properties.Resources.foodImage;
DoubleBuffered = true;
newGame();
}
public void newGame()
{
pacman = new Pacman();
this.Width = Pacman.radius * 2 * (WORLD_WIDTH + 1);
this.Height = Pacman.radius * 2 * (WORLD_HEIGHT + 1);
// овде кодот за иницијализација на матрицата foodWorld
foodWorld = new bool[WORLD_WIDTH][];
for (int i = 0; i < WORLD_WIDTH;i++ )
{
foodWorld[i] = new bool[WORLD_HEIGHT];
}
for (int i = 0; i < WORLD_WIDTH; i+=2) {
for (int j = 0; j < WORLD_HEIGHT; j++) {
foodWorld[i][j] = true;
}
}
// овде кодот за иницијализација и стартување на тајмерот
timer = new Timer();
timer.Interval = TIMER_INTERVAL;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
// овде вашиот код
timer.Interval = TIMER_INTERVAL - 1;
if (TIMER_INTERVAL == 0)
{
timer.Stop();
}
for (int i = 0; i < WORLD_WIDTH; i++) {
for (int j = 0; j < WORLD_HEIGHT; j++) {
if (pacman.x == i && pacman.y == j && foodWorld[i][j])
{
foodWorld[i][j] = false;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
pacman.open = true;
}
else {
pacman.open = false;
}
}
}
pacman.Move(WORLD_WIDTH, WORLD_HEIGHT);
Invalidate();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
// не заборавајте да го додадете настанот на самата форма
// вашиот код овде
if (e.KeyCode == Keys.Up) {
pacman.ChangeDirection("UP");
}
if (e.KeyCode == Keys.Down)
{
pacman.ChangeDirection("DOWN");
}
if (e.KeyCode == Keys.Left)
{
pacman.ChangeDirection("LEFT");
}
if (e.KeyCode == Keys.Right)
{
pacman.ChangeDirection("RIGHT");
}
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.White);
for (int i = 0; i < foodWorld.Length; i++)
{
for (int j = 0; j < foodWorld[i].Length; j++)
{
if (foodWorld[i][j])
{
g.DrawImageUnscaled(foodImage, j * Pacman.radius * 2 + (Pacman.radius * 2 - foodImage.Height) / 2, i * Pacman.radius * 2 + (Pacman.radius * 2 - foodImage.Width) / 2);
}
}
}
pacman.Draw(g);
}
}
}
我应该更改什么才能让吃豆人触摸的图片消失?
当pacman吃食物时,你有这个:
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
此语法是对 Paint 事件的订阅。你应该只做一次。相反,使用:
this.Invalidate();
Invalidate 的调用告诉操作系统,"Hey, I need to be re-painted!"。然后操作系统将生成最终触发 Paint 事件并执行您的绘制代码的事件。
我正在制作吃豆人游戏,但我不知道该怎么做: 当吃豆人碰到食物时,食物应该消失。我的代码不工作。这是初始化食物世界的函数:
namespace pacman
{
public partial class Form1 : Form
{
Timer timer;
Pacman pacman;
static readonly int TIMER_INTERVAL = 250;
static readonly int WORLD_WIDTH = 15;
static readonly int WORLD_HEIGHT = 10;
Image foodImage;
bool[][] foodWorld;
public Form1()
{
InitializeComponent();
foodImage = Properties.Resources.foodImage;
DoubleBuffered = true;
newGame();
}
public void newGame()
{
pacman = new Pacman();
this.Width = Pacman.radius * 2 * (WORLD_WIDTH + 1);
this.Height = Pacman.radius * 2 * (WORLD_HEIGHT + 1);
// овде кодот за иницијализација на матрицата foodWorld
foodWorld = new bool[WORLD_WIDTH][];
for (int i = 0; i < WORLD_WIDTH;i++ )
{
foodWorld[i] = new bool[WORLD_HEIGHT];
}
for (int i = 0; i < WORLD_WIDTH; i+=2) {
for (int j = 0; j < WORLD_HEIGHT; j++) {
foodWorld[i][j] = true;
}
}
// овде кодот за иницијализација и стартување на тајмерот
timer = new Timer();
timer.Interval = TIMER_INTERVAL;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
// овде вашиот код
timer.Interval = TIMER_INTERVAL - 1;
if (TIMER_INTERVAL == 0)
{
timer.Stop();
}
for (int i = 0; i < WORLD_WIDTH; i++) {
for (int j = 0; j < WORLD_HEIGHT; j++) {
if (pacman.x == i && pacman.y == j && foodWorld[i][j])
{
foodWorld[i][j] = false;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
pacman.open = true;
}
else {
pacman.open = false;
}
}
}
pacman.Move(WORLD_WIDTH, WORLD_HEIGHT);
Invalidate();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
// не заборавајте да го додадете настанот на самата форма
// вашиот код овде
if (e.KeyCode == Keys.Up) {
pacman.ChangeDirection("UP");
}
if (e.KeyCode == Keys.Down)
{
pacman.ChangeDirection("DOWN");
}
if (e.KeyCode == Keys.Left)
{
pacman.ChangeDirection("LEFT");
}
if (e.KeyCode == Keys.Right)
{
pacman.ChangeDirection("RIGHT");
}
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.White);
for (int i = 0; i < foodWorld.Length; i++)
{
for (int j = 0; j < foodWorld[i].Length; j++)
{
if (foodWorld[i][j])
{
g.DrawImageUnscaled(foodImage, j * Pacman.radius * 2 + (Pacman.radius * 2 - foodImage.Height) / 2, i * Pacman.radius * 2 + (Pacman.radius * 2 - foodImage.Width) / 2);
}
}
}
pacman.Draw(g);
}
}
}
我应该更改什么才能让吃豆人触摸的图片消失?
当pacman吃食物时,你有这个:
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
此语法是对 Paint 事件的订阅。你应该只做一次。相反,使用:
this.Invalidate();
Invalidate 的调用告诉操作系统,"Hey, I need to be re-painted!"。然后操作系统将生成最终触发 Paint 事件并执行您的绘制代码的事件。