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))
def main(): circle = Circle() redCircle = RedShapeDecorator(Circle()) redRectangle = RedShapeDecorator(Rectangle()) print("Circle with normal border") circle.draw() print("Circle of red border") redCircle.draw() print("Rectangle of red border") redRectangle.draw()
class ShapeMaker: """Facade class""" def __init__(self): self.__circle = Circle() self.__rectangle = Rectangle() self.__square = Square() def drawCircle(self): self.__circle.draw() def drawRectangle(self): self.__rectangle.draw() def drawSquare(self): self.__square.draw()
def main(): print("Composite pattern Shapes..") cir1 = Circle() cir1.draw("red") cir2 = Circle() cir2.draw("blue") tri1 = Triangle() tri1.draw("green") drawing = Drawing() drawing.add(cir1) drawing.add(tri1) drawing.draw("yellow") drawing.add(cir2) drawing.draw("orange") drawing.remove(tri1) drawing.draw("orange")
def draw(self): if int(turtle.heading()) == 180: turtle.right(180) #snowperson drawing sn = SnowPerson(self.posX, self.posY) sn.draw() #SnowMan Features #Hat x = self.posX y = self.posY turtle.up() p = x - 35 q = y + 59 turtle.pen(pencolor="brown", pensize=2) turtle.setposition(p, q) turtle.down() p = x + 35 turtle.setposition(p, q) turtle.up() p = x - 19 turtle.setposition(p, q) turtle.down() p = x + 15 r = Rectangle(p, q) r.draw() #body items p = x q = y - 15 turtle.up() turtle.setposition(p, q) turtle.down() c = Circle(7, "wheat") c.draw() p = x q = y - 55 turtle.up() turtle.setposition(p, q) turtle.down() c = Circle(7, "wheat") c.draw() #base items p = x q = y - 115 turtle.up() turtle.setposition(p, q) turtle.down() c = Circle(7, "wheat") c.draw() p = x q = y - 155 turtle.up() turtle.setposition(p, q) turtle.down() c = Circle(7, "wheat") c.draw() p = x q = y - 200 turtle.up() turtle.setposition(p, q) turtle.down() c = Circle(7, "wheat") c.draw()
ball_color = (255,0,0) ball = Circle(ball_color, ball_radius, ball_center) wall = Wall(win, win_size, platform_color) bricks_rows = 3 bricks_cols = 5 wall.fill_bricks(bricks_rows, bricks_cols, 2) FPS = 60 clock = pygame.time.Clock() game_over = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() win.fill(background_color) if not game_over: #code game_over = ball.random_movement(win_size, platform, wall) wall.draw() ball.draw(win) platform.move_by_keys() platform.draw() else: text_left = (win_size - endgame_text.get_width())/2 text_top = (win_size - endgame_text.get_height())/2 win.blit(endgame_text, (text_left, text_top)) pygame.display.update() clock.tick(FPS)
def draw(self): #wn = turtle.Screen() #t = turtle.Turtle() if int(turtle.heading()) == 180: turtle.right(180) turtle.up() turtle.pen(pencolor="black",pensize=2) turtle.setposition(self.posX,self.posY) x,y = turtle.pos() turtle.down() #face c = Circle(30,"") c.draw() x,y = turtle.pos() x = round(x,3) y = round(y,3) #right eye turtle.up() p = x + 6 q = y + 35 turtle.setposition(p,q) turtle.down() c = Circle(3,"blue") c.draw() #left eye turtle.up() turtle.setposition(x,y) p = x - 6 q = y + 35 turtle.setposition(p,q) turtle.down() c = Circle(3,"blue") c.draw() #smile turtle.up() turtle.right(90) turtle.setposition(x,y) p = x - 6 q = y + 20 turtle.setposition(p,q) turtle.down() turtle.pencolor("red") turtle.circle(7,180) turtle.pencolor("black") #body turtle.up() turtle.setposition(x,y) turtle.left(90) turtle.down() c = Circle(45,"") c.draw() #left hand turtle.up() turtle.pen(pencolor="brown",pensize=5) turtle.setposition(x,y) p = x - 38 q = y - 20 turtle.setposition(p,q) turtle.down() turtle.setposition(p - 45,q + 20) #right hand turtle.up() turtle.setposition(x,y) p = x + 38 q = y - 20 turtle.setposition(p,q) turtle.down() turtle.setposition(p + 45,q + 20) #base turtle.up() turtle.pen(pencolor="black",pensize=2) turtle.setposition(x,y) p = x q = y - 90 turtle.setposition(p,q) turtle.down() c = Circle(70,"") c.draw()
def main(): redCircle = Circle(100, 100, 10, RedCircle()) greenCircle = Circle(100, 100, 10, GreenCircle()) redCircle.draw() greenCircle.draw()
def draw(self): if int(turtle.heading()) == 180: turtle.right(180) #snowperson drawing sn = SnowPerson(self.posX, self.posY) sn.draw() #SnowMan Features #Hair and hat #left x = self.posX y = self.posY turtle.pen(pencolor="brown", pensize=2) turtle.up() p = x - 20 q = y + 55 turtle.setposition(p, q) turtle.down() q = y p = x - 40 turtle.setposition(p, q) turtle.up() p = x - 15 q = y + 55 turtle.setposition(p, q) turtle.down() q = y + 18 p = x - 28 turtle.setposition(p, q) #right turtle.up() p = x + 20 q = y + 55 turtle.setposition(p, q) turtle.down() q = y p = x + 40 turtle.setposition(p, q) turtle.up() p = x + 15 q = y + 55 turtle.setposition(p, q) turtle.down() q = y + 18 p = x + 28 turtle.setposition(p, q) #hat p = x q = y + 110 turtle.up() turtle.setposition(p, q) turtle.down() t = Triangle(p, q) t.draw() #lips p = x q = y + 17.5 turtle.up() turtle.setposition(p, q) turtle.down() turtle.pen(fillcolor="red", pencolor="red", pensize=1) turtle.begin_fill() turtle.circle(3) turtle.end_fill() turtle.pen(fillcolor="", pencolor="black", pensize=2) #body items p = x q = y - 15 turtle.up() turtle.setposition(p, q) turtle.down() c = Circle(7, "yellow") c.draw() p = x q = y - 55 turtle.up() turtle.setposition(p, q) turtle.down() c = Circle(7, "violet") c.draw() #base items p = x q = y - 125 turtle.up() turtle.setposition(p, q) turtle.down() c = Circle(7, "yellow") c.draw() p = x q = y - 180 turtle.up() turtle.setposition(p, q) turtle.down() c = Circle(7, "violet") c.draw()