A package to draw Karel in graphics projects.
For use by students who want to make art or games with Karel as a character.
|
|
17 stundas atpakaļ | |
|---|---|---|
| README.md | 17 stundas atpakaļ | |
| karel_asset.py | 17 stundas atpakaļ |
A package to draw Karel in graphics projects. For use by students who want to make art or games with Karel as a character.
In your main.py or any other project file on CIP, add: from karel_asset import *
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:
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)
To draw a Karel of size 200, use the optional argument size:
draw_karel(canvas, size=200)
To draw a Karel facing north, use the optional argument orientation:
draw_karel(canvas, orientation="north")
Orientation options:
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.
To draw a transparant Karel, which only has outlines and no fill, use the optional argument transparent:
draw_karel(canvas, transparent=True)
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")
To change the colour of any created Karel, use recolour_karel(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_karel(my_karel, colour="green", background="yellow)
To change the position of a Karel on the canvas, use move_karel(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 = move_karel(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 = move_karel(my_karel, 10, -10)
To turn karel, use rotate_karel(karel, orientation). Orientation options:
"south-flipped" To turn a Karel to the right:
my_karel = draw_karel(canvas, colour="red", background="blue")
my_karel = rotate_karel(my_karel, "right")
To turn a Akrel toward the south:
my_karel = draw_karel(canvas, colour="red", background="blue")
my_karel = rotate_karel(my_karel, "south")
To remove a Karel from the canvas, use erase_karel(karel):
my_karel = draw_karel(canvas, colour="red", background="blue")
my_karel = erase_karel(my_karel)
To get a random colour value to use in your own graphical shape, use generate_random_colour()
my_colour = generate_random_colour()
from graphics import Canvas
from karel_asset import *
from time import sleep
# Constants
CANVAS_WIDTH = 120
CANVAS_HEIGHT = 70
def main():
canvas = Canvas(CANVAS_WIDTH, CANVAS_HEIGHT)
# Create a background
rect = canvas.create_rectangle(
1,
1,
CANVAS_WIDTH,
CANVAS_HEIGHT,
generate_random_colour(),
generate_random_colour()
)
# Demo loop
while True: # Keep repeating the following code forever
# 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")
# Wait five seconds
sleep(5)
# Change the colour of one Karel to random colours
karel_east = recolour_karel(karel_east, "random", "random")
# Move one Karel
karel_east_mirrored = move_karel(karel_east_mirrored, 10, 10)
# Wait five seconds
sleep(5)
# Erase four Karels
erase_karel(karel_east)
erase_karel(karel_east_mirrored)
erase_karel(karel_west)
erase_karel(karel_west_mirrored)
if __name__ == '__main__':
main()