Hi, I'm trying to remake pacman with Mono & SdlDotNet, but I've hit a problem. I'm reading colour information off the screen that should stop pacman from going into the walls by setting the xSpeed & ySpeed to 0... But sometimes it goes into the walls and sometimes it stops you from moving (when you are travelling up to the top right hand corner).
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using SdlDotNet.Core;
using SdlDotNet.Graphics;
using SdlDotNet.Graphics.Primitives;
using SdlDotNet.Input;
namespace PMAN
{
public class PMAN
{
Surface sfcGameWindow;
Surface map = new Surface ("media/background.jpg");
const int gameWidth = 800;
const int gameHeight = 800;
const int blockSize = 40;
int score;
Surface point = new Surface ("media/point" + Convert.ToString (blockSize) + ".png");
pacman pman;
List<point> points = new List<point> ();
public PMAN ()
{
string[] lines = File.ReadAllLines ("map" + Convert.ToString (blockSize) + ".txt");
short x, y;
x = y = 0;
foreach (string line in lines) {
foreach (char c in line) {
if (c == '1') {
short xp = Convert.ToInt16 (x + blockSize);
short yp = Convert.ToInt16 (y + blockSize);
map.Draw (new Box (x, y, xp, yp), Color.Blue, false, true);
} else if (c == '0') {
points.Add (new point (blockSize, x, y));
} else if (c == 'p') {
pman = new pacman (blockSize, x, y);
}
x += blockSize;
}
x = 0;
y += blockSize;
}
}
public void Run ()
{
sfcGameWindow = Video.SetVideoMode (gameWidth, gameHeight);
Events.Quit += new EventHandler<QuitEventArgs> (Events_Quit);
Events.Tick += new EventHandler<TickEventArgs> (Events_Tick);
Events.KeyboardDown += new EventHandler<KeyboardEventArgs> (Events_KeyboardDown);
Events.Run ();
}
void Events_Quit (object sender, QuitEventArgs e)
{
Events.QuitApplication ();
}
void Events_MouseDown (object sender, MouseButtonEventArgs e)
{
}
void Events_KeyboardDown (object sender, KeyboardEventArgs e)
{
switch (e.Key) {
case Key.DownArrow:
pman.xSpeed = 0;
pman.ySpeed = 200;
break;
case Key.UpArrow:
pman.xSpeed = 0;
pman.ySpeed = -200;
break;
case Key.RightArrow:
pman.xSpeed = 200;
pman.ySpeed = 0;
break;
case Key.LeftArrow:
pman.xSpeed = -200;
pman.ySpeed = 0;
break;
}
}
void Events_Tick (object sender, TickEventArgs e)
{
Update (e.SecondsElapsed);
Draw ();
}
void Update (float elapsed)
{
pman.update (elapsed, map);
}
void Draw ()
{
sfcGameWindow.Blit (map);
for (int i = points.Count - 1; i >= 0; i--) {
point p = points[i];
Rectangle prec = new Rectangle (p.x, p.y, p.pic.Width / 2, p.pic.Height / 2);
Rectangle parec = new Rectangle (pman.x - 10, pman.y, pman.pic.Width, pman.pic.Height);
if (prec.IntersectsWith (parec)) {
points.RemoveAt (i);
} else {
sfcGameWindow.Blit (p.pic, new Point (p.x, p.y));
}
}
sfcGameWindow.Blit (pman.pic, new Point (pman.x, pman.y));
sfcGameWindow.Update ();
}
}
}
Pacman class:
using System;
using System.Drawing;
using SdlDotNet.Core;
using SdlDotNet.Graphics;
namespace PMAN
{
public class pacman
{
public Surface pic;
public int x,y;
public int xSpeed,ySpeed;
public pacman (int _mode, int _x, int _y)
{
x = _x;
y = _y;
pic = new Surface("media/pacman"+Convert.ToString(_mode)+".png");
xSpeed = 200;
ySpeed = 0;
}
public void update(float elapsed, Surface gmwindow){
if(xSpeed == 200){
if(gmwindow.GetPixel(new Point(x+pic.Width, y+3)).ToArgb()
== Color.Blue.ToArgb()){
xSpeed = 0;
ySpeed = 0;
}
}else if(xSpeed == -200){
if(gmwindow.GetPixel(new Point(x+15, y)).ToArgb()
== Color.Blue.ToArgb()){
xSpeed = 0;
ySpeed = 0;
}
}else if(ySpeed == 200){
if(gmwindow.GetPixel(new Point(x+15, y+pic.Height)).ToArgb()
== Color.Blue.ToArgb()){
xSpeed = 0;
ySpeed = 0;
}
}else if(ySpeed == -200){
if(gmwindow.GetPixel(new Point(x+15, y)).ToArgb()
== Color.Blue.ToArgb()){
xSpeed = 0;
ySpeed = 0;
}
}
x += Convert.ToInt32(xSpeed * elapsed);
y += Convert.ToInt32(ySpeed * elapsed);
}
}
}