【问题标题】:is it possible to move an object diagonally in pygame?是否可以在pygame中对角移动对象?
【发布时间】:2022-01-05 20:27:28
【问题描述】:

我是 python 的初学者,我刚开始学习 pygame。我正在制作一幅画,画出一个螺旋形图,并将每个颜色组的圆圈从中心移开。我的意思是,因为有 7 种颜色,每种颜色有 5 个圆圈,我想将所有圆圈从中心移开,从所有红色圆圈开始,然后是蓝色,然后是绿色等。但问题是,所有每个颜色组5个圆圈必须一次从中心移开,然后当第一组移动完成后,继续一个接一个地移动其他组,直到它画出几乎像手镯一样的图画。

我想知道如何沿对角线移动圆圈,就像在海龟中一样。我还需要知道如何逐个移动组中的圆圈。我是否制作一个列表并将圆圈存储在列表中?老实说,我不知道。我基本上已经几乎放弃尝试这样做了,哈哈。下面是我当前的代码,如果你运行它,你会看到我只能移动整个 spirpograph,而不是圆圈。任何帮助将不胜感激。

import pygame
import math
import time
#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)
circles = []
circles_rect = []
#draw

alpha = turnangle


for i in range(intGroup):
    for cl in listCircleColor:
        surfacetemp = pygame.Surface((width, height))
        surfacetemp = surfacetemp.convert_alpha()
        surfacetemp.fill((0, 0, 0, 0))
        ##circlerect = pygame.rect
        if alpha > 0 and alpha < 90:
            circlerect = pygame.draw.circle(surfacetemp, cl, (300 + radius * math.cos(math.radians(alpha)), 300 + radius * math.sin(math.radians(alpha))), radius, width=2)
        # second quarter of circles
        if alpha > 90 and alpha < 180:
            circlerect = pygame.draw.circle(surfacetemp, cl, (300 - radius * math.cos(math.radians(180 - alpha)), 300 + radius * math.sin(math.radians(180 - alpha))), radius, width=2)
        # third quarter of circles
        if alpha > 180 and alpha < 270:
            circlerect = pygame.draw.circle(surfacetemp, cl, (300 - radius * math.cos(math.radians(270 - alpha)), 300 - radius * math.sin(math.radians(270 - alpha))), radius, width=2)
        # last quarter of circles
        if alpha > 270 and alpha < 360:
            circlerect = pygame.draw.circle(surfacetemp, cl, (300 + radius * math.cos(math.radians(360 - alpha)), 300 - radius * math.sin(math.radians(360 - alpha))), radius, width=2)

        alpha = alpha + turnangle
        ##circles.append(circlerect)
        circles.append(surfacetemp)
        circles_rect.append(surfacetemp.get_rect())



# move"

#exit only when user clicks on exit button
running = True
clock = pygame.time.Clock()


while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()


    screen.fill((0, 0, 0))

    for crect, ret in zip(circles, circles_rect):
        ret.right += 5
        ret.left += 5
        screen.blit(crect, ret)

    pygame.display.flip()
    clock.tick(20)

【问题讨论】:

    标签: python pygame pygame-surface


    【解决方案1】:

    您将圆圈沿 x 轴(左右)移动两次:

    ret.right += 5
    ret.left += 5
    

    您需要沿 x 和 y 轴移动圆圈:

    ret.right += 5
    ret.top += 5
    

    使用centerxcentery 代替lefttop 更容易理解(参见pygame.Rect):

    ret.centerx += 5
    ret.centery += 5
    

    【讨论】:

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