2015-08-09 115 views
2

我正在尝试使用渐变来更改三角形的颜色,并同时定期旋转它。这是我的代码。清理曲面似乎有问题,因为先前的输出没有被清除,并且梯度流动效果仅对第一个三角形可见。如何在pygame中连续旋转三角形?

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) 

由于输出是一个动画,我不能在这里发布它。相反,我张贴了几个screenshots-

enter image description hereenter image description here

回答

0

你基本上做你的循环是什么填充画布上用白色,在准确的从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

+0

你测试你的代码?不适合我。显示一个黑色的屏幕和错误 - “Traceback(最近调用最后一个): 文件”D:/trash/asdasdasd.py“,第28行,在 DISPLAYSURF = pygame.transform.rotate(DISPLAYSURF,d) error :内存不足' – CrakC

+0

@CrakC不好意思,对不起。我今天没有适合的环境。更新了答案以添加一个被遗忘的'pygame.display.update()'。这应该解决黑屏问题。也许是内存错误。如果没有,就像你一样使用'newSurf',但一定要清除它每一帧。 –

+0

不。还是行不通! – CrakC