Advertisement
boris-vlasenko

keys check with set2

Nov 6th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. from tkinter import *
  2. from random import randrange as rnd, choice, random
  3. #from PIL import ImageTk, Image
  4.  
  5. root = Tk()
  6. root.geometry('600x600+100+100')
  7. canv = Canvas(bg='white')
  8. canv.pack(fill=BOTH,expand=1)
  9. colors = ('red','green')
  10.  
  11. class Ball():
  12.     def __init__(self):
  13.         self.x = 300
  14.         self.y = 300
  15.         self.color = 'orange'
  16.         self.r = 15
  17.         self.vx = 0
  18.         self.vy = 0
  19.         self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color)
  20.         self.move()
  21.        
  22.     def move(self):
  23.         self.x += self.vx
  24.         self.y += self.vy
  25.         if self.y > 550:
  26.             self.vy *= -1
  27.         if self.y < 50:
  28.             self.vy *= -1
  29.         if self.x > 550:
  30.             self.vx *= -1
  31.         if self.x < 50:
  32.             self.vx *= -1
  33.         canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  34.         root.after(30,self.move)
  35.        
  36.        
  37. class Platform_P1():
  38.     def __init__(self):
  39.         self.x = 250
  40.         self.y = 550
  41.         self.w = 100
  42.         self.h = 17
  43.         self.color = 'blue'
  44.         self.vx = 0
  45.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill = self.color)
  46.         self.move()
  47.    
  48.     def move(self):
  49.         self.x += self.vx
  50.         if self.x < 0:
  51.             self.x = 0
  52.             self.vx = 0
  53.         if self.x > 500:
  54.             self.x = 500
  55.             self.vx = 0  
  56.         canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
  57.         root.after(30,self.move)
  58.        
  59.  
  60.  
  61.  
  62.  
  63.  
  64. def keyDown(event):
  65.     keys.add(event.keycode)
  66.    
  67. def keyUp(event):
  68.     keys.remove(event.keycode)
  69.  
  70. def check():
  71.     print(keys)
  72.     if 113 in keys:
  73.         p1.vx = -10
  74.     elif 114 in keys:
  75.         p1.vx = 10
  76.     else:
  77.         p1.vx *= 0.5
  78.     root.after(30,check)
  79.  
  80. keys = set()
  81.  
  82. p1 = Platform_P1()
  83. ball = Ball()  
  84.  
  85. root.bind('<Key>',keyDown)
  86. root.bind('<KeyRelease>',keyUp)
  87.  
  88. check()
  89.  
  90.  
  91.  
  92.  
  93. #~ def keyDown(event):
  94.     #~ global state, vx, x
  95.     #~ if event.keycode == 32:
  96.         #~ if state == 'start':
  97.             #~ KAZINO = rnd(1,3)
  98.             #~ if KAZINO == 1:
  99.                 #~ ball.vy = 4
  100.             #~ else:
  101.                 #~ ball.vy = -4
  102.             #~ state = 'game'
  103.     #~ if event.keycode == 37:
  104.         #~ P1.vx = -4
  105.     #~ if event.keycode == 39:
  106.         #~ P1.vx = 4
  107.     #~ checker()       
  108.  
  109. #~ def checker(event):
  110.     #~ global vx
  111.     #~ if event.keycode != 37:
  112.         #~ P1.vx = 0
  113.     #~ root.after(1,checker)
  114.  
  115.  
  116. #state = 'start'
  117. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement