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 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
using System; using System.Collections.Generic; using System.Linq; 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 XNA_sidescroller_tests { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D playerTexture; Texture2D backgroundTexture; Texture2D blockTexture; Vector2 speed = Vector2.Zero; Vector2 direction = Vector2.Zero; Color[] playerTextureData; Color[] blockTextureData; public bool playerBlockCollide; //Other classes PlayerBlockCollision playerBlockCollision; // Vector2 blockPosition = new Vector2(100,100); //Movements int playerSpawnY = 400; int playerSpawnX = 400; int playerSpeed = 150; int playerMoveUp = -1; int playerMoveDown = 1; int playerMoveLeft = -1; int playerMoveRight = 1; // Vector2 playerPosition = new Vector2(400, 400); //the size of the sprite public Rectangle size; //the amount to increase/decrease the size of the original sprite private float mScale = 1.0f; public float scale { get { return mScale; } set { mScale = value; //Recalculate the Size of the Sprite with the new scale size = new Rectangle(0, 0, (int)(playerTexture.Width * scale), (int)(playerTexture.Height * scale)); } } 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() { 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); playerTexture = Content.Load<Texture2D>("circle"); backgroundTexture = Content.Load<Texture2D>("background"); blockTexture = Content.Load<Texture2D>("block"); blockTextureData = new Color[blockTexture.Width * blockTexture.Height]; blockTexture.GetData(blockTextureData); playerTextureData = new Color[playerTexture.Width * playerTexture.Height]; playerTexture.GetData(playerTextureData); } /// <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(); KeyboardState currentState = Keyboard.GetState(); UpdateMovement(currentState); playerPosition += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds; playerBlockCollision = new PlayerBlockCollision(); playerBlockCollision.Update(playerPosition, playerTexture, blockPosition, blockTexture, playerTextureData, blockTextureData); base.Update(gameTime); } private void UpdateMovement(KeyboardState theCurrentState) { speed = Vector2.Zero; direction = Vector2.Zero; //prüft welche Taste gedrückt wurde und bewegt in die Richtung if (theCurrentState.IsKeyDown(Keys.Right) == true) { speed.X = playerSpeed; direction.X = playerMoveRight; } if (theCurrentState.IsKeyDown(Keys.Left) == true) { speed.X = playerSpeed; direction.X = playerMoveLeft; } if (theCurrentState.IsKeyDown(Keys.Up) == true) { speed.Y = playerSpeed; direction.Y = playerMoveUp; } if (theCurrentState.IsKeyDown(Keys.Down) == true) { speed.Y = playerSpeed; direction.Y = playerMoveDown; } } /// <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) { spriteBatch.Begin(); if (playerBlockCollide) { playerPosition = new Vector2(300, 300); GraphicsDevice.Clear(Color.Red); //spriteBatch.Draw(backgroundTexture, new Vector2(0, 0), Color.Black); } else { GraphicsDevice.Clear(Color.CornflowerBlue); //spriteBatch.Draw(backgroundTexture, new Vector2(0, 0), Color.White); } spriteBatch.Draw(playerTexture, playerPosition, new Rectangle(0, 0, playerTexture.Width, playerTexture.Height), Color.Tomato, 0.0f, Vector2.Zero, scale, SpriteEffects.None, 0); spriteBatch.Draw(blockTexture, blockPosition, Color.White); 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 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 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; namespace XNA_sidescroller_tests { class PlayerBlockCollision { //Other classes Game1 game1; public void Update(Vector2 thePlayerPosition, Texture2D thePlayerTexture, Vector2 theBlockPosition, Texture2D theBlockTexture, Color[] thePlayerTextureData,Color[] theBlockTextureData) { Rectangle playerRectangle = new Rectangle((int)thePlayerPosition.X, (int)thePlayerPosition.Y, thePlayerTexture.Width, thePlayerTexture.Height); Rectangle blockRectangle = new Rectangle((int)theBlockPosition.X,(int)theBlockPosition.Y, theBlockTexture.Width, theBlockTexture.Height); //Check collision if(IntersectPixels(playerRectangle,thePlayerTextureData,blockRectangle,theBlockTextureData)) { game1 = new Game1(); game1.playerBlockCollide = true; } } static bool IntersectPixels(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, Color[] dataB) { // Find the bounds of the rectangle intersection int top = Math.Max(rectangleA.Top, rectangleB.Top); int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom); int left = Math.Max(rectangleA.Left, rectangleB.Left); int right = Math.Min(rectangleA.Right, rectangleB.Right); // Check every point within the intersection bounds for (int y = top; y < bottom; y++) { for (int x = left; x < right; x++) { // Get the color of both pixels at this point Color colorA = dataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width]; Color colorB = dataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width]; // If both pixels are not completely transparent, if (colorA.A != 0 && colorB.A != 0) { // then an intersection has been found return true; } } } // No intersection found return false; } } } |
Administrator
Damit kann man schon deutlich mehr anfangen, als mit deiner ersten Fehlerbeschreibung. Dann hättest du dir auch wie bereits gesagt wurde 90% des Codes sparen können.Nun gut nochmal eine genauere Beschreibung :
Nach erfolgreicher kollisionsabfrage wird die Variable zwar auf True gesetzt, aber trotzdem macht die draw Methode nicht das, was sie tun soll. Der Hintergrund bleibt Blau anstatt von rot.
Werbeanzeige