用c#编写飞机大战我方飞机代码
时间: 2023-07-27 17:10:41 浏览: 62
以下是一个简单的 C# 代码示例,用于创建和控制玩家飞机:
```
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PlaneGame
{
public class PlayerPlane : PictureBox
{
private int speed = 10;
public PlayerPlane()
{
InitializePlayerPlane();
}
private void InitializePlayerPlane()
{
this.BackColor = Color.Transparent;
this.Image = Properties.Resources.playerPlane;
this.Size = this.Image.Size;
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.Location = new Point((GameForm.GameAreaSize.Width - this.Width) / 2, GameForm.GameAreaSize.Height - this.Height - 50);
}
public void MoveLeft()
{
if (this.Location.X > 0)
{
this.Location = new Point(this.Location.X - speed, this.Location.Y);
}
}
public void MoveRight()
{
if (this.Location.X < GameForm.GameAreaSize.Width - this.Width)
{
this.Location = new Point(this.Location.X + speed, this.Location.Y);
}
}
public void MoveUp()
{
if (this.Location.Y > 0)
{
this.Location = new Point(this.Location.X, this.Location.Y - speed);
}
}
public void MoveDown()
{
if (this.Location.Y < GameForm.GameAreaSize.Height - this.Height)
{
this.Location = new Point(this.Location.X, this.Location.Y + speed);
}
}
}
}
```
这个类继承自 `PictureBox`,它创建了一个玩家飞机对象,并提供了四个方法 `MoveLeft`、`MoveRight`、`MoveUp` 和 `MoveDown`,用于控制飞机的移动。在这个示例中,我们使用了一些资源文件,如 `playerPlane` 图像,你可以根据自己的需求调整。