Autor |
Beitrag |
Kracker
Beiträge: 19
|
Verfasst: Di 31.05.11 22:39
hallo, ich bin grade dabei Snake zu programmieren, und habe eine Zufallsvariable um zu bestimmen wo das Essen auftaucht. Aber es soll nicht irgendwo gespawnt werden, sondern nur in einem Raster von 10. jetzt weiß ich aber nicht wie das geht, kann man das iwie beim definieren festlegen, oder muss ich da improvisieren? wenn ja, wie?
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Di 31.05.11 23:18
Ich weiß nicht was du genau meinst bzw. wo das Problem ist.
Meinst du, dass der Zufallswert in einem bestimmten Bereich sein soll? Den Bereich kannst du ja direkt mit übergeben wie du in der Doku ja siehst:
msdn.microsoft.com/e...ibrary/zd1bc8e5.aspx
|
|
Kracker
Beiträge: 19
|
Verfasst: Di 31.05.11 23:25
nein, die max bzw. min hab ich schon...
meine Schlange geht immer 10 Pixel, dann wieder 10 Pixel, usw. also in einem Raster von 10. Die Variable Food generiert aber von 0 - X, dh. es kann auch 1, 2, 3 etc. sein
wie mach ich dass, dass die Variable nur 10, 20, 30 generiert wird?
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Di 31.05.11 23:36
Naja, überlege einmal was diese Variablen auszeichnet. Richtig: Sie sind durch 10 teilbar.
Gut, also generiere dir ganzzahlige Zufallsvariablen zwischen z.B. 1 und 10. Wenn du die jetzt mit 10 multiplizierst, hast du genau das gewünschte Ergebnis, nämlich Zufallszahlen zwischen 10 und 100 im 10er Raster.
|
|
Kracker
Beiträge: 19
|
Verfasst: Di 31.05.11 23:51
also so...
C#-Quelltext 1: 2: 3: 4: 5:
| int FoodX2 = (Z.Next(0, graphics.PreferredBackBufferWidth/10)); int FoodY2 = (Z.Next(0, graphics.PreferredBackBufferHeight/10));
FoodX.Add(Z.Next(0, FoodX2 *10)); FoodY.Add(Z.Next(0, FoodY2 *10)); |
hmm.. geht aber nicht, vl hab ich ja was anderes falsch gemacht, ich check das nicht.
Ich stell mal den ganzen Quelltext rauf:
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: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229:
| 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; using System.Collections;
namespace Snake { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch;
ArrayList elementeX = new ArrayList(); ArrayList elementeY = new ArrayList();
ArrayList FoodX = new ArrayList(); ArrayList FoodY = new ArrayList();
string direction = "r"; Random Z = new Random();
Texture2D body, head, food;
public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; }
protected override void Initialize() { graphics.PreferredBackBufferHeight = 540; graphics.PreferredBackBufferWidth = 540; graphics.ApplyChanges();
elementeX.Add(0); elementeY.Add(0);
elementeX.Add(10); elementeY.Add(0);
elementeX.Add(20); elementeY.Add(0);
int FoodX2 = (Z.Next(0, graphics.PreferredBackBufferWidth/10)); int FoodY2 = (Z.Next(0, graphics.PreferredBackBufferHeight/10));
FoodX.Add(Z.Next(0, FoodX2 *10)); FoodY.Add(Z.Next(0, FoodY2 *10));
base.Initialize(); }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice);
body = Content.Load<Texture2D>("body"); head = Content.Load<Texture2D>("head"); food = Content.Load<Texture2D>("food");
}
protected override void UnloadContent() { }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();
KeyboardState ks = Keyboard.GetState();
if (ks.IsKeyDown(Keys.Left)) { if (direction != "r") direction = "l"; }
if (ks.IsKeyDown(Keys.Right)) { if (direction != "l") direction = "r"; }
if (ks.IsKeyDown(Keys.Down)) { if (direction != "u") direction = "d"; }
if (ks.IsKeyDown(Keys.Up)) { if (direction != "d") direction = "u"; }
if (direction == "d") { elementeX.Insert(0, elementeX[0]); elementeX.RemoveAt(elementeX.Count - 1);
elementeY.Insert(0, Convert.ToInt16(elementeY[0]) + 10); elementeY.RemoveAt(elementeY.Count - 1); }
if (direction == "u") { elementeX.Insert(0, elementeX[0]); elementeX.RemoveAt(elementeX.Count - 1);
elementeY.Insert(0, Convert.ToInt16(elementeY[0]) - 10); elementeY.RemoveAt(elementeY.Count - 1); }
if (direction == "r") { elementeY.Insert(0, elementeY[0]); elementeY.RemoveAt(elementeY.Count - 1);
elementeX.Insert(0, Convert.ToInt16(elementeX[0]) + 10); elementeX.RemoveAt(elementeX.Count - 1); }
if (direction == "l") { elementeY.Insert(0, elementeY[0]); elementeY.RemoveAt(elementeY.Count - 1);
elementeX.Insert(0, Convert.ToInt16(elementeX[0]) - 10); elementeX.RemoveAt(elementeX.Count - 1); }
if (Convert.ToInt16(elementeX[0]) < 0) { elementeX[0] = graphics.PreferredBackBufferHeight; } if (Convert.ToInt16(elementeX[0]) > graphics.PreferredBackBufferHeight) { elementeX[0] = 0; }
if (Convert.ToInt16(elementeY[0]) < 0) { elementeY[0] = graphics.PreferredBackBufferHeight; } if (Convert.ToInt16(elementeY[0]) > graphics.PreferredBackBufferHeight) { elementeY[0] = 0; }
if (elementeX[0] == FoodX[0]) { FoodX.Remove(0); FoodY.Remove(0);
FoodX.Add(Z.Next(0, graphics.PreferredBackBufferWidth)); FoodY.Add(Z.Next(0, graphics.PreferredBackBufferHeight)); }
base.Update(gameTime); }
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
int i = 0; while (i < elementeX.Count) { spriteBatch.Draw(body, new Rectangle (Convert.ToInt16(elementeX[i]), Convert.ToInt16(elementeY[i]), 10, 10), Color.White); i++; } spriteBatch.Draw(head, new Rectangle (Convert.ToInt16(elementeX[0]), Convert.ToInt16(elementeY[0]), 10, 10), Color.White); i = 0; while (i < FoodX.Count) { spriteBatch.Draw(food, new Rectangle (Convert.ToInt16(FoodX[i]), Convert.ToInt16(FoodY[i]), 10, 10), Color.White); i++; }
spriteBatch.End();
base.Draw(gameTime); } } } |
|
|
Dr. Hallo
Beiträge: 110
Erhaltene Danke: 13
XP
C/C++/C#
|
Verfasst: Mi 01.06.11 00:49
wenn breite und höhe die maße deiner map in rastereinheiten dann berechenen sich
daraus x und y so...
C#-Quelltext 1: 2:
| x = zufallszahl % breite * rasterbreite; y = zufallszahl / breite * rasterhöhe; |
die zufallszahl muss zwischen 0 und rasteranzahl liegen...
wenn du also zb. ein 4 x 3 raster hast und ein rasterelement 10 x 10 ist...
die zufallszahl muss hier zwischen 0 und 4 x 3 = 12 - 1 = 11 generiert werden. sagen
wir wir genrieren 9...
C#-Quelltext 1: 2:
| x = 9 % 4 * 10; y = 9 / 4 * 10; |
|
|
Kracker
Beiträge: 19
|
Verfasst: Mi 01.06.11 00:57
Wieso nur die breite?
Und was bedeutet "%"?
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Mi 01.06.11 05:36
|
|
Kracker
Beiträge: 19
|
Verfasst: Mi 01.06.11 09:04
hmm.. ich kapier nciht wieso dass so gehen soll, habs aber versucht, und anscheinend falsch gemacht:
C#-Quelltext 1: 2:
| int FoodX2 = (Z.Next(Convert.ToInt16(Z) % Convert.ToInt16(graphics.PreferredBackBufferWidth), graphics.PreferredBackBufferWidth)); int FoodY2 = (Z.Next(Convert.ToInt16(Z) / Convert.ToInt16(graphics.PreferredBackBufferWidth), graphics.PreferredBackBufferHeight)); |
es kam der Fehler, dass er die Typen nicht einfach zu String konvertieren kann...
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Mi 01.06.11 09:47
Wo genau kommt denn der Fehler? In beiden Zeilen, in der ersten, ...?
|
|
Kracker
Beiträge: 19
|
Verfasst: Mi 01.06.11 09:59
in der ersten
Einloggen, um Attachments anzusehen!
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Mi 01.06.11 10:41
|
|
Kracker
Beiträge: 19
|
Verfasst: Mi 01.06.11 10:51
ja stimmt du hast eh recht, hab mich nur verschrieben... hab eh int gemeint..
aber wie lös ich das Problem jetzt?
|
|
thepaine91
Beiträge: 763
Erhaltene Danke: 27
Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
|
Verfasst: Mi 01.06.11 10:58
Indem du nicht versuchst das Objekt Z in int zu konvertieren, sondern anstelle von Z das was du eigentlich konvertieren wolltest dort konvertierst. Was auch immer das sein mag.
|
|
Kracker
Beiträge: 19
|
Verfasst: Mi 01.06.11 11:09
aber ich muss Z konvertieren weil es eine Random Variable ist, und ich mit der keine Rechenvorgänge durchführen kann
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Mi 01.06.11 11:11
Du willst eine durch das Objekt generierte Zufallszahl konvertieren, nicht das Objekt, oder?
|
|
Kracker
Beiträge: 19
|
Verfasst: Mi 01.06.11 11:20
was? sry, das kapier ich grad voll nicht.. Z ist doch die Zahl...
aber ich habs jetzt anders gelöst:
C#-Quelltext 1: 2:
| FoodX.Add(Z.Next(0, Convert.ToInt16(graphics.PreferredBackBufferWidth)/10)); FoodY.Add(Z.Next(0, Convert.ToInt16(graphics.PreferredBackBufferHeight) / 10)); |
und
C#-Quelltext 1:
| spriteBatch.Draw(food, new Rectangle (Convert.ToInt16(FoodX[i])*10, Convert.ToInt16(FoodY[i])*10, 10, 10), Color.White); |
|
|
thepaine91
Beiträge: 763
Erhaltene Danke: 27
Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
|
Verfasst: Mi 01.06.11 11:26
msdn.microsoft.com/d...y/system.random.aspx
Wenn du dir das anguckst verstehst du vielleicht das Problem.
Z ist keine Zahl!
Z ist ein Objekt mit Funktionen zum Erzeugen einer Zufallszahl wie z.B. Z.Next();
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Mi 01.06.11 11:35
Das ist genauso wie beim Metzger. Dem kannst du sagen, dass du gerne Hackfleisch hättest, dann steckt der Fleisch in den Wolf und gibt dir welches.
Hier kannst du mit Next dem Random Objekt sagen, dass du gerne eine Zufallszahl hättest und du bekommst eine.
Du jedoch versuchst das Objekt in eine Zahl umzuwandeln.
Das ist als ob du den Metzger in Hackfleisch umzuwandeln versuchst...
|
|
thepaine91
Beiträge: 763
Erhaltene Danke: 27
Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
|
Verfasst: Mi 01.06.11 12:50
jaenicke hat folgendes geschrieben: | Das ist als ob du den Metzger in Hackfleisch umzuwandeln versuchst... |
Naja das ist durchaus möglich, könnte allerdings eine Sauerei geben.
|
|
|