Advertisement
here2share

# multi_keys.py

Jun 1st, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. # multi_keys.py
  2.  
  3. from Tkinter import *
  4.  
  5. class Playfield:
  6.     def __init__(self):
  7.         # this dict keeps track of keys that have been pressed but not
  8.         # released
  9.         self.pressed = {}
  10.  
  11.         self._create_ui()
  12.  
  13.     def start(self):
  14.         self._animate()
  15.         self.root.mainloop()
  16.  
  17.     def _create_ui(self):
  18.         self.root = Tk()
  19.         self.p1label = Label(text="To Move Player 1: W=Up S=Down",
  20.                              anchor="w")
  21.         self.p2label = Label(text="To Move Player 2: O=Up L=Down",
  22.                              anchor="w")
  23.         self.canvas = Canvas(width=440, height=440)
  24.         self.canvas.config(scrollregion=(-20, -20, 420, 420))
  25.  
  26.         self.p1label.pack(side="top", fill="x")
  27.         self.p2label.pack(side="top", fill="x")
  28.         self.canvas.pack(side="top", fill="both", expand="true")
  29.  
  30.         self.p1 = Paddle(self.canvas, tag="p1", color="red", x=0, y=0)
  31.         self.p2 = Paddle(self.canvas, tag="p2", color="blue", x=400, y=0)
  32.  
  33.         self._set_bindings()
  34.  
  35.     def _animate(self):
  36.         if self.pressed["w"]: self.p1.move_up()
  37.         elif self.pressed["s"]: self.p1.move_down()
  38.         if self.pressed["o"]: self.p2.move_up()
  39.         elif self.pressed["l"]: self.p2.move_down()
  40.         self.p1.redraw()
  41.         self.p2.redraw()
  42.         self.root.after(10, self._animate)
  43.  
  44.     def _set_bindings(self):
  45.         for char in ["w","s","o", "l"]:
  46.             self.root.bind("<KeyPress-%s>" % char, self._pressed)
  47.             self.root.bind("<KeyRelease-%s>" % char, self._released)
  48.             self.pressed[char] = False
  49.  
  50.     def _pressed(self, event):
  51.         self.pressed[event.char] = True
  52.  
  53.     def _released(self, event):
  54.         self.pressed[event.char] = False
  55.  
  56. class Paddle():
  57.     def __init__(self, canvas, tag, color="red", x=0, y=0):
  58.         self.canvas = canvas
  59.         self.tag = tag
  60.         self.x = x
  61.         self.y = y
  62.         self.color = color
  63.         self.redraw()
  64.  
  65.     def move_up(self):
  66.         self.y = max(self.y -2, 0)
  67.  
  68.     def move_down(self):
  69.         self.y = min(self.y + 2, 400)
  70.  
  71.     def redraw(self):
  72.         x0 = self.x - 10
  73.         x1 = self.x + 10
  74.         y0 = self.y - 20
  75.         y1 = self.y + 20
  76.         self.canvas.delete(self.tag)
  77.         self.canvas.create_rectangle(x0,y0,x1,y1,tags=self.tag, fill=self.color)
  78.  
  79. if __name__ == "__main__":
  80.     p = Playfield()
  81.     p.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement