Lösningsförslag till uppgift 8.26¶
Filer i detta program
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
namespace Uppgift_8._26
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D parrotBild;
List<Rectangle> parrotRects = new List<Rectangle>();
List<Vector2> parrotHastighet = new List<Vector2>();
Random slump = new Random();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 4; y++)
{
parrotRects.Add(new Rectangle(20 + 50 * x, 20 + 50 * y, 30, 30));
parrotHastighet.Add(new Vector2(slump.Next(-5, 6), slump.Next(-5, 6)));
}
}
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
parrotBild = Content.Load<Texture2D>("parrot");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
Rectangle tempRect;
Vector2 tempVect;
// Flytta alla parrots
for (int i = 0; i < parrotRects.Count; i++)
{
tempRect = parrotRects[i];
tempRect.X += (int)parrotHastighet[i].X;
tempRect.Y += (int)parrotHastighet[i].Y;
// Kolla om bilden krockar med fönstrets kanter
if (tempRect.X < 0 || tempRect.X > 800 - tempRect.Width)
{
tempVect = parrotHastighet[i];
tempVect.X *= -1;
parrotHastighet[i] = tempVect;
}
if (tempRect.Y < 0 || tempRect.Y > 480 - tempRect.Height)
{
tempVect = parrotHastighet[i];
tempVect.Y *= -1;
parrotHastighet[i] = tempVect;
}
parrotRects[i] = tempRect;
}
// Ta bort alla som krockar
for (int i = parrotRects.Count - 1; i >= 0; i--)
{
// Ibland kör loopen på index som redan har tagits bort, undersök detta först
if (i >= parrotRects.Count)
{
continue;
}
for (int j = i - 1; j >= 0; j--)
{
if (parrotRects[i].Intersects(parrotRects[j]))
{
parrotRects.RemoveAt(i);
parrotRects.RemoveAt(j);
break;
}
}
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (Rectangle rect in parrotRects)
{
spriteBatch.Draw(parrotBild, rect, Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}