我试图编译这段代码,但它不会工作,编译时得到这个错误:
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;
}
}
}
}
3条答案
按热度按时间hfwmuf9z1#
正如注解中所暗示的那样,在.cs文件中更改类名而不更新其他引用可能会破坏一些东西。您从
Form1
类开始,将其中一个引用更改为gameArea
,但未更改其他引用。因此,您有 * 两个 * 类,gameArea
派生自Form
,Form1
隐式派生自object
。Form1
包含Dispose
方法,但object
没有任何Dispose
方法可以覆盖。要解决这个问题,首先将
gameArea
更改回Form1
。这应该可以让你的代码再次编译。然后打开设计器,使用它将Form1
重命名为gameArea
。这将比您手动更新更多。Hans Passant在评论中指出,也可以使用上下文菜单中的“重命名”选项(在“重构”下)而不是设计器。这将更新代码中的任何引用,而无需执行创建设计时窗体并保存该窗体的过程。因此,可能与在设计器中重命名的结果略有不同,例如,如果代码访问窗体的
Name
属性,但对于大多数程序来说,这应该足够好了。eit6fx6z2#
这是由于删除了代码段“InitializeComponent();“用于WinForm应用程序,即段应该首先自己出现。
tnkciper3#
错误消息“error CS0115:'Pong.Form1.Dispose(bool)':“no suitable method found to override”表示Form1类中的Dispose方法存在问题。您可以查看可以帮助您解决此错误的this article。