This question already has answers here:
How do I rotate an image around its center using Pygame?
(6 answers)
Closed 1 year ago.
Currently, I am trying to get my gun to rotate to look towards my mouse - which I have working. However the rotation is just odd and doesn't work how I want it to. I am trying to rotate it with a center of (0,0) so that it rotates around that top left corner, however it just doesn't seem to want to clamp the top left in one position and rotate around it.
What I have:
class Gun():
def __init__(self):
self.original_image = p.transform.scale(p.image.load('D:\Projects\platformer\Assets\Gun.png'),(20,8))
def rotate(self,x,y):
mx,my = p.mouse.get_pos()
rel_x,rel_y = mx - x,my - y
angle = (180/math.pi) * -math.atan2(rel_y,rel_x)
self.image = p.transform.rotate(self.original_image,int(angle))
self.rect = self.image.get_rect(center=(0,0))
WIN.blit(self.image,(x,y))
This is what is happening
yet I would want the top left of the gun to stick in just one position. Like this:
Any suggestions on how to do this, because I understand that pygame rotates weirdly and nothing I can find currently works.
You need to set the center of the bounding rectangle of the rotated image with the center of the bounding rectangle of the original image. Use the rotated rectangle to blit the image:
self.image = p.transform.rotate(self.original_image,int(angle))
self.rect = self.original_image.get_rect(topleft = (x, y))
rot_rect = self.image.get_rect(center = self.rect.center)
WIN.blit(self.image, rot_rect)
See also How do I rotate an image around its center using PyGame?
Related
This question already has answers here:
pygame 2 dimensional movement of an enemy towards the player, how to calculate x and y velocity?
(1 answer)
How to make smooth movement in pygame
(2 answers)
Closed 1 year ago.
class monkey(object):
def init(self, x, y, width, height):
self.walkRight = [ list of images]
self.walkLeft = [list of images]
self.walkDown = [list of images]
self.walkUp = [list of images]
#get rect for each image using list comprehension. I do this 4 times for each arrow key
self.rect_Right = [img.get_rect() for img in
self.walkRight] #repeat for other arrows
class enemy(object):
(basically the same thing as player with enemy images)
def move_towards_player(self, monkey):
dirvect = pygame.math.Vector2(monkey.rect.x - self.rect.x,
monkey.rect.y - self.rect.y)
dirvect.normalize()
dirvect.scale_to_length(self.speed)
self.rect.move_ip(dirvect)
I am running into 2 problems. 1st, since I am uploading multiple images into a list, I don't know whether I am using the get_rect() correctly. I am currently using a list comprehension. How do I get the rect.x and rect. y positions? How do I get the center position?
2nd, I don't know how to get the move_towards_player to get working. I passed the player class into the function, but I don't think it can read the rects.
I am making a game where there are two players and they can shoot each other. Their movement will be defined by a rotation around a fixed point, the point will be(600, 300), which is the center of our screen. The player will keep rotating around the point as long as they are pressing a certain button(which is keep providing force to our player) else they will fall(due to gravity). I think it would help to think of it as a ball attached to a point using a string. The string is attached as long as a button is pressed and gets unattached as soon as the button is released and the ball flies off. Here is my player class
class Player:
def __init__(self):
self.pos = [500, 200]
self.width = 30
self.height = 30
self.player = pygame.image.load("player.png").convert_alpha()
self.player = pygame.transform.scale(self.player, (self.width, self.height))
self.rect = self.player.get_rect()
self.rotated_player = None
self.anguler_vel = 0
self.Fg = 0.05
self.Fp = 0
self.arm_length = 0
Fp is the force perpendicular to the force of gravityFg. Fg is the force which is pulling it down on our player. Fp is defined by math.sin(theta) * Fg. I am keeping track of Fp because i want the player to keep moving in the direction of rotation after its unattatched from the string. arm_length is the length of the string.
I have a Point class, which is the point about which our player will rotate. Here's the point class.
class Point:
def __init__(self,x, y):
self.pos = [x, y]
dx = self.pos[0] - player.pos[0]
dy = self.pos[1] - player.pos[1]
self.angle = math.atan2(dy, dx)
Now, i need help with the actual rotation itself. I am aware that adding a certain value to the angle every single frame would make it go around. But how would i make it go around a certain point that i specify and how would the arm length tie into this?. I find that it is really difficult to implement all of this because the y-axis is flipped and all the positional values have to be scaled down when using them in calculations because of the FPS rate. Any help on how this is done would be appreciated as well. Thanks
When you use pygame.transform.rotate the size of the new rotated image is increased compared to the size of the original image. You must make sure that the rotated image is placed so that its center remains in the center of the non-rotated image. To do this, get the rectangle of the original image and set the position. Get the rectangle of the rotated image and set the center position through the center of the original rectangle. e.g.:
def rotate_center(image, rect, angle):
rotated_image = pygame.transform.rotate(image, angle)
new_rect = rotated_image.get_rect(center = rect.center)
return rotated_image, new_rect
screen.blit(*rotate_center(image, image_rect, angle))
Alos see How do I rotate an image around its center using PyGame? and How to rotate an image(player) to the mouse direction?
I'm trying to select the center of my image to make a hitbox some pixels around it.
I was able to inflate() the hitbox to make it the right size, but it always uses the top left corner of the image. This is fine when i'm moving left, but when i turn right it goes away from the character (in the image the character is dragging a sword, so it goes way off center).
I've been reading about Vector2, pos and offset, but i can't get it to work.
In conclusion i need to learn a way to find the center of my image in order to place it's hitbox a few pixels to each side. Either that or how to "shift" the corner the hitbox uses so it's always in the front of the char.
Use a pygame.Rect object. Get the rectangle from the pygame.Surface object (image) and set the top left corner position (x, y) of the rectangle. e.g.:
rect = image.get_rect()
rect.topleft = (x, y)
respectively
rect = image.get_rect(topleft = (x, y))
pygame.Rect provides a lot of virtual attributes, which can be used to retrieve the corners, borders, center and size of the rectangle. .center returns the center of the rectangle:
center_x, center_y = rect.center
This question already has answers here:
How do I rotate an image around its center using Pygame?
(6 answers)
How to rotate an image(player) to the mouse direction?
(2 answers)
How can you rotate an image around an off center pivot in Pygame
(1 answer)
Closed 2 years ago.
I am creating a game and i need to rotate the ship, how can i rotate the ship from the center rather than the top left corner? I am using python 2.7 and Pygame 1.9.
Here is the code that i already have to rotate the image.
shipImg = pygame.transform.rotate(shipImg,-90)
However this rotates the image from the corner.
Rotate the sprite then set the center of the new rect to the center of the old rect. This way the new one has the same center when you're done, making it look like it rotated around the center.
Here's an example function from the pygame wiki:
def rot_center(image, rect, angle):
"""rotate an image while keeping its center"""
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image,rot_rect
And here's how you would use it in your example:
# Draw ship image centered around 100, 100
oldRect = shipImg.get_rect(center=(100,100))
screen.blit(shipImg, oldRect)
# Now rotate the ship and draw it with the new rect,
# which will keep it centered around 100,100
shipImg, newRect = rot_center(shipImg,oldRect,-90)
screen.blit(shipImg, newRect)
def rotate(self):
#Save the original rect center
self.saved_center=self.rect.center
#Rotates a saved image every time to maintain quality
self.image=pygame.transform.rotate(self.saved_image, self.angle)
#Make new rect center the old one
self.rect.center=self.saved_center
self.angle+=10
When I rotate the image, there is a weird shifting of it despite the fact that I'm saving the old rect center and making the rotated rect center the old one. I want it to rotate right at the center of the square.
Here's what it looks like:
http://i.imgur.com/g6Os9.gif
You are just calculating the new rect wrong. Try this:
def rotate(self):
self.image=pygame.transform.rotate(self.saved_image, self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
self.angle+=10
It tells the new rect to center itself around the original center (the center never changes here. Just keeps getting passed along).
The issue was that the self.rect was never being properly updated. You were only changing the center value. The entire rect changes as the image rotates because it grows and shrinks in size. So what you needed to do was completely set the new rect each time.
self.image.get_rect(center=self.rect.center)
This calculates a brand new rect, while basing it around the given center. The center is set on the rect before it calculate the positions. Thus, you get a rect that is properly centered around your point.
I had this issue. My method has a bit of a different purpose, but I solved it quite nicely.
import pygame, math
def draw_sprite(self, sprite, x, y, rot):
#'sprite' is the loaded image file.
#'x' and 'y' are coordinates.
#'rot' is rotation in radians.
#Creates a new 'rotated_sprite' that is a rotated variant of 'sprite'
#Also performs a radian-to-degrees conversion on 'rot'.
rotated_sprite = pygame.transform.rotate(sprite, math.degrees(rot))
#Creates a new 'rect' based on 'rotated_sprite'
rect = rotated_sprite.get_rect()
#Blits the rotated_sprite onto the screen with an offset from 'rect'
self.screen.blit(rotated_sprite, (x-(rect.width/2), y-(rect.height/2)))