pygame flip image

Solutions on MaxInterview for pygame flip image by the best coders in the world

showing results for - "pygame flip image"
Emmanuel
06 Jan 2018
1# Flip an image
2image = pygame.image.load('image.png')
3flipped = pygame.transform.flip(image, True, False)
Michela
28 Sep 2020
1import pygame
2import pygame.font
3
4pygame.init()
5size = (400,400)
6screen = pygame.display.set_mode(size)
7clock = pygame.time.Clock()
8
9def blitRotate(surf, image, pos, originPos, angle):
10
11    # calcaulate the axis aligned bounding box of the rotated image
12    w, h       = image.get_size()
13    box        = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
14    box_rotate = [p.rotate(angle) for p in box]
15    min_box    = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
16    max_box    = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])
17
18    # calculate the translation of the pivot 
19    pivot        = pygame.math.Vector2(originPos[0], -originPos[1])
20    pivot_rotate = pivot.rotate(angle)
21    pivot_move   = pivot_rotate - pivot
22
23    # calculate the upper left origin of the rotated image
24    origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])
25
26    # get a rotated image
27    rotated_image = pygame.transform.rotate(image, angle)
28
29    # rotate and blit the image
30    surf.blit(rotated_image, origin)
31
32    # draw rectangle around the image
33    pygame.draw.rect (surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)
34
35font = pygame.font.SysFont('Times New Roman', 50)
36text = font.render('image', False, (255, 255, 0))
37image = pygame.Surface((text.get_width()+1, text.get_height()+1))
38pygame.draw.rect(image, (0, 0, 255), (1, 1, *text.get_size()))
39image.blit(text, (1, 1))
40w, h = image.get_size()
41
42angle = 0
43done = False
44while not done:
45    clock.tick(60)
46    for event in pygame.event.get():
47        if event.type == pygame.QUIT:
48            done = True
49        elif event.type == pygame.KEYDOWN:
50            if event.key==pygame.K_ESCAPE:
51                done = True
52
53    pos = (screen.get_width()//2, screen.get_height()//2)
54    pos = (200, 200)
55
56    screen.fill(0)
57    blitRotate(screen, image, pos, (w//2, h//2), angle)
58    angle += 1
59
60    pygame.draw.line(screen, (0, 255, 0), (pos[0]-20, pos[1]), (pos[0]+20, pos[1]), 3)
61    pygame.draw.line(screen, (0, 255, 0), (pos[0], pos[1]-20), (pos[0], pos[1]+20), 3)
62    pygame.draw.circle(screen, (0, 255, 0), pos, 7, 0)
63
64    pygame.display.flip()
65
66pygame.quit()
67
queries leading to this page
how to resize images in pygameflipping image in pygamepygame rotate spriteset rotation of image pygamehow to resize an image pygamepython pygame flippygame transform scale examplepygame how to flip an imagerotate an image pygamehow to resize imgs in pygamehow to rotate a image in pygamewpygame transform fliphow to flip an image pygamehow to make a cimage grow then shrink in pygamehow to rotate img in pygame horizontalypygame flip vs updatepygame flip imagepygame trasnform scalepygame rotate image around pointpygame rotate image around centerscale surface pygame flip in pygamepygame flip imagepygame display flippygame transform flippygame transform rotatetransform an image on a surfacepygame image rotationpygame change angle of imagepygame flipflip a sprtie pygamepygame transform rotozoompygame image rotateflip 28 29 pygamepygame rotate image to mousehow to rotate an image pygamerotation image pygamerotate pygameblit rotate pygametransform rotate pygameset rotation of image in pygamepygame flip 28 29rotate image around center pygametranformation image in pygamehow to flip images in pygamflip a picture mirror image pygamesflip pygamehow to make an image rotate in pygamehow to flip an image in pygameflip 28 29in pygameresizing images pygamescale tranformation image in pygamepygame set rotation of imagedisplay flip pygamescale transformation image in pygamepygame rotate a drawn recthow to flip pygame imagepygame transform rotate 28 29pygame flip image horizontallyhow to rotate images in pygame on an axispygasme scaleflip in pygamepygame scale imagepython pygame rotate functionpygame flip and updatepygame how to flip the screenpygame image transformscale image pygameresize surface pygamepygame dsiplay fliptransform pygame flippygame rotationscaling pygame imagescale pygame surfacepygame blit rotationpygame display flip 28 29how to flip images in pygame mouserotating image pygamepygame rotatingrezise object pygamerotate images in pygamepygame transformcan you flip and image in pygamehow do i flip a image in pygameflip imahe in pygamepygame flip 28 c3 a0flip image pygameflip image in pygame pygame image scaletransform pygamepygame image transform rotate 28 29ptgame rotate imagehow to rotate a image about a point in pygamehow to rotate a image to mouse in pygamehow to scale and rotate image in pygamepygame transform flip vs rotatehow to flip images in pygamehow to rotate a image in pygamepygame transform scalepygame transform rotate objectwhat is pygame display flip 28 29flip image in pygamehow to rotate a picture in pygamepygame transform rotatehow to transform scale in pygamerotate rect pygamepygame scale surfacepygame pygame display flip 28 29how can we rotate a image about a point in pygamehow to resize an image in pygamepygame reverse imagepygame sizetransform scale pygamepygame rotate image pygame rotate transformwhat does pygame display flip 28 29 do 3fhow to scale and transform image pygamepygame ajoute photopygame display flippygame surface get rotationhow to rotate image through mouse in pygamepygame display flip pygame transform scale 2xpygame zoompygame image rotatewhat does display flip do in pygamepygame rotating imagepygame transform recalescale pygame imagehow to rotate image in pygametransform image pygamepygame set image rotationflip display pygamepygame transform flippygame transformhow to resize a image pygamepygame transform scalepygame rotate surfaceresize pygame imagehow to rotate image to a angle in pygamerotate in pygamepygame flip imagespygame image rotate centerdisplay flip pygamehow to flip and image in pygamesmooth transformation pygamepygame flip screenhow to rotate an image in pygameresize image pygamehow to rotate a sprite pygamerotate image pygaemhow to flip image in pygamepygame fliphow to rotate an image using rect and vector in pygamemirror image pygamepygame rotate objectrotate image from center pygamewhat is pygame display flippygame transform rotatepygame rotate imagesrotate recntanlge image pygamewhat does pygame display flip 28 29 dopygame image flippygame change angle imagesrotate image crashing pygamepygame image resizepygame rotaterotate a surface pygmaehow to rotate image pygamepygame scaling imagerotate img pygamepygame scaleflip 28 29 python pygamehow to rotate img in pygamepygame rotate memorypygame change surface sizepygame rotate image around pivotrotate image in pygametransform flip pygamepytgame transform rotate 28 29pygame rotate image 5chow to rotate from centre of image pygamepygame functions mirror picturepygame transform flip examplehow to make pygame tranform scale betterrotate image python pygamepygame crop imagewhat does pygame display flip dopgame how toflip imagepygame transform laplacian 28 29rotater img pygamepygame display flippygame display flipimage pygame transform positionhow to rotate images in pygamepython pygame display fliprotate photo in pygamerotate image pygamepygame resize imagepygame flip image