“wie man das Bild in Pygame dreht” Code-Antworten

wie man den Mauswinkel aus der Mitte bekommt

# if you want to find the angle the mouse position is from the center
# heres the formula
angle = 360 - math.atan2(pos[1] - (half of screenheight), pos[0] - (half of screenwidth)) * 180 / math.pi
Ninja Hatori Cat Codr

Pygame -Drehbild

import pygame
import pygame.font

pygame.init()
size = (400,400)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

def blitRotate(surf, image, pos, originPos, angle):

    # calcaulate the axis aligned bounding box of the rotated image
    w, h       = image.get_size()
    box        = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
    box_rotate = [p.rotate(angle) for p in box]
    min_box    = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
    max_box    = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])

    # calculate the translation of the pivot 
    pivot        = pygame.math.Vector2(originPos[0], -originPos[1])
    pivot_rotate = pivot.rotate(angle)
    pivot_move   = pivot_rotate - pivot

    # calculate the upper left origin of the rotated image
    origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])

    # get a rotated image
    rotated_image = pygame.transform.rotate(image, angle)

    # rotate and blit the image
    surf.blit(rotated_image, origin)

    # draw rectangle around the image
    pygame.draw.rect (surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)

font = pygame.font.SysFont('Times New Roman', 50)
text = font.render('image', False, (255, 255, 0))
image = pygame.Surface((text.get_width()+1, text.get_height()+1))
pygame.draw.rect(image, (0, 0, 255), (1, 1, *text.get_size()))
image.blit(text, (1, 1))
w, h = image.get_size()

angle = 0
done = False
while not done:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key==pygame.K_ESCAPE:
                done = True

    pos = (screen.get_width()//2, screen.get_height()//2)
    pos = (200, 200)

    screen.fill(0)
    blitRotate(screen, image, pos, (w//2, h//2), angle)
    angle += 1

    pygame.draw.line(screen, (0, 255, 0), (pos[0]-20, pos[1]), (pos[0]+20, pos[1]), 3)
    pygame.draw.line(screen, (0, 255, 0), (pos[0], pos[1]-20), (pos[0], pos[1]+20), 3)
    pygame.draw.circle(screen, (0, 255, 0), pos, 7, 0)

    pygame.display.flip()

pygame.quit()
Scary Scarab

Bild in Pygame drehen

# Import pygame
import pygame
  
# Initialise pygame
pygame.init()
  
# Set window size
size = width,height = 600, 600
screen = pygame.display.set_mode(size)
  
# Clock
clock = pygame.time.Clock()
  
# Load image
image = pygame.image.load('gfg.png')
  
# Set the size for the image
DEFAULT_IMAGE_SIZE = (200, 200)
  
# Rotate the image by any degree
image = pygame.transform.rotate(image, 180)
  
# Set a default position
DEFAULT_IMAGE_POSITION = (200,200)
  
# Prepare loop condition
running = False
  
# Event loop
while not running:
  
    # Close window event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = True
  
    # Background Color
    screen.fill((0, 0, 0))
  
    # Show the image
    screen.blit(image, DEFAULT_IMAGE_POSITION)
  
    # Part of event loop
    pygame.display.flip()
    clock.tick(30)
Powerful Porpoise

wie man das Bild in Pygame dreht

# first argument is the original_image
# second one is the angle by how much angle you want you image to be rotated
# third is the zoom amount here it is set to 1 because we don't need to zomm in or out

img_rotated = pygame.transform.rotozoom(original_image,angle,1)

# NOTE: you can use the pygame.transform.rotate but some times rotozoom works better
#** -*=\ Happy Coding /=*- **#

Ninja Hatori Cat Codr (https://github.com/PrathameshBhatkar)
                      (https://www.youtube.com/channel/UCHzOI93T5ZZ4TqYJqz6WmYQ)
Ninja Hatori Cat Codr

Ähnliche Antworten wie “wie man das Bild in Pygame dreht”

Fragen ähnlich wie “wie man das Bild in Pygame dreht”

Weitere verwandte Antworten zu “wie man das Bild in Pygame dreht” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen