Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import random
- class FieldM:
- def __init__(self, master, width=400, height=400):
- self.can = Canvas(master, width=width, height=height, bg='black') # create canvas
- self.can.pack() # pack cancvas
- self.restart(width, height) # render blocks on canvas
- def movement(self):
- # define move location
- if self.sunLocationX >= int(self.can['width']):
- self.sunRoute = 'left'
- elif self.sunLocationX <= -50:
- self.sunRoute = 'right'
- # if the sun location is outside the screen then moving his on right side screen
- x = 515 if self.sunRoute == 'right' else -1
- self.sunLocationX += x
- self.can.move(self.sun, x, 0)
- for ray in self.sunRays:
- self.can.move(ray, x, 0)
- # set render delay
- self.can.after(50, self.movement)
- def restart(self, width, height):
- # define all building blocks
- self.createSun(width, height)
- self.movement()
- self.createClouds(width, 80, 160, 40)
- self.createMounts(height)
- self.createLand(width, height, 350)
- self.createGrass(width, 2, 352)
- self.createHouse(width, height, 220)
- self.createHouse(width, height, 10)
- self.createGrass(width, 11, 365)
- def createBall(self, x, y, r, c):
- # render oval
- self.can.create_oval(x+r, y+r, x-r, y-r, fill=c, outline=c)
- def createSun(self, h, w):
- self.sunLocationX = loc = int(self.can['width'])
- self.can.create_rectangle(0-2, 0-2, w+2, h+2, fill="lightblue") # create sky
- self.sun = self.can.create_oval(loc+20, 0+20, loc-20, 0-20, fill='yellow', outline='yellow', width=1)
- self.sunRays = []
- # define sun rays on canvas
- for i in range(180, 360, 10):
- self.sunRays.append(
- self.can.create_arc(loc+40, 0+40, loc-40, 0-40, start=i, extent=1, outline='yellow', style='arc', width=25)
- )
- def createCloudsCh1(self, x, y, mass):
- # render single cloud
- for i in range(8):
- x += (random()*(mass*2)-mass)
- y += random()*(mass) - (mass/3)
- self.createBall(x, y, mass, "white")
- def createCloudsCh2(self, width, floor):
- # create cloud location
- for i in range(int(width * (-1)), int(width * 2), int(width / 5)):
- r = random() * 20 - 10 # -10 - 10
- m = random() * 2 - 4 # -4 - 1
- self.createCloudsCh1(i, floor + r, 20 + m) # x,y,mass
- def createClouds(self, width, start, end, step):
- # create clouds
- for i in range(start, end, step):
- self.createCloudsCh2(width, i)
- def createMountsCh1(self, heightF, x1, x2, height):
- heightF = heightF - 20
- height = height
- x1 = x1
- x2 = x2
- xcen = int((x1+x2)/2)
- self.can.create_polygon((x1, heightF), (xcen, height), (x2, heightF), fill='lightgrey', outline='darkgrey', width=2)
- x1 = (x1 + xcen)/2
- x2 = (x2 + xcen)/2
- heightF = (heightF + height)/2
- self.can.create_polygon((x1, heightF), (xcen, height), (x2, heightF), fill='white')
- def createMounts(self, height):
- # create mounts
- count = int(random()*100)
- x1=0
- x2=0
- for i in range(count):
- x1 += int(random()*10)
- x2 += x1 + int(random()*(100-40)-40)
- h = int(random()*(250-150)+150)
- self.createMountsCh1(height, x1, x2, h)
- def createLand(self, widthF, heightF, height):
- # render green road background
- self.can.create_rectangle(0, heightF, widthF, height, fill='lightgreen', width=0)
- def createWindow(self, width, height, x, y):
- # define window
- x1 = x
- y1 = y
- x2 = x+width
- y2 = y+height
- xc = int((x+x+width)/2)
- yc = int((y+y+width)/2)
- xs = int((x2-x1)/2)
- ys = int((y2-y1)/2)
- self.can.create_rectangle(x, y, x+width, y+height, outline='lightsalmon3', fill='lightsalmon2', width=2)
- for i in (range(x1, x2, xs)):
- for j in (range(y1, y2, ys)):
- # create windows background
- self.can.create_rectangle(i+1, j+1, i+ys-2, j+ys-2, outline='deepskyblue3', fill='deepskyblue2')
- def createRoof(self, x, y, height):
- # render roofs
- hood = 15
- width = 150
- self.can.create_rectangle(x+width-20, y-5,
- x+width-30, y-height-10,
- width=0, fill='brown')
- self.can.create_polygon(x-hood, y,
- x+10, y-height,
- x+width-10, y-height,
- x+width+hood, y)
- def createHouse(self, widthF, heightF, x):
- self.master = self.can
- x1 = x
- x2 = x1+150
- y1 = heightF - 40
- y2 = heightF - 100 - 40
- self.can.create_rectangle(x1, y1, x2, y2, fill='grey', width=0)
- for j in range(y1, y2, -4):
- for i in range(x1, x2, 10):
- self.can.create_rectangle(i, j-3, i+9, j, fill='brown', width=0)
- self.can.create_rectangle(x1 + 20, y1, x1 + 50, y1 - 60, fill='orange4', width=0)
- self.can.create_rectangle(x1 + 22, y1, x1 + 48, y1 - 58, fill='darkorange4', width=0)
- self.can.create_oval(x1 + 24, y1-30, x1 + 26, y1 - 32)
- self.createWindow(50, 50, x1+75, y1-65)
- self.createRoof(x1, y2+20, 30)
- def createPlant(self, height, x, y):
- w = height/8
- self.can.create_polygon(x+1*w, y,
- x, y-height/3*w,
- x+3*w, y-height*w,
- x+1*w, y-height/4*w,
- x+2*w, y,
- fill='darkgreen', width=0
- )
- def createGrass(self, width, floors, y):
- stepx = 6
- stepy = 6
- n = 2
- for j in range(0,floors,1):
- for i in range(-10*width, width, stepx):
- self.createPlant(11, i+n, y+stepy*j)
- n += n
- if __name__ == '__main__':
- root = Tk()
- root.title("Created by pisya")
- FieldM(root)
- root.mainloop()
Add Comment
Please, Sign In to add comment