Загрузка данных
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace Enter_The_Gungeon_Nnahod_Version
{
public partial class Form1 : Form
{
Image bg = Image.FromFile
Button restartButton;
float playerX = 200;
float playerY = 200;
float speed = 4f;
bool up, down, left, right;
bool isPaused = false;
int score = 0;
Timer timer = new Timer();
List<Bullet> bullets = new List<Bullet>();
float enemyX = 300;
float enemyY = 100;
string scoreFile = "scores.txt";
Point mousePos;
List<int> scores = new List<int>();
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
this.BackColor = Color.DarkRed;
timer = new Timer();
timer.Interval = 16;
timer.Tick += UpdateGame;
timer.Start();
DoubleBuffered = true;
startButton = new Button(); // Кнопка старт
startButton.Text = "START";
startButton.Width = 120;
startButton.Height = 40;
startButton.Left = 200;
startButton.Top = 200;
restartButton = new Button();
restartButton.Text = "Restart";
restartButton.Width = 120;
restartButton.Height = 40;
restartButton.Left = 200;
restartButton.Top = 250;
restartButton.Visible = false;
restartButton.Click += (s, e) =>
{
RestartGame();
state = GameState.Playing;
restartButton.Visible = false;
};
this.Controls.Add(restartButton);
this.Controls.Add(restartButton);
startButton.Click += (s, e) =>
{
state = GameState.Playing;
startButton.Visible = false;
};
this.Controls.Add(startButton);
walls.Add(new Rectangle(100, 100, 200, 20));
walls.Add(new Rectangle(300, 200, 20, 200));
walls.Add(new Rectangle(150, 350, 250, 20));
this.KeyPreview = true;
this.Focus();
void RestartGame()
{
playerX = 200;
playerY = 200;
enemyX = 300;
enemyY = 100;
score = 0;
state = GameState.Playing;
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
state = GameState.Menu;
startButton.Visible = true;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
bool CanMove(float x, float y)
{
Rectangle player = new Rectangle((int)x, (int)y, 30, 30);
foreach (var w in walls)
if (player.IntersectsWith(w))
return false;
return true;
}
enum GameState
{
Menu,
Playing,
GameOver,
Records
}
GameState state = GameState.Menu;
Button startButton;
List<Rectangle> walls = new List<Rectangle>();
void RestartGame()
{
playerX = 200;
playerY = 200;
enemyX = 300;
enemyY = 100;
score = 0;
bullets.Clear();
state = GameState.Playing;
}
void UpdateGame(object sender, EventArgs e)
{
if (state != GameState.Playing)
{
Invalidate();
return;
}
if (state != GameState.Playing) return;
{
foreach (var b in bullets)
{
b.X += b.DirX * b.Speed;
b.Y += b.DirY * b.Speed;
}
if (state != GameState.Playing) return;
if (up) playerY -= speed;
if (down) playerY += speed;
if (left) playerX -= speed;
if (right) playerX += speed;
}
Rectangle player = new Rectangle((int)playerX, (int)playerY, 30, 30);
Rectangle enemy = new Rectangle((int)enemyX, (int)enemyY, 30, 30);
if (player.IntersectsWith(enemy))
{
state = GameState.GameOver;
}
if (isPaused) return;
// движение игрока
if (up) playerY -= speed;
if (down) playerY += speed;
if (left) playerX -= speed;
if (right) playerX += speed;
// движение пуль
foreach (var b in bullets)
{
b.Y -= b.Speed;
}
// враг идёт к игроку
if (enemyX < playerX) enemyX += 2;
if (enemyX > playerX) enemyX -= 2;
if (enemyY < playerY) enemyY += 2;
if (enemyY > playerY) enemyY -= 2;
// попадания
for (int i = 0; i < bullets.Count; i++)
{
var b = bullets[i];
if (Math.Abs(b.X - enemyX) < 20 &&
Math.Abs(b.Y - enemyY) < 20)
{
bullets.RemoveAt(i);
score += 10;
// респавн врага
Random r = new Random();
enemyX = r.Next(50, 500);
enemyY = r.Next(50, 300);
break;
}
}
Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
mousePos = e.Location;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
if (state == GameState.Records)
{
g.DrawString("RECORDS",
new Font("Arial", 24, FontStyle.Bold),
Brushes.White, 150, 80);
for (int i = 0; i < scores.Count; i++)
{
g.DrawString(scores[i].ToString(),
new Font("Consolas", 14),
Brushes.White, 150, 130 + i * 20);
}
g.DrawString("ESC - Back",
new Font("Consolas", 12),
Brushes.Gray, 150, 400);
return;
}
foreach (var w in walls)
{
g.FillRectangle(Brushes.Gray, w);
}
if (state == GameState.GameOver)
{
g.DrawString("GAME OVER",
new Font("Arial", 40, FontStyle.Bold),
Brushes.Red, 150, 150);
g.DrawString("Press R or click Restart",
new Font("Consolas", 14),
Brushes.White, 150, 220);
}
if (state == GameState.GameOver)
{
restartButton.Visible = true;
g.DrawString("GAME OVER",
new Font("Arial", 40, FontStyle.Bold),
Brushes.Red, 150, 150);
g.DrawString("Press Restart",
new Font("Consolas", 14),
Brushes.White, 180, 220);
}
else
{
restartButton.Visible = false;
}
// фон
g.Clear(Color.FromArgb(30, 30, 40));
// игрок
g.FillEllipse(Brushes.Cyan, playerX, playerY, 30, 30);
g.DrawEllipse(new Pen(Color.White, 2), playerX, playerY, 30, 30);
// враг
g.FillEllipse(Brushes.Red, enemyX, enemyY, 30, 30);
g.DrawEllipse(new Pen(Color.Black, 2), enemyX, enemyY, 30, 30);
// пули
foreach (var b in bullets)
{
g.FillEllipse(Brushes.Yellow, b.X, b.Y, 8, 8);
}
// панель
g.FillRectangle(new SolidBrush(Color.FromArgb(120, 0, 0, 0)), 0, 0, Width, 40);
// счёт
g.DrawString("SCORE: " + score,
new Font("Consolas", 14, FontStyle.Bold),
Brushes.White, 10, 10);
// подсказки
g.DrawString("WASD | ЛКМ | ESC | R",
new Font("Consolas", 8),
Brushes.White, 10, Height - 30);
// пауза
if (isPaused)
{
string text = "PAUSED";
Font f = new Font("Arial", 40, FontStyle.Bold);
SizeF size = g.MeasureString(text, f);
g.DrawString(text, f, Brushes.White,
(Width - size.Width) / 2,
(Height - size.Height) / 2);
}
if (state == GameState.Menu)
{
g.DrawString("MY GUN GAME",
new Font("Arial", 30, FontStyle.Bold),
Brushes.White, 150, 100);
g.DrawString("R - Records",
new Font("Consolas", 14),
Brushes.White, 150, 240);
g.DrawString("ESC - Exit",
new Font("Consolas", 14),
Brushes.White, 150, 280);
return;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (state == GameState.Menu)
{
if (e.KeyCode == Keys.R)
{
state = GameState.Records;
return;
}
if (e.KeyCode == Keys.Enter)
{
state = GameState.Playing;
return;
}
}
if (state == GameState.GameOver)
{
if (e.KeyCode == Keys.Enter)
{
RestartGame();
state = GameState.Playing;
}
return;
}
if (state == GameState.Playing)
{
if (e.KeyCode == Keys.W) up = true;
if (e.KeyCode == Keys.S) down = true;
if (e.KeyCode == Keys.A) left = true;
if (e.KeyCode == Keys.D) right = true;
}
if (state == GameState.Records)
{
if (e.KeyCode == Keys.Escape)
{
state = GameState.Menu;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.KeyCode == Keys.W) up = false;
if (e.KeyCode == Keys.S) down = false;
if (e.KeyCode == Keys.A) left = false;
if (e.KeyCode == Keys.D) right = false;
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (state != GameState.Playing) return;
float dx = e.X - (playerX + 15);
float dy = e.Y - (playerY + 15);
float length = (float)Math.Sqrt(dx * dx + dy * dy);
bullets.Add(new Bullet
{
X = playerX + 15,
Y = playerY + 15,
DirX = dx / length,
DirY = dy / length
});
}
void SaveScore()
{
File.AppendAllText(scoreFile, score + "\n");
scores.Add(score);
File.AppendAllLines(scoreFile, new[] { score.ToString() });
}
}
}
public class Bullet
{
public float X, Y;
public float Speed = 10f;
public float DirX, DirY;
}