Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; private IState currentState; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { currentState = new Intro(this); base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); currentState.Load(Content); } protected override void Update(GameTime gameTime) { currentState.Update(gameTime); KeyboardState kbState = Keyboard.GetState(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); currentState.Render(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } } |
C#-Quelltext |
|
1 2 3 4 5 |
public interface IState { void Load(ContentManager content); void Update(GameTime gametime); void Render(SpriteBatch batch); } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class Intro : IState { private IState currentState; Texture2D Titelbildschirm; private Game1 exit; public Intro(Game1 game) { exit = game; } public void Load(ContentManager content) { Titelbildschirm = content.Load<Texture2D>("gruft"); } public void Update(GameTime gametime) { KeyboardState kbState = Keyboard.GetState(); if (kbState.IsKeyDown(Keys.Space)) currentState = new Menu(exit); } public void Render(SpriteBatch batch) { batch.Draw(Titelbildschirm, new Rectangle(0, 0, 1280, 720), Color.White); } } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class Menu:IState { private IState currentState; Texture2D Auswahlbildschirm; private Game1 exit; public Menu(Game1 game) { exit = game; } public void Load(ContentManager content) { Auswahlbildschirm = content.Load<Texture2D>("menubild"); } public void Update(GameTime gametime) { KeyboardState kbState = Keyboard.GetState(); if (kbState.IsKeyDown(Keys.Enter)) currentState = new Hauptspiel(); if (kbState.IsKeyDown(Keys.Escape)) exit.Exit(); } public void Render(SpriteBatch batch) { batch.Draw(Auswahlbildschirm, new Rectangle(0, 0, 1280, 720), Color.White); } } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; private IState currentState; public enum GameStates { IntroState, MenuState, HauptspielState, } private GameStates currentGameState = GameStates.IntroState; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { currentState = new Intro(this); base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); currentState.Load(Content); } protected override void Update(GameTime gameTime) { currentState.Update(gameTime); KeyboardState kbState = Keyboard.GetState(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); currentState.Render(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class Intro : IState { private IState currentState; Texture2D Titelbildschirm; private Game1 game1; public Intro(Game1 game) { game1 = game; } public void Load(ContentManager content) { Titelbildschirm = content.Load<Texture2D>("gruft"); } public void Update(GameTime gametime) { KeyboardState kbState = Keyboard.GetState(); if (kbState.IsKeyDown(Keys.Space)) game1.GameStates = IntroState; currentState = new Menu(game1); } public void Render(SpriteBatch batch) { batch.Draw(Titelbildschirm, new Rectangle(0, 0, 1280, 720), Color.White); } } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; IState currentState; public enum GameStates { IntroState = 0, MenuState = 1, HauptspielState = 2, } public GameStates CurrentState { get { return currentGameState; } set { currentGameState = value; } } private GameStates currentGameState = GameStates.IntroState; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { currentState = new Intro(this); base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); currentState.Load(Content); } protected override void Update(GameTime gameTime) { currentState.Update(gameTime); KeyboardState kbState = Keyboard.GetState(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); currentState.Render(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class Intro : IState { private IState currentState; Texture2D Titelbildschirm; private Game1 game1; public Intro(Game1 game) { game1 = game; } public void Load(ContentManager content) { Titelbildschirm = content.Load<Texture2D>("gruft"); } public void Update(GameTime gametime) { KeyboardState kbState = Keyboard.GetState(); if (kbState.IsKeyDown(Keys.Space)) game1.CurrentState = Game1.GameStates.MenuState; currentState = new Menu(game1); } public void Render(SpriteBatch batch) { batch.Draw(Titelbildschirm, new Rectangle(0, 0, 1280, 720), Color.White); } } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void ChangeGameState(GameStates newState) { switch(newState) { case IntroState: currentState = new Intro(); break; case MenuState: currentState = new Menu(); break; case GameState: currentState = new Game(); break; } currentState.Load(); } |
C#-Quelltext |
|
1 |
currentState.Load(Content); |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; IState currentState; public enum GameStates { IntroState = 0, MenuState = 1, MaingameState = 2, QuitgameState = 3 } public void ChangeGameState(GameStates newState) { switch (newState) { case GameStates.IntroState: currentState = new Intro(this); break; case GameStates.MenuState: currentState = new Menu(this); break; case GameStates.MaingameState: currentState = new Maingame(this); break; case GameStates.QuitgameState: currentState = new Quit(this); break; } currentState.Load(Content); } public GameStates CurrentState { get { return currentGameState; } set { currentGameState = value; } } private GameStates currentGameState = GameStates.IntroState; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { ChangeGameState(GameStates.IntroState); base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); currentState.Load(Content); } protected override void Update(GameTime gameTime) { currentState.Update(gameTime); KeyboardState kbState = Keyboard.GetState(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); currentState.Render(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } } |
Wenn das Ingame-Menü im Vordergrund und das pausierte Hauptspiel im Hintergrund sein soll, dann brauche ich doch immer einen currentState und einen lastState oder nicht?Zu deinem Menü Problem. Da gibt es mehrere Möglichkeiten. Einerseits könnte man das über die Methode lösen, welche im Wiki beschrieben wird. Dort benutzt man einen StateManager, der die Zustände auf einem Stack ablegt. Du hast das Spiel auf dem Stack abgelegt, wenn nun das Ingamemenü aufgerufen wird, dann wird das Spiel "nicht" von Stack entfernt, das Ingamemenü aber einfach oben drauf geschoben. Wenn du jetzt von Ingamemenü wieder ins Spiel willst, wird das Ingamemenü einfach entfernt und das Spiel ist wieder oben. Andererseits könntest du auch einfach immer den Letzten Zustand speichern. Du hast dann also nicht nur currentState sondern auch lastState vom Typ IState. Wenn der Zustand gewechselt wird, dann muss natürlich vorher der alte Zustand neu gesetzt werden. Jetzt könntest du eine Funktion schreiben, die den Zustand auf den letzten Zustand zurück setzt. Die Version mit dem Stack ist natürlich noch etwas flexibler.
Werbeanzeige