最近、私は2人の "Pacmen"とランダムに産んだ石を食べるための簡単なPacmanのようなゲームに取り組んできました。私は本当に、パンクマンが石の近くに来るたびに石をそれらから離れるようにしようと思っていますが、これがどのように行われているか正確には分かりません。おそらく誰かが私のコードを見て、私にいくつかの提案をしてもらえますか?ありがとう、本当にありがとう!クローンのPacmanからドットを移動しようとしたところ
をForm1コードファイル:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Runtime.InteropServices;
using System.Media;
namespace EaterGame1
{
public partial class Form1 : Form
{
[DllImport("winmm.dll")]
public static extern long PlaySound(String lpszName, long hModule, long dwFlags);
private ArrayList Stones = new ArrayList(30);
private Eater TheEater = new Eater(100, 100);
private Eater TheOtherEater = new Eater(100, 50);
private Random RandomGen = new Random();
private const int NumberOfStones = 100;
private Score TheScore = new Score(640, 350);
private Score TheOtherScore = new Score(640, 330);
private int TheSeconds = 0;
private TimerDisplay TheTime = new TimerDisplay(7, 350);
//private System.Windows.Forms.Timer timer1;
//private System.ComponentModel.IContainer components;
private Thread oThread = null;
public Form1()
{
InitializeComponent();
InitializeStones();
InitializeTimer();
// reduce flicker
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}
private string m_strCurrentSoundFile = "miss.wav";
public void PlayASound()
{
if (m_strCurrentSoundFile.Length > 0)
{
//PlaySound(Application.StartupPath + "\\" + m_strCurrentSoundFile, 0, 0);
SoundPlayer simpleSound = new SoundPlayer(Application.StartupPath + "\\" + m_strCurrentSoundFile);
simpleSound.Play();
}
m_strCurrentSoundFile = "";
oThread.Abort();
}
public void PlaySoundInThread(string wavefile)
{
m_strCurrentSoundFile = wavefile;
oThread = new Thread(new ThreadStart(PlayASound));
oThread.Start();
}
public void InitializeTimer()
{
timer1.Start();
}
public void InitializeStones()
{
for (int i = 0; i < NumberOfStones; i++)
{
Stones.Add(new Stone(RandomGen.Next(
10, ClientRectangle.Right - 10),
RandomGen.Next(10, ClientRectangle.Bottom - 30)));
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
string result = e.KeyData.ToString();
Invalidate(TheEater.GetFrame());
Invalidate(TheOtherEater.GetFrame());
switch (result)
{
case "Left":
TheEater.MoveLeft(ClientRectangle);
Invalidate(TheEater.GetFrame());
break;
case "Right":
TheEater.MoveRight(ClientRectangle);
Invalidate(TheEater.GetFrame());
break;
case "Up":
TheEater.MoveUp(ClientRectangle);
Invalidate(TheEater.GetFrame());
break;
case "Down":
TheEater.MoveDown(ClientRectangle);
Invalidate(TheEater.GetFrame());
break;
case "E":
TheOtherEater.MoveNorthWest(ClientRectangle);
Invalidate(TheOtherEater.GetFrame());
break;
case "Q":
TheOtherEater.MoveNorthEast(ClientRectangle);
Invalidate(TheOtherEater.GetFrame());
break;
case "C":
TheOtherEater.MoveSouthWest(ClientRectangle);
Invalidate(TheOtherEater.GetFrame());
break;
case "Z":
TheOtherEater.MoveSouthEast(ClientRectangle);
Invalidate(TheOtherEater.GetFrame());
break;
case "W":
TheOtherEater.MoveUp(ClientRectangle);
Invalidate(TheOtherEater.GetFrame());
break;
case "A":
TheOtherEater.MoveLeft(ClientRectangle);
Invalidate(TheOtherEater.GetFrame());
break;
case "D":
TheOtherEater.MoveRight(ClientRectangle);
Invalidate(TheOtherEater.GetFrame());
break;
case "X":
TheOtherEater.MoveDown(ClientRectangle);
Invalidate(TheOtherEater.GetFrame());
break;
default:
break;
}
int hit = CheckIntersection();
if (hit != -1)
{
TheScore.Increment();
PlaySoundInThread("hit.wav");
Invalidate(TheScore.GetFrame());
Invalidate(((Stone)Stones[hit]).GetFrame());
Stones.RemoveAt(hit);
if (Stones.Count == 0)
{
MessageBox.Show("You Win!\nYour time is " + TheTime.TheString + " seconds.\nThe first eater ate " + TheScore + " stones!\nThe second eater ate " + TheOtherScore + " stones!");
Application.Exit();
}
}
int hit2 = CheckIntersection2();
if(hit2 != -1)
{
TheOtherScore.Increment();
PlaySoundInThread("hit.wav");
Invalidate(TheOtherScore.GetFrame());
Invalidate(((Stone)Stones[hit2]).GetFrame());
Stones.RemoveAt(hit2);
if (Stones.Count == 0)
{
MessageBox.Show("You Win!\nYour time is " + TheTime.TheString + " seconds.\nThe first eater ate " + TheScore + " stones!\nThe second eater ate " + TheOtherScore + " stones!");
Application.Exit();
}
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, 0, 0, this.ClientRectangle.Width, ClientRectangle.Height);
// draw the score
TheScore.Draw(g);
TheOtherScore.Draw(g);
// draw the time
TheTime.Draw(g, TheSeconds);
// draw the stones
for (int i = 0; i < Stones.Count; i++)
{
((Stone)Stones[i]).Draw(g);
}
// also draw the eater
TheEater.Draw(g);
TheOtherEater.Draw(g);
}
private int CheckIntersection()
{
for (int i = 0; i < Stones.Count; i++)
{
Rectangle stoneRect = ((Stone)Stones[i]).GetFrame();
if (TheEater.GetFrame().IntersectsWith(stoneRect))
{
return i;
}
}
return -1;
}
private int CheckIntersection2()
{
for (int i = 0; i < Stones.Count; i++)
{
Rectangle stoneRect = ((Stone)Stones[i]).GetFrame();
if (TheOtherEater.GetFrame().IntersectsWith(stoneRect))
{
return i;
}
}
return -1;
}
private void timer1_Tick(object sender, EventArgs e)
{
TheSeconds++;
Invalidate(TheTime.GetFrame());
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
イーターコードファイル:
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace EaterGame1
{
/// <summary>
/// Eats all the dots
/// </summary>
public class Eater
{
public Point Position;
static Bitmap EaterImage = null;
static Bitmap EaterImage2 = null;
static Bitmap EaterImage3 = null;
static Bitmap EaterImage4 = null;
int inc = 3;
int LastPositionX = 0;
int LastPositionY = 0;
public Eater()
{
//
// TODO: Add constructor logic here
//
Position.X = 30;
Position.Y = 35;
if (EaterImage == null)
{
EaterImage = new Bitmap("eater.gif");
}
if (EaterImage2 == null)
{
EaterImage2 = new Bitmap("eater2.gif");
}
if(EaterImage3 == null)
{
EaterImage3 = new Bitmap("eater3.gif");
}
if (EaterImage4 == null)
{
EaterImage4 = new Bitmap("eater4.gif");
}
}
public Eater(int x, int y)
{
//
// TODO: Add constructor logic here
//
Position.X = x;
Position.Y = y;
if (EaterImage == null)
{
EaterImage = new Bitmap("eater.gif");
}
if (EaterImage2 == null)
{
EaterImage2 = new Bitmap("eater2.gif");
}
if (EaterImage3 == null)
{
EaterImage3 = new Bitmap("eater3.gif");
}
if (EaterImage4 == null)
{
EaterImage4 = new Bitmap("eater4.gif");
}
}
public Rectangle GetFrame()
{
Rectangle myRect = new Rectangle(Position.X, Position.Y, EaterImage.Width, EaterImage.Height);
return myRect;
}
public void Draw(Graphics g)
{
Rectangle destR = new Rectangle(Position.X, Position.Y, EaterImage.Width, EaterImage.Height);
Rectangle srcR = new Rectangle(0, 0, EaterImage.Width, EaterImage.Height);
// make it look like the mouth is moving
if (((Position.X % 2 == 1) && ((Position.X - LastPositionX) != 0)) ||
((Position.Y % 2 == 1) && ((Position.Y - LastPositionY) != 0))
)
g.DrawImage(EaterImage, destR, srcR, GraphicsUnit.Pixel);
else
g.DrawImage(EaterImage2, destR, srcR, GraphicsUnit.Pixel);
if (((Position.X % 2 == 1) && ((Position.X - LastPositionX) != 0)) ||
((Position.Y % 2 == 1) && ((Position.Y - LastPositionY) != 0))
)
g.DrawImage(EaterImage3, destR, srcR, GraphicsUnit.Pixel);
else
g.DrawImage(EaterImage4, destR, srcR, GraphicsUnit.Pixel);
LastPositionX = Position.X;
LastPositionY = Position.Y;
}
public void MoveLeft(Rectangle r)
{
if (Position.X <= 0)
return; // precondition
Position.X -= inc;
}
public void MoveRight(Rectangle r)
{
if (Position.X >= r.Width - EaterImage.Width)
return; // precondition
Position.X += inc;
}
public void MoveUp(Rectangle r)
{
if (Position.Y <= 0)
return; // precondition
Position.Y -= inc;
}
public void MoveDown(Rectangle r)
{
if (Position.Y >= r.Height - EaterImage.Height)
return; // precondition
Position.Y += inc;
}
public void MoveNorthWest(Rectangle r)
{
MoveUp(r);
MoveRight(r);
}
public void MoveNorthEast(Rectangle r)
{
MoveUp(r);
MoveLeft(r);
}
public void MoveSouthWest(Rectangle r)
{
MoveDown(r);
MoveRight(r);
}
public void MoveSouthEast(Rectangle r)
{
MoveDown(r);
MoveLeft(r);
}
}
}
ストーン・コード・ファイル:
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace EaterGame1
{
/// <summary>
/// Summary description for Stone.
/// </summary>
public class Stone
{
public Point Position;
static Bitmap StoneImage = null;
public Stone()
{
//
// TODO: Add constructor logic here
//
Position.X = 0;
Position.Y = 0;
if (StoneImage == null)
{
StoneImage = new Bitmap("stone.gif");
}
}
public Stone(int x, int y)
{
//
// TODO: Add constructor logic here
//
Position.X = x;
Position.Y = y;
if (StoneImage == null)
{
StoneImage = new Bitmap("stone.gif");
}
}
public Rectangle GetFrame()
{
Rectangle myRect = new Rectangle(Position.X, Position.Y, StoneImage.Width, StoneImage.Height);
return myRect;
}
public void Draw(Graphics g)
{
Rectangle destR = new Rectangle(Position.X, Position.Y, StoneImage.Width, StoneImage.Height);
Rectangle srcR = new Rectangle(0, 0, StoneImage.Width, StoneImage.Height);
g.DrawImage(StoneImage, destR, srcR, GraphicsUnit.Pixel);
}
}
}
私は石に方法を与えるだろう
@Aidian、あなたは、おそらく誰もがあなたがあなたの質問に掲載しているもののすべてがあなたを行うデバッグに期待することはできません 'Debugger'使用を開始する必要があります。.. ? – MethodMan
@MethodMan残念ながら、デバッガはここでは役に立たないでしょう。私は多分式やパクメンと石の間の距離を測定する何らかの方法を探しています。問題はデバッグを保証する必要がありますが、本質的に間違っていることはありません。私はそれがほしいと思うほど複雑ではなく、私はいくつかのアドバイスを探していました! – Aidan
@Aidanあなたのメソッドを呼び出す主なメソッドが見つかりませんでした。私はそれがTickイベントであると仮定しました。メソッドを作成し、メインメソッド(tick?)で呼び出すことで、パークマンと石との距離を石ごとにチェックできます。あなたの許容値よりもパクマンに近い場合は、石とパックマンのxとyの位置を取得します - パックマンがその石の北の南東西にあるかどうかを確認し、それに応じて石を移動します。いくつかの擬似コードがあります。 – Radmation