Ich versuch mich gerade an nem einfachen Programm, das Punkte über den Bildschirm schieben soll (Im Stil von Krissels "Dots". ^^).
Allerdings werden keine Punkte gezeichnet, was mich stutzig macht. Könnt ihr mal über den Code gucken, ich seh den Fehler vor lauter Clusterfuck nicht.
Game1.cs
Entities\Dot.cs
dot.png:
Allerdings werden keine Punkte gezeichnet, was mich stutzig macht. Könnt ihr mal über den Code gucken, ich seh den Fehler vor lauter Clusterfuck nicht.
Game1.cs
Quellcode
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Diagnostics;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace MoreDots {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- List<Entities.Dot> dots;
- 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() {
- dots = new List<Entities.Dot>();
- 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);
- // TODO: use this.Content to load your game content here
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all 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) {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- // TODO: Add your update logic here
- if (dots.Count < 60) {
- dots.Add(new Entities.Dot(this));
- //Trace.WriteLine("Dot added!");
- }
- foreach (Entities.Dot d in dots) {
- d.Update(gameTime);
- }
- dots.RemoveAll(isMarked);
- //Trace.WriteLine(dots[0].Position);
- this.Window.Title = "More Dots (" + dots.Count.ToString() + " Dots geladen)";
- base.Update(gameTime);
- }
- private static bool isMarked(Entities.Dot d) {
- return d.marked;
- }
- /// <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.Black);
- // TODO: Add your drawing code here
- spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.Opaque);
- foreach (Entities.Dot d in dots) {
- d.Draw(gameTime);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Entities\Dot.cs
Quellcode
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace MoreDots.Entities {
- class Dot : Microsoft.Xna.Framework.DrawableGameComponent {
- Vector2 Velocity;
- Vector2 Position;
- float Scale;
- Texture2D Texture;
- Vector2 Origin;
- Game1 g1;
- public bool marked;
- public Dot(Game1 g) : base(g) {
- g1 = g;
- this.Texture = g1.Content.Load<Texture2D>("dot");
- Random r = new Random();
- //this.Position = new Vector2((float)r.NextDouble() * g1.Window.ClientBounds.Width, (float)r.NextDouble() * g1.Window.ClientBounds.Height);
- this.Position = new Vector2(300f, 100f); //Debugwert, sollte eigentlich mitten im Fenster auftauchen
- this.Origin = new Vector2(Texture.Width, Texture.Height);
- this.Scale = MathHelper.Clamp((float)r.NextDouble(), 0, .8f);
- }
- public override void Update(GameTime gameTime) {
- this.Velocity += new Vector2(1f,1f) * (float)gameTime.ElapsedGameTime.TotalSeconds;
- this.Position += this.Velocity;
- //marked = !g1.Window.ClientBounds.Contains((int)this.Position.X, (int)this.Position.Y);
- marked = Position.X < 0 || Position.Y > g1.Window.ClientBounds.Height;
- }
- public void Draw(SpriteBatch sb) {
- //sb.Draw(Texture, Position, Texture.Bounds, Color.FromNonPremultiplied(255,50,50,255), 0f, Origin, Scale, SpriteEffects.None, 0f);
- sb.Draw(Texture, Position, Texture.Bounds, Color.Red, 0f, Origin, Scale, SpriteEffects.None, 0f);
- }
- }
- }
dot.png: