Pygame 移动圆圈
Pygame moving circles
我是 python 的新手,我正在 pygame 中研究移动圆圈。我试图将圆圈放在一个组中并移动它们。我卡住了。现在,我正试图将圆圈放入列表中,但我不断收到错误消息。我从事这个项目太久了,我快要放弃了。任何帮助将不胜感激
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
##.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:
pygame.draw.circle(screen, 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:
pygame.draw.circle(screen, 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:
pygame.draw.circle(screen, 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:
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
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"
"""
circlesToMove = []
finishedMovingAllFive = False
for circle in circlesToMove:
circle[1] += circle[3][0] * speed
circle[2] += circle[3][1] * speed
"""
for circle in circles:
circles[circle].right+=1
pygame.time.Clock().tick(40)
#updating display
pygame.display.flip()
#exit only when user clicks on exit button
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.draw.circle
doesn't create an object. pygame.draw.circle
draws a circle on the screen (on a surface) and recturns a pygame.Rect
带有圆边界框的对象。
您必须用圈子的数据创建一个列表。列表中的每个元素都必须包含圆心和圆的颜色
circles = []
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 + turnangle
在应用程序循环中移动并绘制圆圈:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
for circle in circles:
circle[0][0] += 1
screen.fill(0)
for center, color in circles:
pygame.draw.circle(screen, color, center, radius, 2)
pygame.display.flip()
pygame.time.Clock().tick(40)
我是 python 的新手,我正在 pygame 中研究移动圆圈。我试图将圆圈放在一个组中并移动它们。我卡住了。现在,我正试图将圆圈放入列表中,但我不断收到错误消息。我从事这个项目太久了,我快要放弃了。任何帮助将不胜感激
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
##.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:
pygame.draw.circle(screen, 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:
pygame.draw.circle(screen, 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:
pygame.draw.circle(screen, 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:
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
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"
"""
circlesToMove = []
finishedMovingAllFive = False
for circle in circlesToMove:
circle[1] += circle[3][0] * speed
circle[2] += circle[3][1] * speed
"""
for circle in circles:
circles[circle].right+=1
pygame.time.Clock().tick(40)
#updating display
pygame.display.flip()
#exit only when user clicks on exit button
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.draw.circle
doesn't create an object. pygame.draw.circle
draws a circle on the screen (on a surface) and recturns a pygame.Rect
带有圆边界框的对象。
您必须用圈子的数据创建一个列表。列表中的每个元素都必须包含圆心和圆的颜色
circles = []
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 + turnangle
在应用程序循环中移动并绘制圆圈:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
for circle in circles:
circle[0][0] += 1
screen.fill(0)
for center, color in circles:
pygame.draw.circle(screen, color, center, radius, 2)
pygame.display.flip()
pygame.time.Clock().tick(40)