The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Hot answers tagged

43

tl;dr don't mix your event loop with your game loop. When you move your mouse, the game receives a load of pygame.MOUSEMOTION events. You don't actually use these events to update your mouse position though, you are getting the current state of the mouse using pygame.mouse.get_pos(). That's inefficient, but it's not the problem. The problem is you are ...


20

I've recently implemented an algorithm for a procedural city layout. It's still very much a work in progress, but seems promising to me. Take a look: The algorithm used to create this layout is loosely based on L-Systems. I have a base Element class, that has a rectangle marking its place on map and a method Grow, which creates other Elements inside the ...


17

You want a data-driven approach almost certainly unless your game is going to be completely un-expected and/or procedural generated to the core. Essentially, this involves storing information about your weapons in a markup language or file format of your choice. XML and JSON are both good, readable choices that can be used to make editing fairly simple ...


12

Python is pretty portable in itself (runs on many platforms), but you have to take into account the following: What are the limitations of the platform you're targeting? Do you want to sell through a mobile store? For instance, the WP7 marketplace only allows to sell apps written in managed code (.NET). The AppStore is pretty locked down too and only ...


12

You only need the textures bound when you'd need to refer to them during the rendering of an object. You do not need every texture you will ever use bound to the pipeline at once. Thus, to render the floor, you bind only the textures you need for the floor; then you render the floor. Then you bind only the textures you need for the NPCs, and render all the ...


12

A Unit Vector is of length 1. A given vector can be converted to a unit vector by dividing it by it's magnitude. (With the exception of course that a zero length vector can not be converted). Note that magnitude can be calculated using the Pythagorean theorem For example if a vector has components: (x, y, z) magnitude = sqrt( x2+ y2+ z2) unit vector =...


11

Finding an algorithm is usually best done with a data structure that makes the algorithm easy. In this case, your territory. The territory should be an unordered (O(1) hash) set of borders and elements. Whenever you add an element to the territory, you iterate over adjacent tiles and see if they should be a border tile; in this case, they are a border ...


10

Imagine the following setup: 1 1 1 1 1 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 As a side note, I refer to squares in the matrix like this: (row, column). I've represented mines with "1" and empty spaces with "0". Assume the user clicks on the empty space at (2, 2) (the corner at the top-left is (0, 0)). This is what would happen: ...


10

You want to use a weighted choice algorithm. Here's some code. (I modified my own working code to fit your case, but it should work): Ignoring the definition of WeightedChoice() for the moment, using WeightedChoice() is simple: # The weighted list of monsters. Each item is a tuple: (VALUE, WEIGHT) monsterlist = ( ('snake', 60), ('wolf', 80), ('...


10

Whoever's general consensus you have, is wrong. There is nothing special needed to implement collision detection in an isometric game. It is no different from implementing collision detection in Robotron or even Pitfall. This is a common misconception, and one that often leads to a lot of struggling. On contract recently I met a senior developer who couldn't ...


10

A* gives you the shortest path in the graph. When using a grid as your graph there are often multiple shortest paths. In your first diagram, that is one of the shortest paths. It puts all the axial movements first and all the diagonal movements afterwards. But that's the same length path as if you put all the diagonals first, or if you mixed axial and ...


9

Your problem is the fact that you're only looking at KEYDOWN events. What you need to do is toggle a boolean value when a key is pressed or released. Something like this would work: # event loop for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: # check for key ...


9

If I understand right, your map stores whether something is dirt or air, and the simplest thing would be to have dirt and air tiles. However, to make things look better, you have separate images for air above dirt, dirt above air, dirt left of air, and so on. So you're trying to figure out which image to use, given a tile and its neighbors. Is that right? ...


9

If I understand you correctly, you want to create a densely packed maze like this, where each wall is the same thickness as each corridor: But you say the maze algorithms you've found only deal with infinitely thin partitions between cells corridor cells, rather than thick walls like these. Let's look closer. Here I've overlaid a grid on the maze above, ...


9

If you need to find edges of holes in the middle of your territory too, then your linear in the area of the territory bound is the best we can do. Any tile on the interior could potentially be a hole that we need to count, so we need to look at every tile in the area bounded by the territory's outline at least once to be sure we've found all the holes. But ...


8

At the request of commenters... Warning to Pyglet professionals: There may be a nice Pyglet way to do this, and this isn't it. It's a nice OpenGL way. You have been warned! You can do this in OpenGL by first binding the texture, then calling glTexParameteri or similar varients. You can do this in Pyglet by importing OpenGL: from pyglet.gl import * You ...


