def update(self): # Функция обновляет позицию объекта на экране # # Заставляет пролетать объект сквозь границы окна if self.position.as_point()[0] > 801: self.position = Vector((0, self.position.as_point()[1])) if self.position.as_point()[1] > 601: self.position = Vector((self.position.as_point()[0], 0)) if self.position.as_point()[1] < -1: self.position = Vector((self.position.as_point()[0], 599)) if self.position.as_point()[0] < -1: self.position = Vector((799, self.position.as_point()[1])) # # Поворот объекта if self.state == TURN_LEFT: self.speed.rotate(-self.speed_rotate) if self.state == TURN_RIGHT: self.speed.rotate(self.speed_rotate) # # Ускорение и замедление объкта if self.state == SPEED_DOWN: self.speed = self.speed - self.speed.normalize() if self.state == SPEED_UP: self.speed = self.speed + self.speed.normalize() # # Возвращение объекта в центр экрана if self.state == COMEBACK: self.position = self.default_position self.position += self.speed
def update(self): if self.pos.x > Display[0]: self.pos = Vector((-50, self.pos.y)) if self.pos.y > 850: self.pos = Vector((self.pos.x, -50)) if self.pos.y < -50: self.pos = Vector((self.pos.x, 850)) if self.pos.x < -50: self.pos = Vector((850, self.pos.y)) if self.state == TURN_LEFT: self.speed.rotate(-self.angle_speed) # (Так не надо)self.image = pygame.transform.rotate(self.copyimage, self.angle + Angle) if self.state == TURN_RIGHT: self.speed.rotate(self.angle_speed) if self.state == FAST: if self.speed.len() == 0: self.speed = self.direction self.speed = self.speed + self.speed.normalize() if self.speed.len() == 0: return if self.state == SLOW: if self.speed.len() < 1: self.direction = self.speed self.speed = Vector((0, 0)) else: self.speed = self.speed - self.speed.normalize() self.pos += self.speed
def update(self): if self.state == TURN_LEFT: self.speed.rotate(-self.rotate_speed) pygame.transform.rotate(self.image, self.speed.angle) if self.state == TURN_RIGHT: self.speed.rotate(self.rotate_speed) if self.state == SPEED_UP: if self.speed.len() < self.max_speed: self.speed = self.speed + self.speed.normalize() if self.speed.len() == 0: self.speed = self.normal_speed if self.speed.len() == 0: return if self.state == SPEED_DOWN: if self.speed.len() < 1: self.normal_speed = self.speed self.speed = Vector((0, 0)) else: self.speed = self.speed - self.speed.normalize() self.pos += self.speed if self.pos.as_point()[0] > 800: self.pos = Vector((0, self.pos.as_point()[1])) if self.pos.as_point()[1] > 600: self.pos = Vector((self.pos.as_point()[0], 0)) if self.pos.as_point()[1] < -1: self.pos = Vector((self.pos.as_point()[0], 599)) if self.pos.as_point()[0] < -1: self.pos = Vector((799, self.pos.as_point()[1]))
def __init__(self, position): self.default_position = Vector(position) # Стандартная позиция (центр экрана_ self.position = Vector(position) # Вектор позиции объекта self.speed = Vector((3, 0)) # Вектор скорости self.speed_rotate = 10 # Угол поворота вектора скорости self.image = pygame.Surface((30, 50)) # Поверхность, где будет отрисовываться объект self.state = NORMAL self.draw()
def __init__(self, pos): self.pos = Vector(pos) self.image = pygame.Surface((65, 40), pygame.SRCALPHA) self.speed = Vector((5.8, 7.4)) self.state = NORMAL self.direction = self.speed self.draw() self.image_time = self.image
def boxCross(disk): o = Options() additiionalEmptiness = max( o.getProperty('maxh_f'), # t oavoid errors o.getProperty('maxh_sh'), # in netgen nesh o.getProperty('maxh_m')) # generation length = o.getProperty('cubeEdgeLength') c = disk.c() tc = disk.tc() bc = disk.bc() r = disk.r() h = disk.h() v = len(disk.facets()) vtb = Vector(bc, tc) responce = '' for facet in disk.facets(): vToFacet = facet - c vInFacet = vtb.vectorMultiply(vToFacet) realLength = vInFacet.l() needLength = r * math.tan(math.pi / v) vInFacet = vInFacet * (needLength / realLength) x4 = facet + vInFacet + vtb / 2 x5 = facet - vInFacet + vtb / 2 x6 = facet + vInFacet - vtb / 2 x7 = facet - vInFacet - vtb / 2 if additiionalEmptiness > x4.x() or x5.x() < additiionalEmptiness: return True if additiionalEmptiness > x6.x() or x7.x() < additiionalEmptiness: return True if additiionalEmptiness > x4.y() or x5.y() < additiionalEmptiness: return True if additiionalEmptiness > x6.y() or x7.y() < additiionalEmptiness: return True if additiionalEmptiness > x4.z() or x5.z() < additiionalEmptiness: return True if additiionalEmptiness > x6.z() or x7.z() < additiionalEmptiness: return True if x4.x() > length - additiionalEmptiness or\ x5.x() > length - additiionalEmptiness: return True if x4.y() > length - additiionalEmptiness or\ x5.y() > length - additiionalEmptiness: return True if x4.z() > length - additiionalEmptiness or\ x5.z() > length - additiionalEmptiness: return True if x6.x() > length - additiionalEmptiness or\ x7.x() > length - additiionalEmptiness: return True if x6.y() > length - additiionalEmptiness or\ x7.y() > length - additiionalEmptiness: return True if x6.z() > length - additiionalEmptiness or\ x7.z() > length - additiionalEmptiness: return True return False
def __init__(self, pos): self.pos = Vector(pos) self.image = pygame.Surface((40, 40), pygame.SRCALPHA) # self.copyimage = self.image.copy() self.speed = Vector((0, 0)) self.angle_speed = 5 self.state = NORMAL self.direction = Vector((1, 0)) self.draw()
def move(self, dt): if self.track: temp_coords = self.coords Game_Object.move(self, dt) self.rect.center = self.coords.as_point() if self.rect.collidelist(self.track) != -1: #self.acsel = self.acsel*-1 self.speed = Vector((0,0)) else: Game_Object.move(self, dt)
def __init__(self, pos): # self.w = 45 # self.h = 40 self.rotate_speed = 5 self.state = NORMAL self.pos = Vector(pos) self.image = pygame.Surface((45, 40), pygame.SRCALPHA) self.speed = Vector((0, 0)) self.normal_speed = Vector((1, 0)) # Скорость и направление, сохраняемые при полном торможении self.max_speed = 10
def __init__(self, pos): self.pos = Vector(pos) self.image = pygame.Surface((50, 20), pygame.SRCALPHA) self.speed = Vector((50, 0)) self.speed_rotate = 90 self.boost = 100 # ускорение self.w = 50 self.h = 20 self.state = NORMAL self.last_state = NORMAL # ?? тема с поворотом self.draw()
def update(self): if self.state == TURN_LEFT: self.speed.rotate(-5) if self.state == TURN_RIGHT: self.speed.rotate(5) if self.state == SPEED_DOWN: self.speed -= self.speed.normalize() if self.speed.len < 1 and self.speed.len != 0: self.direction = self.speed self.speed = Vector((0, 0)) if self.state == SPEED_UP: if self.speed.len < 1: self.speed = self.direction self.speed += self.speed.normalize() self.pos += self.speed if self.pos.x > 800: self.pos.x = 0 if self.pos.x < 0: self.pos.x = 800 if self.pos.y > 600: self.pos.y = 0 if self.pos.y < 0: self.pos.y = 600
def checkMinimumDistance(disk1, disk2): rMin = 1000000 for facet1 in disk1.facets(): for facet2 in disk2.facets(): length = Vector(facet1, facet2).l() if length < rMin: rMin = length return rMin
def findBorders(self): o = Options() minx = miny = minz = 1000000 maxx = maxy = maxz = -1000000 s = o.getProperty('shellThickness') c = self.c() for facet in self.values['facets']: vToFacet = Vector(self.c(), facet) l = vToFacet.l() vToFacet = vToFacet * ((l + s) / l) realFacet = c + vToFacet if realFacet.x() < minx: minx = realFacet.x() if realFacet.x() > maxx: maxx = realFacet.x() if realFacet.y() < miny: miny = realFacet.y() if realFacet.y() > maxy: maxy = realFacet.y() if realFacet.z() < minz: minz = realFacet.z() if realFacet.z() > maxz: maxz = realFacet.z() return [minx, maxx, miny, maxy, minz, maxz]
def __init__(self, coords=(0,0), speed=(0,0)): self.coords = Vector(coords) self.speed = Vector(speed) #пикселей/сек self.max_speed = 100 self.len_speed = self.speed.len() self.color = (250,0,0) self.acsel = Vector((0,0)) self.len_acsel = self.acsel.len() self.d_acsel = 0 #Изменение ускорения, x пикс/сек self.friction = 0 #Сила трения self.status = STOP self.angle_speed = 40 #Угловая скорость, градусов/сек
class Car(Game_Object): def __init__(self, coords=(0,0), speed=(0,0)): Game_Object.__init__(self, coords=coords, speed=speed) self.image = load_image('yellow_car.png', path='Images', alpha_cannel=True) self.image = pygame.transform.rotate(self.image, -90) self.image = pygame.transform.scale(self.image, (50,50)) self.rect = self.image.get_rect() self.track = None def move(self, dt): if self.track: temp_coords = self.coords Game_Object.move(self, dt) self.rect.center = self.coords.as_point() if self.rect.collidelist(self.track) != -1: #self.acsel = self.acsel*-1 self.speed = Vector((0,0)) else: Game_Object.move(self, dt) def add_track(self, track): self.track = track def render(self, screen): angle_of_rotate = math.degrees(math.acos(self.speed.normalize().x)) #print(angle_of_rotate) if self.speed.y>0: angle_of_rotate = 360-angle_of_rotate rotated_img = pygame.transform.rotate(self.image, angle_of_rotate) rect_img = rotated_img.get_rect() rect_img.center = self.coords.as_point() # центр картинки приравниваем вектору позиции screen.blit(rotated_img, rect_img) # блитуем все на скрин Game_Object.render(self, screen)
class Ship: def __init__(self, pos): self.pos = Vector(pos) self.image = pygame.Surface((50, 20), pygame.SRCALPHA) self.speed = Vector((50, 0)) self.speed_rotate = 90 self.boost = 100 # ускорение self.w = 50 self.h = 20 self.state = NORMAL self.last_state = NORMAL # ?? тема с поворотом self.draw() def events(self, event): # ! if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.state = TURN_LEFT if event.key == pygame.K_RIGHT: self.state = TURN_RIGHT if event.key == pygame.K_DOWN: self.state = TURN_DOWN if event.key == pygame.K_UP: self.state = TURN_UP if event.type == pygame.KEYUP: self.state = NORMAL def update(self, dt): # ! if self.state == TURN_LEFT: self.speed.rotate(-self.speed_rotate*(dt/1000)) if self.state == TURN_RIGHT: self.speed.rotate(self.speed_rotate*(dt/1000)) if self.state == TURN_DOWN: self.speed -= self.speed.normalize()*self.boost*(dt/1000) if self.state == TURN_UP: self.speed += self.speed.normalize()*self.boost*(dt/1000) # self.speed *= (dt/1000) self.pos += self.speed*(dt/1000) #self.last_state = self.state if self.pos.x < 0: self.pos.x = DISPLAY_W - self.w if self.pos.x > DISPLAY_W - self.w: self.pos.x = 0 if self.pos.y < self.h: self.pos.y = DISPLAY_H - self.h if self.pos.y > DISPLAY_H - self.h: self.pos.y = self.h def draw(self): # pygame.draw.polygon(self.image, COLOR_SPACEHIP, [(0, 0), (0, self.h), (self.w * 0.75, self.h * (3 / 4)), # (self.w * (5 / 6), self.h * (3 / 4)), (self.w / 2, self.h / 2), # (self.w * (5 / 6), self.h * (1 / 4)), # (self.w / 4, self.h * (1 / 4)), # (0, 0)]) # pygame.draw.rect(self.image, COLOR_SPACEHIP, [(0, 0), (self.w, self.h)], 1) pygame.draw.polygon(self.image, COLOR_SPACEHIP, [(0, 0), (0, self.h), (self.w, self.h * 0.5), (0, 0)]) def render(self, screen): rot_image = pygame.transform.rotate(self.image, self.speed.angle) rot_rect = rot_image.get_rect() rot_rect.center = self.image.get_rect().center rot_rect.move_ip(self.pos.as_point()) # self.image = image.subsurface(rot_rect).copy() # dv = Vector((self.w / 2, self.h / 2)) screen.blit(rot_image, rot_rect) # screen.blit(self.image, self.pos.as_point()) pygame.draw.line(screen, COLOR_LINE, (self.pos + dv).as_point(), (self.pos + dv + self.speed).as_point())
def disksInTheShellCross(disk1, disk2): o = Options() epsilon = o.getProperty('roughEpsilon') s = o.getProperty('shellThickness') length = o.getProperty('cubeEdgeLength') h = o.getProperty('polygonalDiskThickness') v = o.getProperty('verticesNumber') r = o.getProperty('polygonalDiskRadius') c1 = disk1.c() c2 = disk2.c() vtb1 = Vector(disk1.bc(), disk1.tc()) vtb1 *= (2 * s + h) / h vtb2 = Vector(disk2.bc(), disk2.tc()) vtb2 *= (2 * s + h) / h # facet of disk2 and top/bottom of disk1: for [x1, x2, x3] in [[ c1 + vtb1 / 2, disk1.facets()[0] + vtb1 / 2, disk1.facets()[1] + vtb1 / 2 ], [ c1 - vtb1 / 2, disk1.facets()[0] - vtb1 / 2, disk1.facets()[1] - vtb1 / 2 ]]: v12 = Vector(x2, x1) v32 = Vector(x2, x3) for facet2 in disk2.facets(): vToFacet = Vector(facet2, c2) vToFacet *= (s + r) / r vInFacet = vToFacet.vectorMultiply(vtb2) realLength = vInFacet.l() needLength = (r + s) * math.tan(math.pi / v) vInFacet *= needLength / realLength for [x4, x5] in [[ c2 + vToFacet + vtb2 / 2 + vInFacet, c2 + vToFacet + vtb2 / 2 - vInFacet ], [ c2 + vToFacet - vtb2 / 2 + vInFacet, c2 + vToFacet - vtb2 / 2 - vInFacet ], [ c2 + vToFacet + vtb2 / 2 + vInFacet, c2 + vToFacet - vtb2 / 2 + vInFacet ], [ c2 + vToFacet + vtb2 / 2 - vInFacet, c2 + vToFacet - vtb2 / 2 - vInFacet ]]: v42 = Vector(x2, x4) v52 = Vector(x2, x5) det1 = np.linalg.det( np.array([[v12.x(), v12.y(), v12.z()], [v32.x(), v32.y(), v32.z()], [v42.x(), v42.y(), v42.z()]])) det2 = np.linalg.det( np.array([[v12.x(), v12.y(), v12.z()], [v32.x(), v32.y(), v32.z()], [v52.x(), v52.y(), v52.z()]])) if abs(det1) < epsilon: if Vector(x4, x1).l() < r + s: return True elif abs(det2) < epsilon: if Vector(x5, x1).l() < r + s: return True elif det1 * det2 < 0: tmpVector = Vector( x4, x5) * abs(det1) / (abs(det1) + abs(det2)) if Vector(x1, x4 + tmpVector).l() < r + s: return True return False
class Game_Object: def __init__(self, coords=(0,0), speed=(0,0)): self.coords = Vector(coords) self.speed = Vector(speed) #пикселей/сек self.max_speed = 100 self.len_speed = self.speed.len() self.color = (250,0,0) self.acsel = Vector((0,0)) self.len_acsel = self.acsel.len() self.d_acsel = 0 #Изменение ускорения, x пикс/сек self.friction = 0 #Сила трения self.status = STOP self.angle_speed = 40 #Угловая скорость, градусов/сек def event(self, event): """ Обработка событий объектом """ if event.type == KEYDOWN: if event.key == K_LEFT: self.status = TURN_RIGHT elif event.key == K_RIGHT: self.status = TURN_LEFT elif event.key == K_UP: self.d_acsel = +12 elif event.key == K_DOWN: self.d_acsel = -6 elif event.type == KEYUP: if event.key == K_LEFT: self.status = MOVE elif event.key == K_RIGHT: self.status = MOVE elif event.key == K_UP or event.key == K_DOWN: self.d_acsel = 0 def change_acselerate(self): self.acsel = self.speed.normalize()* (self.d_acsel + self.friction) #? self.len_acsel = self.acsel.len() def change_speed(self, dt): self.speed+= self.acsel*(dt/1000) self.len_speed = self.speed.len() #fixme: добавить ограничение на max_speed def move(self, dt): self.coords += (self.speed*(dt/1000)) def update(self, dt): """ Обновление состояния объкта (вызывается каждый кадр) """ if self.status == TURN_LEFT: self.speed.rotate(self.angle_speed/1000*dt) elif self.status == TURN_RIGHT: self.speed.rotate(-self.angle_speed/1000*dt) self.change_acselerate() self.change_speed(dt) self.move(dt) def render(self, screen): center = self.coords.as_point() dx = 4 dy = 4 #cross pygame.draw.line(screen, (0,250,0), (self.coords.x, self.coords.y-dy),(self.coords.x, self.coords.y+dy)) pygame.draw.line(screen, (0,250,0), (self.coords.x-dx, self.coords.y),(self.coords.x+dx, self.coords.y)) pygame.draw.line(screen, self.color, (self.coords.as_point()),(self.coords+self.speed).as_point()) pygame.draw.line(screen, (0,0,250), (self.coords.as_point()),(self.coords+self.acsel*2).as_point(),4)
class Ship: def __init__(self, pos): # self.w = 45 # self.h = 40 self.rotate_speed = 5 self.state = NORMAL self.pos = Vector(pos) self.image = pygame.Surface((45, 40), pygame.SRCALPHA) self.speed = Vector((0, 0)) self.normal_speed = Vector((1, 0)) # Скорость и направление, сохраняемые при полном торможении self.max_speed = 10 def events(self, event): if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.state = SPEED_UP if event.key == pygame.K_DOWN: self.state = SPEED_DOWN if event.key == pygame.K_LEFT: self.state = TURN_LEFT if event.key == pygame.K_RIGHT: self.state = TURN_RIGHT if event.type == pygame.KEYUP: self.state = NORMAL def update(self): if self.state == TURN_LEFT: self.speed.rotate(-self.rotate_speed) pygame.transform.rotate(self.image, self.speed.angle) if self.state == TURN_RIGHT: self.speed.rotate(self.rotate_speed) if self.state == SPEED_UP: if self.speed.len() < self.max_speed: self.speed = self.speed + self.speed.normalize() if self.speed.len() == 0: self.speed = self.normal_speed if self.speed.len() == 0: return if self.state == SPEED_DOWN: if self.speed.len() < 1: self.normal_speed = self.speed self.speed = Vector((0, 0)) else: self.speed = self.speed - self.speed.normalize() self.pos += self.speed if self.pos.as_point()[0] > 800: self.pos = Vector((0, self.pos.as_point()[1])) if self.pos.as_point()[1] > 600: self.pos = Vector((self.pos.as_point()[0], 0)) if self.pos.as_point()[1] < -1: self.pos = Vector((self.pos.as_point()[0], 599)) if self.pos.as_point()[0] < -1: self.pos = Vector((799, self.pos.as_point()[1])) def draw(self): pygame.draw.lines( self.image, (155, 0, 0), False, [ (40, 20), (38, 18), (32, 18), (32, 16), (26, 16), (26, 18), (26, 15), (30, 10), (30, 8), (36, 8), (36, 6), (30, 6), (28, 4), (20, 4), (14, 10), (10, 10), (6, 14), (6, 24), (12, 30), (16, 30), (22, 36), (30, 36), (32, 34), (36, 34), (36, 32), (30, 32), (30, 30), (26, 26), (26, 22), (26, 24), (32, 24), (32, 22), (38, 22), (40, 20), ], ) pygame.draw.rect(self.image, (255, 255, 255), self.image.get_rect(), 1) def render(self, screen): r = Vector(self.image.get_rect().center) rotate_image = pygame.transform.rotate(self.image, self.speed.angle) origin_rect = self.image.get_rect() rotate_rect = rotate_image.get_rect() rotate_rect.center = origin_rect.center rotate_rect.move_ip(self.pos.as_point()) screen.blit(rotate_image, rotate_rect) pygame.draw.line(screen, (0, 255, 0), (self.pos + r).as_point(), ((self.pos + self.speed * 5) + r).as_point())
def disksCross(disk1, disk2): o = Options() epsilon = o.getProperty('roughEpsilon') c1 = disk1.c() tc1 = disk1.tc() bc1 = disk1.bc() c2 = disk2.c() dc12 = c1 - c2 l = dc12.l() r = disk1.r() h = disk1.h() v = len(disk1.facets()) if 2 * (r**2 + h**2 / 4)**0.5 < l**0.5: return False elif h > l: return True # http://mathworld.wolfram.com/Line-PlaneIntersection.html # facet of disk2 and top or bottom of disk1 vtb1 = Vector(bc1, tc1) tc2 = disk2.tc() bc2 = disk2.bc() vtb2 = Vector(bc2, tc2) for (x1, x2, x3) in [ ( c1 + vtb1 / 2, # top disk1.facets()[0] + vtb1 / 2, disk1.facets()[1] + vtb1 / 2), ( c1 - vtb1 / 2, # bottom disk1.facets()[0] - vtb1 / 2, disk1.facets()[1] - vtb1 / 2) ]: v12 = Vector(x2, x1) v32 = Vector(x2, x3) for facet in disk2.facets(): vToFacet = Vector(c2, facet) vInFacet = vtb2.vectorMultiply(vToFacet) realLength = vInFacet.l() needLength = r * math.tan(math.pi / v) vInFacet = vInFacet * (needLength / realLength) for (x4, x5) in [ ( facet + vInFacet + vtb2 / 2, # top edge facet - vInFacet + vtb2 / 2), ( facet + vInFacet - vtb2 / 2, # bottom edge facet - vInFacet - vtb2 / 2) ]: v42 = Vector(x2, x4) v52 = Vector(x2, x5) det1 = np.linalg.det( np.array([[v12.x(), v12.y(), v12.z()], [v32.x(), v32.y(), v32.z()], [v42.x(), v42.y(), v42.z()]])) det2 = np.linalg.det( np.array([[v12.x(), v12.y(), v12.z()], [v32.x(), v32.y(), v32.z()], [v52.x(), v52.y(), v52.z()]])) if -epsilon < det1 < epsilon or -epsilon < det2 < epsilon: l = ((r / math.cos(math.pi / v))**2 + h**2 / 4)**0.5 if Vector(x1, x4).l() < r / math.cos(math.pi / v): return True if Vector(x1, x5).l() < r / math.cos(math.pi / v): return True #return True elif det1 * det2 < -epsilon: v45 = Vector(x4, x5) pti = x4 + v45 * abs(det1) / (abs(det1) + abs(det2)) if Vector(x1, pti).l() < r: return True for facet1 in disk1.facets(): x1 = facet1 vToFacet1 = Vector(x1, c1) vInFacet1 = vtb1.vectorMultiply(vToFacet1) needLength = r * math.tan(math.pi / v) realLength = vInFacet1.l() vInFacet *= needLength / realLength x2 = x1 + vInFacet + vtb1 / 2 x3 = x1 - vInFacet + vtb1 / 2 v12 = Vector(x2, x1) v32 = Vector(x2, x3) for facet2 in disk2.facets(): vToFacet = Vector(c2, facet) vInFacet = vtb2.vectorMultiply(vToFacet) realLength = vInFacet.l() needLength = r * math.tan(math.pi / v) vInFacet = vInFacet * (needLength / realLength) for (x4, x5) in [ ( facet + vInFacet + vtb2 / 2, # top edge facet - vInFacet + vtb2 / 2), ( facet + vInFacet - vtb2 / 2, # bottom edge facet - vInFacet - vtb2 / 2) ]: v42 = Vector(x4, x2) v52 = Vector(x5, x2) det1 = np.linalg.det( np.array([[v12.x(), v12.y(), v12.z()], [v32.x(), v32.y(), v32.z()], [v42.x(), v42.y(), v42.z()]])) det2 = np.linalg.det( np.array([[v12.x(), v12.y(), v12.z()], [v32.x(), v32.y(), v32.z()], [v52.x(), v52.y(), v52.z()]])) if -epsilon < det1 < epsilon: xi = x4 vectori = Vector(x1, xi) cos = (abs(vectori.x() * vtb1.x() + vectori.y() * vtb1.y() + vectori / z() * vtb1 / z())) / vectori.l() / vtb1.l() sin = (1 - cos**2)**0.5 axelen = cos * vectori.l() norlen = sin * vectori.l() if axelen < (vtb1 / 2).l() and norlen < needLength: return True elif -epsilon < det2 < epsilon: xi = x5 vectori = Vector(x1, xi) cos = (abs(vectori.x() * vtb1.x() + vectori.y() * vtb1.y() + vectori / z() * vtb1 / z())) / vectori.l() / vtb1.l() sin = (1 - cos**2)**0.5 axelen = cos * vectori.l() norlen = sin * vectori.l() if axelen < (vtb1 / 2).l() and norlen < needLength: return True elif det1 * det2 < -epsilon: xi = x4 + Vector( x4, x5) * (abs(det1)) / (abs(det1) + abs(det2)) vectori = Vector(x1, xi) cos = (abs(vectori.x() * vtb1.x() + vectori.y() * vtb1.y() + vectori / z() * vtb1 / z())) / vectori.l() / vtb1.l() sin = (1 - cos**2)**0.5 axelen = cos * vectori.l() norlen = sin * vectori.l() if axelen < (vtb1 / 2).l() and norlen < needLength: return True return False
def boxCrossByDiskInTheShell(disk): o = Options() length = o.getProperty('cubeEdgeLength') c = disk.c() s = o.getProperty('shellThickness') tc = disk.tc() bc = disk.bc() r = disk.r() h = disk.h() v = len(disk.facets()) vtb = Vector(bc, tc) vtb = vtb * (2 * s + h) / h responce = [0 for i in range(6)] for facet in disk.facets(): ifcrossx = False ifcrossy = False ifcrossz = False ptOnFacet = c + (facet - c) / r * (r + s) vToFacet = facet - c vInFacet = vtb.vectorMultiply(vToFacet) realLength = vInFacet.l() needLength = (r + s) * math.tan(math.pi / v) vInFacet = vInFacet * (needLength / realLength) x1 = ptOnFacet + vInFacet + vtb / 2 x2 = ptOnFacet + vInFacet - vtb / 2 x3 = ptOnFacet - vInFacet + vtb / 2 x4 = ptOnFacet - vInFacet - vtb / 2 if x1.x() * x2.x() < 0 or x1.x() * x3.x() < 0 or x1.x() * x4.x() < 0: ifcrossx = True if (x1.x() - length) * (x2.x() - length) < 0 or\ (x1.x() - length) * (x3.x() - length) < 0 or\ (x1.x() - length) * (x4.x() - length) < 0: ifcrossx = True if x1.y() * x2.y() < 0 or x1.y() * x3.y() < 0 or x1.y() * x4.y() < 0: ifcrossy = True if (x1.y() - length) * (x2.y() - length) < 0 or\ (x1.y() - length) * (x3.y() - length) < 0 or\ (x1.y() - length) * (x4.y() - length) < 0: ifcrossy = True if x1.z() * x2.z() < 0 or x1.z() * x3.z() < 0 or x1.z() * x4.z() < 0: ifcrossz = True if (x1.z() - length) * (x2.z() - length) < 0 or\ (x1.z() - length) * (x3.z() - length) < 0 or\ (x1.z() - length) * (x4.z() - length) < 0: ifcrossz = True if ifcrossx and not ifcrossy and not ifcrossz: if 0 < c.y() < length and 0 < c.z() < length: return True if ifcrossy and not ifcrossx and not ifcrossz: if 0 < c.x() < length and 0 < c.z() < length: return True if ifcrossz and not ifcrossy and not ifcrossx: if 0 < c.y() < length and 0 < c.x() < length: return True if ifcrossx and ifcrossy and not ifcrossz: if 0 < c.z() < length: return True if ifcrossx and ifcrossz and not ifcrossy: if 0 < c.y() < length: return True if ifcrossz and ifcrossy and not ifcrossx: if 0 < c.x() < length: return True if ifcrossx and ifcrossy and ifcrossz: return True return False
class SpaceShip: def __init__(self, position): self.default_position = Vector(position) # Стандартная позиция (центр экрана_ self.position = Vector(position) # Вектор позиции объекта self.speed = Vector((3, 0)) # Вектор скорости self.speed_rotate = 10 # Угол поворота вектора скорости self.image = pygame.Surface((30, 50)) # Поверхность, где будет отрисовываться объект self.state = NORMAL self.draw() def update(self): # Функция обновляет позицию объекта на экране # # Заставляет пролетать объект сквозь границы окна if self.position.as_point()[0] > 801: self.position = Vector((0, self.position.as_point()[1])) if self.position.as_point()[1] > 601: self.position = Vector((self.position.as_point()[0], 0)) if self.position.as_point()[1] < -1: self.position = Vector((self.position.as_point()[0], 599)) if self.position.as_point()[0] < -1: self.position = Vector((799, self.position.as_point()[1])) # # Поворот объекта if self.state == TURN_LEFT: self.speed.rotate(-self.speed_rotate) if self.state == TURN_RIGHT: self.speed.rotate(self.speed_rotate) # # Ускорение и замедление объкта if self.state == SPEED_DOWN: self.speed = self.speed - self.speed.normalize() if self.state == SPEED_UP: self.speed = self.speed + self.speed.normalize() # # Возвращение объекта в центр экрана if self.state == COMEBACK: self.position = self.default_position self.position += self.speed def events(self, event): # События if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.state = TURN_LEFT if event.key == pygame.K_RIGHT: self.state = TURN_RIGHT if event.key == pygame.K_SPACE: self.state = COMEBACK if event.key == pygame.K_DOWN: self.state = SPEED_DOWN if event.key == pygame.K_UP: self.state = SPEED_UP if event.type == pygame.KEYUP: self.state = NORMAL def draw(self): # Отрисовывает корабль на поверхности pygame.draw.lines(self.image, (255, 255, 255), False, [(2, 2), (12, 10), (26, 13), (12, 17), (2, 24), (2, 16), (7, 13), (2, 10), (2, 2)]) def render(self, screen): # Вывод изображения на экран origin_rect = self.image.get_rect() rotate_image = pygame.transform.rotate(self.image, self.speed.angle) rotate_rect = rotate_image.get_rect() rotate_rect.center = origin_rect.center rotate_rect.move_ip(self.position.as_point()) screen.blit(rotate_image, rotate_rect) screen.blit(rotate_image, rotate_rect)
def printToCSG(self, f): o = Options() l = o.getProperty('cubeEdgeLength') h = 0.01 cellString = ' and plane(0, 0, {0}; 0, 0, {0})'.format(l - h) cellString += ' and plane(0, {0}, 0; 0, {0}, 0)'.format(l - h) cellString += ' and plane({0}, 0, 0; {0}, 0, 0)'.format(l - h) cellString += ' and plane({1}, {1}, {1}; 0, 0, -{0})'.format(l - h, h) cellString += ' and plane({1}, {1}, {1}; 0, -{0}, 0)'.format(l - h, h) cellString += ' and plane({1}, {1}, {1}; -{0}, 0, 0)'.format(l - h, h) h = o.getProperty('polygonalDiskThickness') maxhFiller = o.getProperty('maxh_f') st = 'solid polygonalDisk{3} = plane({0}, {1}, {2}; ' f.write( st.format(self.values['topCenter'].x(), self.values['topCenter'].y(), self.values['topCenter'].z(), self.number())) dx = self.values['topCenter'].x() - self.values['botCenter'].x() dy = self.values['topCenter'].y() - self.values['botCenter'].y() dz = self.values['topCenter'].z() - self.values['botCenter'].z() f.write('{0}, {1}, {2}) '.format(dx, dy, dz)) st = 'and plane({0}, {1}, {2}; ' f.write( st.format(self.values['botCenter'].x(), self.values['botCenter'].y(), self.values['botCenter'].z())) dx = -self.values['topCenter'].x() + self.values['botCenter'].x() dy = -self.values['topCenter'].y() + self.values['botCenter'].y() dz = -self.values['topCenter'].z() + self.values['botCenter'].z() f.write('{0}, {1}, {2})'.format(dx, dy, dz)) for facet in self.values['facets']: f.write(' and plane({0}, {1}, {2}; '.format( facet.x(), facet.y(), facet.z())) c = self.c() dfc = facet - c f.write('{0}, {1}, {2})'.format(dfc.x(), dfc.y(), dfc.z())) f.write(cellString) f.write(';\n') s = o.getProperty('shellThickness') v = Vector(self.bc(), self.tc()) v = v * (2 * s + h) / 2 / h pt = self.bc() / 2 + self.tc() / 2 + v st = 'solid pdShell{0} = plane({1}, {2}, {3}; ' f.write(st.format(self.number(), pt.x(), pt.y(), pt.z())) f.write('{0}, {1}, {2}) '.format(v.x(), v.y(), v.z())) pt = self.bc() / 2 + self.tc() / 2 - v f.write('and plane({1}, {2}, {3}; '.format(self.number(), pt.x(), pt.y(), pt.z())) f.write('{0}, {1}, {2}) '.format(-v.x(), -v.y(), -v.z())) for facet in self.values['facets']: vToFacet = Vector(self.c(), facet) l = vToFacet.l() vToFacet = vToFacet * ((l + s) / l) f.write(' and plane({0}, {1}, {2}; '.format( c.x() + vToFacet.x(), c.y() + vToFacet.y(), c.z() + vToFacet.z())) f.write('{0}, {1}, {2})'.format(vToFacet.x(), vToFacet.y(), vToFacet.z())) f.write(cellString) f.write(';\n')
def diskDiskInTheShellCross(disk1, disk2): o = Options() epsilon = o.getProperty('roughEpsilon') s = o.getProperty('shellThickness') c1 = disk1.c() tc1 = disk1.tc() bc1 = disk1.bc() c2 = disk2.c() dc12 = c1 - c2 l = dc12.l() r = disk1.r() h = disk1.h() v = len(disk1.facets()) if 2 * (r**2 + (h / 2 + 2 * s)**2)**0.5 < l: return False elif h + 2 * s > l: return True # facet of disk2 and top of disk1 vtb1 = Vector(bc1, tc1) vtb1 = vtb1 * (2 * s + h) / h tc2 = disk2.tc() bc2 = disk2.bc() vtb2 = Vector(bc2, tc2) for (x1, x2, x3) in [ ( c1 + vtb1 / 2, # top disk1.facets()[0] + vtb1 / 2, disk1.facets()[1] + vtb1 / 2), ( c1 - vtb1 / 2, # bottom disk1.facets()[0] - vtb1 / 2, disk1.facets()[1] - vtb1 / 2) ]: v12 = Vector(x2, x1) v32 = Vector(x2, x3) for facet in disk2.facets(): vToFacet = Vector(c2, facet) vInFacet = vtb2.vectorMultiply(vToFacet) realLength = vInFacet.l() needLength = r * math.tan(math.pi / v) vInFacet = vInFacet * (needLength / realLength) for (x4, x5) in [ ( facet + vInFacet + vtb2 / 2, # top edge facet - vInFacet + vtb2 / 2), ( facet + vInFacet - vtb2 / 2, # bottom edge facet - vInFacet - vtb2 / 2) ]: v42 = Vector(x2, x4) v52 = Vector(x2, x5) det1 = np.linalg.det( np.array([[v12.x(), v12.y(), v12.z()], [v32.x(), v32.y(), v32.z()], [v42.x(), v42.y(), v42.z()]])) det2 = np.linalg.det( np.array([[v12.x(), v12.y(), v12.z()], [v32.x(), v32.y(), v32.z()], [v52.x(), v52.y(), v52.z()]])) if -epsilon < det1 < epsilon or -epsilon < det2 < epsilon: l = ((r / math.cos(math.pi / v))**2 + h**2 / 4)**0.5 if Vector(x1, x4).l() < r / math.cos(math.pi / v): return True if Vector(x1, x5).l() < r / math.cos(math.pi / v): return True elif det1 < -epsilon: v45 = Vector(x4, x5) pti = x4 + v45 * abs(det1) / (abs(det1) + abs(det2)) if Vector(x1, pti).l() < r: return True return False
class Ship: def __init__(self, pos): self.pos = Vector(pos) self.image = pygame.Surface((40, 40), pygame.SRCALPHA) # self.copyimage = self.image.copy() self.speed = Vector((0, 0)) self.angle_speed = 5 self.state = NORMAL self.direction = Vector((1, 0)) self.draw() def events(self, event): if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.state = TURN_LEFT if event.key == pygame.K_RIGHT: self.state = TURN_RIGHT if event.key == pygame.K_DOWN: self.state = SLOW if event.key == pygame.K_UP: self.state = FAST if event.key == pygame.K_ESCAPE: sys.exit() if event.type == pygame.KEYUP: self.state = NORMAL def update(self): if self.pos.x > Display[0]: self.pos = Vector((-50, self.pos.y)) if self.pos.y > 850: self.pos = Vector((self.pos.x, -50)) if self.pos.y < -50: self.pos = Vector((self.pos.x, 850)) if self.pos.x < -50: self.pos = Vector((850, self.pos.y)) if self.state == TURN_LEFT: self.speed.rotate(-self.angle_speed) # (Так не надо)self.image = pygame.transform.rotate(self.copyimage, self.angle + Angle) if self.state == TURN_RIGHT: self.speed.rotate(self.angle_speed) if self.state == FAST: if self.speed.len() == 0: self.speed = self.direction self.speed = self.speed + self.speed.normalize() if self.speed.len() == 0: return if self.state == SLOW: if self.speed.len() < 1: self.direction = self.speed self.speed = Vector((0, 0)) else: self.speed = self.speed - self.speed.normalize() self.pos += self.speed def draw(self): pygame.draw.lines(self.image, (0, 0, 200), False, [(35, 20), (20, 15), (0, 0), (0, 40), (20, 25), (35, 20)]) def render(self, screen): r = Vector(self.image.get_rect().center) # r - центр фигуры для вектора направления rotate_image = pygame.transform.rotate(self.image, self.speed.angle) origin_rect = self.image.get_rect() rotate_rect = rotate_image.get_rect() rotate_rect.center = origin_rect.center rotate_rect.move_ip(self.pos.as_point()) screen.blit(rotate_image, rotate_rect) pygame.draw.line(screen, (200, 255, 200), (self.pos + r).as_point(), (self.pos + self.speed * 20 + r).as_point()) pygame.draw.lines(self.image, (0, 0, 200), False, [(35, 20), (20, 15), (0, 0), (0, 40), (20, 25), (35, 20)])
class Ship: def __init__(self, pos): self.pos = Vector(pos) self.image = pygame.Surface((65, 40), pygame.SRCALPHA) self.speed = Vector((5.8, 7.4)) self.state = NORMAL self.direction = self.speed self.draw() self.image_time = self.image def events(self, event): if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.state = TURN_LEFT if event.key == pygame.K_RIGHT: self.state = TURN_RIGHT if event.key == pygame.K_UP: self.state = SPEED_UP if event.key == pygame.K_DOWN: self.state = SPEED_DOWN if event.type == pygame.KEYUP: self.state = NORMAL def update(self): if self.state == TURN_LEFT: self.speed.rotate(-5) if self.state == TURN_RIGHT: self.speed.rotate(5) if self.state == SPEED_DOWN: self.speed -= self.speed.normalize() if self.speed.len < 1 and self.speed.len != 0: self.direction = self.speed self.speed = Vector((0, 0)) if self.state == SPEED_UP: if self.speed.len < 1: self.speed = self.direction self.speed += self.speed.normalize() self.pos += self.speed if self.pos.x > 800: self.pos.x = 0 if self.pos.x < 0: self.pos.x = 800 if self.pos.y > 600: self.pos.y = 0 if self.pos.y < 0: self.pos.y = 600 def draw(self): pygame.draw.polygon(self.image, (220, 220, 220), [(0, 0), (10, 10), (55, 20), (10, 30), (0, 40), (0, 30), (7, 20), (0, 10)]) def render(self, screen): image_rotate = pygame.transform.rotate(self.image, self.speed.angle) origin_rec = self.image.get_rect() rotate_rec = image_rotate.get_rect() rotate_rec.center = origin_rec.center rotate_rec.move_ip(self.pos.as_point()) screen.blit(image_rotate, rotate_rec) pygame.draw.line(screen, (0, 255, 0), self.pos.as_point(), (self.pos + self.speed*10).as_point())