【问题标题】:Simple analog clock get slows then Error shows up with Pygame简单的模拟时钟变慢,然后 Pygame 出现错误
【发布时间】:2016-04-05 14:37:02
【问题描述】:

我已经开始学习 pygame 并且编写了一个简单的模拟时钟。

import sys, pygame
pygame.init()

white = 255, 255, 255
size = width, height = 480, 480
screen = pygame.display.set_mode(size)

minute_hand = pygame.image.load('minute_hand.png')
minute_hand_rect = minute_hand.get_rect()  

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    center = minute_hand_rect.center
    rotate = pygame.transform.rotate

    minute_hand = rotate(minute_hand, -1)
    minute_hand_rect = minute_hand.get_rect(center=center)

    screen.fill(white)
    screen.blit(minute_hand, minute_hand_rect)

    pygame.display.update()
    pygame.time.delay(100)

但是我的手时钟越来越慢了一会儿然后停止运行并说:

Traceback (most recent call last):
  File "clock.py", line 21, in <module>
    minute_hand = rotate(minute_hand, -1)
pygame.error: Width or height is too large

显然我做错了什么,但我不知道哪里出了问题。

【问题讨论】:

  • 这是因为当您以 90 度以外的增量旋转时,它会放大您的图像尺寸以容纳新旋转的图像
  • 增加屏幕尺寸,使其足够大以容纳图像,或使用较小的图像,因为正如 Racialz 所说,旋转会增加图像的大小。

标签: python pygame python-2.x


【解决方案1】:

问题是minute_hand = rotate(minute_hand, -1)

这是使用最后一帧图像来生成下一帧图像,这意味着每次更新时都会存储整个转换数据负载。它还给了我一个“内存不足错误”,您会注意到导致图像随着时间的推移而失真

解决办法是继续使用原图,多旋转一下

如下图:

import sys, pygame
pygame.init()

white = 255, 255, 255
size = width, height = 480, 480
screen = pygame.display.set_mode(size)

minute_hand = pygame.image.load('box4.jpg')
minute_hand_rect = minute_hand.get_rect()  

angle = 0

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    center = minute_hand_rect.center
    rotate = pygame.transform.rotate

    new_image = rotate(minute_hand, angle)
    new_image_rect = new.get_rect(center=center)

    angle -= 1
    if angle == -360:
        angle = 0

    screen.fill(white)
    screen.blit(new, new_rect)

    pygame.display.update()
    pygame.time.delay(100)

这需要一个改变每一帧的角度并将其应用于原始帧

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2015-05-26
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2021-04-16
    相关资源
    最近更新 更多