Public Group

Grand strategy space empire game with python

This topic is 2095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

[attachment=18450: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

Share on other sites
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.

Share on other sites

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

Share on other sites

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

Share on other sites
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.

Share on other sites
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).

Share on other sites
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
• 11
• 9
• 17
• 13