Reputation: 75
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. 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()
Upvotes: 1
Views: 92
Reputation: 210928
The the lines are drawn by rectangles (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:
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()))
Upvotes: 1