【问题标题】:moving circles together in pygame [duplicate]在pygame中一起移动圆圈[重复]
【发布时间】:2022-01-04 05:18:01
【问题描述】:

我从事这个项目的时间太长了。在这一点上我一无所知。我是 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()

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

【问题讨论】:

    标签: python pygame pygame-surface


    【解决方案1】:

    这是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()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()
    

    【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 好的,我添加了,但是运行时还是黑屏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    相关资源
    最近更新 更多