【问题标题】:How can I move circles in groups in pygame at the same time?一次分组移动圆圈pygame
【发布时间】:2022-01-08 04:26:47
【问题描述】:

所以我在这个项目上工作了很长时间,我终于完成了完成这个项目的最后第二步。我的项目所做的是它使用 pygame 绘制了一个螺旋图,然后将所有圆圈从其原始位置移开并停在窗口的边界处。不过,有一个问题。问题是我必须根据它们的颜色一次移动一组所有圆圈。所以基本上,由于每种颜色有 5 个圆圈,我想一次移动所有 5 个颜色的圆圈。然后,当第一组圆圈完成移动时,继续下一个颜色,一次移动组中的所有 5 个圆圈,依此类推,从红色开始,然后一次移动所有蓝色圆圈,然后一次移动所有的绿色圆圈,等等。 到目前为止,我只设法一次移动所有圆圈,而不是每种颜色的每 5 个圆圈。有没有办法做到这一点?请帮我。我真的很想完成这个。这是我当前的代码:

import time
import pygame
import math
import sys

# setting colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
ORANGE = (255, 127, 0)
YELLOW = (255, 255, 0)
PURPLE = (160, 32, 240)
# setting what order the colors go in
listCircleColor = (RED, BLUE, GREEN, ORANGE, YELLOW, PURPLE, WHITE)
# how many circles per color
intGroup = 5
# the space between each circle
turnangle = 360 / 35
# width of screen
width = 600
# height of screen
height = 600
# radius of circles
radius = 100
# making the screen
screen = pygame.display.set_mode((width, height))
# if the code is running, then continue
running = True
clock = pygame.time.Clock()

##.draw.circle(screen, BLUE, (0, 0), radius, width=2)
alpha = turnangle
circles = []
# draw
alpha = turnangle
for i in range(intGroup):
    for cl in listCircleColor:
        if alpha > 0 and alpha < 90:
            x = 300 + radius * math.cos(math.radians(alpha))
            y = 300 + radius * math.sin(math.radians(alpha))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)
            # second quarter of circles
        if alpha > 90 and alpha < 180:
            x = 300 - radius * math.cos(math.radians(180 - alpha))
            y = 300 + radius * math.sin(math.radians(180 - alpha))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)
            # third quarter of circles
        if alpha > 180 and alpha < 270:
            x = 300 - radius * math.cos(math.radians(alpha - 180))
            y = 300 - radius * math.sin(math.radians(alpha - 180))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)
            # last quarter of circles
        if alpha > 270 and alpha < 360:
            x = 300 + radius * math.cos(math.radians(360 - alpha))
            y = 300 - radius * math.sin(math.radians(360 - alpha))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)

        circles.append(([x, y], cl, alpha))
        alpha = alpha + turnangle
        pygame.display.update()
        clock.tick(10)
        #circle = [pygame.draw.circle(screen, cl, (300 + radius * math.cos(math.radians(alpha)), 300 + radius * math.sin(math.radians(alpha))), radius, width=2)]
        #circles = {'circles': circle.get_rect()}

# move"
trangleedge = radius
movetimes = 1
time.sleep(2)



# exit only when user clicks on exit button
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    screen.fill((0, 0, 0))
    while movetimes <= 100:
        trangleedge = trangleedge + 1
        for circle in circles:
            #x = circle[0][0]
            #y = circle[0][1]
            alpha = circle[2]
            cl = circle[1]

            if alpha > 0 and alpha < 90:
                x = 300 + trangleedge * math.cos(math.radians(alpha))
                y = 300 + trangleedge * math.sin(math.radians(alpha))
                #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
                # second quarter of circles
            if alpha > 90 and alpha < 180:
                x = 300 - trangleedge * math.cos(math.radians(180 - alpha))
                y = 300 + trangleedge * math.sin(math.radians(180 - alpha))
                #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
                # third quarter of circles
            if alpha > 180 and alpha < 270:
                x = 300 - trangleedge * math.cos(math.radians(alpha - 180))
                y = 300 - trangleedge * math.sin(math.radians(alpha - 180))
                #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
                # last quarter of circles
            if alpha > 270 and alpha < 360:
                x = 300 + trangleedge * math.cos(math.radians(360 - alpha))
                y = 300 - trangleedge * math.sin(math.radians(360 - alpha))
                #pygame.draw.circle(screen, cl, (x, y), radius, width=2)

            circle[0][0] = x
            circle[0][1] = y
            screen.fill((0, 0, 0))
            for center, color, alpha in circles:
                pygame.draw.circle(screen, color, center, radius, 2)

            pygame.display.flip()
        clock.tick(60)
        movetimes += 1

【问题讨论】:

    标签: python pygame pygame-surface


    【解决方案1】:

    您必须循环遍历不同的颜色并为每个颜色重复动画。这是我的代码(只有第二个循环,其余保持不变。):

    # exit only when user clicks on exit button
    while running:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
        screen.fill((0, 0, 0))
        #prevent the animation from continuing forever
        if moveoutside:
            #loop trough different colors
            for circlecolor in listCircleColor:
                #reset loop variables to prevent weird things
                trangleedge=radius
                movetimes=1
                while movetimes <= 100:
                    
                    trangleedge = trangleedge + 1
                                  #only redraw circles with a certain color
                    for circle in [x for x in circles if x[1]==circlecolor]:
                        #x = circle[0][0]
                        #y = circle[0][1]
                        alpha = circle[2]
                        cl = circle[1]
    
                        if alpha > 0 and alpha < 90:
                            x = 300 + trangleedge * math.cos(math.radians(alpha))
                            y = 300 + trangleedge * math.sin(math.radians(alpha))
                            #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
                            # second quarter of circles
                        if alpha > 90 and alpha < 180:
                            x = 300 - trangleedge * math.cos(math.radians(180 - alpha))
                            y = 300 + trangleedge * math.sin(math.radians(180 - alpha))
                            #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
                            # third quarter of circles
                        if alpha > 180 and alpha < 270:
                            x = 300 - trangleedge * math.cos(math.radians(alpha - 180))
                            y = 300 - trangleedge * math.sin(math.radians(alpha - 180))
                            #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
                            # last quarter of circles
                        if alpha > 270 and alpha < 360:
                            x = 300 + trangleedge * math.cos(math.radians(360 - alpha))
                            y = 300 - trangleedge * math.sin(math.radians(360 - alpha))
                            #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
    
                        circle[0][0] = x
                        circle[0][1] = y
                        screen.fill((0, 0, 0))
                        for center, color, alpha in circles:
                            pygame.draw.circle(screen, color, center, radius, 2)
    
                        pygame.display.flip()
                    clock.tick(60)
                    movetimes += 1
            #prevent the animation from continuing forever
            moveoutside=False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 2022-01-19
      • 2019-10-17
      • 1970-01-01
      • 2018-05-30
      相关资源
      最近更新 更多