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 |
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D image, wasser; float Gravity = 10.0F; float JumpStrength = 40.0F; Vector2 Position = new Vector2(1200,620); Vector2 Velocity; float rotation = 0; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 720; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); image = Content.Load<Texture2D>("delphingrau"); wasser = Content.Load<Texture2D>("meerwasser"); } protected override void Update(GameTime gameTime) { float time = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardState kbState = Keyboard.GetState(); if(kbState.IsKeyDown(Keys.Left)) { Velocity.X -= 200.0F; } if(kbState.IsKeyDown(Keys.Right)) { Velocity.X += 200.0F; } // der Sprung darf nur ausgeführt werden wenn der Delphin ein kleines Stück unter Wasser ist if(Position.Y >= 670) { if (kbState.IsKeyDown(Keys.A)) Velocity.Y = -JumpStrength; } else { Velocity.Y += Gravity * time; } Position += Velocity * time; Velocity.X = 0; if (Position.Y + image.Height/2 > graphics.PreferredBackBufferHeight) Position.Y = graphics.PreferredBackBufferHeight - image.Height/2; rotation = (float)Math.Atan2(Velocity.Y, Velocity.X); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(wasser, new Rectangle(0, graphics.PreferredBackBufferHeight -100, graphics.PreferredBackBufferWidth, 100), Color.White); spriteBatch.Draw(image, Position, null, Color.White, rotation, new Vector2(image.Width/2, image.Height/2), 1, SpriteEffects.None, 1); 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 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 |
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D image, boden; float Gravity = 5.0F; float beschleunigung = 20.0F; Vector2 Position = new Vector2(1200,720); Vector2 Velocity; float rotation = 0; SpriteEffects flip; Vector2 Speed = new Vector2(0, 0); public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 720; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); image = Content.Load("delphingrau"); boden = Content.Load("background"); flip = SpriteEffects.None; } protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } protected override void Update(GameTime gameTime) { float VelocityX = 0f; float VelocityY = 0f; float time = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardState kbState = Keyboard.GetState(); if(kbState.IsKeyDown(Keys.Left)) { rotation = 0; flip = SpriteEffects.None; VelocityX += -5f; } if(kbState.IsKeyDown(Keys.Right)) { rotation = 0; flip = SpriteEffects.FlipHorizontally; VelocityX += 5f; } // der Sprung darf nur ausgeführt werden wenn der Delphin ein kleines Stück unter Wasser ist if(Position.Y >= 670) { if (kbState.IsKeyDown(Keys.A)) { if (flip == SpriteEffects.None) { rotation = 45; VelocityY += 40f; } else { rotation = -45; VelocityY += 40f; } } } else { VelocityY += -10f; } // Geschwindigkeitsänderungen float deltaY = 0; float deltaX = 0; // Gravitation wirken deltaY = Gravity * (float)gameTime.ElapsedGameTime.TotalSeconds; // Beschleunigung wirken lassen deltaX += VelocityX * (float)gameTime.ElapsedGameTime.TotalSeconds * beschleunigung; deltaY += -VelocityY * (float)gameTime.ElapsedGameTime.TotalSeconds * beschleunigung; // Geschwindigkeitsänderung auf den Delphin anwenden Speed = new Vector2(Speed.X + deltaX, Speed.Y + deltaY); // Neue Position des Delphins ermitteln Position += Speed * (float)gameTime.ElapsedGameTime.TotalSeconds; Velocity.X = 0; if (Position.Y + image.Height/2 > graphics.PreferredBackBufferHeight) Position.Y = graphics.PreferredBackBufferHeight - image.Height/2; base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(boden, new Rectangle(0, graphics.PreferredBackBufferHeight -100, graphics.PreferredBackBufferWidth, 100), Color.White); spriteBatch.Draw(image, Position, null, Color.White, MathHelper.ToRadians(rotation), newVector2(image.Width / 2, image.Height / 2), 1, flip, 1); spriteBatch.End(); base.Draw(gameTime); } } |
C-/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 |
a1 = 20;//Waagerecht a2 = 2000://Sprung a3 = 15;//Auftrieb if(kbState.IsKeyDown(Keys.Right)) { ... Speed.X += a1 * time; } if(Position.Y >= 670) { if (kbState.IsKeyDown(Keys.A)) { ... Speed.Y -= a2 * time; } } else { Speed.Y -= a3 * time; //Nicht ganz ok, weil Auftrieb an jeder Position unter Wasser wirkt } ... ... Speed.Y += Gravity * time;//Gavitation ist allgegenwärtig Position += Speed * time; |
Werbeanzeige