vendredi 30 janvier 2015

How to random generate objects on the X axis?

I am busy with a project of my own and I am having trouble at a certain part. I want to have a random that generates my pictureboxes on a random X axis location. I'll send you a pic of the looks of my application and below that, some code.


I am very new to programming so "high class" and very smart solutions are still unknown for me. If awnsering this question. Be kind please :)


enter image description here


As you can see. All the Black bars are alligned to eachother and the Y and foremost the X axis stays the same. So the game is predictable.. it only has one opening that the black picturebox on the bottom need to go trough..


Here is the method that I made for the position of my "enemys/black bars" Btw I added a field on top that has "int position on 0"



public int VijandPositie() // methode om de positie van de vijand te bepalen
{
positie += 1;
return positie;
}


I implemented this method into this timer_tick event.



private void timer1_Tick(object sender, EventArgs e) // timer voor het te laten bewegen van de vijanden
{
Vijand1.Top = VijandPositie();
Vijand2.Top = VijandPositie();
Vijand3.Top = VijandPositie();
Vijand4.Top = VijandPositie();
Vijand5.Top = VijandPositie();
Vijand6.Top = VijandPositie();
Vijand7.Top = VijandPositie();
Vijand8.Top = VijandPositie();

if (Vijand1.Top >= gbSpeelVeld.Height || Vijand2.Top >= gbSpeelVeld.Height || Vijand3.Top >= gbSpeelVeld.Height || Vijand4.Top >= gbSpeelVeld.Height || Vijand5.Top >= gbSpeelVeld.Height || Vijand6.Top >= gbSpeelVeld.Height || Vijand7.Top >= gbSpeelVeld.Height || Vijand8.Top >= gbSpeelVeld.Height)
{
positie = 0; // zorgt ervoor dat de positie gereset wordt op 0 en dan begint de pijl weer van boven af aan
score = Convert.ToInt32(lblScore.Text); // score wordt geconverteerd naar de label
score = score + 1;
lblScore.Text = score.ToString();
}


after the black bars reach the end of the screen they will get back on top again but the X axis location stays the same. I know that I have not putted a random inside yet.. so I know that this will not work. But I don't know where or how to implement the random to get the blackbars randomly on the X axis (Y axis is less important IMO).


Thanks in advance guys!





Aucun commentaire:

Enregistrer un commentaire