【问题标题】:How to rotate a triangle continuosly in pygame?如何在pygame中连续旋转三角形?
【发布时间】:2015-08-09 10:52:59
【问题描述】:

我正在尝试使用渐变来更改三角形的颜色,同时定期旋转它。这是我的代码。清除表面似乎有问题,因为之前的输出没有被清除,并且渐变流效果仅对第一个三角形可见。

import pygame, sys
import time
import random
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((500, 500), 0, 32)
pygame.display.set_caption('Drawing')
t = 0
i = 0
d = 0

# set up the colors
WHITE = (255,255,255)
GREEN = ((113, 201, 106),(98, 187, 91),(84, 174, 77),(69, 160, 63),(54, 147, 49),(40, 134, 35))

# draw on the surface object
DISPLAYSURF.fill(WHITE)
newSurf = pygame.transform.rotate(DISPLAYSURF, d)
pygame.draw.polygon(newSurf,GREEN[i],((250,70),(400,300),(100,300)))

t = 0.2
while True:
    DISPLAYSURF.fill(WHITE)
    DISPLAYSURF.blit(newSurf, (0,0,))
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    if i==5:
        f = 0
        d = -90
        newSurf = pygame.transform.rotate(DISPLAYSURF, d)
        time.sleep(0.4)
    if i==0:
        f = 1
    if f==1:
        i = i+1
    else:
        i = i-1
    pygame.draw.polygon(newSurf,GREEN[i],((250,70),(400,300),(100,300)))
    time.sleep(t)

由于输出是动画,我不能在这里发布。相反,我发布了几个屏幕截图-

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您在循环中的基本操作是用白色填充画布,从newSurf 在其上绘制一个三角形,然后在newSurf 的同一位置用不同的颜色绘制一个新三角形。

    经过一些迭代(当i == 5 和绿色最暗时)您旋转newSurf 而不清除它并在其上绘制一个新三角形,改变它的颜色几次并一次又一次地循环。

    您想要做的事情可能涉及多件事情,但我会同意您希望在旋转三角形的同时更改它的颜色:您需要在每一帧旋转画布并在绘制三角形之前清除它。

    另一个问题是您在绘制之前旋转了newSurf,这使得旋转毫无用处。你只能看到背景中有东西在移动,因为你没有在绘图之前清理newSurf

    最后但同样重要的是,您的代码仅在您每次将方形表面旋转 90 度时才有效。因此不修改其大小。如果您要选择其他值,您的 newSurf 将很快变得太大而无法记忆。

    import pygame, sys
    import time
    import random
    from pygame.locals import *
    
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((500, 500), 0, 32)
    pygame.display.set_caption('Drawing')
    i = 0
    d = 20 # change it to your liking
    step = 1
    
    # set up the colors
    WHITE = (255,255,255)
    TRANSPARENT = WHITE + (0,)
    GREEN = ((113, 201, 106),(98, 187, 91),(84, 174, 77),(69, 160, 63),(54, 147, 49),(40, 134, 35))
    newSurf = DISPLAYSURF.copy()
    newSurf.fill(TRANSPARENT)
    
    while True:
        DISPLAYSURF.fill(WHITE)
        triangle = newSurf.copy() # reset the surface to draw triangle on
        pygame.draw.polygon(triangle, GREEN[i],((250,70),(400,300),(100,300))) # draw the triangle on an unrotated surface
        triangle = pygame.transform.rotate(DISPLAYSURF, d*i)
        # since the rotation will likely enlarge the image, compute the position of the top-left corner relative to the rotated image
        width, height = triangle.get_size()
        width = (500-width)/2 # change 500 with DISPLAYSURF.get_size()[0] if you plan on changing the size
        height = (500-height)/2 # change 500 with DISPLAYSURF.get_size()[1] if you plan on changing the size
        DISPLAYSURF.blit(triangle, (width, height))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        i += step
        if not (i % 5):
            step = -step
        time.sleep(0.2)
    

    您还可以使用GREEN[i%5] 和更改实现连续旋转(与此处介绍的来回相反)

    i += step
    if not (i % 5):
        step = -step
    

    i += 1

    【讨论】:

    • 你测试你的代码了吗?对我不起作用。显示黑屏和错误-Traceback (most recent call last): File "D:/trash/asdasdasd.py", line 28, in <module> DISPLAYSURF = pygame.transform.rotate(DISPLAYSURF, d) error: Out of memory
    • @CrakC 不,抱歉。我今天没有合适的环境。更新了答案以添加一个被遗忘的pygame.display.update()。这应该可以解决黑屏问题。也可能是内存错误。如果没有,只需像您一样使用newSurf,但请确保每帧都清除它。
    • 花了一些时间来测试它,让它工作。无论如何,内存错误来自您旋转 90 度增量而我使用 20 的事实。在我的原始代码中将我的更改为 90 不会产生内存错误,将您的更改为 20 也会产生内存错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多