移动呼吸描记器 pygame
Moving spirograph pygame
所以我是 python 的新手,我想绘制一个螺旋图,每次将每个颜色组的所有圆圈移动到屏幕的两侧。首先将所有红色圆圈移到两侧,然后是蓝色,然后是绿色,等等。不过我很难过。我的意思是总共有 35 个圆圈,每种颜色有 5 个圆圈。 (有 7 种颜色)然后我想要第一个颜色组(红色),将所有 5 个红色圆圈从其余部分移开。然后对以下6种颜色做同样的操作。
import pygame
import math
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)
listCircleColor = (RED, BLUE, GREEN, ORANGE, YELLOW, PURPLE, WHITE)
intGroup = 5
turnangle = 360/35
width = 600
height = 600
radius = 100
screen = pygame.display.set_mode((width, height))
running = True
##.draw.circle(screen, BLUE, (0, 0), radius, width=2)
alpha = turnangle
for i in range(intGroup):
for cl in listCircleColor:
if alpha > 0 and alpha < 90:
pygame.draw.circle(screen, cl, (300 + radius *
math.cos(math.radians(alpha)), 300 + radius *
math.sin(math.radians(alpha))), radius, width=2)
if alpha > 90 and alpha < 180:
pygame.draw.circle(screen, cl, (300 - radius *
math.cos(math.radians(180 - alpha)), 300 + radius *
math.sin(math.radians(180 - alpha))), radius, width=2)
if alpha > 180 and alpha < 270:
pygame.draw.circle(screen, cl, (300 - radius *
math.cos(math.radians(270 - alpha)), 300 - radius *
math.sin(math.radians(270 - alpha))), radius, width=2)
if alpha > 270 and alpha < 360:
pygame.draw.circle(screen, cl, (300 + radius *
math.cos(math.radians(360 - alpha)), 300 - radius *
math.sin(math.radians(360 - alpha))), radius, width=2)
alpha = alpha + turnangle
pygame.display.flip()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
根据您的评论,代码与之前相比没有太大变化。这里是。向左、向右、顶部或底部移动。
import pygame
import math
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)
listCircleColor = (RED, BLUE, GREEN, ORANGE, YELLOW, PURPLE, WHITE)
intGroup = 5
turnangle = 360/35
width = 600
height = 600
screenCentre = (width // 2, height // 2)
radius = 100
screen = pygame.display.set_mode((width, height))
running = True
#circles list will store circles.
#Each circle is represented as a list.
#your original code that generates
#circles will now add the circles to this
#list instead of directly drawing it.
#As you can see bellow, each circle list
#looks like this.
## [
## cl, -> color
## 300 + radius * math.cos(math.radians(alpha)), -> x position
## 300 + radius * math.sin(math.radians(alpha)), -> y position
## (
## 0, -> x direction to move in
## 1 -> y direction to move in
## )
## ]
circles = []
currentColorIndex = 0
speed = 0.4
alpha = turnangle
for i in range(intGroup):
for cl in listCircleColor:
if alpha > 0 and alpha < 90:
circles.append([
cl,
300 + radius * math.cos(math.radians(alpha)),
300 + radius * math.sin(math.radians(alpha)),
#all the circles between 0 and 90 degrees
#will travel down. Remember that y axis
#in computer coordinates are flipped, i.e
#y-axis increases in value as you travel down
#the monitor so 0 -90 degrees actually go clockwise (down)
#rather than anti clockwise. Similar story for other alpha
#values as well, just a different direction.
(
0,
1
)
])
if alpha > 90 and alpha < 180:
circles.append([
cl,
300 - radius * math.cos(math.radians(180 - alpha)),
300 + radius * math.sin(math.radians(180 - alpha)),
(
-1,
0
)
])
if alpha > 180 and alpha < 270:
circles.append([
cl,
300 - radius * math.cos(math.radians(270 - alpha)),
300 - radius * math.sin(math.radians(270 - alpha)),
(
0,
-1
)
])
if alpha > 270 and alpha < 360:
circles.append([
cl,
300 + radius * math.cos(math.radians(360 - alpha)),
300 - radius * math.sin(math.radians(360 - alpha)),
(
1,
0
)
])
alpha = alpha + turnangle
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#move
#this is a list that will contain all the circles that we
#currently need to be moving
circlesToMove = []
#currentColorIndex is a number that is used to index into
#listCircleColor. Everytime we finish moving all circles of
#the same color, currentColorIndex is increases by 1. This is
#important because we want all the circles with the same
#color to be where we want it to be, before moving the next set.
#This way, currentlyMoving will always hold the color of the
#circle that we are currently moving.
currentlyMoving = listCircleColor[currentColorIndex]
#finishedMovingAllFive is initialised as true.
finishedMovingAllFive = True
#go through every circle in the list
for circle in circles:
#see if the color of the circle is the one that we currently need to move.
#remember circle[0] is the color because color is
#the first element in the list.
if circle[0] == currentlyMoving:
#than calculate the distance between the centre of the screen and the circle. If that distance is
#less than 250, we add it to the circlesToMove list. 250 is a number that i choose that
#seemed to fit the needs, so it is up to you to change it.
#As long as the circles of the color that we are currently moving are less than
#250 pixels away from the centre of the screen, they will keep being added to the circlesToMove list.
if math.sqrt( (circle[1] - screenCentre[0])**2 + (circle[2] - screenCentre[1])**2 ) <= 250:
circlesToMove.append(circle)
#Remember that finishedMovingAllFive was set to true before. If the above if statement executes,
#it means that at least one of the circles are still less than 250 pixels away and still need to be moved.
#so finishedMovingAllFive is set to false.
finishedMovingAllFive = False
#currentColorIndex < len(listCircleColor) - 1 is just a safety check to make sure we dont go outside the
#bounds of the list. The important thing here is finishedMovingAllFive. If finishedMovingAllFive is true, we
#increase currentColorIndex by 1, because remember the only way for finishedMovingAllFive to be true is if
#if math.sqrt( (circle[1] - screenCentre[0])**2 + (circle[2] - screenCentre[1])**2 ) <= 250: never executed,
#which also means that all of the circles are now more than 250 pixels away so we can move on to the next set
#of circles.
if currentColorIndex < len(listCircleColor) - 1 and finishedMovingAllFive:
currentColorIndex += 1
#now we simply go through all the circles to move and move them. Remember that direction it needs to move in
#is the fourth element in the circle list which is a tuple. So, circles[3] would get us to that tuple,
#which might look like this, depending on the circle.
# (
# 0,
# 1
# )
#So natually, circles[3][0] would give you the x direction and
#circles[3][1] would give you the y direction.
#Also remember that circles[1] is the x position of the circle is
#circles[2] is the y position.
for circle in circlesToMove:
circle[1] += circle[3][0] * speed
circle[2] += circle[3][1] * speed
#Draw
screen.fill((0, 0, 0))
#then we go through all the circles and draw them.
for circle in circles:
pygame.draw.circle(screen, circle[0], (circle[1], circle[2]), radius, width=2)
pygame.display.flip()
尽管在我看来,按照创建它的总体方向看起来会更好:
alpha = turnangle
for i in range(intGroup):
for cl in listCircleColor:
if alpha > 0 and alpha < 90:
circles.append([
cl,
300 + radius * math.cos(math.radians(alpha)),
300 + radius * math.sin(math.radians(alpha)),
(
math.cos(math.radians(45)),
math.sin(math.radians(45))
)
])
if alpha > 90 and alpha < 180:
circles.append([
cl,
300 - radius * math.cos(math.radians(180 - alpha)),
300 + radius * math.sin(math.radians(180 - alpha)),
(
math.cos(math.radians(180 - 45)),
math.sin(math.radians(180 - 45))
)
])
if alpha > 180 and alpha < 270:
circles.append([
cl,
300 - radius * math.cos(math.radians(270 - alpha)),
300 - radius * math.sin(math.radians(270 - alpha)),
(
math.cos(math.radians(270 - 45)),
math.sin(math.radians(270 - 45))
)
])
if alpha > 270 and alpha < 360:
circles.append([
cl,
300 + radius * math.cos(math.radians(360 - alpha)),
300 - radius * math.sin(math.radians(360 - alpha)),
(
math.cos(math.radians(360 - 45)),
math.sin(math.radians(360 - 45))
)
])
alpha = alpha + turnangle
所以我是 python 的新手,我想绘制一个螺旋图,每次将每个颜色组的所有圆圈移动到屏幕的两侧。首先将所有红色圆圈移到两侧,然后是蓝色,然后是绿色,等等。不过我很难过。我的意思是总共有 35 个圆圈,每种颜色有 5 个圆圈。 (有 7 种颜色)然后我想要第一个颜色组(红色),将所有 5 个红色圆圈从其余部分移开。然后对以下6种颜色做同样的操作。
import pygame
import math
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)
listCircleColor = (RED, BLUE, GREEN, ORANGE, YELLOW, PURPLE, WHITE)
intGroup = 5
turnangle = 360/35
width = 600
height = 600
radius = 100
screen = pygame.display.set_mode((width, height))
running = True
##.draw.circle(screen, BLUE, (0, 0), radius, width=2)
alpha = turnangle
for i in range(intGroup):
for cl in listCircleColor:
if alpha > 0 and alpha < 90:
pygame.draw.circle(screen, cl, (300 + radius *
math.cos(math.radians(alpha)), 300 + radius *
math.sin(math.radians(alpha))), radius, width=2)
if alpha > 90 and alpha < 180:
pygame.draw.circle(screen, cl, (300 - radius *
math.cos(math.radians(180 - alpha)), 300 + radius *
math.sin(math.radians(180 - alpha))), radius, width=2)
if alpha > 180 and alpha < 270:
pygame.draw.circle(screen, cl, (300 - radius *
math.cos(math.radians(270 - alpha)), 300 - radius *
math.sin(math.radians(270 - alpha))), radius, width=2)
if alpha > 270 and alpha < 360:
pygame.draw.circle(screen, cl, (300 + radius *
math.cos(math.radians(360 - alpha)), 300 - radius *
math.sin(math.radians(360 - alpha))), radius, width=2)
alpha = alpha + turnangle
pygame.display.flip()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
根据您的评论,代码与之前相比没有太大变化。这里是。向左、向右、顶部或底部移动。
import pygame
import math
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)
listCircleColor = (RED, BLUE, GREEN, ORANGE, YELLOW, PURPLE, WHITE)
intGroup = 5
turnangle = 360/35
width = 600
height = 600
screenCentre = (width // 2, height // 2)
radius = 100
screen = pygame.display.set_mode((width, height))
running = True
#circles list will store circles.
#Each circle is represented as a list.
#your original code that generates
#circles will now add the circles to this
#list instead of directly drawing it.
#As you can see bellow, each circle list
#looks like this.
## [
## cl, -> color
## 300 + radius * math.cos(math.radians(alpha)), -> x position
## 300 + radius * math.sin(math.radians(alpha)), -> y position
## (
## 0, -> x direction to move in
## 1 -> y direction to move in
## )
## ]
circles = []
currentColorIndex = 0
speed = 0.4
alpha = turnangle
for i in range(intGroup):
for cl in listCircleColor:
if alpha > 0 and alpha < 90:
circles.append([
cl,
300 + radius * math.cos(math.radians(alpha)),
300 + radius * math.sin(math.radians(alpha)),
#all the circles between 0 and 90 degrees
#will travel down. Remember that y axis
#in computer coordinates are flipped, i.e
#y-axis increases in value as you travel down
#the monitor so 0 -90 degrees actually go clockwise (down)
#rather than anti clockwise. Similar story for other alpha
#values as well, just a different direction.
(
0,
1
)
])
if alpha > 90 and alpha < 180:
circles.append([
cl,
300 - radius * math.cos(math.radians(180 - alpha)),
300 + radius * math.sin(math.radians(180 - alpha)),
(
-1,
0
)
])
if alpha > 180 and alpha < 270:
circles.append([
cl,
300 - radius * math.cos(math.radians(270 - alpha)),
300 - radius * math.sin(math.radians(270 - alpha)),
(
0,
-1
)
])
if alpha > 270 and alpha < 360:
circles.append([
cl,
300 + radius * math.cos(math.radians(360 - alpha)),
300 - radius * math.sin(math.radians(360 - alpha)),
(
1,
0
)
])
alpha = alpha + turnangle
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#move
#this is a list that will contain all the circles that we
#currently need to be moving
circlesToMove = []
#currentColorIndex is a number that is used to index into
#listCircleColor. Everytime we finish moving all circles of
#the same color, currentColorIndex is increases by 1. This is
#important because we want all the circles with the same
#color to be where we want it to be, before moving the next set.
#This way, currentlyMoving will always hold the color of the
#circle that we are currently moving.
currentlyMoving = listCircleColor[currentColorIndex]
#finishedMovingAllFive is initialised as true.
finishedMovingAllFive = True
#go through every circle in the list
for circle in circles:
#see if the color of the circle is the one that we currently need to move.
#remember circle[0] is the color because color is
#the first element in the list.
if circle[0] == currentlyMoving:
#than calculate the distance between the centre of the screen and the circle. If that distance is
#less than 250, we add it to the circlesToMove list. 250 is a number that i choose that
#seemed to fit the needs, so it is up to you to change it.
#As long as the circles of the color that we are currently moving are less than
#250 pixels away from the centre of the screen, they will keep being added to the circlesToMove list.
if math.sqrt( (circle[1] - screenCentre[0])**2 + (circle[2] - screenCentre[1])**2 ) <= 250:
circlesToMove.append(circle)
#Remember that finishedMovingAllFive was set to true before. If the above if statement executes,
#it means that at least one of the circles are still less than 250 pixels away and still need to be moved.
#so finishedMovingAllFive is set to false.
finishedMovingAllFive = False
#currentColorIndex < len(listCircleColor) - 1 is just a safety check to make sure we dont go outside the
#bounds of the list. The important thing here is finishedMovingAllFive. If finishedMovingAllFive is true, we
#increase currentColorIndex by 1, because remember the only way for finishedMovingAllFive to be true is if
#if math.sqrt( (circle[1] - screenCentre[0])**2 + (circle[2] - screenCentre[1])**2 ) <= 250: never executed,
#which also means that all of the circles are now more than 250 pixels away so we can move on to the next set
#of circles.
if currentColorIndex < len(listCircleColor) - 1 and finishedMovingAllFive:
currentColorIndex += 1
#now we simply go through all the circles to move and move them. Remember that direction it needs to move in
#is the fourth element in the circle list which is a tuple. So, circles[3] would get us to that tuple,
#which might look like this, depending on the circle.
# (
# 0,
# 1
# )
#So natually, circles[3][0] would give you the x direction and
#circles[3][1] would give you the y direction.
#Also remember that circles[1] is the x position of the circle is
#circles[2] is the y position.
for circle in circlesToMove:
circle[1] += circle[3][0] * speed
circle[2] += circle[3][1] * speed
#Draw
screen.fill((0, 0, 0))
#then we go through all the circles and draw them.
for circle in circles:
pygame.draw.circle(screen, circle[0], (circle[1], circle[2]), radius, width=2)
pygame.display.flip()
尽管在我看来,按照创建它的总体方向看起来会更好:
alpha = turnangle
for i in range(intGroup):
for cl in listCircleColor:
if alpha > 0 and alpha < 90:
circles.append([
cl,
300 + radius * math.cos(math.radians(alpha)),
300 + radius * math.sin(math.radians(alpha)),
(
math.cos(math.radians(45)),
math.sin(math.radians(45))
)
])
if alpha > 90 and alpha < 180:
circles.append([
cl,
300 - radius * math.cos(math.radians(180 - alpha)),
300 + radius * math.sin(math.radians(180 - alpha)),
(
math.cos(math.radians(180 - 45)),
math.sin(math.radians(180 - 45))
)
])
if alpha > 180 and alpha < 270:
circles.append([
cl,
300 - radius * math.cos(math.radians(270 - alpha)),
300 - radius * math.sin(math.radians(270 - alpha)),
(
math.cos(math.radians(270 - 45)),
math.sin(math.radians(270 - 45))
)
])
if alpha > 270 and alpha < 360:
circles.append([
cl,
300 + radius * math.cos(math.radians(360 - alpha)),
300 - radius * math.sin(math.radians(360 - alpha)),
(
math.cos(math.radians(360 - 45)),
math.sin(math.radians(360 - 45))
)
])
alpha = alpha + turnangle