C# - pacman collision detection
hey ik moet samen met een klasgenoot pacman programmeren in csharp maar nu stuiten we op het probleem om onze pacman te laten stoppen bij muren . Iemand die ons opweg kan zetten? alvast bedankt
hey ik moet samen met een klasgenoot pacman programmeren in csharp maar nu stuiten we op het probleem om onze pacman te laten stoppen bij muren . Iemand die ons opweg kan zetten? alvast bedankt
Code:
namespace WindowsFormsApplication1
{
public partial class frmMain : Form
{
private Graphics paper;
private Image imgWall, imgDot;
private Pacman pacman;
private Dots dots;
int a;
private const int breedte = 15;
private int[,] _Level1;
private int position = 2;
public frmMain()
{
InitializeComponent();
paper = pictureBox1.CreateGraphics();
pacman = new Pacman(pictureBox1);
dots = new Dots(pictureBox1);
}
private void frmMain_Load(object sender, EventArgs e)
{
const int iMapWidth = 28;
const int iMapHeight = 28;
_Level1 = new int[iMapWidth,iMapHeight]{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1},
{1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1},
{1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1},
{1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1},
{1,1,1,1,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1,1,1,1,1}};
}
private void timer_Tick(object sender, EventArgs e)
{
paper.Clear(Color.Black);
drawPacman();
positionPacman();
drawLevel();
}
private void drawLevel()
{
Brush potloodWall = new SolidBrush(Color.Gray);
Brush potloodPath = new SolidBrush(Color.Black);
imgWall = Image.FromFile("Sprites/Sprite_wall.png");
imgDot = Image.FromFile("Sprites/dot.png");
int x = 15;
int y = 15;
// walls [i,j]
//.GetLength(0) => eerste dimensie (hoogte)
//.Getlength(1) => tweede dimensie (breedte)
for (int i = 0; i < _Level1.GetLength(0); i++)
{
// DOE DIT ZOLANG "i" KLEINER IS DAN "3"
for (int j = 0; j < _Level1.GetLength(1); j++)
{
// DOE DIT ZOLANG "J" KLEINER IS DAN "3"
if (_Level1[i, j] == 0) // als inhoud array [i,j] = 0 {teken path}
{
paper.DrawImage(imgDot, x, y, 15, 15);
x += 15;
}
if (_Level1[i, j] == 1) // als inhoud array [i,j] != 0 {teken wall}
{
paper.DrawImage(imgWall, x, y, 15, 15);
x += 15;
}
if (_Level1[i, j] == 2)
{
paper.FillRectangle(potloodPath, x, y, 15, 15);
x += 15;
}
if (x > (breedte * _Level1.GetLength(1))) // Begint nieuwe regel
{
y += 15;
x = 15;
}
}
}
}
private void drawPacman()
{
pacman.Display(paper);
}
private void drawDot()
{
dots.Display(paper);
}
#region move-event
private void frmMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
position = 1;
if (a == _Level1.GetLength(1))
{
pacman.MoveLeft();
}
else
{
pacman.MoveRight();
}
}
if (e.KeyCode == Keys.Left)
{
position = 2;
if (a == _Level1.GetLength(1))
{
pacman.MoveRight();
}
else
{
pacman.MoveLeft();
}
}
if (e.KeyCode == Keys.Up)
{
position = 3;
if (a == _Level1.GetLength(1))
{
pacman.MoveDown();
}
else
{
pacman.MoveUp();
}
}
if (e.KeyCode == Keys.Down)
{
position = 4;
if (a == _Level1.GetLength(1))
{
pacman.MoveUp();
}
else
{
pacman.MoveDown();
}
}
}
public void positionPacman()
{
if (position == 1)
{
pacman.pacman_right = Image.FromFile("Sprites/pacman_rechts.png");
}
if (position == 2)
{
pacman.pacman_left = Image.FromFile("Sprites/pacman_links.png");
}
if (position == 3)
{
pacman.pacman_up = Image.FromFile("Sprites/pacman_boven.png");
}
if (position == 4)
{
pacman.pacman_down = Image.FromFile("Sprites/pacman_onder.png");
}
}
#endregion
}
Laatst bewerkt door een moderator: