def draw(self): '''Draws all objects that are in the gameobjects list to the screen''' self.screen.fill((0, 0, 0)) yaxis = Line(WHITE, 5) yaxis.draw(self.screen, [ Vector2(0, -(SCREEN_HEIGHT / 2)), Vector2(0, (SCREEN_HEIGHT / 2)) ]) xaxis = Line(WHITE, 5) xaxis.draw( self.screen, [Vector2(-(SCREEN_WIDTH / 2), 0), Vector2(SCREEN_WIDTH / 2, 0)]) mxpos, mypos = pygame.mouse.get_pos() mouse = Rectangle(WHITE, Vector2(25, 25)) mpos = worldtoscreen(Vector2(mxpos, mypos)) mpos = mpos.scale(-1) mouse.draw(self.screen, mpos) for gameobject in self.gameobjects: gameobject.draw(self.screen)
from shapes import Rectangle, Triangle, Paper, Oval paper = Paper() rect = Rectangle() rect.set_width(200) rect.set_height(50) rect.set_color('red') rect.set_x(10) rect.set_y(10) rect.draw() rect1 = Rectangle() rect1.set_width(200) rect1.set_height(50) rect1.set_color('purple') rect1.set_x(80) rect1.set_y(80) rect1.draw() paper.display()
# Create a convas with the user data canvas = Canvas(height=canvas_height, width=canvas_width, color=colors[canvas_color]) while True: shape_type = input("What do you like to draw? Enter quit to quit. ") if shape_type.lower() == "rectangle": rec_x = int(input("Enter x of the rectangle: ")) rec_y = int(input("Enter y of the rectangle: ")) rec_width = int(input("Enter width of rectangle: ")) rec_height = int(input("Enter height of rectangle: ")) rec_colors = input("Enter weight of Red,Blue,Green for the rectangle: ") rec_colors = rec_colors.split(',') rec = Rectangle(x=rec_x, y=rec_y, width=rec_width, height=rec_height, color=(rec_colors[0], rec_colors[1], rec_colors[2])) rec.draw(canvas) if shape_type.lower() == "square": squ_x = int(input("Enter x of the square: ")) squ_y = int(input("Enter y of the square: ")) squ_side = int(input("Enter side of square: ")) rec_colors = input("Enter weight of Red,Blue,Green for the square: ") rec_colors = rec_colors.split(',') squ = Square(x=squ_x, y=squ_y, side=squ_side, color=(rec_colors[0], rec_colors[1], rec_colors[2])) squ.draw(canvas) if shape_type.lower() == "quit": break
app = App(width=600, height=600) button = PushButton(app, changeMe, text="Click me") p = Paper(app) t = Triangle(p) t.randomize() t.draw() t2 = Triangle(p) t2.randomize() t2.draw() r = Rectangle(p) r.randomize() r.width = 400 r.draw() r.color = 'red' r.width = 20 app.display() #p = Paper() #p.mainloop() #tri = Triangle() #tri.randomize() #tri.draw()
from shapes import Triangle, Rectangle, Oval rect = Rectangle() rect.set_width(200) rect.set_height(100) rect.set_color('blue') rect.draw() square = Rectangle() square.set_width(50) square.set_height(50) square.set_color('yellow') square.set_x(120) square.set_y(120) square.draw() tri = Triangle(5, 5, 100, 5, 100, 200) tri.set_color('red') tri.draw()
def mainPillar(x, y, bottom_height, top_height, width, thickness): # Draw bottom column 1 col1 = Rectangle(x, y, bottom_height, thickness) col1.draw() # Draw bottom column 2 x, y = col1.bottom_right col2 = Rectangle(x + width, y, bottom_height, thickness) col2.draw() # Draw rods inside col1 and col2 roddist = width / 3 x, y = col1.bottom_right draw_rods(x, y, bottom_height, roddist) # Draw bottom column 3 x, y = col2.bottom_right col3 = Rectangle(x + width, y, bottom_height, thickness) col3.draw() # Draw rods inside col2 and col3 x, y = col2.bottom_right draw_rods(x, y, bottom_height, roddist) x1, y1 = col1.top_left x2, y2 = col3.top_left turtle.penup() turtle.setpos(x1, y1) turtle.setheading(0) turtle.pendown() turtle.forward(turtle.distance(x2, y2)) # Draw top column 1 x, y = col1.top_left col4 = Rectangle(x, y, top_height, thickness) col4.draw_right(30) # Draw top column 2 x, y = col2.top_left col5 = Rectangle(x, y, top_height, thickness) col5.draw_right(30) # Draw rods inside col4 and col5 x, y = col4.bottom_right draw_rods(x, y, top_height, roddist, -30) # Draw top column 3 x, y = col2.top_left x, y = col3.top_left col6 = Rectangle(x, y, top_height, thickness) col6.draw_right(30) # Draw rods inside col5 and col6 x, y = col5.bottom_right draw_rods(x, y, top_height, roddist, -30) x, y = col4.top_left beam1 = Rectangle(x, y + 1, thickness, width * 2 + 30) beam1.draw_right(30) # Remove intersection of coloumn 4 and beam 1 x, y = beam1.bottom_left print((x, y)) beam1.erase((x + 1, y), (x + thickness - 1, y)) # Remove intersection of coloumn 5 and beam 1 x, y = col5.top_left print((x, y)) beam1.erase((x + 1, y + 1), (x + thickness + 2, y)) # Remove intersection of coloumn 5 and beam 1 x, y = col6.top_left print((x, y)) beam1.erase((x + 1, y + 1), (x + thickness + 2, y)) return col3.bottom_right
"""Using the 'shapes' module to create shapes and control their attributes""" from shapes import Triangle, Rectangle, Oval # create a rectangle: rect_1 = Rectangle(color=None) # set the attributes: #rect_1.set_width(200) #rect_1.set_height(100) #rect_1.set_color('blue') # draw the rectangle: rect_1.draw() # create an oval: oval_1 = Oval() oval_1.set_width(100) oval_1.set_height(50) oval_1.set_color('yellow') oval_1.set_x(100) oval_1.set_y(200) oval_1.draw() rect_2 = Rectangle() #rect_2.set_width(100) #rect_2.set_height(150) #rect_2.set_color('yellow') #rect_2.set_x(100) #rect_2.set_y(75)
## # This program tests several of the geometric shape classes. # from ezgraphics import GraphicsWindow from shapes import Rectangle, Line # Create the window. win = GraphicsWindow() canvas = win.canvas() # Draw a rectangle. rect = Rectangle(10, 10, 90, 60) rect.setFill("light yellow") rect.draw(canvas) # Draw another rectangle. rect.moveBy(rect.getWidth(), rect.getHeight()) rect.draw(canvas) # Draw six lines of different colors. colors = ["red", "green", "blue", "yellow", "magenta", "cyan"] line = Line(10, 150, 300, 150) for i in range(6): line.setColor(colors[i]) line.draw(canvas) line.moveBy(10, 40) win.wait()
# RENNIE THE ROBOT # FUTURELEARN, SEPTEMBER 03,2017 # Ashmead ALI from shapes import Triangle, Rectangle, Oval mouth = Rectangle() mouth.set_width(200) mouth.set_height(100) mouth.set_color('blue') mouth.set_x(200) mouth.set_y(450) mouth.draw() nose = Oval() nose.set_width(100) nose.set_height(100) nose.set_color('red') nose.set_x(250) nose.set_y(250) nose.draw() left_eye = Rectangle() left_eye.set_width(100) left_eye.set_height(100) left_eye.set_color('blue') left_eye.set_x(100) left_eye.set_y(100) left_eye.draw() left_pupil = Oval()
'''Main file for testing drawing utils''' #pylint: disable=E1101 import pygame from shapes import Rectangle from shapes import Circle from shapes import Text from vector2 import Vector2 pygame.init() SCREEN = pygame.display.set_mode((1080, 720)) SCREEN.fill((255, 255, 255)) RECT = Rectangle(Vector2(100, 100), (0, 255, 0), (100, 100), SCREEN) CIRC = Circle(Vector2(400, 400), (255, 0, 0), 50, SCREEN) TEXT = Text(Vector2(10, 10), (255, 0, 0), "Hello World", 24, SCREEN) RUNNING = True while RUNNING: for event in pygame.event.get(): if event.type == pygame.QUIT: RUNNING = False RECT.draw() CIRC.draw() TEXT.draw() pygame.display.flip()
from shapes import Triangle, Rectangle, Oval rect1 = Rectangle() rect1.set_width(200) rect1.set_height(100) rect1.set_color("blue") rect1.draw() rec2 = Rectangle() rec2.set_width(50) rec2.set_height(150) rec2.set_color("yellow") rec2.draw() rec2.set_x(100) rec2.set_y(100) ov1 = Oval() ov1.randomize(20) ov1.draw() tri1 = Triangle(5, 10, 100, 5, 100, 200) tri1.draw()
) # If user enters rectangle if shape_type.lower() == "rectangle": rec_width = get_sanitized_input("Enter the rectangle's width: ", int, 1, canvas_width) rec_height = get_sanitized_input("Enter the rectangle's height: ", int, 1, canvas_height) rec_x = get_sanitized_input("Enter the rectangle's x coordinate: ", int, 0, canvas_width - rec_width) rec_y = get_sanitized_input("Enter the rectangle's y coordinate: ", int, 0, canvas_height - rec_height) red = get_sanitized_input("How much red should the rectangle have? ", int, 0, 255) green = get_sanitized_input("How much green should the rectangle have? ", int, 0, 255) blue = get_sanitized_input("How much blue should the rectangle have? ", int, 0, 255) # Draw the rectangle rectangle = Rectangle(x=rec_x, y=rec_y, height=rec_height, width=rec_width, color=(red, green, blue)) rectangle.draw(canvas) # If user enters rectangle if shape_type.lower() == "square": square_side = get_sanitized_input( "Enter the square's side: ", int, 1, canvas_width if canvas_width < canvas_height else canvas_height) square_x = get_sanitized_input("Enter the square's x coordinate: ", int, 0, canvas_width - square_side) square_y = get_sanitized_input("Enter the square's y coordinate: ", int, 0, canvas_height - square_side) red = get_sanitized_input("How much red should the square have? ", int, 0, 255) green = get_sanitized_input("How much green should the square have? ", int, 0, 255) blue = get_sanitized_input("How much blue should the square have? ", int, 0, 255) # Draw the square square = Square(x=square_x, y=square_y, side=square_side, color=(red, green, blue)) square.draw(canvas)
class Car: def __init__(self, pos=Point2D(0, 0), angle=0, velocity=0, maxVelocity=5, bodyWidth=30, bodyLength=60, headlightsOn=False, headlightPower=100): self.pos = pos #type: Point self.velocity = velocity #type: float self.maxVelocity = maxVelocity self.angle = angle #type: float self.headlightsOn = headlightsOn #type: bool self.headlightPower = headlightPower #type: int self.height = bodyWidth #type: int self.width = bodyLength #type: int self.__body = Rectangle(bodyLength, bodyWidth, pos, color=(1, 0, 0)) self.__headlight1 = Sector(Point2D(pos.x, pos.y), angle, inner=40, color=(1, 1, 0.5)) self.__headlight1.hide() self.__headlight2 = Sector(Point2D(pos.x, pos.y), angle, inner=40, color=(1, 1, 0.5)) self.__headlight2.hide() def velocityVector(self): x = cos(rad(self.angle)) * self.velocity y = sin(rad(self.angle)) * self.velocity return Vector2D(x, y) def draw(self): if self.headlightsOn: self.__headlight1.draw() self.__headlight2.draw() self.__body.draw() def turnBy(self, angle, mustMove=True): newAngle = self.angle if (mustMove): newAngle += angle * (self.velocity / self.maxVelocity) else: newAngle += angle if newAngle >= 360: newAngle -= 360 elif newAngle < 0: newAngle += 360 self.face(newAngle) def turnToward(self, point, gradual=True): if self.pos.dist(point) < 0.1: return posdiff = point - self.pos angle = Vector2D(posdiff.x, posdiff.y) angle = deg(Vector2D(1, 0).angle(angle)) diff = angle - self.__body.angle if diff > 180: diff -= 360 elif diff <= -180: diff += 360 if abs(diff) < 0.01: diff = 0 return if gradual and diff > 0: diff = sqrt(diff / 100) elif gradual and diff < 0: diff = -sqrt(-diff / 100) if diff != 0: self.turnBy(diff, mustMove=False) def face(self, angle): self.angle = angle self.__body.angle = self.angle self.__headlight1.angle = self.__body.angle - self.__headlight1.inner / 2 self.__headlight2.angle = self.__body.angle - self.__headlight2.inner / 2 def accelerate(self, deltaV): self.velocity += deltaV if self.velocity > self.maxVelocity: self.velocity = self.maxVelocity elif self.velocity < -self.maxVelocity: self.velocity = -self.maxVelocity def brake(self, deltaV): if self.velocity > 0: self.velocity -= deltaV if self.velocity < 0: self.velocity += deltaV if abs(self.velocity) < 0.0000001: self.velocity = 0 #Implementation of Craig Reynold's Boids Arrival Simulation #https://slsdo.github.io/steering-behaviors/ def arrival(self, pos, radius): e = pos - self.pos dist = e.hypot() if dist < 0.01: dist = 0 if dist > 0 and dist < radius: self.velocity = self.maxVelocity * (dist / radius) elif dist > radius: self.velocity = self.maxVelocity def drive(self): self.pos += self.velocityVector() if self.headlightsOn: self.__headlight1.pos.x = self.pos.x + cos( rad(self.angle + 20)) * 30 self.__headlight1.pos.y = self.pos.y + sin( rad(self.angle + 20)) * 30 self.__headlight2.pos.x = self.pos.x + cos( rad(self.angle - 20)) * 30 self.__headlight2.pos.y = self.pos.y + sin( rad(self.angle - 20)) * 30 self.__body.pos = self.pos def toggleHeadlights(self): self.headlightsOn = not self.headlightsOn if not self.headlightsOn: self.__headlight1.hide() self.__headlight2.hide() else: self.__headlight1.show() self.__headlight2.show() def isMoving(self): return self.velocity != 0 @property def body(self): return self.__body @property def headlight1(self): return self.__headlight1 @property def headlight2(self): return self.__headlight2 @property def pos(self): return self.__pos @pos.setter def pos(self, pos): self.__pos = pos @property def maxVelocity(self): return self.__maxVelocity @maxVelocity.setter def maxVelocity(self, maxVelocity): self.__maxVelocity = maxVelocity @property def velocity(self): return self.__velocity @velocity.setter def velocity(self, velocity): self.__velocity = velocity @property def angle(self): return self.__angle @angle.setter def angle(self, angle): self.__angle = angle @property def headlightsOn(self): return self.__headlightsOn @headlightsOn.setter def headlightsOn(self, headlightsOn): self.__headlightsOn = headlightsOn @property def headlightPower(self): return self.__headlightPower @headlightPower.setter def headlightPower(self, headlightPower): self.__headlightPower = headlightPower
class Pillar(object): """ Pillar object. A pillar is a complex shape composed of three rectangle. 2 columns and a beam. Example: 2 ________ |______| 1 | | | | 3 | | | | | | | | Args: x (int): x coordinate of the bottom left corner of the first pillar y (int): y coordinate of the bottom left corner of the first pillar height (int): height of the pillar width (int): width of the pillar thickness (int): Thickness of the pillar columns and beams """ def __init__(self, x, y, height, width, thickness): self.x = x self.y = y self.height = height self.width = width self.thickness = thickness self.col1 = None self.beam2 = None self.col2 = None def draw(self): """ Draw pillar """ self.col1 = Rectangle(self.x, self.y, self.height, self.thickness) self.col1.draw() x, y = self.col1.top_left self.beam1 = Rectangle(x, y, self.thickness, self.width) self.beam1.draw() x, y = self.col1.bottom_left x = x + self.width - self.thickness self.col2 = Rectangle(x, y, self.height, self.thickness) self.col2.draw() def far_left(self): """ Returns bottom left coordinate of the left column """ return self.x, self.y def far_right(self): """ Returns bottom right coordinate of the right column """ return self.col2.bottom_right def prettify(self): """ Erase common section between left column and beam. And between right column and beam. """ x1, y1 = self.beam1.bottom_left x2, y2 = x1 + self.thickness, y1 self.beam1.erase((x1 + 1, y1), (x2 - 1, y2)) x1, y1 = self.beam1.bottom_right x2, y2 = x1 - self.thickness, y1 self.beam1.erase((x2 + 1, y2), (x1 - 1, y1))
from shapes import Triangle, Rectangle, Oval, Paper sky = Rectangle() sky.set_x(100) sky.set_y(100) sky.set_width(400) sky.set_height(200) sky.set_color("lightblue") sky.draw() sun = Oval() sun.set_width(75) sun.set_height(75) sun.set_x(400) sun.set_y(125) sun.set_color("yellow") sun.draw() grass = Rectangle() grass.set_x(100) grass.set_y(300) grass.set_width(400) grass.set_height(200) grass.set_color("green") grass.draw() wall = Rectangle() wall.set_x(150) wall.set_y(250) wall.set_width(300) wall.set_height(175)
if shape_type.lower() == "rectangle": rec_x = int(input("Enter x of the rectangle: ")) rec_y = int(input("Enter y of the rectangle: ")) rec_width = int(input("Enter the width of the rectangle: ")) rec_height = int(input("Enter the height of the rectangle: ")) red = int(input("How much red should the rectangle have? ")) green = int(input("How much green ? ")) blue = int(input("How much blue do you like? ")) # Create the rectangle r1 = Rectangle(x=rec_x, y=rec_y, width=rec_width, height=rec_height, color=(red, green, blue)) r1.draw(canvas) # Ask for square data and create square if user entered 'square' if shape_type.lower() == "square": sqr_x = int(input("Enter x of the square: ")) sqr_y = int(input("Enter y of the square: ")) sqr_side = int(input("Enter the side length of the square: ")) red = int(input("How much red should the square have? ")) green = int(input("How much green? ")) blue = int(input("How much blue do you like? ")) s1 = Square(x=sqr_x, y=sqr_y, side=sqr_side, color=(red, green, blue)) s1.draw(canvas) # Break the loop if user entered "quit" if shape_type.lower() == "quit": break
class NodeVisual(object): def __init__(self, node, draw_pos, scale, draw_surface): '''Initializes the visual node. The node argument is the a_star node that we will be polling property information from.''' self.node = node #A_Star node self.shape = Rectangle(draw_pos, (255, 255, 255), scale, draw_surface, 0) #Visual drawn to the screen self.is_hoovered = False #True if the mouse is hoovering over the node self.is_start = False #True if the node is the starting node of the algorithm self.is_goal = False #True if the node is the goal node of the algorithm self.is_path = False #True if the node is part of the algorithm path self.is_open = False #True if the node is in the open list self.is_closed = False #True if the node is in the closed list self.show_scores = False #Toggles the node scores to be drawn to the screen self.border = Rectangle( draw_pos, (255, 255, 255), scale, draw_surface, 4) #Highlight border for when the mouse is over the node def update(self, events): '''Handles all the bevhavious and color changes of the visual node''' self.node_clicked(events) if not self.node.traversable: self.shape.change_color((0, 0, 0)) elif self.is_closed and not self.is_start and not self.is_goal: self.shape.change_color((247, 143, 143)) elif self.is_open and not self.is_start and not self.is_goal: self.shape.change_color((143, 183, 247)) elif self.is_start: self.shape.change_color((0, 255, 0)) elif self.is_goal: self.shape.change_color((255, 0, 0)) else: self.shape.change_color((255, 255, 255)) if self.is_hoovered: self.border.change_color((255, 0, 0)) else: self.border.change_color((0, 0, 0)) def reset_node(self): '''Resets the node to its initial state''' self.is_closed = False self.is_open = False self.is_path = False self.node.parent = None def draw(self, graph_visual): '''All the drawing behaviours for the visual node''' self.shape.draw() self.border.draw() #If the nodes has a parent we will draw a line from the parent to this node if self.node.parent is not None: par = graph_visual.get_visual(self.node.parent) line = pygame.draw.lines( self.shape.draw_surface, (0, 255, 0), True, [[ self.shape.position.x_pos + self.shape.scale[0] / 2, self.shape.position.y_pos + self.shape.scale[1] / 2 ], [ par.shape.position.x_pos + par.shape.scale[0] / 2, par.shape.position.y_pos + par.shape.scale[1] / 2 ]], 1) #Drawing of the text to the screen #Will only draw if the node was used in the algorithm and show_scores is true if (self.is_open or self.is_closed or self.is_goal or self.is_start) and self.show_scores: text = Text(Vector2(5, 5), (0, 0, 0), str(self.node.f_score), 12, self.shape.draw_surface) text.draw_on_surface(self.shape.rect) text2 = Text(Vector2(25, 20), (0, 0, 0), str(self.node.h_score), 12, self.shape.draw_surface) text2.draw_on_surface(self.shape.rect) text3 = Text(Vector2(5, 20), (0, 0, 0), str(self.node.g_score), 12, self.shape.draw_surface) text3.draw_on_surface(self.shape.rect) def node_clicked(self, events): '''Click behaviour for the visual node. Highlights the node if it is being hoovered. Also if the mouse was clicked while it was hoovered it will make the node not traversable''' mouse_position = pygame.mouse.get_pos() if self.shape.rect.collidepoint(mouse_position): self.is_hoovered = True for event in events: if event.type == pygame.MOUSEBUTTONDOWN: self.node.traversable = not self.node.traversable else: self.is_hoovered = False
rectangle1.set_height(100) rectangle1.set_color("blue") rectangle1.set_x(50) rectangle1.set_y(250) triangle1 = Triangle() # triangle1.randomise() triangle1.set_color("red") triangle1.x=150 triangle1.y=150 triangle1.x2=150 triangle1.y2=200 triangle1.x3=200 triangle1.y3=150 oval1 = Oval() # oval1.randomise() oval1.set_color("green") oval1.set_x(300) oval1.set_y(300) oval1.set_height(200) oval1.set_width(200) rectangle1.draw() oval1.draw() triangle1.draw() root = Tk() gui = MyGUI(root) root.mainloop()
from shapes import Paper, Rectangle paper = Paper() rect = Rectangle() rect.set_color('yellow') rect.set_height(50) rect.set_width(150) rect.draw() rect.set_x(0) rect.set_y(0) rect2 = Rectangle() rect2.set_color('blue') rect2.set_height(10) rect2.set_width(10) rect2.draw() rect2.set_x(150) rect2.set_y(200) paper.display()
from shapes import Paper, Triangle, Rectangle, Oval paper = Paper() rect1 = Rectangle() rect1.set_width(200) rect1.set_height(100) rect1.set_color("blue") rect1.draw() rect2 = Rectangle() rect2.set_x(50) rect2.set_y(30) rect2.set_width(200) rect2.set_height(100) rect2.set_color("orange") rect2.draw() paper.display()
from shapes import Triangle, Rectangle, Oval r1 = Rectangle() r1.set_width(200) r1.set_height(100) r1.set_color("blue") r1.draw() r2 = Rectangle() r2.set_width(50) r2.set_height(150) r2.set_color("yellow") r2.set_x(100) r2.set_y(100) r2.draw()