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 ...
Conceptually you've got it, just think of the rectangle as a helper for you to deal with position and collision detection of your image. To implement it you could use:
mySprite.image = pygame.transform.rotate(Surface, angle)
This will give you a rotated Surface (image), then you can use:
mySprite.rect = mySprite.image.get_rect()
To give you your new ...
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:
elif event.type == pygame.KEYDOWN: # check for key ...
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?
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 ...
Back in January, 2011, I looked at five GUI toolkits for pygame, and tried to get all of them working with Python 3. The five were: Albow, GooeyPy, PGU, pqGUI, and sgc.
I didn't succeed with GooeyPy, but I did get the other four to work with Python 3. (I wanted to also try poutine, by Shandy Brown, but I couldn't find it, and I entirely overlooked Ocemp.)
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 ...
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 ...
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!
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)
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 ...
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,...
You can use the size method
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....
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 ...
Don't know python, but here is a basic arbitrary-language example.
You should only have one game loop that updates all other entities. You can determine how much time has passed between each running of the loop and pass that information on to the entities who can use this information. For Example:
lastSystemTime = currentSystemTime;
There is no 'proper' way, just different approaches that vary from simple-but-slow to complex-but-fast.
You can stick with that method, if your car list is small enough. However, there is a bug in your basic algorithm, as it will compare each pair of cars twice. To fix that, your second loop should start the value of car2 from the position in the list after ...
I suggest you implement some flocking behavior principles on the movement method.
If you don't know about flocking algorithms, here are some useful links and tutorials:
The trick would be the following:
1- The movement and its direction each frame will be controlled ...
The program is trying to convert a unicode wide character format into a standard ascii format. The code you are trying to convert is out of the available ASCII format range.
This is the code you are trying to convert, and ASCII only supports 128 different values, with extended ASCII supporting 256.
I don't know python, but have you tried setting a bool to true when the key is pressed, and changing the speed in an if statement based on that bool. When the key gets released you just have to set the bool back to false.
You are probably looking for the absolute value!
You can implement it like this:
if value < 0:
You can use it like this:
radius = 20
if abs(playerPositionX - enemyPositionX) < radius:
However, if your game is in 2D, and you wish to find the ...
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 ...
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())
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 ...
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
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:
clock = pygame.time.Clock()
fps = 60
for i in range(20):
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)\$, ...
If I'm understanding your question correctly, you want to select agents within the map, but outside of the current view/camera.
The approach is to convert your mouse/screen coordinates into world coordinates, by using your view position to offset the mouse.
def ViewToWorld(view, mouseX, mouseY):
finalPos.x = view.LeftX + mouseX
Probably not, unless you want to create extra work for yourself.
Threading is an optimization, which adds significant complexity to the program, and if you don't move the right thing on to another thread it won't even add much performance. The only way to know what the right thing to optimize is, is to use a profiler.
If you do want to use threads to ...