8

There's this GDC talk on procedural building generation from a couple of years ago. It's for creating individual buildings based on a set of templates, but not for creating whole cities (laying out streets and so forth). There isn't any free code to go with it, unfortunately. The system described in the talk is implemented in Unreal although I'm not clear ...


8

This isn't answering your specific programming question, but consider that creating lakes and rivers isn't about randomly placing blobs of water and strips of water between them. It's about terrain height - about depressions (basins) that turn into lakes, and water that flows from higher to lower spots. If you want a great example of creating lakes and ...


8

You might want to divide your world into segments/ a grid so that you only check collision for agents that are relevant. Agent A thats on the other side of the screen will NOT collide with B on te opposite side. So why check collision? Google for Spatial partitioning / quadtrees /spatial hashing. I googled some for you: Quadtrees Grid Also your second ...


8

You're checking each agent against every other agent more than once. For example, consider a simple list of 3 agents, there should only 3 checks, you're checking 9 times, and that gets much worse with larger numbers. At 100 you're checking 10,000 times instead of 5,050. When you iterate like you're doing, the comparison happens like this: A1->A1 //...


8

A very basic formula would be: damageAfterArmorCalc = damageAmt*(1-armorReductionPercent) damageAmt Is the original amount of damage to be done. armorReductionPercent is the percent of damage the armor negates, this percentage should be a value from 0 to 1, so the 20% in your example would be .2. If we're doing 100 damage initially, we can look at some ...


8

convert() is used to convert the pygame.Surface to the same pixel format as the one you use for final display, the same one created from pygame.display.set_mode(). If you don't call it, then every time you blit a surface to your display surface, a pixel conversion will be needed - this is a per pixel operation, very slow - instead of a series of memory ...


8

If you read the pygame.time.Clock.tick() and pygame.time.Clock.get_fps() documents, you'll find that: tick() works with milliseconds - it returns the number of milliseconds since the last call get_fps() returns an average of the last 10 calls to tick(). If you do the math, that's pretty damn good that you're getting 59.8 most of the time, since with 60 FPS ...


7

Both, in general. Your scripts should talk to an abstracted -- or at least intermediate -- layer of functionality and not the engine itself. First, this provides you an extra measure of control and security. It allows you to easily, cleanly define the interface a script is allowed to have with your game and thus what it can muck about with, as well as ...


7

I've figured out how to do this. As I expected, since IronPython compiles down to CLR, C# and IronPython objects can interact with each other just fine with no special treatment necessary. I've created a script which actually creates a new type of Component which can be referenced just fine from C#. A brief word on my game's structure: Its solution has two ...


7

Well, as it seems, you are not seeding the random number generator. In python, it can be easily done with just a random.seed(). And I can see too you're generating a number between 1 and 500000 and making it be between 0 and 1. It's a functional method, but it is capped to just 500000 possibilities. You're better with just using random.random() it already ...


7

Like this: import pyglet # Create and open a window window = pyglet.window.Window(200, 200) # Load sprites s0 = pyglet.resource.image('01.jpg') s1 = pyglet.resource.image('02.jpg') sprites = [s0, s1] # Animation anim = pyglet.image.Animation.from_image_sequence(sprites, 0.5, True) sprite = pyglet.sprite.Sprite(anim) @window.event def on_draw(): window....


7

What you experience right now is a typical consequence of keeping a complex global state. Global states, and complex global states in particular, make side effects hard to track and your code impossible to test. Which is why, whenever something goes wrong, you have to debug your system the hard way. Your program has "only" 1.000 LOC, so not everything is ...


7

Do python games use Lua? Generally? No. Is it a resonable thing or I should just stick to pure python? Define "reasonable"? Python has been used in many game development scenarios. While Lua may be well known among some game mod circles (like WoW GUIs, Garry's Mod, and so forth), Python was the language of choice for Civilization IV modding. So it's ...


7

Sounds like you're after something like a flood fill algorithm. Basically, something like the following algorithm (you can see other examples on the wikipedia page): 1. Add your castle to the Checklist 2. Get the first item from the Checklist 3. For each surrounding position 4. If not on Complete list 5. If `0` add to Checklist 6. If `1` ...


Only top voted, non community-wiki answers of a minimum length are eligible