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
De_Struktor
unregistriert
C#-Quelltext |
|
1 2 3 4 |
if (player_rect.X <= winWidth - 70) { player_rect.X += 30; } |
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; namespace FrostBomber { public class Player : FrostBomber { private Texture2D player_Text; private Rectangle player_rect; private bool[] active = new bool[4]; /// Here begins the Constructor which initialize the right values /// for our object /// </summary> public Player(Texture2D text, Rectangle rect) { player_Text = text; player_rect = rect; } /// <summary> /// Here ends the initialization /// </summary> public void UpdatePlayer(GameTime gameTime) { if (Keyboard.GetState().IsKeyUp(Keys.W)) { active[0] = false; } if (Keyboard.GetState().IsKeyUp(Keys.A)) { active[1] = false; } if (Keyboard.GetState().IsKeyUp(Keys.S)) { active[2] = false; } if (Keyboard.GetState().IsKeyUp(Keys.D)) { active[3] = false; } if (Keyboard.GetState().IsKeyDown(Keys.W)) { if (!active[0]) { active[0] = true; if (player_rect.Y >= 30) { player_rect.Y -= 30; } } } else if (Keyboard.GetState().IsKeyDown(Keys.A)) { if (!active[1]) { active[1] = true; player_rect.X -= 30; } } else if (Keyboard.GetState().IsKeyDown(Keys.S)) { if (!active[2]) { active[2] = true; player_rect.Y += 30; } } else if (Keyboard.GetState().IsKeyDown(Keys.D)) { if (!active[3]) { active[3] = true; if (player_rect.X <= winWidth - 70) { player_rect.X += 30; } // Der entscheidende Codepfad ist hier, alles was bei den Tasten W-A-S-D passiert, passiert auch wie gewollt, nur das eben nicht foreach (Rectangle rect in Red_Block) { if (player_Rect.Intersects(rect)) { player_Rect.X = 30; } } } } } public void Draw(SpriteBatch spritebatch) { spritebatch.Draw(player_Text, player_rect, 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 58 59 60 61 62 63 64 65 66 67 68 69 70 |
public class FrostBomber : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; /// <summary> /// Datas for the Player /// </summary> protected Rectangle player_Rect; Player player; /// <summary> /// Datas for the map /// </summary> Map Block_map; protected Rectangle[] Red_Block = new Rectangle[216]; protected Rectangle[] Tree_rect = new Rectangle[225]; protected const int winWidth = 810; protected const int winHeight = 630; public FrostBomber() { 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() { graphics.PreferredBackBufferWidth = winWidth; graphics.PreferredBackBufferHeight = winHeight; IsMouseVisible = true; graphics.ApplyChanges(); player = new Player(Content.Load<Texture2D>("playerpic"), player_Rect = new Rectangle(30, 30, 30, 30)); Block_map = new Map(Content.Load<Texture2D>("Tree_Block"), Content.Load<Texture2D>("red_Block"), Red_Block, Tree_rect); base.Initialize(); } protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Block_map.LoadConstblocks(); Block_map.LoadBombableBlocks(); } protected override void UnloadContent() { base.UnloadContent(); } protected override void Update(GameTime gameTime) { player.UpdatePlayer(gameTime); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.LightGreen); spriteBatch.Begin(); /////////////////// Block_map.Draw(spriteBatch); player.Draw(spriteBatch); /////////////////// spriteBatch.End(); base.Draw(gameTime); } |
De_Struktor
unregistriert
De_Struktor
unregistriert
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private KeyboardState currentState; private KeyboardState lastState; // Das hier kommt in die Update Methode ganz an den Anfang: lastState = currentState; currentState = Keyboard.GetState(); // Hier wird jetzt ein Tastendruck abgefragt: if( currentState.IsKeyDown(Keys.D) && lastState.IsKeyUp(Keys.D)) { // Die Taste D wird in diesem Frame gedrückt, im letzten wurde sie es jedoch noch nicht. } |
C#-Quelltext |
|
1 2 3 4 |
public bool KeyHit(Keys key) { return currentState.IsKeyDown(key) && lastState.IsKeyUp(key); } |
De_Struktor
unregistriert
De_Struktor
unregistriert
De_Struktor
unregistriert
Werbeanzeige