Jan-02-2020, 08:47 PM
(This post was last modified: Jan-02-2020, 09:01 PM by stuckbunny.)
(Nov-06-2016, 05:40 PM)LavaCreeperKing Wrote: Hey I did some looking online about this but I wasn't able to figure out how to get it to work. How do you rotate an image? For example I want to create a program the displays an image that just rotates on the screen. How would I do that? And is it possible to rotate text? P.s. I am using pygame.
i have same problem and i was wondering how could i rotate image using mouse by right click or a key , but it not working for me so can u hellp me out with it
(Nov-06-2016, 06:42 PM)metulburr Wrote: When you rotate an image you need to make sure that you make an original of the image, and use that to rotate, otherwise it gets distorted if you keep rotating the image from a rotated image, etc.
Whatever you rotate, whether it be an image or text, both are going to be a surface and rotated the same way.
You are going to use pygame.transform.rotate
Here is an example of a surface rotated based on mouse position
import pygame as pg import math class Rotator: def __init__(self, screen_rect): self.orig_image = pg.Surface([10,100]).convert_alpha() # self.image = self.orig_image self.image.fill((255,255,255)) self.rect = self.image.get_rect(center=screen_rect.center) self.angle = 0 self.distance = 0 self.angle_offset = 0 def render(self, screen): screen.blit(self.image, self.rect) def get_angle(self): mouse = pg.mouse.get_pos() offset = (self.rect.centerx-mouse[0],self.rect.centery-mouse[1]) self.angle = math.degrees(math.atan2(*offset)) - self.angle_offset old_center = self.rect.center self.image = pg.transform.rotate(self.orig_image, self.angle) self.rect = self.image.get_rect(center=old_center) self.distance = math.sqrt((offset[0] * offset[0]) + (offset[1] * offset[1])) def update(self): self.get_angle() self.display = 'angle:{:.2f} disatance:{:.2f}'.format(self.angle, self.distance) if __name__ == '__main__': running = True pg.init() screen = pg.display.set_mode((600,400)) screen_rect = screen.get_rect() rotator = Rotator(screen_rect) clock = pg.time.Clock() while running: screen.fill((0,0,0)) for event in pg.event.get(): if event.type == pg.QUIT: running = False rotator.update() rotator.render(screen) pg.display.set_caption(rotator.display) pg.display.update() clock.tick(60)here the surface is just a generic rectangle, but you can plug in an image or text surface. The lines you would need to change to do that is
#self.orig_image = pg.Surface([10,100]).convert_alpha() # self.orig_image = pg.image.load("your_image.png").convert() self.image = self.orig_image #self.image.fill((255,255,255))where you load your image as the original instead and comment out the image fill
another example, a non-interactive example a constant turning object. Again you can plugin your image/text surface instead of a generic surface
import pygame as pg screen = pg.display.set_mode((800,600)) screen_rect = screen.get_rect() clock = pg.time.Clock() done = False class Rotator: def __init__(self, screen_rect): self.screen_rect = screen_rect self.master_image = pg.Surface([100,100]).convert_alpha() self.master_image.fill((255,0,0)) self.image = self.master_image.copy() self.rect = self.image.get_rect(center=self.screen_rect.center) self.delay = 10 self.timer = 0.0 self.angle = 0 def new_angle(self): self.angle += 1 self.angle %= 360 def rotate(self): self.new_angle() self.image = pg.transform.rotate(self.master_image, self.angle) self.rect = self.image.get_rect(center=self.screen_rect.center) def update(self): if pg.time.get_ticks()- self.timer > self.delay: self.timer = pg.time.get_ticks() self.rotate() def draw(self, surf): surf.blit(self.image, self.rect) rotator = Rotator(screen_rect) while not done: for event in pg.event.get(): if event.type == pg.QUIT: done = True screen.fill((0,0,0)) rotator.update() rotator.draw(screen) pg.display.update()
i was having same problem i hope i could contact u so u could help me out with my program