class Game(object): def __init__(self, displaySurf: pygame.Surface, ballsCount, speed): self.displaySurf = displaySurf self.ballsCount = ballsCount self.speed = speed self.targetCircle = Circle(self.displaySurf, color=randint(0, len(Circle.COLORS)-1), pos=(self.displaySurf.get_width() // 2, self.displaySurf.get_height() // 2), radius=30) self.pontFelirat = Pont(self.displaySurf, "Elért pont", (300, 10)) self.ballsList = [] self.ballsCreate() self.pont = 10 self.startTimeBalls = pygame.time.get_ticks() def ballCreate(self): pos = (randint(0, self.displaySurf.get_width()), randint(0, self.displaySurf.get_height())) vec = (self.targetCircle.getPos( )[0] - pos[0], self.targetCircle.getPos( )[1] - pos[1]) self.ballsList.append(Circle(self.displaySurf, color=randint(0, len(Circle.COLORS) - 1), \ pos=pos, \ radius=20, \ vec=vec, speed=pygame.math.Vector2(vec).length( ) * self.speed)) def ballsCreate(self): for i in range(self.ballsCount): self.ballCreate() def update(self): gameOver = False for ball in self.ballsList: if ball.getPos().distance_to(self.targetCircle.getPos()) < self.targetCircle.radius: if ball.getColor() != self.targetCircle.getColor(): self.pont -= 1 self.clearBall(ball) if len(self.ballsList) < 3: self.ballCreate() self.draw() if self.pont <= 0: gameOver = True return gameOver def draw(self): self.displaySurf.fill((0, 0, 0)) self.targetCircle.draw() self.pontFelirat.draw(self.pont) for ball in self.ballsList: ball.draw() def clicked(self, pos): i = 0 while i < len(self.ballsList) and not (self.ballsList[i].getPos().distance_to(pos) < self.ballsList[i].radius): i += 1 if i < len(self.ballsList): self.ballsList[i].setColor(randint(0, len(Circle.COLORS)-1)) def clearBall(self, ball): self.ballsList.pop(self.ballsList.index(ball))
class TestCircle: def setup(self): self.X = Circle("orange", 4) self.Y = Circle("green", 8) def testgetRadius(self): assert self.X.getRadius() == 4 assert self.Y.getRadius() == 8 def testsetRadius(self): self.X.setRadius(50) assert self.X.radius == 50 self.Y.setRadius(100) assert self.Y.radius == 100 def testcomputeself(self): assert self.X.computeArea() == (3.14159 * (4**2)) assert self.Y.computeArea() == (3.14159 * (8**2)) def testcomputerPerimeter(self): assert self.X.computePerimeter() == (3.14159 * 4 * 2) assert self.Y.computePerimeter() == (3.14159 * 8 * 2) def testgetShapeProperties(self): assert self.X.getShapeProperties( ) == f"Shape: CIRCLE, color: {self.X.color}, Radius: {self.X.radius}," '\n' f"Area: {self.X.computeArea()}, Perimeter: {self.X.computePerimeter()}" assert self.Y.getShapeProperties( ) == f"Shape: CIRCLE, color: {self.Y.color}, Radius: {self.Y.radius}," '\n' f"Area: {self.Y.computeArea()}, Perimeter: {self.Y.computePerimeter()}" def testsetColor(self): self.X.setColor("blue") assert self.X.color == "blue" def testgetColor(self): assert self.X.getColor() == "orange"
class TestCircle: def setup(self): self.A = Circle("red", 3) self.B = Circle("blue", 5) def testgetRadius(self): assert self.A.getRadius() == 3 assert self.B.getRadius() == 5 def testsetRadius(self): self.A.setRadius(1000) assert self.A.radius == 1000 self.B.setRadius(100) assert self.B.radius == 100 def testcomputeself(self): assert self.A.computeArea() == (3.14159 * (3**2)) assert self.B.computeArea() == (3.14159 * (5**2)) def testcomputePerimeter(self): assert self.A.computePerimeter() == (3.14159 * 3 * 2) assert self.B.computePerimeter() == (3.14159 * 5 * 2) def testgetShapeProperties(self): assert self.A.getShapeProperties() == f"Shape: CIRCLE, Color: {self.A.color}, Radius: {self.A.radius}, " \ f"Area: {self.A.computeArea()}, Perimeter: {self.A.computePerimeter()}" assert self.B.getShapeProperties() == f"Shape: CIRCLE, Color: {self.B.color}, Radius: {self.B.radius}, " \ f"Area: {self.B.computeArea()}, Perimeter: {self.B.computePerimeter()}" def testsetColor(self): self.A.setColor("magenta") assert self.A.color == "magenta" def testgetColor(self): assert self.A.getColor() == "red"
# -*- coding: utf-8 -*- """ Created on Tue Jun 9 22:11:35 2020 @author: mushroom """ from GeometricObject import GeometricObject from Circle import Circle from Rectangle import Rectangle cr = int(input()) rw = int(input()) rh = int(input()) cc = str(input()) cf = bool(input()) rc = str(input()) rf = bool(input()) c = Circle(cr, cc, cf) r = Rectangle(rw, rh, rc, rf) print("Circle:") print("Radius is {0}\nDiameter is {1}\nArea is {2}\nPerimeter is {3}"\ .format(c.getRadius(),c.getDiameter(), c.getArea(), c.getPerimeter())) print("color: %s and filled: %s" % (c.getColor(), c.isFilled())) print("\nRectangle:") print("Width is {0}\nHeight is {1}\nArea is {2}\nPerimeter is {3}\ncolor: {4} and filled: {5}"\ .format(r.getWidth(), r.getHeight(), r.getArea(), r.getPerimeter(), r.getColor(), r.isFilled()))