I have two problems with my code, check the code down below, so you understand which section i'm talking about!
Problem 1 - adding new levels:
I'm trying to add new block levels when "block = null", you know when all my blocks are destroyed by the ball. I want it to start a new level with a different block path. But my code "if (block.Count == 0)" doesn't work.
Problem 2 - adding abilities to random block:
I'm gonna add some abilities to random blocks, u know that drops down when you hit a random block. Like you get 2 balls or bigger paddle or slower ball speed. I know I need to create a new list and then add different blocks with different abilities. But I don't know how to write the code block that chooses a random block and adding a ability to that block. That drops down so I can take it with the paddle.
(Sorry about my bad english)
- poang = points
- linje = line
- liv = lives
- boll = ball
- poang = points
- blockröd = blockred
-
blockgrön = blockgreen
public class Game1 : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont spritefont; Texture2D linje_texture; Texture2D boll_texture; Texture2D blockröd_texture; Texture2D blockgrön_texture; Texture2D gameover_texture; Rectangle linje_rect; Rectangle boll_rect; Rectangle blockröd_rect; Rectangle blockgrön_rect; Rectangle gameover_rect; Vector2 linje_speed; Vector2 boll_speed; Random random; StreamReader sr; StreamWriter sw; int liv = 3; int poang = 0; int highscore; int raknare = 0; int sek = 0; List<Rectangle> block = new List<Rectangle>(); List<Rectangle> block2 = 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(); linje_speed.X = 6f; boll_speed.X = random.Next(-1, 1); boll_speed.Y = 7f; 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"); linje_texture = Content.Load<Texture2D>("Pics/linje"); boll_texture = Content.Load<Texture2D>("Pics/boll"); blockgrön_texture = Content.Load<Texture2D>("Pics/block-grön"); blockröd_texture = Content.Load<Texture2D>("Pics/block-röd"); gameover_texture = Content.Load<Texture2D>("Pics/GameOver"); linje_rect = new Rectangle((Window.ClientBounds.Width - linje_texture.Width) / 2, 580, linje_texture.Width, linje_texture.Height); boll_rect = new Rectangle((Window.ClientBounds.Width - boll_texture.Width) / 2, 556, boll_texture.Width, boll_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); block.Add(blockgrön_rect); block2.Add(blockröd_rect); for (int i = 1; i < 2; i++) { for (int g = 1; g < 13; g++) { block2.Add(new Rectangle((g * 63) - 60, (i * 40), blockröd_texture.Width, blockröd_texture.Height)); } } for (int i = 1; i < 2; i++) { for (int g = 1; g < 13; g++) { block.Add(new Rectangle((g * 63) - 60, (i * 20) + 40, blockgrön_texture.Width, blockgrön_texture.Height)); } } // TODO: use this.Content to load your game content here } protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); if (Start == true) { boll_rect.X += (int)boll_speed.X; boll_rect.Y += (int)boll_speed.Y; } if (Start == false) { boll_rect.X = linje_rect.X + ((linje_rect.Width / 2) - (boll_texture.Width / 2)); } if (boll_rect.X > Window.ClientBounds.Width - boll_texture.Width || boll_rect.X < 0) boll_speed.X *= -1; if (boll_rect.Y > Window.ClientBounds.Height - boll_texture.Height || boll_rect.Y < 0) boll_speed.Y *= -1; if (boll_rect.Y > Window.ClientBounds.Height - boll_texture.Height) { liv -= 1; Start = false; boll_rect.X = (Window.ClientBounds.Width - boll_texture.Width) / 2; boll_rect.Y = 556; linje_rect.X = (Window.ClientBounds.Width - linje_texture.Width) / 2; linje_rect.Y = 580; } KeyboardState ks = Keyboard.GetState(); if (ks.IsKeyDown(Keys.Left)) { linje_rect.X -= (int)linje_speed.X; holdingleft = true; } else if (ks.IsKeyDown(Keys.Right)) { linje_rect.X += (int)linje_speed.X; holdingright = true; } else if (ks.IsKeyDown(Keys.Space)) { Start = true; } else if (ks.Equals(new KeyboardState())) { resetballspeed = true; } if (linje_rect.X > Window.ClientBounds.Width - linje_rect.Width) linje_rect.X = (Window.ClientBounds.Width - linje_rect.Width); if (linje_rect.X < 0) linje_rect.X = 0; if (linje_rect.Intersects(boll_rect)) { boll_speed.Y *= -1; boll_rect.Y += (int)boll_speed.Y; if (holdingleft == true) { boll_speed.X -= 3; } else if (holdingright == true) { boll_speed.X += 3; } else if (resetballspeed == true) { boll_speed.X = 1; } } if (poang == highscore) { sw = new StreamWriter("highscore.txt"); sw.WriteLine(poang); sw.Close(); } for (int j = 1; j < block.Count; j++) { if (boll_rect.Intersects(block[j])) { boll_speed.Y *= -1; poang += 1; block.RemoveAt(j); if (poang > 9) { linje_rect.Width = 60; } if (poang > highscore) { highscore = poang; } } } for (int k = 1; k < block2.Count; k++) { if (boll_rect.Intersects(block2[k])) { boll_speed.Y *= -1; poang += 5; if (poang > highscore) { highscore = poang; } block2.RemoveAt(k); } } holdingleft = false; holdingright = false; raknare++; if (raknare == 60) { sek++; raknare = 0; } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); // TODO: Add your drawing code here spriteBatch.Begin(); if (liv > 0) { spriteBatch.Draw(boll_texture, boll_rect, Color.White); spriteBatch.Draw(linje_texture, linje_rect, Color.White); spriteBatch.DrawString(spritefont, "Lives left: " + liv, Vector2.Zero, Color.White); spriteBatch.DrawString(spritefont, "Points: " + poang, new Vector2(350, 0), Color.White); spriteBatch.DrawString(spritefont, "Timer: " + sek, new Vector2(350, 600), Color.White); spriteBatch.DrawString(spritefont, "Highscore: " + highscore, new Vector2(660, 0), Color.White); foreach (Rectangle g in block) { spriteBatch.Draw(blockgrön_texture, g, Color.White); } foreach (Rectangle t in block2) { spriteBatch.Draw(blockröd_texture, t, Color.White); } } else if ((block.Count == 0) && (block.Count == 0)) { gonna write out new blocks here } else if (liv == 0) { spriteBatch.Draw(gameover_texture, gameover_rect, Color.White); } spriteBatch.End(); base.Draw(gameTime); } }
Code blocks i'm having trouble with (problem 1):
else if ((block.Count == 0) && (block.Count == 0))
{
gonna write out new blocks here
}
Code blocks i'm having trouble with (problem 2):
for (int j = 1; j < block.Count; j++)
{
if (boll_rect.Intersects(block[j]))
{
boll_speed.Y *= -1;
poang += 1;
block.RemoveAt(j);
if (poang > 9)
{
linje_rect.Width = 60;
}
if (poang > highscore)
{
highscore = poang;
}
}
}
for (int k = 1; k < block2.Count; k++)
{
if (boll_rect.Intersects(block2[k]))
{
boll_speed.Y *= -1;
poang += 5;
if (poang > highscore)
{
highscore = poang;
}
block2.RemoveAt(k);
}
}
Aucun commentaire:
Enregistrer un commentaire