Scaling Problem with Pygame
When I attempt to scale an object, only the top and left of the image get bigger. The rest stays the same. I want an even scale.
import pygame._view
import pygame, sys
from pygame.locals import *
import random
pygame.init()
barrel = pygame.image.load("images\Barrel.gif")
barrelx = 0
barrely = 0
while running:
barrel = pygame.transform.scale(barrel, (int(barrely/4), int(barrely开发者_如何学运维/4)))
screen.blit(barrel, (barrelx, barrely))
barrely is always getting bigger (as a number) until it gets off-screen. I'm using Python 2.7 on Windows XP.
I figured out the problem! The way I wrote the program, the new barrel image was kept being reused. So, right after I displayed the barrel, I put in at the end:
while running:
...
barrel = pygame.transform.scale(barrel, (init(barrely/4),init(barrely/4)))
screen.blit(barrel, (barrelx, barrely))
barrel = pygame.image.load("images\Barrel.gif")
This way, while the scale variable is changing, it affects a fresh image, not a modified image.
Does "the top and left of the image get bigger" mean that the image doesn't get bigger around the middle, the upper left always stays at the same position? If I understood it right you could try this:
import pygame._view
import pygame, sys
from pygame.locals import *
import random
pygame.init()
barrel1 = pygame.image.load("images\Barrel.gif")
barrel1x = 0
barrel1y = 0
#get width,height of image
width1,height1 = barrel1.get_size()
running = True
while running:
barrel2 = pygame.transform.scale(barrel1, (int(barrely/4), int(barrely/4)))
width2,height2 = barrel2.get_size()
#position - difference of width or height /2
screen.blit(barrel, [round(barrelx - (width1 - width2)/2),
round(barrely - (height1 - height2)/2)])
Shouldn't that be:
import pygame._view
import pygame, sys
from pygame.locals import *
import random
pygame.init()
barrel = pygame.image.load("images\Barrel.gif")
barrelx = 0
barrely = 0
while running:
barrel = pygame.transform.scale(barrel, (int(barrelx/4), int(barrely/4)))
screen.blit(barrel, (barrelx, barrely))
In your code you give barrely for both the width and the height of the pygame.transform.scale()
Also I can't see where you update barrelx and barrely. In the code you pasted barrelx and barrely will always be 0.
It is not completely clear to me what your problem is. Are you saying that only the left and upper sides are getting bigger? This would probably be a bug in pygame, because scale is not supposed to work like that. A rectangle stays a rectangle (and every image is a rectangle).
If the picture expands to the left and top, that means that somewhere in the loop, before or after resizing the image, you are moving the barrel image to the left and up, the same amount as the barrel grows. Just remove that. (When you scale an image, the top-left corner always stays in the same spot).
If that didn't solve your problem, you should probably go into more detail with your explanation.
精彩评论