简体   繁体   English

如何在pygame中连续旋转三角形?

[英]How to rotate a triangle continuosly in pygame?

I am trying to use a gradient to change the color of a triangle and at the same time rotate it at regular intervals. 我正在尝试使用渐变来更改三角形的颜色,并同时以规则的间隔旋转它。 This is my code. 这是我的代码。 There seems to be a problem with clearing the surface as the previous output does not get cleared and the gradient flow effect is only visible for the first triangle. 清理表面似乎存在问题,因为先前的输出没有被清除,并且仅对第一个三角形可见梯度流效果。

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)

Since the output is an animation, I cannot post it here. 由于输出是动画,因此无法在此处发布。 Instead I am posting a couple of screenshots- 相反,我发布了几个屏幕截图-

在此处输入图片说明 在此处输入图片说明

What you basically do in your loop is filling the canvas with white, drawing a triangle on it from newSurf and drawing a new triangle on newSurf at the exact same place with a different color. 循环中的基本操作是用白色填充画布,在newSurf绘制一个三角形, newSurfnewSurf上的同一位置以不同的颜色绘制一个新的三角形。

After some iterations (when i == 5 and the green is the darkest) you rotate newSurf without clearing it and drawing a new triangle on it, changing its color for a few times and looping again and again. 经过一些迭代(当i == 5且绿色最暗时),您旋转newSurf而不清除它并在其上绘制一个新的三角形,更改其颜色几次并一次又一次地循环。

What you want to do might involve multiple things but I'll go with the fact that you want to change the triangle color while rotating it: you need to rotate the canvas each frame and clearing it before drawing the triangle. 您想要做的事情可能涉及很多事情,但是我要继续您要在旋转三角形时更改三角形颜色的事实:您需要在绘制三角形之前每帧旋转画布并清除它。

An other issue is that you rotate your newSurf before drawing on it, making the rotation useless. 另一个问题是您在绘制newSurf之前先对其进行旋转,从而使旋转无效。 You only see something moving in the background because you do not clean newSurf before the drawing. 您只看到背景中有东西在移动,因为您没有在绘图之前清理newSurf

Last but not least, your code only works because you're rotating a square surface by 90 degrees each time. 最后但并非最不重要的一点是,您的代码仅能起作用,因为您每次都将一个正方形表面旋转90度。 Thus not modifying its size. 因此不修改其大小。 If you were to choose an other value, your newSurf would quickly grow too large for your memory. 如果要选择其他值,则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)

You can also achieve a continuous rotation (in opposition to the back-and-forth presented here) using GREEN[i%5] and changing 您还可以使用GREEN[i%5]并通过更改来实现连续旋转(与此处介绍的来回相反)

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

by i += 1 i += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM