用c#编写飞机大战
时间: 2023-07-27 14:10:45 浏览: 107
以下是一个简单的C#飞机大战游戏代码示例,仅供参考:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PlaneGame
{
public partial class MainForm : Form
{
private Graphics graphics;
private Bitmap backBuffer;
private Random random = new Random();
private const int planeSpeed = 5;
private const int bulletSpeed = 10;
private const int enemySpeed = 3;
private const int maxEnemyCount = 10;
private const int maxBulletCount = 30;
private Rectangle playerRect;
private Point playerLocation;
private Image playerImage;
private Bullet[] bullets = new Bullet[maxBulletCount];
private Enemy[] enemies = new Enemy[maxEnemyCount];
private int score = 0;
private bool gameOver = false;
public MainForm()
{
InitializeComponent();
InitializeGame();
}
private void InitializeGame()
{
backBuffer = new Bitmap(ClientSize.Width, ClientSize.Height);
graphics = Graphics.FromImage(backBuffer);
playerImage = Image.FromFile("player.png");
playerLocation = new Point((ClientSize.Width - playerImage.Width) / 2, ClientSize.Height - playerImage.Height - 50);
playerRect = new Rectangle(playerLocation, playerImage.Size);
for (int i = 0; i < maxBulletCount; i++)
{
bullets[i] = new Bullet();
}
for (int i = 0; i < maxEnemyCount; i++)
{
enemies[i] = new Enemy(random.Next(ClientSize.Width));
}
}
protected override void OnPaint(PaintEventArgs e)
{
graphics.Clear(BackColor);
graphics.DrawImage(playerImage, playerLocation);
foreach (Bullet bullet in bullets)
{
if (bullet.Visible)
{
graphics.FillEllipse(Brushes.Red, bullet.Location.X, bullet.Location.Y, bullet.Size.Width, bullet.Size.Height);
}
}
foreach (Enemy enemy in enemies)
{
if (enemy.Visible)
{
graphics.DrawImage(enemy.Image, enemy.Location);
}
}
if (gameOver)
{
graphics.DrawString("Game Over\nScore: " + score, Font, Brushes.Black, ClientSize.Width / 2 - 50, ClientSize.Height / 2 - 20);
}
else
{
graphics.DrawString("Score: " + score, Font, Brushes.Black, 10, 10);
}
e.Graphics.DrawImage(backBuffer, 0, 0);
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (!gameOver)
{
if (e.KeyCode == Keys.Left)
{
playerLocation.X -= planeSpeed;
}
else if (e.KeyCode == Keys.Right)
{
playerLocation.X += planeSpeed;
}
else if (e.KeyCode == Keys.Space)
{
for (int i = 0; i < maxBulletCount; i++)
{
if (!bullets[i].Visible)
{
bullets[i].Visible = true;
bullets[i].Location = new Point(playerLocation.X + playerImage.Width / 2 - bullets[i].Size.Width / 2, playerLocation.Y);
break;
}
}
}
playerRect.Location = playerLocation;
CheckCollision();
Invalidate();
}
}
private void MoveBullets()
{
foreach (Bullet bullet in bullets)
{
if (bullet.Visible)
{
bullet.Location.Y -= bulletSpeed;
if (bullet.Location.Y < 0)
{
bullet.Visible = false;
}
}
}
}
private void MoveEnemies()
{
foreach (Enemy enemy in enemies)
{
if (enemy.Visible)
{
enemy.Location.Y += enemySpeed;
if (enemy.Location.Y > ClientSize.Height)
{
enemy.Visible = false;
}
}
}
}
private void GenerateEnemies()
{
foreach (Enemy enemy in enemies)
{
if (!enemy.Visible && random.Next(100) < 5)
{
enemy.Visible = true;
enemy.Location = new Point(random.Next(ClientSize.Width), -enemy.Image.Height);
break;
}
}
}
private void CheckCollision()
{
foreach (Bullet bullet in bullets)
{
if (bullet.Visible)
{
foreach (Enemy enemy in enemies)
{
if (enemy.Visible && bullet.Bounds.IntersectsWith(enemy.Bounds))
{
bullet.Visible = false;
enemy.Visible = false;
score++;
}
}
}
}
foreach (Enemy enemy in enemies)
{
if (enemy.Visible && enemy.Bounds.IntersectsWith(playerRect))
{
gameOver = true;
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
MoveBullets();
MoveEnemies();
GenerateEnemies();
CheckCollision();
Invalidate();
}
}
public class Bullet
{
private Point location = new Point(0, 0);
private Size size = new Size(10, 10);
private bool visible = false;
public Point Location { get => location; set => location = value; }
public Size Size { get => size; set => size = value; }
public bool Visible { get => visible; set => visible = value; }
public Rectangle Bounds { get => new Rectangle(Location, Size); }
}
public class Enemy
{
private Point location = new Point(0, 0);
private Image image = Image.FromFile("enemy.png");
private bool visible = false;
public Point Location { get => location; set => location = value; }
public Image Image { get => image; set => image = value; }
public bool Visible { get => visible; set => visible = value; }
public Rectangle Bounds { get => new Rectangle(Location, Image.Size); }
public Enemy(int x)
{
location.X = x;
}
}
}
```
需要注意的是,此代码示例仅是一个简单的飞机大战游戏,没有涉及到音效、难度等方面的调整。如果您希望开发更完整的飞机大战游戏,可以考虑增加游戏元素、美化游戏界面、优化游戏体验等方面。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)