pisya

python picture using tkinter

Jul 27th, 2020 (edited)
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.39 KB | None | 0 0
  1. from tkinter import *
  2. from random import random
  3.  
  4. class FieldM:
  5.     def __init__(self, master, width=400, height=400):
  6.        
  7.         self.can = Canvas(master, width=width, height=height, bg='black') # create canvas
  8.         self.can.pack() #  pack cancvas
  9.  
  10.         self.restart(width, height) # render blocks on canvas
  11.  
  12.  
  13.     def movement(self):
  14.         # define move location
  15.         if self.sunLocationX >= int(self.can['width']):
  16.             self.sunRoute = 'left'
  17.         elif self.sunLocationX <= -50:
  18.             self.sunRoute = 'right'
  19.  
  20.         # if the sun location is outside the screen then moving his on right side screen
  21.         x = 515 if self.sunRoute == 'right' else -1
  22.         self.sunLocationX += x
  23.  
  24.         self.can.move(self.sun, x, 0)
  25.         for ray in self.sunRays:
  26.             self.can.move(ray, x, 0)
  27.         # set render delay
  28.         self.can.after(50, self.movement)
  29.  
  30.  
  31.     def restart(self, width, height):
  32.         # define all building blocks
  33.         self.createSun(width, height)
  34.         self.movement()
  35.         self.createClouds(width, 80, 160, 40)
  36.         self.createMounts(height)
  37.         self.createLand(width, height, 350)
  38.  
  39.         self.createGrass(width, 2, 352)
  40.  
  41.         self.createHouse(width, height, 220)
  42.         self.createHouse(width, height, 10)
  43.  
  44.         self.createGrass(width, 11, 365)
  45.  
  46.  
  47.     def createBall(self, x, y, r, c):
  48.         # render oval
  49.         self.can.create_oval(x+r, y+r, x-r, y-r, fill=c, outline=c)
  50.  
  51.     def createSun(self, h, w):
  52.         self.sunLocationX = loc = int(self.can['width'])
  53.         self.can.create_rectangle(0-2, 0-2, w+2, h+2, fill="lightblue") # create sky
  54.  
  55.         self.sun = self.can.create_oval(loc+20, 0+20, loc-20, 0-20, fill='yellow', outline='yellow', width=1)
  56.         self.sunRays = []
  57.  
  58.         # define sun rays on canvas
  59.         for i in range(180, 360, 10):
  60.             self.sunRays.append(
  61.                 self.can.create_arc(loc+40, 0+40, loc-40, 0-40, start=i, extent=1, outline='yellow', style='arc', width=25)
  62.             )
  63.  
  64.     def createCloudsCh1(self, x, y, mass):
  65.         # render single cloud
  66.         for i in range(8):
  67.             x += (random()*(mass*2)-mass)
  68.             y += random()*(mass) - (mass/3)
  69.  
  70.             self.createBall(x, y, mass, "white")
  71.  
  72.     def createCloudsCh2(self, width, floor):
  73.         # create cloud location
  74.         for i in range(int(width * (-1)), int(width * 2), int(width / 5)):
  75.             r = random() * 20 - 10  # -10 - 10
  76.             m = random() * 2 - 4  # -4 - 1
  77.             self.createCloudsCh1(i, floor + r, 20 + m)  # x,y,mass
  78.  
  79.     def createClouds(self, width, start, end, step):
  80.         # create clouds
  81.         for i in range(start, end, step):
  82.             self.createCloudsCh2(width, i)
  83.  
  84.  
  85.     def createMountsCh1(self, heightF, x1, x2, height):
  86.         heightF = heightF - 20
  87.         height = height
  88.         x1 = x1
  89.         x2 = x2
  90.         xcen = int((x1+x2)/2)
  91.  
  92.         self.can.create_polygon((x1, heightF), (xcen, height), (x2, heightF), fill='lightgrey', outline='darkgrey', width=2)
  93.  
  94.         x1 = (x1 + xcen)/2
  95.         x2 = (x2 + xcen)/2
  96.         heightF = (heightF + height)/2
  97.  
  98.         self.can.create_polygon((x1, heightF), (xcen, height), (x2, heightF), fill='white')
  99.  
  100.     def createMounts(self, height):
  101.         # create mounts
  102.         count = int(random()*100)
  103.         x1=0
  104.         x2=0
  105.         for i in range(count):
  106.             x1 += int(random()*10)
  107.             x2 += x1 + int(random()*(100-40)-40)
  108.             h = int(random()*(250-150)+150)
  109.             self.createMountsCh1(height, x1, x2, h)
  110.  
  111.     def createLand(self, widthF, heightF, height):
  112.         # render green road background
  113.         self.can.create_rectangle(0, heightF, widthF, height, fill='lightgreen', width=0)
  114.  
  115.     def createWindow(self, width, height, x, y):
  116.         # define window
  117.         x1 = x
  118.         y1 = y
  119.         x2 = x+width
  120.         y2 = y+height
  121.         xc = int((x+x+width)/2)
  122.         yc = int((y+y+width)/2)
  123.         xs = int((x2-x1)/2)
  124.         ys = int((y2-y1)/2)
  125.  
  126.         self.can.create_rectangle(x, y, x+width, y+height, outline='lightsalmon3', fill='lightsalmon2', width=2)
  127.  
  128.         for i in (range(x1, x2, xs)):
  129.             for j in (range(y1, y2, ys)):
  130.                 # create windows background
  131.                 self.can.create_rectangle(i+1, j+1, i+ys-2, j+ys-2, outline='deepskyblue3', fill='deepskyblue2')
  132.  
  133.     def createRoof(self, x, y, height):
  134.         # render roofs
  135.         hood = 15
  136.         width = 150
  137.  
  138.         self.can.create_rectangle(x+width-20, y-5,
  139.                                   x+width-30, y-height-10,
  140.                                   width=0, fill='brown')
  141.  
  142.         self.can.create_polygon(x-hood, y,
  143.                                 x+10, y-height,
  144.                                 x+width-10, y-height,
  145.                                 x+width+hood, y)
  146.  
  147.     def createHouse(self, widthF, heightF, x):
  148.         self.master = self.can
  149.  
  150.         x1 = x
  151.         x2 = x1+150
  152.         y1 = heightF - 40
  153.         y2 = heightF - 100 - 40
  154.  
  155.         self.can.create_rectangle(x1, y1, x2, y2, fill='grey', width=0)
  156.  
  157.         for j in range(y1, y2, -4):
  158.             for i in range(x1, x2, 10):
  159.                 self.can.create_rectangle(i, j-3, i+9, j, fill='brown', width=0)
  160.  
  161.         self.can.create_rectangle(x1 + 20, y1, x1 + 50, y1 - 60, fill='orange4', width=0)
  162.         self.can.create_rectangle(x1 + 22, y1, x1 + 48, y1 - 58, fill='darkorange4', width=0)
  163.  
  164.         self.can.create_oval(x1 + 24, y1-30, x1 + 26, y1 - 32)
  165.  
  166.         self.createWindow(50, 50, x1+75, y1-65)
  167.  
  168.         self.createRoof(x1, y2+20, 30)
  169.  
  170.     def createPlant(self, height, x, y):
  171.         w = height/8
  172.         self.can.create_polygon(x+1*w, y,
  173.                                   x, y-height/3*w,
  174.                                   x+3*w, y-height*w,
  175.                                   x+1*w, y-height/4*w,
  176.                                   x+2*w, y,
  177.                                 fill='darkgreen', width=0
  178.         )
  179.  
  180.     def createGrass(self, width, floors, y):
  181.         stepx = 6
  182.         stepy = 6
  183.         n = 2
  184.  
  185.         for j in range(0,floors,1):
  186.             for i in range(-10*width, width, stepx):
  187.                 self.createPlant(11, i+n, y+stepy*j)
  188.             n += n
  189.  
  190.  
  191. if __name__ == '__main__':
  192.     root = Tk()
  193.     root.title("Created by pisya")
  194.     FieldM(root)
  195.     root.mainloop()
Add Comment
Please, Sign In to add comment