在Pygame中鼠标单击时调整图像大小

时间:2015-04-25 13:07:10

标签: python pygame

此代码显示图像并起作用:

import pygame

from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((900,900))

lion = pygame.image.load("lion.jpg")

while true:
   screen.blit(lion, (0,0))
   pygame.display.update()

我还希望能够右键单击图像来调整其大小。例如:

pygame.event.get()
buttonpress = pygame.mouse.get_pressed()
press = pygame.key.get_pressed()
screen.blit(lion,(100-(lion.get_width()/2, 100-(lion.get_height()/2))))
pygame.event.quit

然而,只要我点击pygame窗口,它就会停止响应而我无法做任何事情。

1 个答案:

答案 0 :(得分:1)

screen.blit()接受两个参数:surface和destination。您似乎正在尝试使用它来调整图像大小。你可以使用pygame.transform.scale()来获取surface和size参数。例如:

done = False
while not done:
    for event in pygame.event.get():
        if event.type == QUIT: #so you can close your window without it crashing or giving an error
            done = True
    pressed_buttons = pygame.mouse.get_pressed() #get a tuple of boolean values for the pressed buttons
    if pressed_buttons[2]: #if the right mouse button is down
        adjusted_lion_image = pygame.transform.scale(lion, (lion.get_wdith() / 2, lion.get_height() / 2)) #set the adjusted image to an image equal to half the size of the original image

    else: #if the right mouse button is not down
        adjusted_lion_image = lion #set the adjusted image back to the lion image

    screen.fill((0, 0, 0)) #fill the screen with black before we draw to make it look cleaner
    screen.blit(adjusted_lion_image, (0, 0)) #blit the adjusted image
    pygame.display.update() #update the screen

pygame.quit() #make sure this is OUTSIDE of the while loop.

这应该可以实现你想要的。您还可能希望在加载狮子图像后添加.convert()以将图像转换为一个pygame可以更容易使用:

lion = pygame.image.load("lion.jpg").convert()