Anda di halaman 1dari 26

PEMROGRAMAN GRAFIS

TURTLE

• Pada pertemuan sebelumnya, kita telah bekenalan dengan turtle


• Turtle adalah sebuah pena yang awalnya berada di tengah layar, pada koodinat
(0,0).
• Ketika digerakkan, turtle meninggalkan jejak di belakangnya
• Untuk membuat gerakan pertama, kita menggunakan metode forward()
• Contoh: t.forward(100)
Sintaks/Fungsi Kegunaan
forward(n) atau fd(n) Untuk menggerakkan tanda panah ke depan sejauh n pixel
backward(n) atau bk(n) Untuk menggerakkan tanda panah ke belakang sejauh n pixel
left(n) atau lt(n) Untuk mengubah arah tanda panah ke kiri sejauh n derajat
right(n) atau rt(n) Untuk mengubah arah panah ke kanan sejauh n derajat
circle() Untuk embuat lingkaran dengan diameter n pixel
color() Untuk mengganti warna kanvas
begin_fill() Untuk memulai pemberian warna terhadapat garis yang dibuat. Fungsi ini merupakan
fungsi yang disadingkan dengan fungsi color()
end_fill() Untuk mengakhiri pemberian warna terhadap garis yang dibuat
reset() Untuk menghapus gambar turtle dan mengembalikan turtle pada posisi awal
clear() Untuk menghapus gambar turtle, tapi membiarkan turtle pada posisi terakhir.
Memiliki fungsi yang sama dengan fungsu begin_fill() dan end_fill(). Fungsi ini
fill()
mengambil argument berupa true atau false
Sintaks/Fungsi Kegunaan
undo() Membatalkan pergerakan sebelumnya
goto(x,y) Pindah ke koordinat x dan y, jika status pendown maka meninggalkan jejak garis
setx(x) Set koordinat awal di x (default 0)
sety(y) Set koordinat awal di y (default 0)
setheading(a) Set arah turtle dengan sudut a derajat, 0 derajat ke arah timur, 90 derajat arah utara,
dst
Circle(radius) Menggambar lingkaran dengan jari-jari r
Circle(radius, angle) Mengambar lingkaran hanya sebatas sudut (angle) yang diberikan
Dot(diameter, color) Menggambar titik dengan diameter tertentu dan warna tertentu
Penup() mengangkat pena dari kanvas, tidak meninggalkan jejak ketika berpindah
Pendown() Meletakkan pena di kanvas, meninggalkan jejak ketika berpindah
Pensize(x) Set ketebalan pena sebesar x
Pencolor(x) Set warna pena menjadi x
Sintaks/Fungsi Kegunaan
turtle.color(r,g,b) Mewarnai turtle dengan 3 parameter angka red green blue
turtle.color(s) Mewarnai turtle dengan String warna tertentu (red, magenta, blue, yellow,dll)
turtle.fillcolor(r,g,b) Mengisi warna objek dengan 3 parameter angka red green blue
turtle.fillcolor(s) Mewarnai objek dengan String warna tertentu (red, magenta, blue, yellow,dll)
turtle.begin_fill(), Untuk memberi warna gambar, gunakan turtle.begin_fill() sebelum Anda mulai
turtle.end_fill() menggambar gambar. Gambarlah sebuah objek. Kemudian jalankan turtle.end_fill().
Gambar yang digambar di antara dua perintah isian akan diisi dengan pengaturan
warna saat ini.
turtle.hideturtle(), Mengatur status untuk menyembunyikan / menampilkan turtle.
turtle.showturtle()
turtle.xcor(), Mengembalikan nilai koordinat x, dan mengembalikan nilai koordinat y
turtle.ycor()
turtle.bye() Menutup canvas
turtle.speed(x) Mengatur kecepatan gerak dari turtle sebesar x
Turning the Turtle

• The turtle's initial heading is 0 degrees (east)

• Use the turtle.right(angle) statement to turn the turtle right by angle degrees.

• Use the turtle.left(angle) statement to turn the turtle left by angle degrees.
TURNING THE TURTLE

import turtle
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
TURNING THE TURTLE

import turtle
turtle.forward(100)
turtle.right(45)
turtle.forward(100)
Setting the Turtle's Heading

• Use the turtle.setheading(angle) statement to set the turtle's heading to a


specific angle.

import turtle
turtle.forward(50)
turtle.setheading(90)
turtle.forward(100)
turtle.setheading(180)
turtle.forward(50)
turtle.setheading(270)
turtle.forward(100)
Setting the Pen Up or Down

• When the turtle's pen is down, the turtle draws a line as it moves. By default, the
pen is down.

• When the turtle's pen is up, the turtle does not draw as it moves.

• Use the turtle.penup() statement to raise the pen.

• Use the turtle.pendown() statement to lower the pen.


SETTING THE PEN UP OR DOWN

import turtle
turtle.forward(50)
turtle.penup()
turtle.forward(25)
turtle.pendown()
turtle.forward(50)
turtle.penup()
turtle.forward(25)
turtle.pendown()
turtle.forward(50)
Drawing Circles

• Use the turtle.circle(radius) statement to draw a circle with a


specified radius.

import turtle
turtle.circle(100)
Drawing Dots

• Use the turtle.dot() statement to draw a simple dot at the turtle's


current location.
import turtle
turtle.dot()
turtle.forward(50)
turtle.dot()
turtle.forward(50)
turtle.dot()
turtle.forward(50)
Changing the Pen Size and Drawing Color

• Use the turtle.pensize(width) statement to change the width of the


