📜  如何使用鼠标在 PyGame 中缩放和旋转图像?

📅  最后修改于: 2022-05-13 01:55:18.013000             🧑  作者: Mango

如何使用鼠标在 PyGame 中缩放和旋转图像?

在本文中,我们将讨论如何在 Pygame 中使用鼠标转换图像,即(缩放和旋转图像)。

方法

步骤 1:首先,导入库 Pygame 和 math。

import pygame
import math
from pygame.locals import *

第 2 步:现在,将颜色作为我们要在游戏中使用的输入。

color_1 = #RGB value of color 1
color_2 = #RGB value of color 2
color_n = #RGB value of color n

第三步:然后,初始化构造函数

pygame.init()

第 4 步:设置 GUI 游戏的尺寸。



w, h = #Width dimension, #Height dimension
screen = pygame.display.set_mode((w, h))

第五步:设置运行游戏的运行值,可以移动的角度。

running = True
angle = 0
scale = 1

第 6 步:接下来,将图像作为我们想要用鼠标移动的输入

img_logo = pygame.image.load('#Enter the image url')
img_logo.convert()

第 7 步:在图像周围绘制边框。

rect_logo = img_logo.get_rect()
pygame.draw.rect(img_logo, color_1, rect_logo, 1)

步骤8:定位GUI游戏的中心并获得鼠标的位置。

center = w//2, h//2
mouse = pygame.mouse.get_pos()

步骤 9:将图像存储在一个新变量中并在图像周围构建一个矩形。

img = img_logo
rect = img.get_rect()
rect.center = center

第 10 步:设置您希望应用在运行状态下执行的操作。

while running:
    for event in pygame.event.get():
  • 步骤 10.1:一旦应用程序处于运行状态,如果用户想退出,请使其退出。
if event.type == QUIT:
            running = False
  • 步骤10.2:如果用户不想退出,设置图像应该旋转的角度。
if event.type == KEYDOWN:
            if event.key == K_ra:
                if event.mod & KMOD_SHIFT:
                
                    # angle at which it should move left
                    angle -= 
                else:
                
                    # angle at which it should move right
                    angle += 
  • 步骤 10.3:另外,设置图像大小应该减少或增加的比例。
elif event.key == K_sa:
                if event.mod & KMOD_SHIFT:
                    scale /= #scale at which the image size should decrease
                else:
                    scale *= #scale at which the image size should increase
  • 步骤 10.4:设置图像将旋转或调整大小的坐标、角度和比例。
elif event.type == MOUSEMOTION:
  • 步骤 10.4.1:将事件的当前位置存储在新变量中。
mouse = event.pos
  • 步骤 10.4.2:借助鼠标变量和图像中心定位笛卡尔坐标以旋转图像。
x = mouse[0] - center[0]
            y = mouse[1] - center[1]
  • 步骤 10.4.3:进一步,借助公式 √x 2 +y 2计算两点 (0,0) 和 (x, y) 之间的距离
d = math.sqrt(x ** 2 + y ** 2)
  • 步骤 10.4.4:现在,使用Python方法math.atan2()计算图像应该旋转的角度,该方法返回 y/x 的反正切,以弧度为单位。
angle = math.degrees(-math.atan2(y, x))
  • 步骤 10.4.5:使用函数abs计算图像大小应该减小或增加的比例,该函数返回数字的大小。
scale = abs(5 * d / w)
  • 步骤 10.4.6:使用组合缩放和旋转变换的 rotozoom函数计算运行状态下图像的更新位置。
img = pygame.transform.rotozoom(img_logo, angle, scale)
  • 步骤 10.4.7:在更新后的图像周围构建矩形
rect = img.get_rect()
            rect.center = center

第 11 步:接下来,您需要设置屏幕颜色和屏幕上的图像。

screen.fill(color_3)
    screen.blit(img, rect)

第 12 步:稍后,绘制矩形线 和有助于移动图像的圆圈



pygame.draw.rect(screen, color_2, rect, #thickness of rectangle)
    pygame.draw.line(screen, color_3, center, mouse, #Thickness of line)
    pygame.draw.circle(screen, color_1, center, #radius of circle, #thickness of circumference)
    pygame.draw.circle(screen, color_2, mouse, #radius of circle, #thickness of circumference)

步骤 13:此外,更新在 GUI 游戏中所做的更改。

pygame.display.update()

步骤14:最后,退出GUI游戏。

pygame.quit()

下面是实现。

Python
# Python program to transform the 
# image with the mouse
  
#Import the libraries pygame and math
import pygame
import math
from pygame.locals import *
  
# Take colors input
RED = (255, 0, 0)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
  
#Construct the GUI game
pygame.init()
  
#Set dimensions of game GUI
w, h = 600, 440
screen = pygame.display.set_mode((w, h))
  
# Set running, angle and scale values
running = True
angle = 0
scale = 1
  
# Take image as input
img_logo = pygame.image.load('gfg_image.jpg')
img_logo.convert()
  
# Draw a rectangle around the image
rect_logo = img_logo.get_rect()
pygame.draw.rect(img_logo, RED, rect_logo, 1)
  
# Set the center and mouse position
center = w//2, h//2
mouse = pygame.mouse.get_pos()
  
#Store the image in a new variable
#Construct the rectangle around image
img = img_logo
rect = img.get_rect()
rect.center = center
  
# Setting what happens when game is
# in running state
while running:
    for event in pygame.event.get():
  
        # Close if the user quits the game
        if event.type == QUIT:
            running = False
  
        # Set at which angle the image will
        # move left or right
        if event.type == KEYDOWN:
            if event.key == K_ra:
                if event.mod & KMOD_SHIFT:
                    angle -= 5
                else:
                    angle += 5
  
            # Set at what ratio the image will
            # decrease or increase
            elif event.key == K_sa:
                if event.mod & KMOD_SHIFT:
                    scale /= 1.5
                else:
                    scale *= 1.5
                  
        # Move the image with the specified coordinates,
        # angle and scale        
        elif event.type == MOUSEMOTION:
            mouse = event.pos
            x = mouse[0] - center[0]
            y = mouse[1] - center[1]
            d = math.sqrt(x ** 2 + y ** 2)
            angle = math.degrees(-math.atan2(y, x))
            scale = abs(5 * d / w)
            img = pygame.transform.rotozoom(img_logo, angle, scale)
            rect = img.get_rect()
            rect.center = center
      
    # Set screen color and image on screen
    screen.fill(YELLOW)
    screen.blit(img, rect)
  
    # Draw the rectangle, line and circle through
    # which image can be transformed 
    pygame.draw.rect(screen, BLACK, rect, 3)
    pygame.draw.line(screen, RED, center, mouse, 2)
    pygame.draw.circle(screen, RED, center, 6, 1)
    pygame.draw.circle(screen, BLACK, mouse, 6, 2)
      
    # Update the GUI game
    pygame.display.update()
  
# Quit the GUI game
pygame.quit()


输出: