Hazırlayan: Chris Piech
Çevirenler: Serhat Arslan, Ceren Kocaoğullar
Ders Notları: Grafikler Referansı
Ekranda rastgele konumlarda 1000 adet daire oluşturacağız. Sol alt köşeye daha yakın olan daireler mavi olurken sağ üste yakın olanlar yeşil olacak. Bu örnek döndürülen (return) değerlere dair bir örnek.
Her daire 20 piksele 20 piksel.
Her dairenin tam olarak tuval üzerine çizildiğine dikkat edin.
time.sleep(0.002)
fonksiyon çağrısı daireler çizerken her seferinde 2 milisaniye bekliyor.
"""File: half_green.py-------------------Draws circles randomly, where circles in the top-right half are greenand circles in the bottom-left half are blue."""from graphics import Canvasimport randomimport time# The size of the canvasCANVAS_WIDTH = 500CANVAS_HEIGHT = 500# The diameter of each circleCIRCLE_SIZE = 20# The number of circles to drawNUM_CIRCLES = 1000def main():canvas = Canvas(CANVAS_WIDTH, CANVAS_HEIGHT)canvas.set_canvas_title("Half Green")# Draw 1000for i in range(NUM_CIRCLES):x = random.randint(0, canvas.get_canvas_width() - CIRCLE_SIZE)y = random.randint(0, canvas.get_canvas_height() - CIRCLE_SIZE)circle = canvas.create_oval(x, y, x + CIRCLE_SIZE, y + CIRCLE_SIZE)# Set both the fill and outline color to be either green or bluecanvas.set_color(circle, get_color(x, y))canvas.update()time.sleep(0.002)canvas.mainloop()def get_color(x, y):"""Returns the name of the color the circle with top-left coordinate (x, y)should have. Since (0, 0) is the top-left corner of the canvas, x increasesas we go to the right, and y increases as we go down. So the line y = x isthe diagonal line from top-left to bottom right. If y < x, the dot is abovethis line (in the top-right corner), so it's green. If y >= x, the dot is ator below this line (in the bottom-left corner, so it's blue."""if x > y:return "green"return "blue"if __name__ == "__main__":main()