Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_sling.py
- from Tkinter import *
- import random
- from math import *
- WIDTH = 400
- HEIGHT = 400
- farben = ["#ff0000","#00ff00","#0000ff","#ffff00","#ff00ff","#00ffff"]
- ball_count = 0
- gravity = 0.05
- friction = 0.001
- class GUI():
- def __init__(self):
- self.root = Tk()
- self.root.title("Sling")
- self.root.bind_all('<Escape>',self.quit)
- self.canvas = Canvas(self.root,width=WIDTH,height=HEIGHT,bg="#000000")
- self.arrow = self.canvas.create_line(-100,-100,-100,-100,
- fill="#a0a0a0",width=3,
- arrow="first",arrowshape=(12,16,6))
- self.canvas.pack()
- def quit(self,event):
- self.root.quit()
- self.root.destroy()
- class Ball():
- def __init__(self,field,gui,x=None,y=None,vx=0,vy=0):
- self.field = field
- self.RADIUS = random.randint(10,40)
- if x:
- if x<self.RADIUS: x = self.RADIUS
- if x>WIDTH-self.RADIUS: x=WIDTH-self.RADIUS
- self.x = x
- else:
- self.x = random.randint(WIDTH/4,WIDTH*3/4)
- if y:
- if y<self.RADIUS: y = self.RADIUS
- if y>HEIGHT-self.RADIUS: y=HEIGHT-self.RADIUS
- self.y = y
- else:
- self.y = random.randint(HEIGHT/4,HEIGHT*3/4)
- farbe = random.choice(farben)
- self.ball = field.create_oval(self.x-self.RADIUS,self.y-self.RADIUS,
- self.x+self.RADIUS,self.y+self.RADIUS,
- fill=farbe,outline="")
- field.tag_lower(self.ball,gui.arrow)
- self.vx, self.vy = vx,vy
- def move(self):
- if self.NOMOVE: return
- self.x += self.vx
- self.y += self.vy
- if self.x<self.RADIUS or self.x>WIDTH-self.RADIUS:
- self.vx = -self.vx
- self.x += self.vx
- if self.y<self.RADIUS or self.y>HEIGHT-self.RADIUS:
- self.vy = -self.vy
- self.y += self.vy
- self.vy += gravity
- self.vx *= (1-friction)
- self.vy *= (1-friction)
- self.field.coords(self.ball,self.x-self.RADIUS,self.y-self.RADIUS,
- self.x+self.RADIUS,self.y+self.RADIUS)
- class Play():
- def __init__(self):
- self.gui = GUI()
- self.gui.canvas.bind("<Button-1>",self.clicked)
- self.gui.canvas.bind("<ButtonRelease-1>",self.dragged)
- self.gui.canvas.bind("<B1-Motion>",self.moved)
- self.gui.canvas.bind("<Button-3>",self.unclicked)
- self.balls = []
- for i in range(ball_count):
- self.balls.append(Ball(self.gui.canvas,self.gui))
- self.action()
- def clicked(self,event):
- self.gui.root["cursor"] = "target"
- self.balls.append(Ball(self.gui.canvas,self.gui,event.x,event.y))
- self.balls[-1].NOMOVE = True
- self.mouse_x0, self.mouse_y0 = event.x, event.y
- def moved(self,event):
- if len(self.balls)==0: return
- ball = self.balls[-1]
- self.gui.canvas.coords(self.gui.arrow,self.mouse_x0,self.mouse_y0,
- event.x,event.y)
- def dragged(self,event):
- self.gui.canvas.coords(self.gui.arrow,-100,-100,-100,-100)
- if len(self.balls)==0: return
- self.gui.root["cursor"] = "arrow"
- distance_x, distance_y = event.x-self.mouse_x0, event.y-self.mouse_y0
- ball = self.balls[-1]
- ball.vx = -distance_x/20.0
- ball.vy = -distance_y/20.0
- self.balls[-1].NOMOVE = False
- def unclicked(self,event):
- if len(self.balls)>0:
- removeball = self.balls[-1]
- self.gui.canvas.delete(removeball.ball)
- self.balls = self.balls[:-1]
- def action(self):
- while True:
- for ball in self.balls:
- ball.move()
- self.gui.canvas.after(10)
- self.gui.canvas.update()
- Play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement