Exemplo n.º 1
0
def get_image_path():
    width, height = 300, 150
    win = GraphWin("Image Path", width, height)
    win.setBackground(color_rgb(0, 0, 0))

    path_entry = Entry(Point(width // 2, height // 2 - 50), 30)
    path_entry.draw(win)

    ok_button = Rectangle(Point(width - 50, height - 50),
                          Point(width - 5, height - 5))
    ok_button.setFill(color_rgb(255, 255, 255))
    ok_button.draw(win)
    ok_button_text = Text(ok_button.getCenter(), "OK")
    ok_button_text.setSize(15)
    ok_button_text.draw(win)

    while True:
        click = win.getMouse()
        clickx = click.getX()
        clicky = click.getY()

        if ok_button.getP1().getX() <= clickx <= ok_button.getP2().getX(
        ) and ok_button.getP1().getY() <= clicky <= ok_button.getP2().getY():
            win.close()
            return str(path_entry.getText())
Exemplo n.º 2
0
class Cell(object):
    __window = None

    def __init__(self,
                 pos,
                 cell_type,
                 win,
                 cell_pos,
                 color=EMPTY_COLOR,
                 text=""):

        self.__pos = pos

        self.__cell_pos = cell_pos

        self.__rectangle = Rectangle(
            Point(pos[0], pos[1]),
            Point(pos[0] + CELL_DIMENSION[0], pos[1] + CELL_DIMENSION[1]))
        self.__rectangle.setFill(color)
        self.type = cell_type

        self.__text = Text(self.__rectangle.getCenter(), text)
        self.__text.setSize(7)

        if not self.__window:
            self.__window = win

    def draw(self):
        self.__rectangle.draw(self.__window)

    def set_fill(self, color):
        self.__rectangle.setFill(color)

    def set_text(self, text):
        self.__text.setText(text)

    def get_pos(self):
        return copy(self.__cell_pos)

    def undraw(self):
        try:
            self.__rectangle.undraw()
        except Exception as e:
            print(e)
            pass

    def draw_text(self):
        self.__text.draw(self.__window)

    def undraw_text(self):
        self.__text.undraw()
Exemplo n.º 3
0
class Button:
    def __init__(self,
                 pos,
                 width,
                 height,
                 type=None,
                 bc=color_rgb(255, 255, 255),
                 text="",
                 size=18,
                 txtc=color_rgb(0, 0, 0),
                 style="normal",
                 font="arial"):

        oWidth = width / 2  #offset width
        oHeight = height / 2  #offset height

        pos1 = Point(pos.getX() - oWidth, pos.getY() - oHeight)
        pos2 = Point(pos.getX() + oWidth, pos.getY() + oHeight)

        self.object = Rectangle(pos1, pos2)
        self.object.setFill(bc)
        self.object.setOutline(color_rgb(255, 255, 255))
        self.pos = pos
        self.pos1 = pos1  #Left-Up Corner
        self.pos2 = pos2  #Right-Down Corner
        self.type = type
        self.width = width
        self.height = height

        self.text = Text(self.object.getCenter(), text, size, txtc, style,
                         font)

    def draw(self, win):
        self.object.draw(win)
        self.text.draw(win)

    def undraw(self):
        self.object.undraw()
        self.text.undraw()

    def getPos(self):
        return self.pos

    def getPos1(self):
        return self.pos1

    def getPos2(self):
        return self.pos2

    def getType(self):
        return self.type
Exemplo n.º 4
0
class Button:
    def __init__(self, x, y, w, h, text, toggle):
        self.rect = Rectangle(Point(x, y), Point(x + w, y + h))
        self.text = Text(self.rect.getCenter(), text)
        self.toggle = toggle
        self.pressed = False
        self.func = None
        self.locked = False

    def press(self, win):
        if not self.locked:
            if (self.toggle):
                self.pressed = not self.pressed
            self.func(self.pressed)
            self.draw(win)

    def draw(self, win):
        if (self.pressed or self.locked):
            self.rect.setFill(color_rgb(100, 100, 100))
        else:
            self.rect.setFill(color_rgb(170, 170, 170))
        self.rect.undraw()
        self.rect.draw(win)
        self.text.undraw()
        self.text.draw(win)

    def isInside(self, x, y):
        xmin = self.rect.getP1().getX()
        ymin = self.rect.getP1().getY()
        xmax = self.rect.getP2().getX()
        ymax = self.rect.getP2().getY()
        return (xmin <= x <= xmax) and (ymin <= y <= ymax)

    def setFunc(self, func):
        self.func = func

    def lock(self, win):
        self.locked = True
        self.draw(win)

    def unlock(self, win):
        self.locked = False
        self.draw(win)
Exemplo n.º 5
0
def func_clear(pressed):
    butClear.lock(win)
    if (butStart.pressed): butStart.press(win)
    setText("Tablero limpiado!")
    tablero.clear()
    tablero.draw(win)
    butClear.unlock(win)


butStart.setFunc(func_start)
butWalls.setFunc(func_walls)
butRobot.setFunc(func_robot)
butClear.setFunc(func_clear)

info = Rectangle(Point(425, 5), Point(745, 45))
infoText = Text(info.getCenter(), "")


def main():
    global win
    win = GraphWin("Practica 1 - Robot", tablero.cols * tablero.cell_size,
                   tablero.rows * tablero.cell_size + 50)
    win.setBackground(color_rgb(192, 192, 192))
    tablero.draw(win)
    butStart.draw(win)
    butWalls.draw(win)
    butRobot.draw(win)
    butClear.draw(win)

    win.bind("<Button-1>", mousepressHandler)
    win.bind("<B1-Motion>", mousedragHandler)
Exemplo n.º 6
0
from math import radians as rad
from time import sleep

size = 500
half = size / 2
win = GraphWin("SPRING", size, size)

rect = Rectangle(Point(half - 25, size - 50), Point(half + 25, size))
rect.setFill("red")
rect.draw(win)

pos = win.getMouse()
print(pos, "start")
x = pos.getX() - half

cen = rect.getCenter()

for i in range(1000):

    dist = 4 * pi * sin(rad(4 * pi * i))
    rect.move(dist + x, 0)

    if cen.getX() >= rect.getCenter().getX():
        print(cen.getX(), "min")
    else:
        print(cen.getX())

    cen = rect.getCenter()

    sleep(0.1)
Exemplo n.º 7
0
def movePete(pete, field, x, y, score, sensors, spin_square, blackcenter1,
             blackcenter2, goodsensors):

    # Find the location of Pete and spin square, and derive the edges of Pete
    pete_center = pete.getCenter()
    clickX = x
    clickY = y
    peteX, peteY = clickCoords(pete_center)
    spinX, spinY = clickCoords(spin_square.getCenter())

    # Define bounds of Pete object
    peteUpperX = peteX + 20
    peteLowerX = peteX - 20
    peteUpperY = peteY + 20
    peteLowerY = peteY - 20

    # Undraw Pete object so it can be re-drawn
    pete.undraw()

    # Check if Pete is on spin square, and randomly move him in a direction
    #   that is not a wall
    if peteX == spinX and peteY == spinY:
        direction = {
            '-1,-1': 'up,left',
            '-1,0': 'left',
            '-1,1': 'down,left',
            '0,-1': 'up',
            '0,0': 'none',
            '0,1': 'down',
            '1,-1': 'up,right',
            '1,0': 'right',
            '1,1': 'down,right'
        }
        x_movement = randint(-1, 1)
        y_movement = randint(-1, 1)
        peteX += x_movement * 40
        peteY += y_movement * 40
        direction_key = str(x_movement) + "," + str(y_movement)
        direction_value = direction[direction_key]
        score = checkSensor(direction_value, sensors, score, peteX, peteY,
                            goodsensors)

    # If not on spin tile, perform a normal action
    else:

        # Create list of directions
        direction = []

        # Move Pete up if the user clicks above him
        if clickY < peteLowerY:
            peteY -= 40
            direction.append("up")

        # Move Pete down if the user clicks below him
        if clickY > peteUpperY:
            peteY += 40
            direction.append("down")

        # Move Pete left if the user clicks left of him
        if clickX < peteLowerX:
            peteX -= 40
            direction.append("left")

        # Move Pete right if the user clicks to the right of him
        if clickX > peteUpperX:
            peteX += 40
            direction.append("right")
        direction = ",".join(direction)
        score = checkSensor(direction, sensors, score, peteX, peteY,
                            goodsensors)

    # Define and re-draw Pete Rectangle object
    pete = Rectangle(Point(peteX - 16, peteY - 16),
                     Point(peteX + 16, peteY + 16))
    pete.setFill("gold")
    pete.draw(field)

    # Return the center of Pete so the victory condition can be checked
    pete_center = pete.getCenter()

    # Get the center of Pete
    petecenter = pete.getCenter()
    # If center of Pete matches blackcenter1, move him to blackcenter2
    if petecenter.getX() == blackcenter1.getX() and petecenter.getY(
    ) == blackcenter1.getY():
        pete.undraw()
        petecenter = blackcenter2
        pete = Rectangle(
            Point(blackcenter2.getX() - 20,
                  blackcenter2.getY() - 20),
            Point(blackcenter2.getX() + 20,
                  blackcenter2.getY() + 20))
        pete.setFill('gold')
        pete.draw(field)

    # If center of Pete matches blackcenter2, move him to blackcenter1
    elif petecenter.getX() == blackcenter2.getX() and petecenter.getY(
    ) == blackcenter2.getY():
        pete.undraw()
        petecenter = blackcenter1
        pete = Rectangle(
            Point(blackcenter1.getX() - 20,
                  blackcenter1.getY() - 20),
            Point(blackcenter1.getX() + 20,
                  blackcenter1.getY() + 20))
        pete.setFill('gold')
        pete.draw(field)

    # Return objects
    return pete, pete_center, score
Exemplo n.º 8
0
def drawSensors(field):
    # Set initial coordinates to potentially draw sensors and empty list
    #   of sensor locations
    x = 40
    y = 40
    sensors = []
    goodsensors = []

    # Initialize loop to create horizontal sensors
    for column in range(10):
        for row in range(10):
            # Create random number between 0 and 1
            h_chance_border = random()
            v_chance_border = random()

            # Creates 40% chance horizontal sensor will be drawn
            if h_chance_border >= 0.6:
                # Define, draw, and append location of sensor
                horizontal_sensor = Rectangle(Point(x - 35.5, y),
                                              Point(x - 4.5, y))
                horizontal_sensor.setWidth(2.5)
                horizontal_sensor.setOutline("orange")
                horizontal_sensor.draw(field)
                h_x = horizontal_sensor.getCenter().getX()
                h_y = horizontal_sensor.getCenter().getY()
                sensors.append([h_x, h_y])

            # Creates 40% chance horizontal sensor will be drawn
            if v_chance_border >= 0.6:
                # Define, draw, and append location of sensor
                vertical_sensor = Rectangle(Point(x, y - 35.5),
                                            Point(x, y - 4.5))
                vertical_sensor.setWidth(2.5)
                vertical_sensor.setOutline("orange")
                vertical_sensor.draw(field)
                v_x = vertical_sensor.getCenter().getX()
                v_y = vertical_sensor.getCenter().getY()
                sensors.append([v_x, v_y])

            # Move to next row
            y += 40
        # Set back to first row after finishing final row
        y = 40
        # Move to the next column
        x += 40
    x = 40
    y = 40

    # Initialize loop to create good horizontal sensors
    for column in range(9):
        for row in range(9):

            # Create random number between 0 and 1
            h_chance_border = random()
            v_chance_border = random()

            # Creates 40% chance horizontal sensor will be drawn
            if h_chance_border <= .25:
                # Define, draw, and append location of sensor
                horizontal_sensorgood = Rectangle(Point(x - 35.5, y),
                                                  Point(x - 4.5, y))
                horizontal_sensorgood.setWidth(2.5)
                horizontal_sensorgood.setOutline("green")
                h_xg = horizontal_sensorgood.getCenter().getX()
                h_yg = horizontal_sensorgood.getCenter().getY()
                if [h_xg, h_yg] not in sensors:
                    horizontal_sensorgood.draw(field)
                    goodsensors.append([h_xg, h_yg])

            # Creates 40% chance horizontal sensor will be drawn
            if v_chance_border <= 0.25:
                # Define, draw, and append location of sensor
                vertical_sensorgood = Rectangle(Point(x, y - 35.5),
                                                Point(x, y - 4.5))
                vertical_sensorgood.setWidth(2.5)
                vertical_sensorgood.setOutline("green")
                v_xg = vertical_sensorgood.getCenter().getX()
                v_yg = vertical_sensorgood.getCenter().getY()
                if [v_xg, v_yg] not in sensors:
                    vertical_sensorgood.draw(field)
                    goodsensors.append([v_xg, v_yg])

            # Move to next row
            y += 40
        # Set back to first row after finishing final row
        y = 40
        # Move to the next column
        x += 40

    # Draw vertical sensors

    return sensors, goodsensors