Beispiel #1
0
class Basilisk:
    """Die Klasse Basilisk beschreibt die Eigenschaften und das Verhalten einer Schlange auf dem Spielfeld"""
    def __init__(self, window, shape):
        self.speed = 0.1
        self.score = 0
        self.highScore = 0

        self.mouth = RawTurtle(window)
        self.mouth.speed(0)
        self.mouth.shape(shape)
        self.mouth.home()
        self.mouth.direction = "stop"
        self.mouth.penup()

        self.deadFromPoison = False
        self.tempScore = 1
        self.body = []

    def getMouth(self):
        return self.mouth

    def getBodyList(self):
        return self.body

    def getTempScore(self):
        return self.tempScore

    def setTempScore(self, tempScore):
        self.tempScore = tempScore

    def getBodyLen(self):
        return len(self.body)

    def getYPos(self):
        return self.mouth.ycor()

    def getXPos(self):
        return self.mouth.xcor()

    def setMouthPos(self, x, y):
        self.mouth.goto(x, y)

    def getSpeed(self):
        return self.speed

    def getScore(self):
        return self.score

    def getHighScore(self):
        return self.highScore

    def setSpeed(self, speed):
        self.speed = speed

    def setScore(self, score):
        self.score = score

    def setHighScore(self, highScore):
        self.highScore = highScore

    def getMouthDirection(self):
        return self.mouth.direction

# Mit der setMouthDirection Methode bewegt sich die Schlange in eine bestimmte Richtung.

    def setMouthDirection(self, mouthDirection):
        if mouthDirection == "left":
            self.moveLeftwards()
        elif mouthDirection == "right":
            self.moveRightwards()
        elif mouthDirection == "up":
            self.moveUpwards()
        else:
            self.moveDownwards()

# Mit der moveUpwards Methode bewegt sich die Schlange nach oben.

    def moveUpwards(self):
        gifUp = "model/resources/up.gif"
        if self.mouth.direction != "down":
            self.mouth.direction = "up"
            self.mouth.shape(gifUp)

# Mit der moveDownwards Methode bewegt sich die Schlange nach unten.

    def moveDownwards(self):
        gifDown = "model/resources/down.gif"
        if self.mouth.direction != "up":
            self.mouth.direction = "down"
            self.mouth.shape(gifDown)

# Mit der moveLeftwards Methode bewegt sich die Schlange nach links.

    def moveLeftwards(self):
        gifLeft = "model/resources/left.gif"
        if self.mouth.direction != "right":
            self.mouth.direction = "left"
            self.mouth.shape(gifLeft)

# Mit der moveRightwards Methode bewegt sich die Schlange nach rechts.

    def moveRightwards(self):
        gifRight = "model/resources/right.gif"
        if self.mouth.direction != "left":
            self.mouth.direction = "right"
            self.mouth.shape(gifRight)

# Mit der move Methode wird beschrieben, wie groß ein Sritt der Schlange in alle Richtungen ist.

    def move(self):
        if self.mouth.direction == "up":
            self.mouth.sety(self.mouth.ycor() + 20)

        if self.mouth.direction == "down":
            self.mouth.sety(self.mouth.ycor() - 20)

        if self.mouth.direction == "left":
            self.mouth.setx(self.mouth.xcor() - 20)

        if self.mouth.direction == "right":
            self.mouth.setx(self.mouth.xcor() + 20)

    def bodyFollowMouth(self):
        for index in range(len(self.body) - 1, 0, -1):
            self.body[index].goto(self.body[index - 1].xcor(),
                                  self.body[index - 1].ycor())
        if len(self.body) > 0:
            self.body[0].goto(self.mouth.xcor(), self.mouth.ycor())

# Erweitere den Körper um einen Teil.

    def basiliskFeeded(self, window, shape):
        oneBodyBlock = RawTurtle(window)
        oneBodyBlock.speed(0)
        oneBodyBlock.shape(shape)
        oneBodyBlock.penup()
        self.body.append(oneBodyBlock)

# Gib ein Dictionary mit der Position von jedem Teil des Körpers zurück.

    def getBodyPosInListOfDic(self):
        if len(self.body) > 0:
            allBlockskPos = []
            for i in range(0, len(self.body)):
                x = self.body[i].xcor()
                y = self.body[i].ycor()
                allBlockskPos.append({"bodyBlock" + str(i): {'x': x, 'y': y}})

            return allBlockskPos

    def setBodyBlockPos(self, i, x, y):
        self.body[i].goto(x, y)

    def basiliskPoisoned(self):
        if len(self.body) > 0:
            self.body[-1].goto(1000, 1000)
            self.body.pop()
            return
        else:
            self.deadFromPoison = True

    def basiliskLives(self):
        self.deadFromPoison = False

# Die Schlange darf durch Wände laufen.

    def basiliskPushTheWall(self):
        if self.mouth.xcor() > 290:
            self.mouth.setx(self.mouth.xcor() - 580)
        elif self.mouth.xcor() < -290:
            self.mouth.setx(self.mouth.xcor() + 580)
        elif self.mouth.ycor() > 290:
            self.mouth.sety(self.mouth.ycor() - 580)
        elif self.mouth.ycor() < -290:
            self.mouth.sety(self.mouth.ycor() + 580)

# Die Schlange ist tod, wenn sie gegen ihre Körperteile stößt.

    def basiliskIsDead(self):
        if self.deadFromPoison:
            return True

        for item in self.body:
            basiliskEatsHerself = item.distance(self.mouth) < 20
            if basiliskEatsHerself:
                return True

    def basiliskEats(self, obj):
        return self.mouth.distance(obj) < 20

    def basiliskGoHome(self):
        self.mouth.home()
        self.mouth.direction = "stop"

    def basiliskDeleteBody(self):
        for item in self.body:
            item.goto(1000, 1000)
        self.body.clear()

    def hideMouth(self):
        self.mouth.hideturtle()

    def isVisible(self):
        return self.mouth.isvisible()

    def showMouth(self):
        self.mouth.showturtle()
Beispiel #2
0
class Vehicle:
    def __init__(self, turtle_window, id_number):

        # comment here
        self.turtle_window = turtle_window
        self.id_number = id_number

        # comment here
        self.speed_params = [20, 0.2, 6]
        self.turn_parameters = [20]

        # comment here
        self.turtle_object = RawTurtle(self.turtle_window.wn)
        self.turtle_object.hideturtle()
        self.turtle_object.shape('turtle')
        self.turtle_object.turtlesize(1)
        self.turtle_object.penup()

        # comment here
        self.likes_food_dict = {'Sugar': random.choice([True, False])}

        # comment here
        if self.likes_food_dict['Sugar']:
            self.turtle_object.color("red", (1, 0.85, 0.85))
        else:
            self.turtle_object.color("blue", (0.85, 0.85, 1))

        # comment here
        self.place()
        self.turtle_object.showturtle()

    def place(self):
        # comment here
        self.turtle_object.goto(random.randint(-MAX_LOCATION, MAX_LOCATION),
                                random.randint(-MAX_LOCATION, MAX_LOCATION))
        self.turtle_object.right(random.randint(0, 360))

    def move(self):

        # comment here
        cumulative_speed = 0
        cumulative_turn_amount = 0

        # comment here
        for food_source in self.turtle_window.food_source_list:

            # comment here
            likes_food = self.likes_food_dict[food_source.name]

            # comment here
            input_distance = self.turtle_object.distance(
                food_source.turtle_object.pos())

            # comment here
            input_angle = self.turtle_object.heading(
            ) - self.turtle_object.towards(food_source.turtle_object.pos())

            # comment here
            sin_angle = math.sin(math.radians(input_angle))

            # comment here
            left_sensor_distance = input_distance - sin_angle
            right_sensor_distance = input_distance + sin_angle

            # comment here
            left_speed, right_speed, combined_speed = self.compute_speed(
                left_sensor_distance, right_sensor_distance, likes_food)

            # comment here
            turn_amount = self.turn_parameters[0] * (right_speed - left_speed)

            # comment here
            cumulative_speed += combined_speed
            cumulative_turn_amount += turn_amount

        # comment here
        if isinstance(cumulative_turn_amount, complex):
            cumulative_turn_amount = 0

        # comment here
        if cumulative_speed < 0:
            cumulative_speed = 0

        # comment here
        self.turtle_object.right(cumulative_turn_amount)
        self.turtle_object.forward(cumulative_speed)

        # comment here
        self.check_border_collision()

    def check_border_collision(self):
        '''
        comment here. Make one big comment for the function, but it must be more specific and detailed then
        just repeating the function name...
        '''

        if self.turtle_object.xcor() > MAX_LOCATION:
            self.turtle_object.goto(MAX_LOCATION, self.turtle_object.ycor())
        if self.turtle_object.xcor() < -MAX_LOCATION:
            self.turtle_object.goto(-MAX_LOCATION, self.turtle_object.ycor())

        if self.turtle_object.ycor() > MAX_LOCATION:
            self.turtle_object.goto(self.turtle_object.xcor(), MAX_LOCATION)
        if self.turtle_object.ycor() < -MAX_LOCATION:
            self.turtle_object.goto(self.turtle_object.xcor(), -MAX_LOCATION)

        if self.turtle_object.ycor() <= -MAX_LOCATION:
            if 0 <= self.turtle_object.heading() <= 180:
                turn_angle = 180 - self.turtle_object.heading()
                self.turtle_object.setheading(turn_angle)
            else:
                turn_angle = abs(360 - self.turtle_object.heading())
                self.turtle_object.setheading(turn_angle)

        if self.turtle_object.ycor() >= MAX_LOCATION:
            if 0 <= self.turtle_object.heading() <= 180:
                turn_angle = 360 - self.turtle_object.heading()
                self.turtle_object.setheading(turn_angle)
            else:
                turn_angle = 360 - (self.turtle_object.heading() - 180)
                self.turtle_object.setheading(turn_angle)

        if self.turtle_object.xcor() <= -MAX_LOCATION:
            if 0 <= self.turtle_object.heading() <= 90:
                turn_angle = 360 - self.turtle_object.heading()
                self.turtle_object.setheading(turn_angle)
            if 270 < self.turtle_object.heading() <= 360:
                turn_angle = 360 - self.turtle_object.heading()
                self.turtle_object.setheading(turn_angle)
            if 90 < self.turtle_object.heading() < 180:
                turn_angle = self.turtle_object.heading() - 90
                self.turtle_object.setheading(turn_angle)
            if 180 <= self.turtle_object.heading() <= 360:
                turn_angle = self.turtle_object.heading() + 90
                self.turtle_object.setheading(turn_angle)

        if self.turtle_object.xcor() >= MAX_LOCATION:
            if 0 <= self.turtle_object.heading() <= 180:
                turn_angle = self.turtle_object.heading() + 90
                self.turtle_object.setheading(turn_angle)
            else:
                turn_angle = self.turtle_object.heading() - 90
                self.turtle_object.setheading(turn_angle)

    ###############################################################################################################
    def compute_speed(self, left_distance, right_distance, likes_food):
        '''
        comment here. Make one big comment for the function, but it must be more specific and detailed then
        just repeating the function name... explain this in Braitenberg's terms
        '''

        if likes_food:
            left_speed = (
                self.speed_params[0] /
                (right_distance**self.speed_params[1])) - self.speed_params[2]
            right_speed = (
                self.speed_params[0] /
                (left_distance**self.speed_params[1])) - self.speed_params[2]
        else:
            left_speed = (
                self.speed_params[0] /
                (left_distance**self.speed_params[1])) - self.speed_params[2]
            right_speed = (
                self.speed_params[0] /
                (right_distance**self.speed_params[1])) - self.speed_params[2]
        combined_speed = (left_speed + right_speed) / 2
        return left_speed, right_speed, combined_speed
Beispiel #3
0
class Vehicle:
    ###############################################################################################################
    def __init__(self, turtle_window, id_number):
        self.speed_params = [20, 0.2, 6]
        self.turn_parameters = [20]
        self.turtle_window = turtle_window
        self.max_location = self.turtle_window.screen_size / 2 - 10
        self.vehicle = RawTurtle(self.turtle_window.wn)
        self.vehicle.hideturtle()
        self.id_number = id_number
        self.type = random.choice(["crossed", "direct"])
        self.vehicle.shape('turtle')
        self.vehicle.turtlesize(1)
        self.vehicle.penup()
        if self.type == 'crossed':
            self.vehicle.color("red", (1, 0.85, 0.85))
        else:
            self.vehicle.color("blue", (0.85, 0.85, 1))

        self.place()
        self.vehicle.showturtle()

    def place(self):

        self.vehicle.goto(
            random.randint(-self.max_location, self.max_location),
            random.randint(-self.max_location, self.max_location))
        self.vehicle.right(random.randint(0, 360))

    ###############################################################################################################
    def move(self):
        cumulative_speed = 0
        cumulative_turn_amount = 0
        for heat_source in self.turtle_window.heat_source_list:
            input_distance = self.vehicle.distance(
                heat_source.heat_source.pos())
            input_angle = self.vehicle.heading() - self.vehicle.towards(
                heat_source.heat_source.pos())
            sin_angle = math.sin(math.radians(input_angle))
            left_sensor_distance = input_distance - sin_angle
            right_sensor_distance = input_distance + sin_angle
            left_speed, right_speed, combined_speed = self.compute_speed(
                left_sensor_distance, right_sensor_distance)
            turn_amount = self.turn_parameters[0] * (right_speed - left_speed)
            cumulative_speed += combined_speed
            cumulative_turn_amount += turn_amount

        if isinstance(cumulative_turn_amount, complex):
            cumulative_turn_amount = 0

        if cumulative_speed < 0:
            cumulative_speed = 0

        self.vehicle.right(cumulative_turn_amount)
        self.vehicle.forward(cumulative_speed)
        self.check_border_collision()

    def check_border_collision(self):
        if self.vehicle.xcor() > self.max_location:
            self.vehicle.goto(self.max_location, self.vehicle.ycor())
        if self.vehicle.xcor() < -self.max_location:
            self.vehicle.goto(-self.max_location, self.vehicle.ycor())

        if self.vehicle.ycor() > self.max_location:
            self.vehicle.goto(self.vehicle.xcor(), self.max_location)
        if self.vehicle.ycor() < -self.max_location:
            self.vehicle.goto(self.vehicle.xcor(), -self.max_location)

        if self.vehicle.ycor() <= -self.max_location:
            if 0 <= self.vehicle.heading() <= 180:
                turn_angle = 180 - self.vehicle.heading()
                self.vehicle.setheading(turn_angle)
            else:
                turn_angle = abs(360 - self.vehicle.heading())
                self.vehicle.setheading(turn_angle)

        if self.vehicle.ycor() >= self.max_location:
            if 0 <= self.vehicle.heading() <= 180:
                turn_angle = 360 - self.vehicle.heading()
                self.vehicle.setheading(turn_angle)
            else:
                turn_angle = 360 - (self.vehicle.heading() - 180)
                self.vehicle.setheading(turn_angle)

        if self.vehicle.xcor() <= -self.max_location:
            if 0 <= self.vehicle.heading() <= 90:
                turn_angle = 360 - self.vehicle.heading()
                self.vehicle.setheading(turn_angle)
            if 270 < self.vehicle.heading() <= 360:
                turn_angle = 360 - self.vehicle.heading()
                self.vehicle.setheading(turn_angle)
            if 90 < self.vehicle.heading() < 180:
                turn_angle = self.vehicle.heading() - 90
                self.vehicle.setheading(turn_angle)
            if 180 <= self.vehicle.heading() <= 360:
                turn_angle = self.vehicle.heading() + 90
                self.vehicle.setheading(turn_angle)

        if self.vehicle.xcor() >= self.max_location:
            if 0 <= self.vehicle.heading() <= 180:
                turn_angle = self.vehicle.heading() + 90
                self.vehicle.setheading(turn_angle)
            else:
                turn_angle = self.vehicle.heading() - 90
                self.vehicle.setheading(turn_angle)

    ###############################################################################################################
    def compute_speed(self, left_distance, right_distance):
        if self.type == 'crossed':
            left_speed = (
                self.speed_params[0] /
                (right_distance**self.speed_params[1])) - self.speed_params[2]
            right_speed = (
                self.speed_params[0] /
                (left_distance**self.speed_params[1])) - self.speed_params[2]
        else:
            left_speed = (
                self.speed_params[0] /
                (left_distance**self.speed_params[1])) - self.speed_params[2]
            right_speed = (
                self.speed_params[0] /
                (right_distance**self.speed_params[1])) - self.speed_params[2]
        combined_speed = (left_speed + right_speed) / 2
        return left_speed, right_speed, combined_speed