A module to draw Karel in graphics projects.
For use by students who want to make art or games with Karel as a character.

root a70ee658ba +demos 3 주 전
demos a70ee658ba +demos 3 주 전
README.md bd0ef29ca0 Updated example 3 주 전
karel_asset.py 8fec4ffdfa random colour +ansi-print 3 주 전

README.md

karel_asset

A package to draw Karel in graphics projects. For use by students who want to make art or games with Karel as a character.

Projects by other students using karel_asset:

Getting started

  1. In CIP6 click on "Code / You own", or on CIP self-guided clock on "Code / You own"
  2. Create a new or open an existing graphics project
  3. Go to the Project Files view by clicking the "Files" button in the ribbon on the left
  4. Add the karel_asset.py file by:
    • Either clicking the CIP icon of a file with a plus sign (+) in it to add a new file and paste the content of karel_asset.py (The icon willonly appear after letting the mouse hover over the row representing the root project directory bearing the project name)
    • Or by downloading/cloning this project and uploading the karel_asset.py file with the upload button on the CIP IDE

Using karel_asset.py when it is added to a CIP project

In your main.py or any other project file on CIP, add: from karel_asset import *

Drawing Karel

To simply draw a Karel in the top left corner use: draw_karel(canvas) But surely this will not suffice for any project so there are optional arguments to pass:

  • centre_x
  • centre_y
  • size
  • orientation
  • colour
  • background
  • transparent

Coordinates

To draw a Karel in the middle of X / Y coordinates 100 / 100, use the optional arguments centre_x, centre_y:

draw_karel(canvas, 100, 100)

Size

To draw a Karel of size 200, use the optional argument size:

draw_karel(canvas, size=200)

Orientation

To draw a Karel facing north, use the optional argument orientation:

draw_karel(canvas, orientation="north")

Orientation options:

  • "east"
  • "east-flipped"
  • "north"
  • "north-flipped"
  • "west"
  • "west-flipped"
  • "south"
  • "south-flipped" Flipped makes Karel appear upside down.

Colour

To draw a Karel in any colour use the optional argument colour:

draw_karel(canvas, colour="red", background="#FF8833")

Colour names and hex codes are accepted, just like normal for any CIP graphical object. In addition it is possible to make use of random colours:

draw_karel(canvas, colour="random", background="random")

Transparancy

To draw a transparant Karel, which only has outlines and no fill/body, use the optional argument transparent:

draw_karel(canvas, transparent=True)

Examples

Draw a Karel at 85x and 35y upside down facing east with a red outline and blue filling:

karel_east_mirrored = draw_karel(canvas, 85, 35, 100, "east-flipped", "red", "blue")

Draw a Karel at 135X and 35y, facing west with a red background:

karel_west = draw_karel(canvas, centre_x=135, centre_y=35, orientation="west", background="red")

Changing colour

To change the colour of any asset, use recolour_asset(karel, colour, background). For example changing a red/blue Karel to a green/yellow Karel:

my_karel = draw_karel(canvas, colour="red", background="blue")
my_karel = recolour_asset(my_karel, colour="green", background="yellow)

Random colours are supported:

my_karel = draw_karel(canvas, colour="random", background="random")
my_karel = recolour_asset(my_karel, colour="random", background="random)

Changing position relative to current position

To change the position of an asset on the canvas relative to its current position, use relative_move_asset(karel, x, y). Where x is the amout of pixels shifted horizontally, and y is the amount of pixels shifted vertically. To move a Karel up 10 pixes:

my_karel = draw_karel(canvas, colour="red", background="blue")
my_karel = relative_move_asset(my_karel, y=-10)

To move a Karel down 10 and left 10 pixels:

my_karel = draw_karel(canvas, colour="red", background="blue")
my_karel = relative_move_asset(my_karel, 10, -10)

Changing position relative to its orientation

To change the position of an asset on the canvas relative to its current orientation, use orientation_move_asset(asset, direction, amount) Directional options:

  • forward
  • front
  • left
  • right
  • backward
  • back To move an asset to its right by 10 pixels:

    my_karel = draw_karel(canvas, colour="red", background="blue")
    my_karel = orientation_move_asset(my_karel, "right", 10)
    

Changing absolute position on the canvas

To change the position of an asset on the canvas with absolute coordinates, use absolute_move_asset(asset, x, y). Where x is the absolute width centre and y is the absolute height centre. To move an asset to 100 by 100:

my_karel = draw_karel(canvas, colour="red", background="blue")
my_karel = absolute_move_asset(my_karel, 100, 100)

Rotating

To turn an asset, use rotate_asset(asset, orientation). Orientation options:

  • "right"
  • "left"
  • "east"
  • "east-flipped"
  • "north"
  • "north-flipped"
  • "west"
  • "west-flipped"
  • "south"
  • "south-flipped" To turn a Karel to the right:

    my_karel = draw_karel(canvas, colour="red", background="blue")
    my_karel = rotate_asset(my_karel, "right")
    

    To turn a Akrel toward the south:

    my_karel = draw_karel(canvas, colour="red", background="blue")
    my_karel = rotate_asset(my_karel, "south")
    

Relative resizing

To resize an asset relative to its current size, use relative_resize_asset(asset, size_difference). To schrink an asset by 10 pixels:

my_karel = draw_karel(canvas, size=100)
my_kaerl = relative_resize_asset(my_karel, -10)

Absoulute resizing

To resize an asset to a new absolute size, use absolute_resize_asset(asset, size). To resize an asset to 50 pixels:

my_karel = draw_karel(canvas, size=100)
my_karel =  absolute_resize_asset(asset, 50).

Erasing an asset

To remove a Karel from the canvas, use erase_asset(asset):

my_karel = draw_karel(canvas, colour="red", background="blue")
my_karel = erase_asset(my_karel)

Generating a random karel

To draw a Karel with a rondom properties, use generate_random_karel(canvas, centre_x="random", centre_y="random", size="random"):

my_karel = generate_random_karel(canvas)

Optionally the coordiantes and/or size can be specified.

Generating a Karel outside the canvas

To draw a Karel outside of the canvas, to perhaps be later slid into view, use generate_outofbounds_random_karel(canvas, side) Side options:

  • top
  • left
  • bottom
  • right To generate a Karel left of teh canvas:

    my_karel = generate_outofbounds_random_karel(canvas, "left")
    

Drawing a beeper

Works the same as drawing a Karel, available arguments:

  • canvas
  • centre_x
  • centre_y
  • size
  • orientation
  • colour
  • background
  • transparent Draw a beeper:

    my_beeper = draw_beeper(canvas)
    

Generating a random beeper

Works the same as generating a random Karel, use generate_random_beeper(canvas, centre_x="random", centre_y="random", size="random"):

generate_random_beeper(canvas)

Optionally the coordiantes and/or size can be specified.

Generating a random beeper outside the canvas

To generate a beeper outside of the canvas, to slide it into it later, use generate_outofbounds_random_beeper(canvas, side) Side options:

  • top
  • left
  • bottom
  • right To draw a beeper under(south of) the canvas:

    generate_outofbounds_random_beeper(canvas, "bottom")
    

Generating a random asset

To generate a random asset, use generate_random_asset(canvas, karel_prevelance:int=4, centre_x="random", centre_y="random", size="random"):

my_asset = generate_random_asset(canvas)

Optionally the coordiantes and/or size can be specified.

By default there will be approximately 1 in four assets will be a beeper the remaining three will be Karels, this can be changed with the karel_prevelance argument. Draw approximately 50% beepers and 50% Karels:

my_asset = generate_random_asset(canvas, karel_prevelance:int=2)

In case the preference is to only have random orientations and colours, optionally the size and or centre_x & centre_y coordiantes can be specified. Generate a random asset in the middle of the canvas:

my_asset = generate_random_asset(canvas, centre_x=int(canvas.width / 2), centre_y=int(canvas.height / 2))

Generating a random asset outside the canvas

To generate a random asset outside of the canvas, use generate_outofbounds_random_asset(canvas, side, karel_prevelance:int=4) Side options:

  • top
  • left
  • bottom
  • right Generate an asset above the canvas:

    my_asset = generate_outofbounds_random_asset(canvas, "top")
    

Generating a random colour

To get a random colour value to use in your own graphical shape, use generate_random_colour().

To generate a random HEX colour:

my_colour = generate_random_colour()

To generate a random RGB colour:

my_colour = generate_random_colour("rgb")

To generate a random ANSI colour as print statement:

print("White or default")
generate_random_colour("print-ansi")
print("Some colour")
colour = generate_random_colour("ansi")
print(f"{colour}Some other colour")

print(f"{generate_random_colour("ansi")}Some {generate_random_colour("ansi")}other {generate_random_colour("ansi")}colour {generate_random_colour("ansi")}for {generate_random_colour("ansi")}each {generate_random_colour("ansi")}word{generate_random_colour("ansi")}.")

Generating a random background

To draw a random coloured square filling the whole canvas, use generate_random_background(canvas):

background = generate_random_background(canvas)

Example

from graphics import Canvas
from karel_asset import *   # https://git.h0v1n8.nl/Stanford/karel_asset
from time import sleep

# Constants    
CANVAS_WIDTH = 200
CANVAS_HEIGHT = 200

def main():
    # Demo loop
    while True:    # Keep repeating the following code forever
        # Into text in random colour
        generate_random_colour("print-ansi")
        print("Welcome")
        print(generate_random_colour("ansi") + "To", generate_random_colour("ansi") + "the", generate_random_colour("ansi") + "demo" + generate_random_colour("ansi") + "!")
        

        canvas = Canvas(CANVAS_WIDTH, CANVAS_HEIGHT)    # Create the canvas
        
        # Create a random background
        background = generate_random_background(canvas)
        
        
        # Draw four Karels
        karel_east = draw_karel(canvas, 35, 35)
        karel_east_mirrored = draw_karel(canvas, 85, 35, orientation="east-flipped", colour="red")
        karel_west = draw_karel(canvas, 135, 35, orientation="west", background="red")
        karel_west_mirrored = draw_karel(canvas, 185, 35, orientation="west-flipped", colour="red", background="red")
        
        # Draw a beeper
        my_beeper = draw_beeper(canvas, 100, 100)
        
        # Wait two seconds
        sleep(2)
        
        # Change the colour of one Karel to random colours
        karel_east = recolour_asset(karel_east, "random", "random")

        # Wait half a second
        sleep(.5)
        
        # Move one Karel relative to its position
        karel_east_mirrored = relative_move_asset(karel_east_mirrored, 10, 120)

        # Wait half a second
        sleep(.5)
        
        # Move another Karel to new coordinates
        karel_west_mirrored = absolute_move_asset(karel_west_mirrored, 150, 150)

        # Wait half a second
        sleep(.5)
        
        # Move another Karel forward
        karel_west = orientation_move_asset(karel_west, "forward", 10)

        # Wait half a second
        sleep(.5)
        
        # Rotate another karel eight times, and wait one second after each time
        for i in range(8):
            karel_east_mirrored = rotate_asset(karel_east_mirrored, "right")
            sleep(.5)
        
        # Change the colour of a Karel to a random colour
        karel_east = recolour_asset(karel_east, "random")

        # Wait half a second
        sleep(.5)
        
        # Generate a random Karel
        random_karel = generate_random_karel(canvas)
        
        # Wait half a second
        sleep(.5)
        
        # Shrink the random Karel
        for i in range(random_karel[1][3]):
            random_karel = relative_resize_asset(random_karel, -1)
            sleep(0.01)
        
        # Wait half a second
        sleep(.5)
        
        # Set the size of the random Karel to 25
        random_karel = absolute_resize_asset(random_karel, 25)

        # Wait half a second
        sleep(.5)
        
        # Generate a random asset
        random_asset = generate_random_asset(canvas)
        
        # Draw a random asset outside the canvas and slide it into the canvas
        random_OOB_asset = generate_outofbounds_random_asset(canvas, "left")
        for i in range(random_OOB_asset[1][3] + 5):
            random_OOB_asset = relative_move_asset(random_OOB_asset, x=1)
            sleep(0.01)
        
        # Wait half a second
        sleep(.5)
        
        # Erase four Karels
        erase_asset(karel_east)
        erase_asset(karel_east_mirrored)
        erase_asset(karel_west)
        erase_asset(karel_west_mirrored)

        # Wait half a second
        sleep(.5)

        # Clear the canvas
        for obj_id in list(canvas.shapes.keys()):
            canvas.delete(obj_id)


if __name__ == '__main__':
    main()