Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import randrange as rnd, choice, random
- #from PIL import ImageTk, Image
- root = Tk()
- root.geometry('600x600+100+100')
- canv = Canvas(bg='white')
- canv.pack(fill=BOTH,expand=1)
- colors = ('red','green')
- class Ball():
- def __init__(self):
- self.x = 300
- self.y = 300
- self.color = 'orange'
- self.r = 15
- self.vx = 0
- self.vy = 0
- self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color)
- self.move()
- def move(self):
- self.x += self.vx
- self.y += self.vy
- if self.y > 550:
- self.vy *= -1
- if self.y < 50:
- self.vy *= -1
- if self.x > 550:
- self.vx *= -1
- if self.x < 50:
- self.vx *= -1
- canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
- root.after(30,self.move)
- class Platform_P1():
- def __init__(self):
- self.x = 250
- self.y = 550
- self.w = 100
- self.h = 17
- self.color = 'blue'
- self.vx = 0
- self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill = self.color)
- self.move()
- def move(self):
- self.x += self.vx
- if self.x < 0:
- self.x = 0
- self.vx = 0
- if self.x > 500:
- self.x = 500
- self.vx = 0
- canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
- root.after(30,self.move)
- def keyDown(event):
- keys.add(event.keycode)
- def keyUp(event):
- keys.remove(event.keycode)
- def check():
- print(keys)
- if 113 in keys:
- p1.vx = -10
- elif 114 in keys:
- p1.vx = 10
- else:
- p1.vx *= 0.5
- root.after(30,check)
- keys = set()
- p1 = Platform_P1()
- ball = Ball()
- root.bind('<Key>',keyDown)
- root.bind('<KeyRelease>',keyUp)
- check()
- #~ def keyDown(event):
- #~ global state, vx, x
- #~ if event.keycode == 32:
- #~ if state == 'start':
- #~ KAZINO = rnd(1,3)
- #~ if KAZINO == 1:
- #~ ball.vy = 4
- #~ else:
- #~ ball.vy = -4
- #~ state = 'game'
- #~ if event.keycode == 37:
- #~ P1.vx = -4
- #~ if event.keycode == 39:
- #~ P1.vx = 4
- #~ checker()
- #~ def checker(event):
- #~ global vx
- #~ if event.keycode != 37:
- #~ P1.vx = 0
- #~ root.after(1,checker)
- #state = 'start'
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement