Autor Beitrag
Kracker
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 31.05.11 23:18 
Ich weiß nicht was du genau meinst bzw. wo das Problem ist. :gruebel:

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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: 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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 31.05.11 23:51 
also so...

ausblenden 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:

ausblenden volle Höhe 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:
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
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    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";
        }

        /// <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()
        {
            // TODO: Add your initialization logic here

            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();
        }

        /// <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);

            body = Content.Load<Texture2D>("body");
            head = Content.Load<Texture2D>("head");
            food = Content.Load<Texture2D>("food");

            // TODO: use this.Content to load your game content here
        }

        /// <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();

            // TODO: Add your update logic here

            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);
        }

        



        /// <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)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            spriteBatch.Begin();


            int i = 0;
            while (i < elementeX.Count)
            {
                spriteBatch.Draw(body, new Rectangle (Convert.ToInt16(elementeX[i]), Convert.ToInt16(elementeY[i]), 1010), Color.White);
                i++;
            }
            spriteBatch.Draw(head, new Rectangle (Convert.ToInt16(elementeX[0]), Convert.ToInt16(elementeY[0]), 1010), Color.White);
            
            i = 0;
            while (i < FoodX.Count)
            {
                spriteBatch.Draw(food, new Rectangle (Convert.ToInt16(FoodX[i]), Convert.ToInt16(FoodY[i]), 1010), Color.White);
                i++;
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
Dr. Hallo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110
Erhaltene Danke: 13

XP
C/C++/C#
BeitragVerfasst: 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...

ausblenden C#-Quelltext
1:
2:
x = zufallszahl % breite * rasterbreite;                //x in pixel
y = zufallszahl / breite/*nicht höhe!*/ * rasterhöhe;     //y in pixel


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...
ausblenden C#-Quelltext
1:
2:
x = 9 % 4 * 10//x=10 
y = 9 / 4 * 10//y=20
Kracker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 01.06.11 00:57 
Wieso nur die breite?

Und was bedeutet "%"?
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 01.06.11 05:36 
user profile iconKracker hat folgendes geschrieben Zum zitierten Posting springen:
Wieso nur die breite?
Nicht nur die Breite, aber für die Berechnung brauchst du die. Die Idee hinter dieser Berechnung ist, dass du aus einer einzigen Zufallszahl die Zeile und Spalte berechnest.

user profile iconKracker hat folgendes geschrieben Zum zitierten Posting springen:
Und was bedeutet "%"?
Siehe Doku... Das ist der modulo Operator:
msdn.microsoft.com/d...ibrary/0w4e0fzs.aspx
Kracker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 01.06.11 09:04 
hmm.. ich kapier nciht wieso dass so gehen soll, habs aber versucht, und anscheinend falsch gemacht:

ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 01.06.11 09:47 
Wo genau kommt denn der Fehler? In beiden Zeilen, in der ersten, ...?
Kracker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 01.06.11 09:59 
in der ersten
Einloggen, um Attachments anzusehen!
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 01.06.11 10:41 
Und was hat die Fehlermeldung im Bild jetzt mit deiner Aussage zu tun? :autsch:
user profile iconKracker hat folgendes geschrieben Zum zitierten Posting springen:
es kam der Fehler, dass er die Typen nicht einfach zu String konvertieren kann...
Da steht doch ganz klar, dass er das Random-Objekt nicht konvertieren kann. Logischerweise...
ausblenden C#-Quelltext
1:
2:
Convert.ToInt16(Z)
// da versuchst du dein Objekt Z in eine Zahl umzuwandeln...
Kracker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 763
Erhaltene Danke: 27

Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
BeitragVerfasst: 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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 01.06.11 11:11 
Du willst eine durch das Objekt generierte Zufallszahl konvertieren, nicht das Objekt, oder? :roll:
Kracker Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: 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:
ausblenden 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
ausblenden C#-Quelltext
1:
spriteBatch.Draw(food, new Rectangle (Convert.ToInt16(FoodX[i])*10, Convert.ToInt16(FoodY[i])*101010), Color.White);					
thepaine91
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 763
Erhaltene Danke: 27

Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 763
Erhaltene Danke: 27

Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
BeitragVerfasst: Mi 01.06.11 12:50 
user profile iconjaenicke 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. :mrgreen: