【问题标题】:moving circles in pygame在pygame中移动圆圈
【发布时间】:2022-01-06 02:54:08
【问题描述】:

我有一个代码,可以将螺旋计上的所有圆圈从中心移开。这就是它的样子:

然后,我想将所有的圆圈一起从中心移开:

这是我当前的代码:

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

这是结果:

它基本上会创建 2 张图片并将它们拼凑在一起,但我不希望这样。我希望它一开始是一个螺旋计,然后所有的圆圈都慢慢地从中心移开。

编辑:https://imgur.com/a/lFZNtqw这是完成的项目应该看起来的样子,但是当圆圈移动时没有“延迟”。我的意思是圆圈应该一起移动,而不是一个接一个此处显示的代码。

编辑 2:感谢有人向我展示了他们的代码,我实现了我想要的。然后,我以他们的代码为例,并按照我的描述进行了编写。现在我只需要将圆圈按颜色分组即可。

【问题讨论】:

    标签: python pygame pygame-surface


    【解决方案1】:

    您需要更改每个圆的中心到窗口中心的距离。使用pygame.math.Vector2。计算从窗口中心到圆心的向量(v)并用scale_to_length缩放:

    window_center = pygame.math.Vector2(300, 300)
    for circle in circles:
        circle_center = pygame.math.Vector2(circle[0])
        v = circle_center - window_center 
        v.scale_to_length(distance_from_center)
        new_circle_center = window_center + v
        circle[0][0] = round(new_circle_center.x)
        circle[0][1] = round(new_circle_center.y)
    

    按时间更改distance_from_center

    direction = 1
    distance_from_center = radius
    
    while running:
        # [...]
    
        distance_from_center += direction
        if distance_from_center >= 200 or distance_from_center <= 100:
            direction *= -1
    

    如果您只想将圆圈向外移动,您可以执行以下操作:

    distance_from_center = radius
    
    while running:
        # [...]
    
        if distance_from_center < 200:
            distance_from_center += 1
    

    完整示例:

    import pygame
    import math
    
    # 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
    
    ##.draw.circle(screen, BLUE, (0, 0), radius, width=2)
    alpha = turnangle
    circles = []
    # draw
    alpha = turnangle
    for i in range(intGroup):
        for cl in listCircleColor:
            x = 300 + radius * math.cos(math.radians(alpha))
            y = 300 + radius * math.sin(math.radians(alpha))
            circles.append(([x, y], cl, alpha))
            alpha = alpha + turnangle
    
    clock = pygame.time.Clock()
    direction = 1
    distance_from_center = radius
    
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        window_center = pygame.math.Vector2(300, 300)
        for circle in circles:
            circle_center = pygame.math.Vector2(circle[0])
            v = circle_center - window_center 
            v.scale_to_length(distance_from_center)
            new_circle_center = window_center + v
            circle[0][0] = round(new_circle_center.x)
            circle[0][1] = round(new_circle_center.y)
    
        # move in and out
        distance_from_center += direction
        if distance_from_center >= 200 or distance_from_center <= 100:
            direction *= -1
    
        # only move out
        #if distance_from_center < 200:
        #    distance_from_center += 1
    
        screen.fill((0, 0, 0))
        for center, color, alpha in circles:
            pygame.draw.circle(screen, color, center, radius, 2)
        pygame.display.update()
        clock.tick(40)
    
    pygame.quit()
    exit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      相关资源
      最近更新 更多