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 |
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; World world; Body rectangle, rectangle2, rectangle3, rectangle4, rectangle5, playerrect, ladderrectangle; Texture2D rectangleSprite, playerSprite, ladderSprite; private static float _displayUnitsToSimUnitsRatio = 100f; public static Vector2 ToDisplayUnits(Vector2 simUnits) { return simUnits * _displayUnitsToSimUnitsRatio; } private bool isOnGround; bool Player_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact) { if (fixtureB.CollisionCategories == Category.Cat5) { isOnGround = true; } return true; } void Player_OnSeparation(Fixture fixtureA, Fixture fixtureB) { if (fixtureB.CollisionCategories == Category.Cat5) { isOnGround = false; } } public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); playerSprite = Content.Load<Texture2D>("retromario"); rectangleSprite = Content.Load<Texture2D>("retrotile"); ladderSprite = Content.Load<Texture2D>("retroladder"); if (world == null) { world = new World(new Vector2(0, 2)); } else { world.Clear(); } playerrect = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f); playerrect.BodyType = BodyType.Dynamic; playerrect.Position = new Vector2(0.80f, 0f); playerrect.FixedRotation = true; rectangle = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f); rectangle.BodyType = BodyType.Static; rectangle.Position = new Vector2(1.60f, 4.16f); rectangle.CollisionCategories = Category.Cat5; rectangle2 = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f); rectangle2.BodyType = BodyType.Static; rectangle2.Position = new Vector2(0.32f, 4.80f); rectangle2.CollisionCategories = Category.Cat5; rectangle3 = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f); rectangle3.BodyType = BodyType.Static; rectangle3.Position = new Vector2(0.96f, 4.80f); rectangle3.CollisionCategories = Category.Cat5; rectangle4 = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f); rectangle4.BodyType = BodyType.Static; rectangle4.Position = new Vector2(1.60f, 2.36f); rectangle4.CollisionCategories = Category.Cat5; rectangle5 = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f); rectangle5.BodyType = BodyType.Static; rectangle5.Position = new Vector2(1.60f, 4.80f); rectangle5.CollisionCategories = Category.Cat5; ladderrectangle = BodyFactory.CreateRectangle(world, 0.40f, 1.80f, 1.0f); ladderrectangle.BodyType = BodyType.Static; ladderrectangle.Position = new Vector2(1.60f, 2.94f); playerrect.OnCollision += Player_OnCollision; playerrect.OnSeparation += Player_OnSeparation; } protected override void Update(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; world.Step(Math.Min(elapsed, (1f / 60f))); KeyboardState kbState = Keyboard.GetState(); // Move the player if (kbState.IsKeyDown(Keys.Left)) { playerrect.ApplyForce(new Vector2(-0.5f, 0)); } if (kbState.IsKeyDown(Keys.Right)) { playerrect.ApplyForce(new Vector2(0.5f, 0)); } if (isOnGround == true) { if (kbState.IsKeyDown(Keys.Space)) { playerrect.ApplyForce(new Vector2(0, -24.0f)); } } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(playerSprite, ConvertUnits.ToDisplayUnits(playerrect.Position), null, Color.White, playerrect.Rotation, new Vector2(playerSprite.Width / 2.0f, playerSprite.Height / 2.0f), 1f, SpriteEffects.None, 0f); spriteBatch.Draw(rectangleSprite, ConvertUnits.ToDisplayUnits(rectangle.Position), null, Color.White, rectangle.Rotation, new Vector2(rectangleSprite.Width / 2.0f, rectangleSprite.Height / 2.0f), 1f, SpriteEffects.None, 0f); spriteBatch.Draw(rectangleSprite, ConvertUnits.ToDisplayUnits(rectangle2.Position), null, Color.White, rectangle2.Rotation, new Vector2(rectangleSprite.Width / 2.0f, rectangleSprite.Height / 2.0f), 1f, SpriteEffects.None, 0f); spriteBatch.Draw(rectangleSprite, ConvertUnits.ToDisplayUnits(rectangle3.Position), null, Color.White, rectangle3.Rotation, new Vector2(rectangleSprite.Width / 2.0f, rectangleSprite.Height / 2.0f), 1f, SpriteEffects.None, 0f); spriteBatch.Draw(rectangleSprite, ConvertUnits.ToDisplayUnits(rectangle4.Position), null, Color.White, rectangle4.Rotation, new Vector2(rectangleSprite.Width / 2.0f, rectangleSprite.Height / 2.0f), 1f, SpriteEffects.None, 0f); spriteBatch.Draw(rectangleSprite, ConvertUnits.ToDisplayUnits(rectangle5.Position), null, Color.White, rectangle5.Rotation, new Vector2(rectangleSprite.Width / 2.0f, rectangleSprite.Height / 2.0f), 1f, SpriteEffects.None, 0f); spriteBatch.Draw(ladderSprite, ConvertUnits.ToDisplayUnits(ladderrectangle.Position), null, Color.White, ladderrectangle.Rotation, new Vector2(ladderSprite.Width / 2.0f, ladderSprite.Height / 2.0f), 1f, SpriteEffects.None, 0f); spriteBatch.End(); base.Draw(gameTime); } } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Werbeanzeige