Can anyone tell me the use of Pygame.Rect. I am creating a simple program to draw and display rectangle in screen and check for collision with screen. I saw a video on youtube where the Youtuber uses the following code:
moving_rect=pygame.Rect(300,300,100,100)
Now I don't know what is Pygame.Rect is used for and what the above line of code actually does.
A pygame.Rec describes a rectangular area. It can be use to define the bounding box of an object. This can be useful for a collision test. See How do I detect collision in pygame?.
Actually it is used to define the location of a pygame.sprite.Sprite object.
Notice that a pygame.Surface object has no position. It only contains the pixel information. However, a Rect and a Surface together define the position of an image (Sprite) in the window.
Related
I'm writing a player class that, among other things, has mesh attributes (I use the py3d library and the mesh class from it) and collider (a class that I need to implement myself). The collider is a simple cube and should have a method to determine whether it collided with another collider-cube or not. I have a class that allows you to rotate and move 3d objects, I inherit the collider from it. The main problem is precisely to write a collision check function
I tried to use the methods built into Pygame to detect collisions, but it didn't work, because when the camera is removed, the collider remains the same size, and it can't be rotated. I'm bad at math, and all the guides I found were in C.game example
One way to detect box-box collisions in 3D using PyOpenGL and Pygame is to use the Bullet physics engine. Bullet is a 3D physics engine that can be used to detect collisions, apply forces, and simulate the motion of rigid bodies. To use Bullet, you would need to implement the collider class as a Bullet body, and then use the Bullet functions to detect collisions between the collider objects. You can also use the Bullet functions to rotate and move the colliders, which will allow you to keep the same size collider regardless of the camera position.
Here's a link to a tutorial on how to integrate bullet
http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-a-physics-library/
I am making a game in python with pygame, and I am wondering if there is any class (with collisions) in pygame that supports non-AABB polygons. I know there is the pygame.draw.polygon function, but that takes a list of points instead of a rect like pygame.draw.rect.
Is there any way to do this in pygame?
If not, are there any pygame-compatible libraries that could do this?
If there are no libraries, how could I implement it?
The pygame.draw module does not generate objects. It just draws something on a Surface. You cannot use this for collision detection. I think you're confusing pygame.Rect and the pygame.draw module.
The collision detection in pygame is based on rectangles or circles. See How do I detect collision in pygame?.
Another option is to use pygame.mask.Mask objects, which you can use to detect overlapping shapes and sprites. See Pygame mask collision
As I understood pygame drawing method, the 2nd argument of the blit function (screen.blit(surface, (0,0))) tells pygame where to draw the given surface (like an offset to start drawing from, or rather a placement of the surface on the screen). Although, from recent experimenting, it seems that pygame surfaces placements are fixed, and that (0,0) is used to crop the surface before pygame blit it to the screen, for efficiency purposes.
Are surfaces placement really fixed (is my latter observation correct)? and if so, is there another way to conveniently move an already drawn surface to another position on the screen? Or should i implement my own way of moving complex "drawable" objects?
Thanks!
A pygame.Surface object has no position, it has a size only. Note, the location of the Rect object which is returned by pygame.Surface.get_rect() is always (0, 0).
When you blit a Surface on another Surface, then each pixel is copied and placed at the corresponding position of the destination Surface. Thus always a position has to be specified, when a Surface is blit on a destination Surface.
See also Why is my collision test always returning 'true' and why is the position of the rectangle of the image always wrong (0, 0)?
[...] if so, is there another way to conveniently move an already drawn surface to another position [...]
You have a basic misunderstanding. A Surface cannot be "moved". A Surface is copied on the Surface object which is associated to the game window.
A Surface appears to be moving, because the entire scene is drawn in every frame. First the background is drawn, then the objects (Sprites, Surfaces) are drawn on top of the background and finally the display is updated (in every frame). If an object is placed at a slightly different position in every frame, then the object appears to be moving smoothly.
I have created a surface that is intended to be used as a 'wall' for an rpg style game, however I rotated this surface so it takes the form of a diamond since this is how the game design looks. My question is when using spritecollide it only works in rect arguments (rectangles). How can I make a 'wall' surface that has been rotated and essentially gives a rotated rect argument for a diamond? Thanks for the help!!
You can use collision masks for anything more complex than a rectangle or circle.
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_mask
What is the difference between importing a rocket sprite (for example) and setting it as an image vs importing a rocket sprite and setting it as a sprite in pygame?
I think you're just getting confused about the terminology:
Image
An image is just a collection of pixels. You're using "sprite" to refer to an image on the disk, but that's just an image file. To use your rocket example, you would load the image like this:
rocket_img = pygame.image.load('rocket.png').convert_alpha()
You can then draw this image anywhere you want with:
screen.blit(rocket_img, (x, y))
Sprite
A sprite in Pygame is an object, with a whole collection of built-in functionality. Sprites have an image as one of their properties, but there are a whole lot more. Plus you can put sprites together in groups to make them easier to update or draw. Sprites have collision functionality built into them. You can add your own properties to track location, velocity, animation, etc.
A simple sprite:
class Rocket(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('rocket.png').convert_alpha()
self.rect = self.image.get_rect()
def update(self):
self.rect.x += 1
This would be a rocket sprite that you would instantiate by using
rocket = Rocket()
You can draw by using
screen.blit(rocket.image, rocket.rect)
and it moves slowly to the right (if you call update() in the game loop:
rocket.update()
I recommend looking at the Sprite docs - there's lots more you can do with groups to make working with lots of sprites very easy.
http://www.pygame.org/docs/ref/sprite.html
In Pygame "images" generally refer only to image files: importing and exporting them to disk. There is the "Surface" object that is a Python object that holds pixels, and can be used to stamp other surfaces, be transformed (scaled/rotated) , yielding other surfaces and so on.
The main screen itself is a Surface subclass - so when you stamp a Surface with data read from a disk image, using the blit method the image shows up on the screen.
Sprites on the other hand are a base class for objects in your game, and they don't even depend of having attached pixels data with them. Some of the Pygame API expect Sprite objects to have a rect attribute, which denotes the position where it will be rendered on a Surface - and an image attribute. If it is to be used, the sprite.image attribute should hold a surface object - usually read from disk (but could have been programatically drawn).
The main call using the sprite image attribute is the Group.draw() method.
But it is possible to create an entirely different game than an interactive one - one that would be the server side for a MMO game, without anything on the screen, for example, using the Sprite and Group classes, without ever making use of the image attribute on sprites.
Worth reading:
https://www.pygame.org/docs/ref/sprite.html
Conversely, you can bypass all the helper logic provided by Sprites and Groups and create a game that will only ever have Surface objects - representing images read from disk. Them you are responsible to track were and when to draw them, without using the elpe rmethods in sprite Groups.