Skip to content

Instantly share code, notes, and snippets.

@Lucas98
Last active April 22, 2017 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lucas98/a9da343b58239c36104b5fab92315d52 to your computer and use it in GitHub Desktop.
Save Lucas98/a9da343b58239c36104b5fab92315d52 to your computer and use it in GitHub Desktop.
Monogame: Breakout Game
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.IO;
namespace Spelprojekt
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont spritefont;
SpriteFont spritefont2;
Texture2D paddle_texture;
Texture2D ball_texture;
Texture2D block_texture;
Texture2D abilityballs_texture;
Texture2D abilitylong_texture;
Texture2D abilityslow_texture;
Texture2D gameover_texture;
Texture2D play_texture;
Texture2D quit_texture;
Rectangle paddle_rect;
Rectangle ball_rect;
Rectangle gameover_rect;
Rectangle play_rect;
Rectangle quit_rect;
Rectangle abilityballs_rect;
Rectangle abilitylong_rect;
Rectangle abilityslow_rect;
Vector2 paddle_speed;
Vector2 ball_speed;
Random random;
StreamReader sr;
StreamWriter sw;
int lives = 3;
int points = 0;
int highscore;
int counter = 0;
int seconds = 0;
int level = 1;
int type;
List<Rectangle> block = new List<Rectangle>();
List<Rectangle> block2 = new List<Rectangle>();
List<Rectangle> block3 = new List<Rectangle>();
List<Rectangle> block4 = new List<Rectangle>();
List<Rectangle> block5 = new List<Rectangle>();
List<Rectangle> block6 = new List<Rectangle>();
List<Rectangle> powerups = new List<Rectangle>();
bool Start = false;
bool holdingleft = false;
bool holdingright = false;
bool resetballspeed = false;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 760;
graphics.PreferredBackBufferHeight = 620;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected override void Initialize()
{
random = new Random();
paddle_speed.X = 6f;
ball_speed.Y = 7;
sr = new StreamReader("highscore.txt");
highscore = int.Parse(sr.ReadLine());
sr.Close();
base.Initialize();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
spritefont = Content.Load<SpriteFont>("Fonts/Myfont");
spritefont2 = Content.Load<SpriteFont>("Fonts/Myfont2");
paddle_texture = Content.Load<Texture2D>("Pics/linje");
ball_texture = Content.Load<Texture2D>("Pics/boll");
block_texture = Content.Load<Texture2D>("Pics/block");
gameover_texture = Content.Load<Texture2D>("Pics/GameOver");
play_texture = Content.Load<Texture2D>("Pics/play");
quit_texture = Content.Load<Texture2D>("Pics/quit");
abilityballs_texture = Content.Load<Texture2D>("Pics/AbilityBalls");
abilitylong_texture = Content.Load<Texture2D>("Pics/AbilityLong");
abilityslow_texture = Content.Load<Texture2D>("Pics/AbilitySlow");
paddle_rect = new Rectangle((Window.ClientBounds.Width - paddle_texture.Width) / 2, 580, paddle_texture.Width, paddle_texture.Height);
ball_rect = new Rectangle((Window.ClientBounds.Width - ball_texture.Width) / 2, 556, ball_texture.Width, ball_texture.Height);
gameover_rect = new Rectangle((Window.ClientBounds.Width / 2) - (gameover_texture.Width / 2), ((Window.ClientBounds.Height / 2) - gameover_texture.Height / 2) - 70, gameover_texture.Width, gameover_texture.Height);
quit_rect = new Rectangle(240, ((Window.ClientBounds.Height / 2) - quit_texture.Height / 2) + 150, quit_texture.Width, quit_texture.Height);
play_rect = new Rectangle(400, ((Window.ClientBounds.Height / 2) - quit_texture.Height / 2) + 150, quit_texture.Width, quit_texture.Height);
AddBlocks();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected override void Update(GameTime gameTime)
{
Menu();
KeyboardMouse();
BallPaddleCollisions();
DeleteBlocks();
Timer();
BallPosition();
//SKRIVER TILL HIGHSCORE.TXT OM POINTS ÄR MER ÄN HIGHSCORE
if (points == highscore)
{
sw = new StreamWriter("highscore.txt");
sw.WriteLine(points);
sw.Close();
}
if (lives == 0)
{
this.IsMouseVisible = true;
}
base.Update(gameTime);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
//SÅLÄNGE LIV ÄR STÖRRE ÄN 0 SÅ GÖRS DET HÄR
if (lives > 0)
{
spriteBatch.Draw(ball_texture, ball_rect, Color.White);
spriteBatch.Draw(paddle_texture, paddle_rect, Color.White);
spriteBatch.DrawString(spritefont, "Lives left: " + lives, new Vector2(2, 0), Color.White);
spriteBatch.DrawString(spritefont, "Points: " + points, new Vector2(350, 0), Color.White);
spriteBatch.DrawString(spritefont, "Highscore: " + highscore, new Vector2(652, 0), Color.White);
spriteBatch.DrawString(spritefont, "Timer: " + seconds, new Vector2(350, 600), Color.White);
if (level == 1)
{
if (block.Count == 14 && block2.Count == 14)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(252, 400), Color.White);
}
foreach (Rectangle g in block)
{
spriteBatch.Draw(block_texture, g, Color.LimeGreen);
}
foreach (Rectangle r in block2)
{
spriteBatch.Draw(block_texture, r, Color.IndianRed);
}
}
else if (level == 2)
{
if (block3.Count == 18 && block4.Count == 27)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(246, 400), Color.White);
}
foreach (Rectangle b in block3)
{
spriteBatch.Draw(block_texture, b, Color.CornflowerBlue);
}
foreach (Rectangle y in block4)
{
spriteBatch.Draw(block_texture, y, Color.Yellow);
}
}
else if (level == 3)
{
if (block5.Count == 36 && block6.Count == 18)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(246, 400), Color.White);
}
foreach (Rectangle o in block5)
{
spriteBatch.Draw(block_texture, o, Color.Orange);
}
foreach (Rectangle p in block6)
{
spriteBatch.Draw(block_texture, p, Color.HotPink);
}
}
}
//OM LIV ÄR 0 TAS ALLT ANNAT BORT OCH GAMEOVER BILDEN KOMMER FRAM
else if (lives == 0)
{
spriteBatch.Draw(gameover_texture, gameover_rect, Color.White);
spriteBatch.Draw(quit_texture, quit_rect, Color.White);
spriteBatch.Draw(play_texture, play_rect, Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Menu()
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ResetButton()
{
lives = 3;
points = 0;
level = 1;
counter = 0;
seconds = 0;
ball_speed.Y = 7;
paddle_rect = new Rectangle((Window.ClientBounds.Width - paddle_texture.Width) / 2, 580, paddle_texture.Width, paddle_texture.Height);
ball_rect = new Rectangle((Window.ClientBounds.Width - ball_texture.Width) / 2, 556, ball_texture.Width, ball_texture.Height);
Start = false;
holdingleft = false;
holdingright = false;
resetballspeed = false;
block.Clear();
block2.Clear();
block3.Clear();
block4.Clear();
block5.Clear();
block6.Clear();
powerups.Clear();
AddBlocks();
PowerUp();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Pause()
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void PowerUp()
{
//powerups.Add(abilityballs_rect);
//powerups.Add(abilitylong_rect);
//powerups.Add(abilityslow_rect);
//for (int p = 0; p < powerups.Count; p++)
//{
//random.Next(powerups[p]);
//}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AddBlocks()
{
//LEVEL 1
for (int i = 1; i < 3; i++)
{
for (int f = 1; f < 8; f++)
{
block.Add(new Rectangle((f * 63) + 94, (i * 40) + 60, block_texture.Width, block_texture.Height));
}
}
for (int i = 1; i < 3; i++)
{
for (int g = 1; g < 8; g++)
{
block2.Add(new Rectangle((g * 63) + 94, (i * 40) + 40, block_texture.Width, block_texture.Height));
}
}
//LEVEL 2
for (int i = 1; i < 3; i++)
{
for (int j = 1; j < 10; j++)
{
block3.Add(new Rectangle((j * 63) + 34, (i * 200) - 60, block_texture.Width, block_texture.Height));
}
}
for (int i = 1; i < 10; i++)
{
for (int k = 1; k < 4; k++)
{
block4.Add(new Rectangle((k * 103) + 143, (i * 20) + 140, block_texture.Width, block_texture.Height));
}
}
//LEVEL 3
for (int i = 1; i < 7; i++)
{
for (int j = 1; j < 7; j++)
{
block5.Add(new Rectangle((j * 63) + 127, (i * 20) + 190, block_texture.Width, block_texture.Height));
}
}
for (int i = 1; i < 10; i++)
{
for (int k = 1; k < 3; k++)
{
block6.Add(new Rectangle((k * 443) - 317, (i * 20) + 160, block_texture.Width, block_texture.Height));
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void StartValueBallPaddle()
{
//BOLLENS OCH PADDLES NYA VÄRDEN EFTER MAN DÖTT
ball_rect.X = (Window.ClientBounds.Width - ball_texture.Width) / 2;
ball_rect.Y = 556;
paddle_rect.X = (Window.ClientBounds.Width - paddle_texture.Width) / 2;
paddle_rect.Y = 580;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DeleteBlocks()
{
//KOLLAR IGENOM ALLA BLOCK OCH TAR BORT DET BLOCK SOM TRÄFFAS AV BOLLEN
if (level == 1)
{
for (int j = 0; j < block.Count; j++)
{
if (ball_rect.Intersects(block[j]))
{
ball_speed.Y *= -1;
points += 1;
block.RemoveAt(j);
if (points > highscore)
{
highscore = points;
}
}
}
for (int k = 0; k < block2.Count; k++)
{
if (ball_rect.Intersects(block2[k]))
{
ball_speed.Y *= -1;
points += 1;
block2.RemoveAt(k);
if (points > highscore)
{
highscore = points;
}
}
}
if (block.Count == 0 && block2.Count == 0)
{
level++;
StartValueBallPaddle();
Start = false;
}
}
else if (level == 2)
{
for (int l = 0; l < block3.Count; l++)
{
if (ball_rect.Intersects(block3[l]))
{
ball_speed.Y *= -1;
points += 1;
block3.RemoveAt(l);
if (points > highscore)
{
highscore = points;
}
}
}
for (int m = 0; m < block4.Count; m++)
{
if (ball_rect.Intersects(block4[m]))
{
ball_speed.Y *= -1;
points += 1;
block4.RemoveAt(m);
if (points > highscore)
{
highscore = points;
}
}
}
if (block3.Count == 0 && block4.Count == 0)
{
level++;
StartValueBallPaddle();
Start = false;
}
}
else if (level == 3)
{
for (int n = 0; n < block5.Count; n++)
{
if (ball_rect.Intersects(block5[n]))
{
ball_speed.Y *= -1;
points += 1;
block5.RemoveAt(n);
if (points > highscore)
{
highscore = points;
}
}
}
for (int o = 0; o < block6.Count; o++)
{
if (ball_rect.Intersects(block6[o]))
{
ball_speed.Y *= -1;
points += 1;
block6.RemoveAt(o);
if (points > highscore)
{
highscore = points;
}
}
}
}
if (block5.Count == 0 && block6.Count == 0)
{
//visa en vinnarbild
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BallPosition()
{
//GÖR SÅ BOLLEN KAN ÅKA IVÄG
if (Start == true)
{
ball_rect.X += (int)ball_speed.X;
ball_rect.Y += (int)ball_speed.Y;
}
//UTRÄKNING PÅ HUR BOLLENS X-VÄRDE SKA FÖRHÅLLA SIG TILL PADDLES X-VÄRDE
if (Start == false)
{
ball_rect.X = paddle_rect.X + ((paddle_rect.Width / 2) - (ball_texture.Width / 2));
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BallPaddleCollisions()
{
//OM BOLLEN TRÄFFAR HÖGER/VÄNSTER KANT
if (ball_rect.X > Window.ClientBounds.Width - ball_texture.Width || ball_rect.X < 0)
ball_speed.X *= -1;
//OM BOLLEN TRÄFFAR ÖVRE KANT
if (ball_rect.Y > Window.ClientBounds.Height - ball_texture.Height || ball_rect.Y < 0)
ball_speed.Y *= -1;
//OM BOLLEN TRÄFFAR UNDRE KANT
if (ball_rect.Y > Window.ClientBounds.Height - ball_texture.Height)
{
lives -= 1;
Start = false;
StartValueBallPaddle();
}
//OM PADDLE TRÄFFAR HÖGER KANT, SÅ STANNAR DEN
if (paddle_rect.X > Window.ClientBounds.Width - paddle_rect.Width)
paddle_rect.X = (Window.ClientBounds.Width - paddle_rect.Width);
//OM PADDLE TRÄFFAR VÄNSTER KANT, SÅ STANNAR DEN
if (paddle_rect.X < 0)
paddle_rect.X = 0;
//OM BOLLEN TRÄFFAR PADDLE
if (paddle_rect.Intersects(ball_rect))
{
ball_speed.Y *= -1;
//FÖRHÅLLANDE OM MAN HÅLLER HÖGER/VÄNSTER ELLER INTE HÅLLER NÅGON HÖGER/VÄNSTER
if (holdingleft == true)
{
ball_speed.X -= 3;
}
else if (holdingright == true)
{
ball_speed.X += 3;
}
else if (resetballspeed == true)
{
ball_speed.X *= -1;
}
}
holdingleft = false;
holdingright = false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Timer()
{
//TIMER SOM RÄKNAR HUR LÄNGE MAN SPELAT I SEKUNDER
counter++;
if (counter == 60)
{
seconds++;
counter = 0;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void KeyboardMouse()
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
{
Exit();
}
KeyboardState ks = Keyboard.GetState();
MouseState mouse = Mouse.GetState();
if (ks.IsKeyDown(Keys.Left))
{
paddle_rect.X -= (int)paddle_speed.X;
holdingleft = true;
}
else if (ks.IsKeyDown(Keys.Right))
{
paddle_rect.X += (int)paddle_speed.X;
holdingright = true;
}
else if (ks.Equals(new KeyboardState()))
{
resetballspeed = true;
}
//BOLLEN RÖR SIG OM MAN KLICKAR SPACE
else if (ks.IsKeyDown(Keys.Space))
{
Start = true;
}
else if (ks.IsKeyDown(Keys.R))
{
ResetButton();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment