Du bist nicht angemeldet.

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

11

14.05.2016, 23:22

ich hab das versucht aber das klappt noch nicht so ganz aber ich probier mal noch rum

12

14.05.2016, 23:40

ich hab das versucht aber das klappt noch nicht so ganz aber ich probier mal noch rum

Ich hab das versucht:

Quellcode

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
 private void screen_Paint(object sender, PaintEventArgs e)
        {
            IEnumerable<Control> boxCollection = myControls.Where(control => control.GetType() == typeof(PictureBox));
            bool isColliding = boxCollection.Any(PictureBox => player.Bounds.IntersectsWith(PictureBox.Bounds));
            if (isColliding)
            {
                collidedBox = boxCollection.FirstOrDefault(PictureBox => player.Bounds.IntersectsWith(PictureBox.Bounds));
                if (collidedBox != null)
                {
                    if (player.Bounds.IntersectsWith(collidedBox.Bounds))
                        
                    {
                        Console.WriteLine(collidedBox);
                        // TODO: Implement logic to set player on top of the box. (Stop falling)
                        collision = true;
                        jump = false;
                    }
                    else
                    {
                        collision = false;
                    }
                }
                else
                {
                    // do other things^^
                }
            }
           
        }

pl3x

Frischling

Beiträge: 14

Beruf: Fachinformatiker Anwendungsentwicklung

  • Private Nachricht senden

13

14.05.2016, 23:59

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private void screen_Paint(object sender, PaintEventArgs e)
        {
            IEnumerable<Control> boxCollection = myControls.Where(control => control.GetType() == typeof(Box));
            bool isColliding = boxCollection.Any(box => player.Bounds.IntersectsWith(box.Bounds));

            if (isColliding)
            {
                Control collidedBox = boxCollection.FirstOrDefault(box => player.Bounds.IntersectsWith(box.Bounds));

                if (collidedBox != null)
                {
                    // TODO: Implement logic to set player on top of the box. (Stop falling)
                    jump = false;
                    Console.WriteLine("Collision detected!");
                }
                else
                {
                    // NO COLLISION 
                    // do other things^^
                }
            }
        }

14

15.05.2016, 17:56

Bei mir klappt jz alles außer wenn man oben drauf läuft dann ist dei topcollision nicht aktiv und ich hab keine ahnung warum

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Dommi« (15.05.2016, 18:25)


15

15.05.2016, 18:26

Bei mir klappt jz alles außer wenn man oben drauf läuft dann ist dei topcollision nicht aktiv und ich hab keine ahnung warum

