-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame1.cs
More file actions
148 lines (136 loc) · 4.17 KB
/
Game1.cs
File metadata and controls
148 lines (136 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Comora;
using System.IO;
using System;
namespace ProgGame
{
public class Game1 : Game
{
private const int defaultLives = 30;
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private GameLevel level;
private SpriteFont spriteFont;
private SpriteFont boldFont;
private bool isTest = false;
private int livesCount = defaultLives;
private int levelIndex = -1;
private int maxLevelIndex = 4;
private bool gameEnded = false;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
_graphics.PreferredBackBufferWidth = 1920;
_graphics.PreferredBackBufferHeight = 1080;
_graphics.HardwareModeSwitch = false;
_graphics.ToggleFullScreen();
this._graphics.ApplyChanges();
_spriteBatch = new SpriteBatch(GraphicsDevice);
LoadNextLevel();
base.Initialize();
}
private void LoadTestLevel()
{
string path = "Content/levels/testlevel.txt";
using (Stream fileStream = TitleContainer.OpenStream(path))
level = new GameLevel(Services, fileStream, GraphicsDevice);
}
private void LoadNextLevel()
{
if (isTest)
{
LoadTestLevel();
return;
}
if (level != null)
level.content.Unload();
if (levelIndex != maxLevelIndex)
++levelIndex;
else
gameEnded = true;
string path = $"Content/levels/{levelIndex}.txt";
using (Stream fileStream = TitleContainer.OpenStream(path))
level = new GameLevel(Services, fileStream, GraphicsDevice);
}
protected override void LoadContent()
{
spriteFont = Content.Load<SpriteFont>("font");
boldFont = Content.Load<SpriteFont>("boldfont");
}
protected override void Update(GameTime gameTime)
{
if (gameEnded)
return;
KeyboardState state = Keyboard.GetState();
if (!level.player.isAlive && state.IsKeyDown(Keys.R))
{
levelIndex -= 1;
if (--livesCount <= 0)
{
livesCount = defaultLives;
levelIndex = -1;
}
LoadNextLevel();
}
if (level.levelEnded)
LoadNextLevel();
if (state.IsKeyDown(Keys.Escape))
Exit();
level.Update(gameTime);
base.Update(gameTime);
}
private void DrawShadowedString(SpriteFont font, string value, Vector2 position, Color color)
{
_spriteBatch.DrawString(font, value, position + new Vector2(2, 2), Color.Black);
_spriteBatch.DrawString(font, value, position, color);
}
private void DrawUI()
{
_spriteBatch.Begin();
string s = $"Жизней осталось: {livesCount}";
Vector2 size = spriteFont.MeasureString(s);
DrawShadowedString(spriteFont, s, new Vector2((1920 / 2) - (size.X / 2), 10), Color.White);
if (gameEnded)
{
string a = "Игра пройдена!";
Vector2 strSize = spriteFont.MeasureString(a);
DrawShadowedString(spriteFont, a, new Vector2((GraphicsDevice.Viewport.Width / 2) - (strSize.X / 2),
(GraphicsDevice.Viewport.Height / 2) - (strSize.Y / 2)), Color.White);
}
if (!level.player.isAlive)
{
if (livesCount <= 1)
{
string a = "Вы проиграли\nНажмите \"R\" чтобы перезапустить игру";
Vector2 strSize = spriteFont.MeasureString(a);
DrawShadowedString(spriteFont, a, new Vector2((GraphicsDevice.Viewport.Width / 2) - (strSize.X / 2),
(GraphicsDevice.Viewport.Height / 2) - (strSize.Y / 2)), Color.White);
}
else
{
string a = "Вы умерли\nНажмите \"R\" чтобы перезапустить уровень";
Vector2 strSize = spriteFont.MeasureString(a);
DrawShadowedString(spriteFont, a, new Vector2((GraphicsDevice.Viewport.Width / 2) - (strSize.X / 2),
(GraphicsDevice.Viewport.Height / 2) - (strSize.Y / 2)), Color.White);
}
}
_spriteBatch.End();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(new Color(80, 187, 255));
_spriteBatch.Begin(level.camera); // Рисуем объекты в вьюпорте камеры
level.Draw(_spriteBatch);
_spriteBatch.End();
DrawUI();
base.Draw(gameTime);
}
}
}