import math import time from pygame.locals import * import pygame class Colors: BLACK = Color("black") RED = Color("red") GREEN = Color("green") BLUE = Color("blue") YELLOW = Color("yellow") CYAN = Color("cyan") class Shape: def perimeter(self): raise NotImplementedError() def area(self): raise NotImplementedError() class Showable: def draw(self, surface): raise NotImplementedError() def show(self): pygame.init() self.w, self.h = size = 200, 200 screen = pygame.display.set_mode(size) running = True while running: screen.fill(Colors.BLACK) self.draw(screen) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() running = False elif event.type == KEYDOWN: if event.key in [K_ESCAPE, K_q]: pygame.quit() running = False class Point(Shape, Showable): def perimeter(self): return 0 def area(self): return 0 def draw(self, surface): center = (self.w // 2, self.h // 2) pygame.draw.circle(surface, Colors.GREEN, center, radius=2) class Circle(Shape, Showable): def __init__(self, radius): self.r = radius def perimeter(self): return 2 * math.pi * self.r def area(self): return math.pi * self.r * self.r def draw(self, surface): center = (self.w // 2, self.h // 2) pygame.draw.circle(surface, Colors.RED, center, radius=self.r) class Square(Shape, Showable): def __init__(self, side): self.side = side def perimeter(self): return self.side * 4 def area(self): return self.side * self.side def draw(self, surface): center_x = self.w // 2 center_y = self.h // 2 left_top = (center_x - self.side // 2, center_y - self.side // 2) size = (self.side, self.side) pygame.draw.rect(surface, Colors.BLUE, Rect(left_top, size)) class Rectangle(Shape, Showable): def __init__(self, a, b): self.a = a self.b = b def perimeter(self): return (self.a + self.b) * 2 def area(self): return self.a * self.b def draw(self, surface): center_x = self.w // 2 center_y = self.h // 2 left_top = (center_x - self.a // 2, center_y - self.b // 2) size = (self.a, self.b) pygame.draw.rect(surface, Colors.CYAN, Rect(left_top, size)) class EquilateralPolygon(Shape, Showable): def __init__(self, sides, radius): self.n = sides self.r = radius def perimeter(self): a = 2 * self.r * math.sin(math.pi / self.n) return a * self.n def area(self): return self.n / 2 * self.r * self.r * math.sin(2 * math.pi / self.n) def draw(self, surface): points = [] xc = self.w // 2 yc = self.h // 2 for i in range(self.n): points.append(( xc + self.r * math.cos(2 * math.pi * i / self.n), yc + self.r * math.sin(2 * math.pi * i / self.n) )) pygame.draw.polygon(surface, Colors.YELLOW, points) def main(): print("===== Point =====") p = Point() print(p.perimeter()) print(p.area()) p.show() print("===== Circle =====") c = Circle(40) print(c.perimeter()) print(c.area()) c.show() print("===== Square =====") sq = Square(120) print(sq.perimeter()) print(sq.area()) sq.show() print("===== Rectangle =====") r = Rectangle(120, 60) print(r.perimeter()) print(r.area()) r.show() print("===== EquilateralPolygon =====") poly = EquilateralPolygon(10, 60) print(poly.perimeter()) print(poly.area()) poly.show() if __name__ == "__main__": main()