Pygame 的扩展问题

发布于 2024-12-05 11:58:58 字数 452 浏览 1 评论 0原文

当我尝试缩放对象时,只有图像的顶部和左侧变大。其余部分保持不变。我想要一个均匀的比例。

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))

桶状总是越来越大(作为一个数字),直到它离开屏幕。 我在 Windows XP 上使用 Python 2.7。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笔芯 2024-12-12 11:58:58

我找到问题所在了!按照我编写程序的方式,新的桶图像不断被重用。因此,在我显示桶之后,我在最后输入:

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")

这样,当比例变量发生变化时,它会影响新图像,而不是修改后的图像。

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.

陪我终i 2024-12-12 11:58:58

“图像的顶部和左侧变大”是否意味着图像在中间不会变大,左上角始终保持在同一位置?
如果我理解正确的话你可以尝试这个:

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)])

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)])
紫南 2024-12-12 11:58:58

不应该是这样吗:

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))

在您的代码中,您为 pygame.transform.scale() 的宽度和高度都指定了barrely,

而且我看不到您更新barrelx和barrely的位置。在您粘贴的代码中barrelx和barrely将始终为0。

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.

潦草背影 2024-12-12 11:58:58

我不完全清楚你的问题是什么。你是说只有左边和上边变大了吗?这可能是 pygame 中的一个错误,因为 scale 不是应该像那样工作。矩形始终是矩形(每个图像都是矩形)。

如果图片向左和顶部扩展,则意味着在循环中的某个位置,在调整图像大小之前或之后,您将桶形图像向左和向上移动,移动量与桶增长的量相同。只需删除它即可。 (缩放图像时,左上角始终保持在同一位置)。

如果这没有解决您的问题,您可能应该更详细地进行解释。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文