Skip to content

Instantly share code, notes, and snippets.

@Lucas98
Created May 23, 2017 20:10
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/e57e73d6d8f8b386e6fbf73b1141d0a3 to your computer and use it in GitHub Desktop.
Save Lucas98/e57e73d6d8f8b386e6fbf73b1141d0a3 to your computer and use it in GitHub Desktop.
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 gameover_texture;
Texture2D pausemenu_texture;
Texture2D winner_texture;
Rectangle paddle_rect;
Rectangle ball_rect;
Rectangle gameover_rect;
Rectangle pausemenu_rect;
Rectangle abilityballs_rect;
Rectangle winner_rect;
Vector2 paddle_speed;
Vector2 ball_speed;
Vector2 abilityballs_speed;
Random random;
StreamReader sr;
StreamWriter sw;
int lives = 3;
int points = 0;
int highscore;
int counter = 0;
int seconds = 0;
int level = 2;
int powerup = 0;
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> balls = new List<Rectangle>();
bool Start = false;
bool paused = false;
bool DrawPowerup = false;
KeyboardState Previousks;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 544;
graphics.PreferredBackBufferHeight = 480;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected override void Initialize()
{
random = new Random();
paddle_speed.X = 6f;
ball_speed = new Vector2(1, -3);
abilityballs_speed.Y = 2f;
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/paddle");
ball_texture = Content.Load<Texture2D>("Pics/ball");
block_texture = Content.Load<Texture2D>("Pics/block");
gameover_texture = Content.Load<Texture2D>("Pics/GameOver");
abilityballs_texture = Content.Load<Texture2D>("Pics/AbilityBalls");
pausemenu_texture = Content.Load<Texture2D>("Pics/PauseMenu");
winner_texture = Content.Load<Texture2D>("Pics/Winner");
paddle_rect = new Rectangle((Window.ClientBounds.Width - paddle_texture.Width) / 2, 420, paddle_texture.Width, paddle_texture.Height + 12);
ball_rect = new Rectangle((Window.ClientBounds.Width - ball_texture.Width) / 2, 396, 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), gameover_texture.Width, gameover_texture.Height);
pausemenu_rect = new Rectangle((Window.ClientBounds.Width / 2) - (pausemenu_texture.Width / 2), ((Window.ClientBounds.Height / 2) - pausemenu_texture.Height / 2), pausemenu_texture.Width, pausemenu_texture.Height);
abilityballs_rect = new Rectangle((Window.ClientBounds.Width / 2) - (abilityballs_texture.Width / 2), ((Window.ClientBounds.Height / 2) - abilityballs_texture.Height / 2), abilityballs_texture.Width, abilityballs_texture.Height);
winner_rect = new Rectangle((Window.ClientBounds.Width / 2) - (winner_texture.Width / 2), ((Window.ClientBounds.Height / 2) - winner_texture.Height / 2), winner_texture.Width, winner_texture.Height);
AddBlocks();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected override void Update(GameTime gameTime)
{
KeyboardMouse();
BallPaddleCollisions();
PowerupPaddleCollisions();
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();
}
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(5, 0), Color.White);
spriteBatch.DrawString(spritefont, "Points: " + points, new Vector2(235, 0), Color.White);
spriteBatch.DrawString(spritefont, "Highscore: " + highscore, new Vector2(435, 0), Color.White);
spriteBatch.DrawString(spritefont, "Timer: " + seconds, new Vector2(240, 460), Color.White);
if (level == 1)
{
if (block.Count == 14 && block2.Count == 14)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(140, 260), 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);
}
foreach (Rectangle b in balls)
{
spriteBatch.Draw(ball_texture, b, Color.White);
}
if (powerup == 3 && DrawPowerup == false)
{
PowerUp();
spriteBatch.Draw(abilityballs_texture, abilityballs_rect, Color.White);
}
}
else if (level == 2)
{
if (block3.Count == 14 && block4.Count == 27)
{
powerup = 0;
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(136, 310), 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);
}
foreach (Rectangle b in balls)
{
spriteBatch.Draw(ball_texture, b, Color.White);
}
if (powerup == 3 && DrawPowerup == false)
{
PowerUp();
spriteBatch.Draw(abilityballs_texture, abilityballs_rect, Color.White);
}
}
else if (level == 3)
{
if (block5.Count == 30 && block6.Count == 18)
{
powerup = 0;
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(120, 290), 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);
}
foreach (Rectangle b in balls)
{
spriteBatch.Draw(ball_texture, b, Color.White);
}
if (powerup == 3 && DrawPowerup == false)
{
PowerUp();
spriteBatch.Draw(abilityballs_texture, abilityballs_rect, Color.White);
}
}
else if (block5.Count == 0 && block6.Count == 0)
{
spriteBatch.Draw(winner_texture, winner_rect, Color.White);
}
}
//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);
}
if (paused == true)
{
spriteBatch.Draw(pausemenu_texture, pausemenu_rect, Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ResetButton()
{
lives = 3;
points = 0;
level = 1;
counter = 0;
seconds = 0;
powerup = 0;
paddle_speed.X = 6f;
ball_speed = new Vector2(1, -3);
paddle_rect = new Rectangle((Window.ClientBounds.Width - paddle_texture.Width) / 2, 420, paddle_texture.Width, paddle_texture.Height + 12);
ball_rect = new Rectangle((Window.ClientBounds.Width - ball_texture.Width) / 2, 396, ball_texture.Width, ball_texture.Height);
abilityballs_rect = new Rectangle((Window.ClientBounds.Width / 2) - (abilityballs_texture.Width / 2), ((Window.ClientBounds.Height / 2) - abilityballs_texture.Height / 2), abilityballs_texture.Width, abilityballs_texture.Height);
Start = false;
paused = false;
DrawPowerup = false;
block.Clear();
block2.Clear();
block3.Clear();
block4.Clear();
block5.Clear();
block6.Clear();
AddBlocks();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void PowerUp()
{
abilityballs_rect.X += (int)abilityballs_speed.X;
abilityballs_rect.Y += (int)abilityballs_speed.Y;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void PowerupPaddleCollisions()
{
if (paddle_rect.Intersects(abilityballs_rect))
{
DrawPowerup = true;
balls.Add(new Rectangle(2, 160, ball_texture.Width, ball_texture.Height));
for (int b = 0; b < balls.Count; b++)
{
if (balls[b].Y > Window.ClientBounds.Height - ball_texture.Height)
{
balls.RemoveAt(b);
if (points > highscore)
{
highscore = points;
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AddBlocks()
{
//LEVEL 1
for (int i = 1; i < 3; i++)
{
for (int f = 1; f < 8; f++)
{
block.Add(new Rectangle((f * 63) - 9, (i * 40) + 70, 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) - 9, (i * 40) + 50, block_texture.Width, block_texture.Height));
}
}
//LEVEL 2
for (int i = 1; i < 3; i++)
{
for (int j = 1; j < 8; j++)
{
block3.Add(new Rectangle((j * 63) - 10, (i * 200) - 110, 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) + 36, (i * 20) + 90, block_texture.Width, block_texture.Height));
}
}
//LEVEL 3
for (int i = 1; i < 7; i++)
{
for (int j = 1; j < 6; j++)
{
block5.Add(new Rectangle((j * 63) + 54, (i * 20) + 150, 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 * 380) - 327, (i * 20) + 120, block_texture.Width, block_texture.Height));
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void StartValueBallPaddle()
{
//BOLLENS OCH PADDLES NYA VÄRDEN EFTER MAN DÖTT
paddle_rect = new Rectangle((Window.ClientBounds.Width - paddle_texture.Width) / 2, 420, paddle_texture.Width, paddle_texture.Height + 12);
ball_rect = new Rectangle((Window.ClientBounds.Width - ball_texture.Width) / 2, 396, ball_texture.Width, ball_texture.Height);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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;
if (powerup != 3)
{
powerup = random.Next(10);
}
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;
if (powerup != 3)
{
powerup = random.Next(10);
}
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;
if (powerup != 3)
{
powerup = random.Next(10);
}
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;
if (powerup != 3)
{
powerup = random.Next(10);
}
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;
if (powerup != 3)
{
powerup = random.Next(10);
}
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;
if (powerup != 3)
{
powerup = random.Next(10);
}
block6.RemoveAt(o);
if (points > highscore)
{
highscore = points;
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BallPosition()
{
//GÖR SÅ BOLLEN KAN ÅKA IVÄG
if (Start == true && paused == false)
{
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
else if (ball_rect.Y < 0)
ball_speed.Y *= -1;
//OM BOLLEN TRÄFFAR UNDRE KANT
else 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
else if (paddle_rect.X < 0)
{
paddle_rect.X = 0;
}
//OM BOLLEN TRÄFFAR PADDLE
if (paddle_rect.Intersects(ball_rect))
{
Rectangle BallLeft_rect = new Rectangle(ball_rect.X, ball_rect.Y, 3, ball_rect.Height);
Rectangle BallRight_rect = new Rectangle(ball_rect.X + ball_rect.Width - 3, ball_rect.Y, 3, ball_rect.Height);
Rectangle BallBottom_rect = new Rectangle(ball_rect.X, ball_rect.Y + ball_rect.Height - 3, ball_rect.Width, 3);
Rectangle PaddleLeft_rect = new Rectangle(paddle_rect.X, paddle_rect.Y, 2, paddle_rect.Height);
Rectangle Paddle1_rect = new Rectangle(PaddleLeft_rect.Right, paddle_rect.Y, (paddle_rect.Width - 4) / 4, paddle_rect.Height);
Rectangle Paddle2_rect = new Rectangle(Paddle1_rect.Right, paddle_rect.Y, (paddle_rect.Width - 4) / 4, paddle_rect.Height);
Rectangle Paddle3_rect = new Rectangle(Paddle2_rect.Right, paddle_rect.Y, (paddle_rect.Width - 4) / 4, paddle_rect.Height);
Rectangle Paddle4_rect = new Rectangle(Paddle3_rect.Right, paddle_rect.Y, (paddle_rect.Width - 4) / 4, paddle_rect.Height);
Rectangle PaddleRight_rect = new Rectangle(Paddle4_rect.Right, paddle_rect.Y, 2, paddle_rect.Height);
if (BallBottom_rect.Intersects(Paddle1_rect))
{
ball_speed.X = -2f;
ball_speed.Y = -2.5f;
}
else if (BallBottom_rect.Intersects(Paddle2_rect))
{
ball_speed.X = -1f;
ball_speed.Y = -2.2f;
}
else if (BallBottom_rect.Intersects(Paddle3_rect))
{
ball_speed.X = 1f;
ball_speed.Y = -2.2f;
}
else if (BallBottom_rect.Intersects(Paddle4_rect))
{
ball_speed.X = 2f;
ball_speed.Y = -2.5f;
}
else if (BallRight_rect.Intersects(PaddleLeft_rect))
{
ball_speed.X = -ball_speed.X;
ball_speed.Y *= -1;
}
else if (BallLeft_rect.Intersects(PaddleRight_rect))
{
ball_speed.X = -ball_speed.X;
ball_speed.Y *= -1;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Timer()
{
//TIMER SOM RÄKNAR HUR LÄNGE MAN SPELAT I SEKUNDER
if (paused == false)
{
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();
if (lives > 0)
{
if (ks.IsKeyDown(Keys.Left) && paused == false)
{
paddle_rect.X -= (int)paddle_speed.X;
}
else if (ks.IsKeyDown(Keys.Right) && paused == false)
{
paddle_rect.X += (int)paddle_speed.X;
}
else if (ks.IsKeyDown(Keys.P) && Previousks.IsKeyUp(Keys.P))
{
if (paused == false)
paused = true;
else if (paused == true)
{
paused = false;
paddle_speed.X = 6f;
}
}
//BOLLEN RÖR SIG OM MAN KLICKAR SPACE
else if (ks.IsKeyDown(Keys.Space))
{
Start = true;
}
Previousks = ks;
}
else if (lives == 0)
{
if (ks.IsKeyDown(Keys.R))
{
ResetButton();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment