-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame1.cs
More file actions
189 lines (154 loc) · 6.12 KB
/
Copy pathGame1.cs
File metadata and controls
189 lines (154 loc) · 6.12 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Diagnostics;
using System.Security.AccessControl;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
namespace PeggleAI
{
public class Game1 : Game
{
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
private BasicEffect spriteBatchEffect;
private SpriteFont infoFont;
private Texture2D background;
// AI stuffs
private const int POPULATION_SIZE = 10;
private const int GENERATION_COUNT = 5;
private const int SHOT_COUNT = 10;
private LevelComponent[] levels;
private PeggleAlgorithm peggleAI;
// Program will crash if the level is reset while the world is stepping.
// This mutex pauses Game1 thread before calling LevelComponent.Update(), and LevelComponent.Reset();
private Mutex levelMutex;
// Camera
private Vector3 cameraPosition = new Vector3(0, 0, 0);
float cameraViewWidth = 12.5f;
private Vector2 cameraView;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 768;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
levels = new LevelComponent[POPULATION_SIZE];
for (int i=0; i<POPULATION_SIZE; i++)
{
levels[i] = new LevelComponent();
}
levelMutex = new Mutex();
peggleAI = new PeggleAlgorithm(POPULATION_SIZE, GENERATION_COUNT, SHOT_COUNT, levels, levelMutex);
Thread t = new Thread(peggleAI.main);
t.Start();
// Get the width and height of the screen
var vp = GraphicsDevice.Viewport;
cameraView = new Vector2(cameraViewWidth, cameraViewWidth / vp.AspectRatio);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// BasicEffect is used with the camera
spriteBatchEffect = new BasicEffect(GraphicsDevice);
spriteBatchEffect.TextureEnabled = true;
// Load sprites
background = Content.Load<Texture2D>("Level1");
infoFont = Content.Load<SpriteFont>("infoFont");
Texture2D ballTexture = Content.Load<Texture2D>("CircleSprite");
Texture2D arrowTexture = Content.Load<Texture2D>("Arrow");
Texture2D bluePegTexture = Content.Load<Texture2D>("BluePeg");
Texture2D orangePegTexture = Content.Load<Texture2D>("OrangePeg");
// Call loadContent to give each game object the textures they need
Peg.loadContent(bluePegTexture, orangePegTexture);
Ball.loadContent(ballTexture);
BallShooter.loadContent(arrowTexture);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
// Wait for Level Resetting to complete before stepping
levelMutex.WaitOne();
if (peggleAI.replayingSol)
{
levels[0].Update(gameTime);
}
else
{
foreach (LevelComponent level in levels)
{
// Just as a note, each level is still tracking input,
// so although the algorithm will manually set angles, pressing buttons will still mess with it.
level.Update(gameTime);
//System.Diagnostics.Debug.Write(level.ballShot);
}
}
levelMutex.ReleaseMutex();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// Update camera View and Projection
var vp = GraphicsDevice.Viewport;
spriteBatchEffect.View = Matrix.CreateLookAt(cameraPosition, cameraPosition + Vector3.Forward, Vector3.Up);
spriteBatchEffect.Projection = Matrix.CreateOrthographic(cameraViewWidth, cameraViewWidth / vp.AspectRatio, 0f, -1f);
// TODO: Add your drawing code here
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, RasterizerState.CullClockwise, spriteBatchEffect);
Vector2 bgPos = new Vector2(background.Width/2f, background.Height/2f);
spriteBatch.Draw(
background,
Vector2.Zero,
null,
Color.White,
0f,
bgPos,
0.0156f,
SpriteEffects.FlipVertically,
0f
);
levelMutex.WaitOne();
if (peggleAI.replayingSol)
{
levels[0].Draw(spriteBatch);
}
else
{
foreach (LevelComponent level in levels)
{
level.Draw(spriteBatch);
}
}
levelMutex.ReleaseMutex();
// Write out important information to the screen
writeString(spriteBatch, "Shot: " + (peggleAI.currentShot + 1), new Vector2(-6, 4));
writeString(spriteBatch, "Generation: " + (peggleAI.currentGen + 1), new Vector2(-6, 3.5f));
writeString(spriteBatch, "Previous Best: " + peggleAI.topScores[0] + ", " + peggleAI.topScores[1], new Vector2(-6, 3));
writeString(spriteBatch, "Parents: " + Math.Round(peggleAI.parents[0]) + ", " + Math.Round(peggleAI.parents[1]), new Vector2(-6, 2.5f));
spriteBatch.End();
base.Draw(gameTime);
}
private void writeString(SpriteBatch sb, string str, Vector2 pos)
{
sb.DrawString(
infoFont,
str,
pos,
Color.Black,
0f,
Vector2.Zero,
0.025f,
SpriteEffects.FlipVertically,
0f
);
}
}
}