是否可以在 pygame 中沿对角线移动对象?
is it possible to move an object diagonally in pygame?
我是 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)
您沿 x 轴(左和右)移动圆圈两次:
ret.right += 5
ret.left += 5
您需要沿 x 轴和 y 轴移动圆圈:
ret.right += 5
ret.top += 5
使用 centerx
和 centery
比 left
和 top
更容易理解(参见 pygame.Rect
):
ret.centerx += 5
ret.centery += 5
我是 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)
您沿 x 轴(左和右)移动圆圈两次:
ret.right += 5 ret.left += 5
您需要沿 x 轴和 y 轴移动圆圈:
ret.right += 5
ret.top += 5
使用 centerx
和 centery
比 left
和 top
更容易理解(参见 pygame.Rect
):
ret.centerx += 5
ret.centery += 5