import tkinter as tk
W,H,G=900,600,16
COL={"s":"#5d4037","b":"#78909c","q":"#e53935","g":"#43a047","c":"#ffb300"}
class M:
 def __init__(s,r):
  s.c=tk.Canvas(r,width=W,height=H,bg="#5dade2",highlightthickness=0)
  s.c.pack()
  r.title("050.马里奥")
  s.b=s.c.create_rectangle(400,H-80,432,H-48,fill=COL["c"])
  s.vx=0
  s.vy=0
  s.g=0.8
  s.o=[]
  s.build()
  r.bind("<Left>",lambda e:s.setvx(-6))
  r.bind("<Right>",lambda e:s.setvx(6))
  r.bind("<KeyRelease>",lambda e:s.setvx(0))
  r.bind("<space>",lambda e:s.jump())
  s.run()
 def build(s):
  for i in range(56):s.o.append(s.c.create_rectangle(i*G,H-G,i*G+G,H,fill=COL["s"]))
  for i in range(10,14):s.o.append(s.c.create_rectangle(i*G,H-2*G,i*G+G,H-G,fill=COL["s"]))
  for i in range(30,35):s.o.append(s.c.create_rectangle(i*G,H-2*G,i*G+G,H-G,fill=COL["b"]))
  s.o.append(s.c.create_rectangle(26*G,H-4*G,27*G,H-3*G,fill=COL["q"]))
  s.o.append(s.c.create_oval(32*G,H-5*G,32*G+G/2,H-5*G+G/2,fill=COL["g"]))
 def setvx(s,v):
  s.vx=v
 def jump(s):
  if s.g==0.8:
   s.vy=-15
   s.g=0.4
 def run(s):
  s.c.move(s.b,s.vx,0)
  s.c.move(s.b,0,s.vy)
  bx1,by1,bx2,by2=s.c.coords(s.b)
  if by2<H-48:s.vy+=s.g
  else:s.vy=0;s.g=0.8
  for o in s.o:
   x1,y1,x2,y2=s.c.coords(o)
   if x1<bx2 and x2>bx1 and y1<by2 and y2>by2-10 and s.vy>0:s.vy=0;s.g=0.8
   if x1<bx2 and x2>bx1 and y1<by1 and y2>by1-10 and s.vy<0:s.vy=0
   if x1<bx2 and x2>bx1 and y1<by1 and y2>by2 and s.vx>0:s.vx=0
   if x1<bx1 and x2>bx1 and y1<by1 and y2>by2 and s.vx<0:s.vx=0
   if s.c.itemcget(o,"fill")==COL["q"]and x1<bx2 and x2>bx1 and y1<by2 and y2>by1:s.c.delete(o);s.o.remove(o)
   if s.c.itemcget(o,"fill")==COL["g"]and x1<bx2 and x2>bx1 and y1<by2 and y2>by1:s.c.delete(o);s.o.remove(o);s.c.create_text(W/2,H/2,text="通关！",font=("Arial",48),fill="#fff")
  s.c.after(20,s.run)
tk.Tk()
M(tk.Tk())
tk.mainloop()