Hot answers tagged

42

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 ...


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

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

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 ...


10

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

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 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

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

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

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

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

One option is to use Marching Cubes. There are plenty of tutorials and example code in the Internet such as this. Implementing this is not very easy, but doable. Thanks to the increased power of computers and especially GPUs, raycasting has also become a fast enough solution for rendering metaballs and other surfaces. Check for example this tutorial. This ...


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

(I'm sorry to submit the answer instead of a comment, but I don't have rep yet.) Vaughan's answer is great, but I'd like to add my two cents. One of the main reasons you'd want to use XML or JSON and parse it in runtime is to change and experiment with new values without having to recompile the code. As Python is interpreted and, in my opinion, pretty ...


7

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 ...


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` ...


7

REST is not the right paradigm for bidirectional communication, because with REST you can not send data which wasn't specifically requested. Hammering the server with repeated requests is not a good solution because it generates lots of unnecessary network traffic and still gives you additional delays up to the length of your hammering interval. But there ...


7

Notice: Whether or not a tile is on the boundary only depends on it and its neighbors. Because of that: It is easy to run this query lazily. For instance: You do not need to search for the boundary on the whole map, only on what is visible. It is easy to run this query in parallel. In fact, I could image some shader code that does this. And if you need it ...


6

First we need to clarify what "smooth movement" is. Let's first talk about smooth movement in a pixelated space. Not taking into account motion blur and sub-pixel movement, the smoothest amount of movement you can have on a pixel screen is one pixel. So your code pygame.draw.circle(SCREEN, (255, 255, 255), (newx, SCREENRECT.centery), 30) pygame.display....


6

My understanding is that you should never trust the client in a multiplayer game and therefore aim to do any gameplay critical processing server-side. The disadvantage of pure client-side logic is that there is no verification In other words you should probably go with a third option; send an abstract representation of what the player wants to do to the ...


6

It's only randomized the first time because you're only calling the random function one time (when you create the monster object). When you have ma=random.randmint(5, 20) in your class then you are creating that variable with a random amount at that time, but you are not altering it. In order to get a random value every time you need to reset the value every ...


6

In a game, music would be the a way to play background music and sound the way to play sound effects (ej. jumping, firing, etc). Music is a special streaming channel of the Mixer. This means the file is streamed from disk in small chuncks and not loaded at once. Pygame only supports one Music at a time but you can have several Sound objects playing at once,...


6

You can use the size method size() size(text) -> (width, height) Returns the dimensions needed to render the text. This can be used to help determine the positioning needed for text before it isrendered. It can also be used for wordwrapping and other layout effects. Here is an example myFont = pygame.font.Font(None, fontSize) width = myFont....


6

You could look at Skulpt.org which provides a completely brower-based implementation of PyGame. However I do not know what external APIs PyGame uses and which are supported by Skulpt. Edit: it seems Skulpt also has WebGL bindings so this should be very possible!


6

This site showed me how to do it: https://skellykiernan.wordpress.com/2015/01/04/python-pygame-install/ Note that the site uses Python 3.4.2 and pygame version 34cp, but I did it with Python 3.5 and pygame version 35cp and it worked perfectly.


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