在 pygame 中一起移动圆圈

moving circles together in pygame

我从事这个项目的时间太长了。在这一点上我一无所知。我完全是 python 的菜鸟。我刚开始学习python,我应该画画。我现在只想放弃,因为基本上没有人愿意帮助我。这就是该项目的实际外观:

from turtle import Screen, Turtle

listCircleColors = ['red', 'blue', 'green', 'orange', 'yellow', 'purple', 'white']

intGroup = 5
angleLeft = 360 / (intGroup * len(listCircleColors))

def moveAndDraw(turtle):
    turtle.left(90)
    turtle.penup()
    turtle.forward(15)
    turtle.pendown()
    turtle.right(90)
    turtle.circle(100)

def moveSameColorCircle():
    for color in listCircleColors:
        for _ in range(12):
            for turtle in listCircleObjects:
                if turtle.pencolor() == color:
                    turtle.undo()
                    moveAndDraw(turtle)

            screen.update()

screen = Screen()
screen.setup(900, 900)
screen.bgcolor('black')
screen.tracer(False)

headAngle = 0
listCircleObjects = list()

for _ in range(intGroup):
    for color in listCircleColors:
         turtle = Turtle()
        turtle.hideturtle()
        turtle.setheading(headAngle)
        turtle.color(color)
        turtle.pensize(2)
        turtle.circle(100)
     headAngle += angleLeft

    listCircleObjects.append(turtle)

screen.update()

screen.ontimer(moveSameColorCircle, 500)
screen.exitonclick()

基本上,我应该画一个万能图,然后按颜色将它们移到屏幕的两侧,但是在一组中,首先将所有红色圆圈移到屏幕的两侧,然后是绿色,然后蓝色等。我想做完全相同的事情,但使用 pygame。到目前为止,我已经能够绘制一个螺旋图并移动它,一次不是一个颜色组,而是整个螺旋图向右移动。我已经阅读了很多关于移动多个对象和创建不同屏幕的堆栈溢出帖子,但我似乎仍然无法弄清楚如何通过其颜色组移动圆圈。这是我当前的代码:

import pygame
import math
import sys
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

#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
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


    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.update()
clock.tick(20)

pygame.quit()
exit()

另外,出于某种原因,当我 运行 这段代码时,什么也没有出现,只有黑屏。我不知道我做错了什么,但在写这篇文章之前,我确保一切正常。感谢您的帮助。

为了显示对屏幕所做的更改,请将 pygame.display.flip() 添加到游戏循环的末尾。
只有翻转后才会显示更改。
Further documentation here

这是Indentation的事情。需要在应用程序循环中更新显示:

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


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

# INDENTATION
#-->|

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

pygame.quit()
exit()

如果您在与 PyGame 显示关联的 Surface 上绘图,则不会立即在显示中显示。当使用 pygame.display.update() or pygame.display.flip().

更新显示时,更改变得可见

完整且经过测试的代码:

import pygame
import math
import sys
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

#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
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


    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.update()
    clock.tick(20)

pygame.quit()
exit()