Jump to content

  • Log In with Google      Sign In   
  • Create Account


Grand strategy space empire game with python


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Ironmaggot   Members   -  Reputation: 152

Like
2Likes
Like

Posted 19 October 2013 - 01:03 PM

I am currently creating a  grand strategy space empire game as my python course project. I am using Pygame library and all was going suprisingly good until...

 

...until I wanted to generate a random star field.

 

Using the usual randint(a,b) occationally results in some stars being too tight clusters.

 

Here is an attachment with a picture of the problem.

 

Untitasdfled.png

 

I have managed to come up with something like this, but this is still not enough, because some stars still bleed into each other.

for i in range(200):
        x = random.randint(17, 776)
        y = random.randint(17, 471)

        while x in x_used == True or y in y_used == True:
            x = random.randint(17, 776)
            y = random.randint(17, 471)
        
        pygame.draw.circle(DISPLAYSURF, WHITE, (x, y), 2, 0)
        pygame.draw.circle(DISPLAYSURF, WHITE, (x, y), 5, 1)

        counter = 0

        while counter < 19:
            x_used.append((x-9) + counter)
            y_used.append((y-9) + counter)
            counter += 1

The "200" in the range command is the number of stars I want to generate. 

 

So, my question is: how can I generate random coordinates, so that each point generated has an area around it, into which other points cannot be generated?


Edited by Ironmaggot, 19 October 2013 - 02:33 PM.


Sponsor:

#2 blewisjr   Members   -  Reputation: 620

Like
0Likes
Like

Posted 19 October 2013 - 03:58 PM

Well rand is pseudorandom not real random.

To fix this you may have to find a way to also use a random offset to limit clusters.

On the other hand why fix it. If you looked at a real night sky with no city lights there would be clusters. Clusters are natural I think the way it is now looks more realistic then some space type games that use a non generated back drop.

#3 Tom Sloper   Moderators   -  Reputation: 9174

Like
0Likes
Like

Posted 19 October 2013 - 06:45 PM

I had originally locked this thread because I read it ("I am currently creating a  grand strategy space empire game as my python course project") as meaning that the OP wanted help with a homework assignment. The OP assures me that he was not seeking solutions but rather guidance, so I have unlocked it.


Edited by Tom Sloper, 19 October 2013 - 08:57 PM.

-- Tom Sloper
Sloperama Productions
Making games fun and getting them done.
www.sloperama.com

Please do not PM me. My email address is easy to find, but note that I do not give private advice.

#4 Ironmaggot   Members   -  Reputation: 152

Like
1Likes
Like

Posted 20 October 2013 - 04:13 AM

Well rand is pseudorandom not real random.

To fix this you may have to find a way to also use a random offset to limit clusters.

On the other hand why fix it. If you looked at a real night sky with no city lights there would be clusters. Clusters are natural I think the way it is now looks more realistic then some space type games that use a non generated back drop.

 

The randomness that randint() can offer me, is sufficent for my needs.

 

I don't want to limit clusters - clusters are cool in this type of game. I need to limit the minimum distance between any two stars that the generator can create.

 

I woudln't have anything against these clusters, but I need to show some numbers and a little text underneath each one of them. This information is quite important from the perspective of a player, so I have to make this sacrifice.

 

------------------------

 

Does anyone know if there are any commands in built in python library that do this kind of generation or do I have to construct my own function for this. I have read the python random library documentation, but half the commands have too complex descriptions for me to understand.


Edited by Ironmaggot, 20 October 2013 - 04:16 AM.


#5 blewisjr   Members   -  Reputation: 620

Like
0Likes
Like

Posted 20 October 2013 - 04:43 AM

As far as I know there is no built in function for this.

My best guess for getting some space between them would be to wrap it all up in a container object with a method that iterates through all the points and adds a offset to the ones which are to close together before you draw. I can not imagine it being elegant or efficient but can not think of an alternative method at the moment.

#6 Oralordos   Members   -  Reputation: 423

Like
1Likes
Like

Posted 20 October 2013 - 03:36 PM

The isn't exactly what you are looking for, but it is a similar problem that you might get some information from.

http://www.arcengames.com/forums/index.php/topic,14037.0.html



#7 LorenzoGatti   Crossbones+   -  Reputation: 2621

Like
0Likes
Like

Posted 21 October 2013 - 04:39 AM

Given a minimum acceptable star distance R, you could simply reject star locations closer than R to any another star and pick another random position until you succeed. Simple spatial indexing can ensure that the speed remains decent with many stars; for example you can use a grid (with a cell size of R by R, to visit no more than 8 adjacent cells in each search).
Produci, consuma, crepa

#8 rip-off   Moderators   -  Reputation: 8067

Like
1Likes
Like

Posted 21 October 2013 - 05:27 AM

Your code to reserve a space around existing stars is, I believe, both surprising and incorrect. If I understand it correctly, it will reserve a "band" of the screen when a star is generated. This will likely result in a very sparse star field.

As for incorrect, I believe you have an operator precedence issue in your condition. If you change the condition to:
while (x in x_used) == True or (y in y_used) == True:
Or:
while (x in x_used) or (y in y_used):
Then the code will act as intended. Note that instead of learning the precise precedence rules, I always parenthesise complex expressions. This prevents accidental precedence bugs, but also clarifies the intended precedence. When reading code with complex expressions, while you can use the precedence rules to deduce the actual result that will be calculated, unfortunately you cannot do the same to derive the programmer's intent. It is much harder for a programmer to parenthesis incorrectly by accident, if so the expression should probably be divided into separate steps to ease cognitive load.

However, this will result in an infinite loop generating the star field for the dimensions given. Reserving 9 pixels either side when attempting to generate 200 stars simply cannot fit into the screen size you currently have. Best case you'll have a star every 10 pixels, which would require 2,000 pixels at least in each direction.

You might have intended instead to reserve a rectangle around each star, a simple implementation would be a two dimensional array of booleans indicating if the given grid position has been reserved yet.

Note that regardless of the algorithm chosen, you'll probably want to have a way to bail out of this generation process if you're not finding a good candidate position in a couple of tries.

Edited by rip-off, 21 October 2013 - 05:30 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS