【发布时间】:2015-03-21 02:59:46
【问题描述】:
我试图编译这段代码,但它不起作用,编译时出现这个错误:
Pong\Form1.Designer.cs(14,33,14,40): 错误 CS0115: 'Pong.Form1.Dispose(bool)': 找不到合适的方法来覆盖
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace Pong
{
public partial class gameArea : Form
{
PictureBox picBoxPlayer, picBoxAI, picBoxBall;
Timer gameTime; // also the game loop
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
Size sizePlayer = new Size(25, 100);
Size sizeAI = new Size(25, 100);
Size sizeBall = new Size(20, 20);
const int gameTimeInterval = 1;
const int ballStartSpeed = 2;
const int ballIncreaseSpeedRate = 1;
const int ballSpeedLimited = 15;
const int aiOffSetLoops = 15;
int ballSpeedX = ballStartSpeed;
int ballSpeedY = ballStartSpeed;
Random rad;
int aiOffSet;
int aiOffSetCounter;
Dictionary<string, SoundPlayer> sounds;
public gameArea()
{
InitializeComponent();
this.DoubleBuffered = true;
picBoxPlayer = new PictureBox();
picBoxAI = new PictureBox();
picBoxBall = new PictureBox();
gameTime = new Timer();
gameTime.Interval = gameTimeInterval;
gameTime.Tick += new EventHandler(gameTime_Tick);
this.Width = SCREEN_WIDTH;
this.Height = SCREEN_HEIGHT;
this.StartPosition = FormStartPosition.CenterScreen;
this.BackColor = Color.Black;
picBoxPlayer.Size = sizePlayer;
picBoxPlayer.Location = new Point(picBoxPlayer.Width / 2, ClientSize.Height / 2 - picBoxPlayer.Height / 2);
picBoxPlayer.BackColor = Color.Blue;
this.Controls.Add(picBoxPlayer);
picBoxAI.Size = sizeAI;
picBoxAI.Location = new Point(ClientSize.Width - (picBoxAI.Width + picBoxAI.Width / 2), ClientSize.Height / 2 - picBoxPlayer.Height / 2); // TODO: why picBoxPlayer and not picBoxAI?
picBoxAI.BackColor = Color.Red;
this.Controls.Add(picBoxAI);
rad = new Random();
aiOffSet = 0;
aiOffSetCounter = 1;
picBoxBall.Size = sizeBall;
picBoxBall.Location = new Point(ClientSize.Width / 2 - picBoxBall.Width / 2, ClientSize.Height / 2 - picBoxBall.Height / 2);
picBoxBall.BackColor = Color.Green;
this.Controls.Add(picBoxBall);
// Load Sounds
sounds = new Dictionary<string, SoundPlayer>();
for (int k = 1; k <= 10; k++)
{
sounds.Add(String.Format(@"pong{0}", k), new SoundPlayer(String.Format(@"pong{0}.wav", k)));
}
// Start Game loop
gameTime.Enabled = true;
}
void gameTime_Tick(object sender, EventArgs e)
{
picBoxBall.Location = new Point(picBoxBall.Location.X + ballSpeedX, picBoxBall.Location.Y + ballSpeedY);
gameAreaCollosions();
padlleCollision();
playerMovement();
aiMovement();
}
private void iaChangeOffSet()
{
if (aiOffSetCounter >= aiOffSetLoops)
{
aiOffSet = rad.Next(1, picBoxAI.Height + picBoxBall.Height);
aiOffSetCounter = 1;
}
else
{
aiOffSetCounter++;
}
}
private void gameAreaCollosions()
{
if (picBoxBall.Location.Y > ClientSize.Height - picBoxBall.Height || picBoxBall.Location.Y < 0)
{
iaChangeOffSet();
ballSpeedY = -ballSpeedY;
sideCollision();
}
else if (picBoxBall.Location.X > ClientSize.Width)
{
padlleSideCollision();
resetBall();
}
else if (picBoxBall.Location.X < 0)
{
padlleSideCollision();
resetBall();
}
}
private void resetBall()
{
if (ballSpeedX > 0)
ballSpeedX = -ballStartSpeed;
else
ballSpeedX = ballStartSpeed;
if (ballSpeedY > 0)
ballSpeedY = -ballStartSpeed;
else
ballSpeedY = ballStartSpeed;
aiOffSet = 0;
picBoxBall.Location = new Point(ClientSize.Width / 2 - picBoxBall.Width / 2, ClientSize.Height / 2 - picBoxBall.Height / 2);
}
private void playerMovement()
{
if (this.PointToClient(MousePosition).Y >= picBoxPlayer.Height / 2 && this.PointToClient(MousePosition).Y <= ClientSize.Height - picBoxPlayer.Height / 2)
{
int playerX = picBoxPlayer.Width / 2;
int playerY = this.PointToClient(MousePosition).Y - picBoxPlayer.Height / 2;
picBoxPlayer.Location = new Point(playerX, playerY);
}
}
private void aiMovement()
{
int aiX = ClientSize.Width - (picBoxAI.Width + picBoxAI.Width / 2);
int aiY = (picBoxBall.Location.Y - picBoxAI.Height / 2) + aiOffSet;
if (aiY < 0)
aiY = 0;
if (aiY > ClientSize.Height - picBoxAI.Height)
aiY = ClientSize.Height - picBoxAI.Height;
picBoxAI.Location = new Point(aiX, aiY);
}
private void padlleCollision()
{
if (picBoxBall.Bounds.IntersectsWith(picBoxAI.Bounds))
{
picBoxBall.Location = new Point(picBoxAI.Location.X - picBoxBall.Width, picBoxBall.Location.Y);
ballSpeedX = -ballSpeedX;
aiCollision();
}
if (picBoxBall.Bounds.IntersectsWith(picBoxPlayer.Bounds))
{
picBoxBall.Location = new Point(picBoxPlayer.Location.X + picBoxPlayer.Width, picBoxBall.Location.Y);
ballSpeedX = -ballSpeedX;
playerCollision();
}
}
private void playerCollision()
{
sounds["pong1"].Play();
SlowDownBall();
}
private void aiCollision()
{
sounds["pong2"].Play();
SlowDownBall();
}
private void sideCollision()
{
sounds["pong3"].Play();
SpeedUpBall();
}
private void padlleSideCollision()
{
sounds["pong9"].Play();
}
private void SpeedUpBall()
{
if (ballSpeedY > 0)
{
ballSpeedY += ballIncreaseSpeedRate;
if (ballSpeedY >= ballSpeedLimited)
ballSpeedY = ballSpeedLimited;
}
else
{
ballSpeedY -= ballIncreaseSpeedRate;
if (ballSpeedY <= -ballSpeedLimited)
ballSpeedY = -ballSpeedLimited;
}
if (ballSpeedX > 0)
{
ballSpeedX += ballIncreaseSpeedRate;
if (ballSpeedX >= ballSpeedLimited)
ballSpeedX = ballSpeedLimited;
}
else
{
ballSpeedX -= ballIncreaseSpeedRate;
if (ballSpeedX <= -ballSpeedLimited)
ballSpeedX = -ballSpeedLimited;
}
}
private void SlowDownBall()
{
if (ballSpeedY > 0)
{
ballSpeedY -= ballIncreaseSpeedRate;
if (ballSpeedY <= ballStartSpeed)
ballSpeedY = ballStartSpeed;
}
else
{
ballSpeedY += ballIncreaseSpeedRate;
if (ballSpeedY >= -ballStartSpeed)
ballSpeedY = -ballStartSpeed;
}
if (ballSpeedX > 0)
{
ballSpeedX -= ballIncreaseSpeedRate;
if (ballSpeedX <= ballStartSpeed)
ballSpeedX = ballStartSpeed;
}
else
{
ballSpeedX += ballIncreaseSpeedRate;
if (ballSpeedX >= -ballStartSpeed)
ballSpeedX = -ballStartSpeed;
}
}
}
}
【问题讨论】:
-
您是否修改了设计器生成的代码(Pong\Form1.Designer.cs)?
-
你不会真的指望我们阅读所有的垃圾吧?删除所有不相关的代码。
-
我注意到一件事,错误提示
Pong.Form1.Dispose(bool)其中类名是gameArea你修改了类名吗?