简体   繁体   English

为什么我的矩形不绘制? - 蟒蛇和pygame

[英]Why won't my rectangles draw? - python and pygame

I have a few questions.我有几个问题。 The first: I'm sure the below code can be optimized to not use 4 classes, however I'm not sure how to go about it.第一个:我确定下面的代码可以优化为不使用 4 个类,但是我不确定如何去做。 I'm trying to draw a row and column of circles with smaller circles orbiting the center of each circle.我正在尝试绘制一行和一列圆圈,其中较小的圆圈围绕每个圆圈的中心。 The row of circles has vertical lines that follow the rotation of the smaller orbiting circles.一排圆圈具有跟随较小轨道圆圈旋转的垂直线。 The columns follow the same pattern however the lines are horizontal.列遵循相同的模式,但线条是水平的。

As of now, I'm trying to use four classes, one for the horizontal circles, one for the vertical circles, one for the horizontal rectangles (using rectangles because I will detect collisions), and one more for the vertical rectangles.到目前为止,我正在尝试使用四种类,一种用于水平圆,一种用于垂直圆形,一种用于水平矩形(使用矩形是因为我会检测碰撞),另一种用于垂直矩形。 I'm doing this to pass info to the rectangles in order to correspond their position with the orbiting circles.我这样做是为了将信息传递给矩形,以便将它们的位置与轨道圆相对应。

The second question/issue: as the code is now, the vertical lines will not show up when the program is run.第二个问题/问题:就像现在的代码一样,程序运行时不会显示垂直线。 I'm not entirely sure why as the code for drawing the horizontal lines is functioning properly and they are matching.我不完全确定为什么绘制水平线的代码运行正常并且它们匹配。

Any advice as to how to make this better would be much appreciated.任何关于如何改善这一点的建议将不胜感激。

import pygame as pg
import pygame.gfxdraw
import math

pg.init()
windowWidth = 800
windowHeight = 800
surface = pg.display.set_mode((windowWidth, windowHeight))
pg.display.set_caption("Circle")
clock = pg.time.Clock()
black = (0, 0, 0)
white = (255, 255, 255)
gray = (50, 50, 50)
red = (255, 0, 0)

class VerticalCircle(object):
    def __init__(self, posX, posY):
        self.circlePositionX = posX
        self.circlePositionY = posY
        self.radius = 38
        self.theta = 0
        self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta))))
        self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta))))

    def draw(self, surface):
        self.x = int((self.circlePositionX + (self.radius * math.cos(-self.theta))))
        self.y = int((self.circlePositionY + (self.radius * math.sin(-self.theta))))
        pygame.gfxdraw.aacircle(surface, self.circlePositionX, self.circlePositionY, self.radius, white)
        pygame.gfxdraw.filled_circle(surface, self.x, self.y, 2, white)

    def method_circle(self):
        return self.x, self.y

class HorizontalCircle(object):
    def __init__(self, posX, posY):
        self.circlePositionX = posX
        self.circlePositionY = posY
        self.radius = 38
        self.theta = 0
        self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta))))
        self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta))))

    def draw(self, surface):
        self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta))))
        self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta))))
        pygame.gfxdraw.aacircle(surface, self.circlePositionX, self.circlePositionY, self.radius, white)
        pygame.gfxdraw.filled_circle(surface, self.x, self.y, 2, white)

    def method_circle(self):
        return self.x, self.y


class VerticalLine(HorizontalCircle):
    def __init__(self, surface, Target_Circle):
        pg.draw.rect(surface, gray, (Target_Circle.x, 0, 800, 1))

    def draw(self,surface, Target_Circle):
            pg.draw.rect(surface, gray, (Target_Circle.x, 0, 800, 1))


class HorizontalLine(VerticalCircle):
    def __init__(self, surface, Target_Circle):
        pg.draw.rect(surface, gray, (0, Target_Circle.y, 800, 1))

    def draw(self,surface, Target_Circle):
            pg.draw.rect(surface, gray, (0, Target_Circle.y, 800, 1))





HorizontalCircleList = []
VerticalCircleList =[]

HorizontalRectList = []
VerticalRectList = []
x = 120
for i in range(1, 10):
    VCircle = VerticalCircle(40, x)
    VerticalCircleList.append(VCircle)
    VCircle.draw(surface)

    HLine = HorizontalLine(surface, VCircle)
    HorizontalRectList.append(HLine)
    HLine.draw(surface, VCircle)

    HCircle = HorizontalCircle(x, 40)
    HorizontalCircleList.append(HCircle)
    HCircle.draw(surface)

    VLine = VerticalLine(surface, HCircle)
    VerticalRectList.append(VLine)
    VLine.draw(surface, HCircle)

    x += 80 # offset to place circles

pg.display.update()
loop = 0

run = True
while run:

    clock.tick(160)
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False

    if loop == len(HorizontalCircleList):
        loop = 0
    print("x ", len(HorizontalRectList))
    print("y ", len(VerticalRectList))

    print("circle ", len(HorizontalCircleList))
    surface.fill(0)
    for i in range(int(len(HorizontalCircleList))):
        HorizontalRectList[i].draw(surface, VerticalCircleList[i])
        VerticalRectList[i].draw(surface, HorizontalCircleList[i])

        HorizontalCircleList[i].theta += .002 + i/100
        HorizontalCircleList[i].draw(surface)
        VerticalCircleList[i].theta += .002 + i/100
        VerticalCircleList[i].draw(surface)


    pg.display.update()
    loop += 1


pg.quit()

The the lines are drawn by rectangles ( pygame.draw.rect() ).线条由矩形( pygame.draw.rect() )绘制。 The vertical rectangles are drawn, buy you have confused the width and height:绘制了垂直矩形,买你混淆了宽度和高度:

pg.draw.rect(surface, gray, (Target_Circle.x, 0, 800, 1))

pg.draw.rect(surface, gray, (Target_Circle.x, 0, 1, 800))

Anyway, I recommend to use pygame.draw.line() for drawing lines:无论如何,我建议使用pygame.draw.line()来绘制线条:

class VerticalLine(HorizontalCircle):
    def __init__(self, surface, Target_Circle):
        self.surface = surface

    def draw(self,surface, Target_Circle):
        x = Target_Circle.x
        pg.draw.line(self.surface, gray, (x, 0), (x, self.surface.get_height()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM