Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.cmm.msu.ru/trac/tanchiki/browser/tk_ui.py?format=txt
Дата изменения: Sat Dec 25 01:51:31 2010
Дата индексирования: Fri Feb 11 13:52:34 2011
Кодировка:
#!/usr/bin/python
import math
import Tkinter as tk
import body
from body import Tank as BaseTank, Bullet
from game import Game
from user import User
from vector import Vector

game_size = 500, 500
keys1 = {
'ocircumflex': 'base_left',
'acircumflex': 'base_right',
'eacute': 'turret_left',
'oacute': 'turret_right',
'odiaeresis': 'accelerate',
'ucircumflex': 'decelerate',
'division': 'fire',
}

keys2 = {
'icircumflex': 'base_left',
'adiaeresis': 'base_right',
'atilde': 'turret_left',
'ugrave': 'turret_right',
'oslash': 'accelerate',
'ediaeresis': 'decelerate',
'udiaeresis': 'fire',
}

welcome = """Press F5 to start

Keys are:


User 1 :

a, d -- turn tank
q, e -- turn muzzle
w, s -- change speed
x -- fire

User 2 :

j, l -- turn tank
u, o -- turn muzzle
i, k -- change speed
m -- fire
"""

class Tank(BaseTank):

def on_tick(self, tanks, bullets):
if self.user.base_left:
self.rotate_base(-1 * body.base_angle)
if self.user.base_right:
self.rotate_base(body.base_angle)
if self.user.turret_left:
self.rotate_turret(-1 * body.turret_angle)
if self.user.turret_right:
self.rotate_turret(body.turret_angle)
if self.user.accelerate:
self.accelerate(body.speed_delta)
if self.user.decelerate:
self.accelerate(-1 * body.speed_delta)
if self.user.fire:
self.fire()

class UI(object):

def __init__(self):
self.init_game()
self.init_ui()
self.display_welcome()

def init_game(self):
self.user1 = User(keys1)
self.user2 = User(keys2)
w, h = game_size
game = self.game = Game([], [self.user1, self.user2], w, h)
tank1 = Tank(Vector(*game_size) * 0.5, self.user1, self.game)
tank2 = Tank(Vector(*game_size) * 0.5, self.user2, self.game)
game.bodies.append(tank1)
game.bodies.append(tank2)

def init_ui(self):
root = self.root = tk.Tk()
root.bind("", self.on_keypress)
root.bind("", self.on_keyrelease)
w, h = game_size
canvas = self.canvas = tk.Canvas(root,
background="black", width=w, height=h)
canvas.pack(fill="both", expand="yes")

def display_welcome(self):
root, canvas = self.root, self.canvas
root.update()
w, h = canvas.winfo_width(), canvas.winfo_height()
canvas.create_rectangle(0, 0, w, h, fill="#211")
canvas.create_text(w/2, h/2, text=welcome,
fill="red", justify="center", font="bold 15")

def on_keypress(self, ev):
if ev.keysym == "F5":
self.step()
self.user1.on_key(ev.keysym, True)
self.user2.on_key(ev.keysym, True)

def on_keyrelease(self, ev):
self.user1.on_key(ev.keysym, False)
self.user2.on_key(ev.keysym, False)

def step(self):
self.root.after(100, self.step)
self.game.step()
self.redraw()

def redraw(self):
self.canvas.delete("all")
for i in self.game.bodies:
p = i.position
lt = p + Vector(-1,-1) * i.radius
rb = p + Vector(1,1) * i.radius

v1 = Vector(i.radius, 0)
if isinstance(i, body.Tank):
v1.phi = i.base_orientation.phi
else :
v1.phi = i.velocity.phi
vb = p - v1
ve = p + v1 * 2
self.canvas.create_line(vb.x, vb.y, ve.x, ve.y, fill="darkgray", width=i.radius*2)

if isinstance(i, BaseTank):
t = i.turret * i.radius * 1.5 + p
self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, fill="darkgreen")
self.canvas.create_line(p.x, p.y, t.x, t.y, fill="orange", width=2)
elif isinstance(i, Bullet):
self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, outline="red")
else:
raise AssertionError("Unknown object type: %s" % i.__class__.__name__)

if __name__ == "__main__":
UI().root.mainloop()