• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ironmaggot

Grand strategy space empire game with python

7 posts in this topic

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
2

Share this post


Link to post
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.
0

Share this post


Link to post
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
0

Share this post


Link to post
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
1

Share this post


Link to post
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.
0

Share this post


Link to post
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).
0

Share this post


Link to post
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
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0