turtle's pen, in pixels.

• Use the turtle.pencolor(color) statement to change the turtle's drawing


color.
• See Appendix D in your textbook for a complete list of colors.

import turtle
turtle.pensize(5)
turtle.pencolor('red')
turtle.circle(100)
Working with the Turtle's Window

• Use the turtle.bgcolor(color) statement to set the window's


background color.
• See Appendix D in your textbook for a complete list of colors.

• Use the turtle.setup(width, height) statement to set the size of the


turtle's window, in pixels.
• The width and height arguments are the width and height, in pixels.
• For example, the following interactive session creates a graphics window that is 640 pixels
wide and 480 pixels high:

import turtle
turtle.setup(640, 480)
Resetting the Turtle's Window
• The turtle.reset() statement:
• Erases all drawings that currently appear in the graphics window.
• Resets the drawing color to black.
• Resets the turtle to its original position in the center of the screen.
• Does not reset the graphics window’s background color.
• The turtle.clear() statement:
• Erases all drawings that currently appear in the graphics window.
• Does not change the turtle's position.
• Does not change the drawing color.
• Does not change the graphics window’s background color.
• The turtle.clearscreen() statement:
• Erases all drawings that currently appear in the graphics window.
• Resets the drawing color to black.
• Resets the turtle to its original position in the center of the screen.
• Resets the graphics window’s background color to white.
Working with Coordinates

• The turtle uses Cartesian Coordinates


Moving the Turtle to a Specific Location

• Use the turtle.goto(x, y) statement to move the


turtle to a specific location.

import turtle
turtle.goto(0, 100)
turtle.goto(−100, 0)
turtle.goto(0, 0)

• The turtle.pos() statement displays the turtle's current X,Y coordinates.


• The turtle.xcor() statement displays the turtle's current X coordinate and the
turtle.ycor() statement displays the turtle's current Y coordinate.
SHAPE OF TURTLE
import turtle
turtle.bgcolor("magenta")

turtle.shape("circle")
turtle.fillcolor("yellow")
turtle.shapesize(20, 10, 5) # (length, width, outline)

turtle.exitonclick()

import turtle import turtle


turtle.shape("turtle") turtle.shape("arrow")
turtle.fillcolor("green") turtle.fillcolor("grey")
turtle.shapesize(15, 15, 10) turtle.shapesize(15, 15, 10)
turtle.exitonclick() turtle.exitonclick()
FILLING SHAPES

import turtle
turtle.hideturtle()
turtle.fillcolor('red')
turtle.begin_fill()
turtle.circle(100)
turtle.end_fill()
import turtle
# for default shape

CREATE AN OBJECT turtle.forward(100)

# for circle shape


turtle.right(60)
turtle.forward(100)

# for triangle shape


turtle.right(60)
turtle.forward(100)

# for square shape


turtle.right(60)
turtle.forward(100)

# for arrow shape


turtle.right(60)
turtle.forward(100)

# for turtle shape


turtle.right(60)
turtle.forward(100)

turtle.exitonclick()
import turtle
#buat objek turtle

CREATE AN OBJECT turtle.screensize(400,700)


turtle.pencolor('white')
t = turtle.Turtle()
x=50
y=50
t.penup()
t.goto(x,y)
t.pendown()
t.circle(100)
t.penup()
t.goto(x-35, y+120)
t.pendown()
t.dot(20)
t.penup()
t.goto(x+35, y+120)
t.pendown()
t.dot(20)
t.penup()
t.goto(x-60,y+65)
t.pendown()
t.setheading(-60)
t.circle(70,120)
turtle.exitonclick()
from turtle import * penup()
setup(500,700) goto(0,150)
pensize(10) pendown()
#daun goto(0,0)
pencolor("green") penup()
penup() goto(0,150)
goto(-50,100) pencolor("magenta")
pendown() pendown()
fillcolor("greenyellow") fillcolor("pink")
begin_fill() begin_fill()
goto(50,100) circle(70)
goto(0,50) end_fill()
goto(-50,100) penup()
end_fill() goto(0,190)
pencolor("yellow")
pendown()
fillcolor("yellow")
begin_fill()
CREATE AN OBJECT circle(30)
end_fill()
exitonclick()
import turtle t.penup()
t= turtle.Turtle() t.right(90) t.pencolor("red")
s=turtle.Screen() t.forward(20) t.forward(30)
t.hideturtle() t.right(90) t.penup()
s.bgcolor("skyblue") t.pendown() t.right(90)
t.pencolor("red") t.pendown()
t.fillcolor("red") t.fillcolor("white") t.fillcolor("red")
t.pensize(7) t.begin_fill() t.begin_fill()
t.begin_fill() t.forward(23) t.circle(5)
for i in range(0,4): t.right(90) t.end_fill()
t.forward(50) t.forward(7)
t.left(90) t.right(90) turtle.exitonclick()
t.end_fill() t.forward(23)
t.right(90)
t.pencolor("white") t.forward(7)
t.penup() t.right(90)
t.left(90) t.end_fill()
t.forward(30)
t.right(90) t.penup()
t.forward(12) t.forward(25)
t.pendown() t.right(90)
t.pensize(3) t.forward(35)
CREATE AN OBJECT t.circle(5)
t.penup()
t.pendown()

t.forward(25)
t.pendown()
t.circle(5)
TUGAS
1. Buat coding untuk menampilkan objek berikut!
TUGAS
2. Buat coding untuk menampilkan objek berikut!

Anda mungkin juga menyukai