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


10

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


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


7

PyGame itself, being a wrapper around SDL, is unable to directly use hardware acceleration. You can, however, use PyGame to create an OpenGL context and then render into that via an OpenGL library like pyglet or PyOpenGL, which will let you then also use GLSL vertex and fragment shaders. This blog post does a good job of explaining the basics of using ...


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

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!


7

First you need to calculate the vector pointing from your player to the current mouse position. This can be done by subtracting the player's position with the mouse's position: mouse_x, mouse_y = pygame.mouse.get_pos() rel_x, rel_y = mouse_x - self.x, mouse_y - self.y Then calculate the angle: angle = math.atan2(rel_y, rel_x) This will calculate 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

Use a file format that supports alpha (png), an editor that can save it with a transparent background (gimp2, photoshop, basically not paint) and make sure you load the sprite correctly


4

The concept of a surface in this situation is simply describing a texture. To understand this better, you should also understand the rendering process. When rendering anything using modern graphics API's, the end result is always going to be the same, a buffer (texture) of color data that is presented to the screen. How you get to that buffer can vary quite ...


4

The read() function in this line: map01 = f.read() doesn't do any format checking to try determine what the text is. It just returns raw bytes as a string. You need to convert it to a number explicitly: map01 = int(f.read())


4

I think what you mean is, how to load an animated GIF in pygame. It's true that pygame doesn't support animated GIFs natively, and you'd need GIFImage for that. Not sure how well it works, but I wouldn't recommend it anyway. This is because in games you usually want to control the animation yourself. Use a spritesheet or separate images for your sprite ...


4

Yes, thats called dirty blitting. The Newbie Guide to pygame touches on this. The steps they recommend to implement it are: Blit a piece of the background over the sprite’s current location, erasing it. Append the sprite’s current location rectangle to a list called dirty_rects. Move the sprite. Draw the sprite at it’s new location. Append the sprite’s new ...


4

The Pygame Rect already comes with a few collision detection functions that may just do what you need: pygame.Rect.contains: test if one rectangle is inside another pygame.Rect.collidepoint: test if a point is inside a rectangle pygame.Rect.colliderect: test if two rectangles overlap pygame.Rect.collidelist: test if one rectangle in a list intersects pygame....


4

Looking at the implementation of clock.tick() and where in that module the FPS value is set (search for _clock->fps =), the documentation seems to be correct that it determines the FPS after 10 calls (which means at the 11th call) to tick(). Let's try with a more isolated example: import pygame clock = pygame.time.Clock() fps = 60 for i in range(20): ...


4

Your problem here is that this does not work for CPU, it should be done using GPU, probably you can access it in pygame. It's the work of pixel shader and it works on very low level, with thousands of microprocessors that have no memory - that what makes it so fast to process each pixel. When you are trying to do it in loops - its complexity is \$O(n m)\$, ...


3

My suggestion would be to use surfarrays in conjunction with NumPy. Assuming you already have an algorithm for the post-processing effects you want, all you'll have to do is port it into NumPy syntax, which will probably just take some tinkering. Examples of usage can be found here, here, here, and here. After manipulating the surface, you just have to ...


3

Makeup of angles: Basically you have to look at the angles between the center of the rectangle you are using for a paddle and the center of the rectangle of the ball (can also be a point since we collapse the ball rectangle into a point anyways). The angle between the center points is then compared to the quadrants given by the corner points of the paddle (...


3

There is a "fadein" option, it just isn't a function. From the pygame.mixer.Sound documentation: play() begin sound playback play(loops=0, maxtime=0, fade_ms=0) -> Channel ... The fade_ms argument will make the sound start playing at 0 volume and fade up to full volume over the time given. The sample may end before the fade-in is ...


3

Pygame has a vector math module. There you have the methods normalize and rotate, which you can use to construct unit and perpendicular vectors respectively. http://www.pygame.org/docs/ref/math.html


3

Here is the code to make it. I is the same code of the tutorial that I made on my blog. Check there to learn the Mode 7 method and the RayCasting. Basically, the pseudo code is it: //This is the pseudo-code to generate the basic mode7 for each y in the view do y' <- y / z for each x in the view do x' <- x / z put x',y' ...


3

Can't say much without seeing your whole code, but I guess you're just testing collision for all objects in the game. Instead, do a AABB (rectangular) collision test to see if objects are near enough to matter (usually if they are on-screen). If they are, then do your more complex collision detection. Note that this will actually degrade performance ...


3

The way to figure out where a dot in 3d space should be positioned on the screen is by imagining the monitor is a window and the dot is behind that window. To know where that dot is displayed on the monitor, you need to imagine you have one eye and the draw an imaginary line from that eye to that dot beyond the window and see where that line intersects with ...


3

I'm not sure what your actual problem is (I first thought it was that you wanted to bias the initial midpoint range, but it looks like you actually don't), but here's a simple midpoint displacement implementation in Python: # generic midpoint displacement routine def displace ( points, max_disp ): last_point = points[0] new_points = [ last_point ]...


3

Use pygame.transform.rotate function: screen.blit(background, (0,0)) angle = None if movex == 0 and movey < 0: angle = -180 elif movex > 0 and movey >= 0: angle = 20 elif movex < 0 and movey >= 0: angle = -20 elif movex > 0 and movey < 0: angle = 160 elif movex < 0 and movey ...


3

This is what you need to do, in the following order: pygame.init() - this needs to happen before any other use of pygame pygame.display.set_mode() - this needs to be done before any other use of pygame.display Load and scale your images. Note that pygame.image.load() doesn't need pygame.display.set_mode(), but you're also using pygame.display.Info() to ...


3

My question is -- do I have to establish faces/sides/etc in order for this to "just work", or is there a way to suss out which pairings should not be considered, without just excluding certain shapes from being rendered at all? Yes. The problem is that its completely ambiguous what a face even is in your formulation. Right now, you're implicitly assuming ...


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