Advertisement
acclivity

pyBrickPyramid-tkinter

Nov 3rd, 2021 (edited)
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. # Draw a pyramid of bricks using tkinter Canvas
  2.  
  3. from tkinter import *
  4.  
  5. def draw_brick(x, y, bw, bh):
  6.     # x, y are the co-ordinates of the top left corner of a brick
  7.     # bw, bh are the width and height of a brick in pixels
  8.     canvas.create_rectangle(
  9.         x, y, x + bw, y + bh,
  10.         outline="#fb0")             # Set the colour of the brick outline
  11.  
  12. ws = Tk()
  13. ws.title('Brick Pyramid')
  14. ws.geometry('600x600')
  15. ws.config(bg='#300')
  16. w = 500
  17. h = 500
  18. brickwidth = 30
  19. brickheight = 15
  20.  
  21. canvas = Canvas(
  22.     ws,
  23.     height=h,
  24.     width=w,
  25.     bg="#fff"
  26. )
  27.  
  28. canvas.pack()
  29.  
  30. ybase = 450             # Start near the bottom of the canvas
  31. xbase = 40              # Start near the left edge
  32. for num in range(14, 0, -1):       # Bottom row has 14 bricks. Decrease by one brick each layer up
  33.     xx = xbase
  34.     for i in range(num):
  35.         draw_brick(xx, ybase, brickwidth, brickheight)
  36.         xx += brickwidth            # Move right by one brick width
  37.  
  38.     # Move up one level
  39.     ybase -= brickheight            # y coordinate decreases by brick height
  40.     xbase += (brickwidth // 2)      # x start point shifts right by half a brick
  41.  
  42. ws.mainloop()
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement