11.18 breakOut
초기화>
protected override void Initialize()
{
// Remember resolution
width = graphics.GraphicsDevice.Viewport.Width;
height = graphics.GraphicsDevice.Viewport.Height;
// Init all blocks, set positions and bounding boxes
for (int y = 0; y < NumOfRows; y++)
for (int x = 0; x < NumOfColumns; x++)
{
blockPositions[x, y] = new Vector2(
0.05f + 0.9f * x / (float)(NumOfColumns - 1),
0.066f + 0.5f * y / (float)(NumOfRows - 1) );
Vector3 pos = new Vector3(blockPositions[x, y], 0);
Vector3 blockSize = new Vector3(
GameBlockRect.X / 1024.0f, GameBlockRect.Y / 768, 0);
blockBoxes[x, y] = new BoundingBox(
pos - blockSize / 2, pos + blockSize / 2);
}
protected override void LoadContent()
{
// Load all our content
backgroundTexture = Content.Load<Texture2D>(“Background");
gameTexture = Content.Load<Texture2D>("BreakoutGame");
// Create all sprites
paddle = new SpriteHelper(gameTexture, GamePaddleRect);
ball = new SpriteHelper(gameTexture, GameBallRect);
block = new SpriteHelper(gameTexture, GameBlockRect);
youWon = new SpriteHelper(gameTexture, GameYouWonRect);
youLost = new SpriteHelper(gameTexture, GameYouLostRect);
background = new SpriteHelper(backgroundTexture, null);
base.LoadContent();
} // LoadContent()
protected override void UnloadContent(bool unloadAllContent)
{
Content.Unload();
SpriteHelper.Dispose();
base.UnloadContent();
} // UnloadContent()
void StartLevel()
{
// Randomize levels, but make it more harder each level
for (int y = 0; y < NumOfRows; y++) //레벨1 10%, 레벨2 20%, 레벨3 30%
for (int x = 0; x < NumOfColumns; x++)
blocks[x, y] = RandomHelper.GetRandomInt(10) < level + 1; //블럭ㅇ 1이 들어갈 확률을 높혀줌.
// Use the lower blocks only for later levels
if (level < 6) //하단 벽둘 한 줄 제거.
for (int x = 0; x < NumOfColumns; x++)
blocks[x, NumOfRows - 1] = false;
if (level < 4) //하단 벽둘 두 줄 제거.
for (int x = 0; x < NumOfColumns; x++)
blocks[x, NumOfRows - 2] = false;
if (level < 2) //하단 벽둘 세 줄 제거.
for (int x = 0; x < NumOfColumns; x++)
blocks[x, NumOfRows - 3] = false;
// Halt game
ballSpeedVector = Vector2.Zero;
// Wait until user presses space or A to start a level.
pressSpaceToStart = true;
// Update title
Window.Title =
"XnaBreakout - Level " + (level + 1) + " - Score " + Math.Max(0, score);
} // StartLevel
protected override void Draw(GameTime gameTime)
{
// Render background
background.Render();
SpriteHelper.DrawSprites(width, height);
// Render all game graphics
paddle.RenderCentered(paddlePosition, 0.95f); //Background,Paddle.Ball.block을 화면에 그린다.
ball.RenderCentered(ballPosition); // SpriteHelper 클래스 사용
// Render all blocks
for (int y = 0; y < NumOfRows; y++)
for (int x = 0; x < NumOfColumns; x++)
if (blocks[x, y]) block.RenderCentered(blockPositions[x, y]);
if (pressSpaceToStart && score >= 0)
{
if (lostGame)
youLost.RenderCentered(0.5f, 0.65f, 2);
else
youWon.RenderCentered(0.5f, 0.65f, 2);
} // if
// Draw all sprites on the screen
SpriteHelper.DrawSprites(width, height);
base.Draw(gameTime);
} // Draw(gameTime)
protected override void Update(GameTime gameTime)// Keyboard 체크 Paddle 좌,우로 조정 Space,A누르면 게임시작 충돌검사 Ball 위치 업데이트 Ball Lost ?All blocks are killed?
{
KeyboardState keyboard;
// The time since Update was called last
float elapsed =
(float)gameTime.ElapsedGameTime.TotalSeconds;
// Get keyboard and gamepad states
keyboard = Keyboard.GetState();
// Escape and back exit the game
if (keyboard.IsKeyDown(Keys.Escape) )
this.Exit();
// Move half way across the screen each second
float moveFactorPerSecond = 0.75f *
(float)gameTime.ElapsedRealTime.TotalMilliseconds / 1000.0f;
// Move left and right if we press the cursor or gamepad keys. //패들조정
if (keyboard.IsKeyDown(Keys.Right)) paddlePosition += moveFactorPerSecond;
if (keyboard.IsKeyDown(Keys.Left)) paddlePosition -= moveFactorPerSecond;
// Make sure paddle stay between 0 and 1 (offset 0.05f for paddle width)
if (paddlePosition < 0.05f) paddlePosition = 0.05f;
if (paddlePosition > 1 - 0.05f) paddlePosition = 1 - 0.05f;
// Game not started yet? Then put ball on paddle. //space를누르면 시작!
if (pressSpaceToStart)
{
ballPosition = new Vector2(paddlePosition, 0.95f - 0.035f);
// Handle space
if (keyboard.IsKeyDown(Keys.Space) )
{
StartNewBall();
} // if
} // if
else
{
// Check collisions
CheckBallCollisions(moveFactorPerSecond); // 충돌 검사!
// Update ball position and bounce off the borders
ballPosition += ballSpeedVector *
moveFactorPerSecond * BallSpeedMultiplicator; // 충돌 검사 후,공의 위치 업데이트
// Ball lost?
if (ballPosition.Y > 0.985f)
{
// Show lost message, reset is done above in StartNewBall!
lostGame = true; // Lost Game
pressSpaceToStart = true;
} // if
// Check if all blocks are killed and if we won this level
bool allBlocksKilled = true;
for (int y = 0; y < NumOfRows; y++)
for (int x = 0; x < NumOfColumns; x++)
if (blocks[x, y])
{
allBlocksKilled = false; // All blocks are killed?
break;
} // for for if
// We won, start next level
if (allBlocksKilled ==true) // Start new level
{
lostGame = false;
level++;
StartLevel();
} // if
} // else
base.Update(gameTime);
} // Update(gameTime)
#endregion