Hier is der Code:

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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Schulprojekt
{
    public partial class Ingame : Form
    {

        bool right;
        bool left;
        bool jump;
        bool collision;
        bool topcollision;
        bool sidecollision;
        bool botcollision;
        bool space;
        int G = 30;
        int Force;
        Control collidedBox= null;
        Control collidedspike = null;

        List<Control> boxlist = new List<Control>();

        List<Control> spikelist = new List<Control>();


        public Ingame()
        {
            InitializeComponent();
            //add boxes
            boxlist.Add(box);
            boxlist.Add(box2);

            //add spikes
            spikelist.Add(spike1);

        }

        private void Ingame_Load(object sender, EventArgs e)
        {

        }

        private void screen_Paint(object sender, PaintEventArgs e)
        {
            IEnumerable<Control> boxCollection = boxlist.Where(control => control.GetType() == typeof(PictureBox));
            bool isColliding = boxCollection.Any(PictureBox => player.Bounds.IntersectsWith(PictureBox.Bounds));
            if (isColliding)
            {
                collidedBox = boxCollection.FirstOrDefault(PictureBox => player.Bounds.IntersectsWith(PictureBox.Bounds));
                if (collidedBox != null)
                {
                    if (player.Bounds.IntersectsWith(collidedBox.Bounds))
                        
                    {
                        // TODO: Implement logic to set player on top of the box. (Stop falling)
                        collision = true;
                        jump = false;
                    }
                    else
                    {
                        collision = false;
                        topcollision = false;

                    }
                }
                else
                {
                    collision = false;
                    topcollision = false;
                }
            }
            else
            {
                collidedBox = null;
                topcollision = false;
            }

            IEnumerable<Control> spikeCollection = spikelist.Where(control => control.GetType() == typeof(PictureBox));
            bool isspikeColliding = spikeCollection.Any(PictureBox => player.Bounds.IntersectsWith(PictureBox.Bounds));
            if (isspikeColliding)
            {
                collidedspike = spikeCollection.FirstOrDefault(PictureBox => player.Bounds.IntersectsWith(PictureBox.Bounds));
                if (collidedspike != null)
                {
                    if (player.Bounds.IntersectsWith(collidedspike.Bounds))

                    {
                        // TODO: Implement logic to set player on top of the box. (Stop falling)
                        collision = true;
                        jump = false;
                    }
                    else
                    {
                        collision = false;
                        topcollision = false;

                    }
                }
                else
                {
                    collision = false;
                    topcollision = false;
                }
            }
            else
            {
                collidedspike = null;
                topcollision = false;
            }

        }

        private void Ingame_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
                if (e.KeyCode == Keys.D)
                {
                    right = true;
            }

                if (e.KeyCode == Keys.A)
                {
                    left = true;
            }
            
            

            if (e.KeyCode == Keys.Space && jump != true && sidecollision == false)
            {
                space = true;
                if (botcollision != true)
                {
                    topcollision = false;
                    collision = false;
                    jump = true;
                    Force = G;
                }
                
            }
        }

        private void Ingame_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.D)
            {
                right = false;
            }

            if (e.KeyCode == Keys.A)
            {
                left = false;
            }
            if (e.KeyCode == Keys.Space)
            {
                space = false;
            }
        }

        private void t1_Tick_1(object sender, EventArgs e)
        {
          /*Console.WriteLine(collision);
            Console.WriteLine(topcollision);
            Console.WriteLine(sidecollision);
            Console.WriteLine(player.Top);
            Console.WriteLine(box.Top+box.Height);
            Console.WriteLine(box.Bottom);*/
            Console.WriteLine(botcollision);

            //bot
            for (int i = 0; i < boxlist.Count; i++)
            {
                if (!(player.Top >= boxlist[i].Top + boxlist[i].Height && player.Left + player.Width - 1 >= boxlist[i].Left && player.Left + player.Width + 5 <= boxlist[i].Left + boxlist[i].Width + player.Width))
                {
                    botcollision = false;

                }
            }

            for (int i= 0; i<boxlist.Count;i++)
            {
                if (player.Top >= boxlist[i].Top + boxlist[i].Height && player.Left + player.Width - 1 >= boxlist[i].Left && player.Left + player.Width + 5 <= boxlist[i].Left + boxlist[i].Width + player.Width)
                {
                    topcollision = false;
                    sidecollision = false;
                    botcollision = true;
                    if (space == true)
                    {
                        player.Top = boxlist[i].Bottom;

                        player.Top -= Force;
                        Force -= 1;
                    }
                }
            }

            //collision box
            if (collidedBox != null)
            {
                // top
                if (collision == true&& botcollision == false && player.Left + player.Width - 1 >= collidedBox.Left && player.Left + player.Width + 5 <= collidedBox.Left + collidedBox.Width + player.Width && player.Top + player.Height >= collidedBox.Top && player.Top < collidedBox.Top)
                {
                    player.Top = collidedBox.Top - player.Height;
                    Force = 0;
                    topcollision = true;
                    sidecollision = false;
                    botcollision = false;
                }
                

                //side

                if (player.Right > collidedBox.Left && player.Left < collidedBox.Right - player.Width / 2 && player.Bottom > collidedBox.Top)
                {
                    right = false;
                    player.Top -= Force;
                    Force -= 1;
                    jump = false;
                    sidecollision = true;
                }

                if (player.Left < collidedBox.Right && player.Right > collidedBox.Left + player.Width / 2 && player.Bottom > collidedBox.Top)
                {
                    left = false;
                    player.Top -= Force;
                    Force -= 1;
                    jump = false;
                    sidecollision = true;
                }
            }

            //colission spike
            if (collidedspike != null)
            {
                if (collision == true)
                {
                    player.Top = Respawn.Bottom + player.Height;
                    player.Left = Respawn.Left;
                    left = false;
                    right = false;
                    MessageBox.Show("You Died");
                }
            }

            //walk right
            if (right == true)
            {
                player.Left += 5;
            }

            //walk left
            if (left == true)
            {
                player.Left -= 5;
            }

            //jump
            if (jump == true && botcollision == false)
            {
                    player.Top -= Force;
                    Force -= 1;
                

            }

            if (player.Top + player.Height >= screen.Height)
            {
                player.Top = screen.Height - player.Height;
                jump = false;
                collision = false;
                sidecollision = false;

            }
            else if(topcollision == false)
            {
                player.Top += 5;
            }
           

            
        }
    }
}

16

15.05.2016, 19:46

hab alles gefixt Problem war : beim laufen wurde die Kollision nicht erkannt weil der player 1 pixel zu hoch war

Werbeanzeige