Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/tanchiki/annotate/dba440ba9a00/tk_ui.py
Дата изменения: Unknown
Дата индексирования: Sat Mar 1 00:08:57 2014
Кодировка:
tanchiki: tk_ui.py annotate

tanchiki

annotate tk_ui.py @ 46:dba440ba9a00

Bugfixed: constant names for tank movement; added the tank to the game
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 20:37:54 +0300
parents 1d0efaea3702
children 3f2ae407ae0c
rev   line source
me@43 1 #!/usr/bin/python
me@34 2 import math
me@34 3 import Tkinter as tk
me@38 4 import body
me@34 5 from game import Game
me@40 6 from user import User
me@46 7 from vector import Vector
me@38 8
me@38 9 game_size = 100, 100
me@38 10 keys = {
me@38 11 'a': 'base_left',
me@38 12 'd': 'base_right',
me@38 13 'q': 'turret_left',
me@38 14 'e': 'turret_right',
me@38 15 'w': 'accelerate',
me@38 16 's': 'decelerate',
me@38 17 'x': 'fire',
me@38 18 }
me@38 19
me@44 20 welcome = """Press F5 to start
me@44 21
me@44 22 Keys are:
me@44 23 a, d -- turn tank
me@44 24 q, e -- turn muzzle
me@44 25 w, s -- change speed
me@44 26 x -- fire"""
me@44 27
me@38 28 class Tank(body.Tank):
me@38 29
me@38 30 def on_tick(self, tanks, bullets):
me@38 31 if self.user.base_left:
me@46 32 self.rotate_base(body.base_angle)
me@38 33 if self.user.base_right:
me@46 34 self.rotate_base(-body.base_angle)
me@38 35 if self.user.turret_left:
me@46 36 self.rotate_turret(body.turret_angle)
me@38 37 if self.user.turret_right:
me@46 38 self.rotate_turret(-body.turret_angle)
me@38 39 if self.user.accelerate:
me@46 40 self.accelerate(body.speed_delta)
me@38 41 if self.user.decelerate:
me@46 42 self.accelerate(-body.speed_delta)
me@38 43 if self.user.fire:
me@38 44 self.fire()
me@34 45
me@34 46 class UI(object):
me@38 47
me@34 48 def __init__(self):
me@34 49 self.init_game()
me@34 50 self.init_ui()
me@45 51 self.display_welcome()
me@34 52
me@38 53 def init_game(self):
me@38 54 self.user = User(keys)
me@40 55 w, h = game_size
me@46 56 game = self.game = Game([], [self.user], w, h)
me@46 57 tank = Tank(Vector(50, 50), self.user, self.game)
me@46 58 game.bodies.append(tank)
me@38 59
me@34 60 def init_ui(self):
me@34 61 root = self.root = tk.Tk()
me@40 62 root.bind("<KeyPress>", self.on_key)
me@34 63 canvas = self.canvas = tk.Canvas(root, background="black")
me@34 64 canvas.pack(fill="both", expand="yes")
me@34 65
me@44 66 def display_welcome(self):
me@45 67 root, canvas = self.root, self.canvas
me@41 68 root.update()
me@41 69 w, h = canvas.winfo_width(), canvas.winfo_height()
me@44 70 canvas.create_rectangle(0, 0, w, h, fill="#211")
me@44 71 canvas.create_text(w/2, h/2, text=welcome,
me@44 72 fill="red", justify="center", font="bold 15")
me@41 73
me@34 74 def on_key(self, ev):
me@41 75 if ev.keysym == "F5":
me@41 76 self.step()
me@40 77 self.user.on_keypress(ev.keysym)
me@34 78
me@34 79 def step(self):
me@34 80 self.root.after(100, self.step)
me@40 81 self.game.step()
me@34 82 self.redraw()
me@34 83
me@34 84 def redraw(self):
me@34 85 self.canvas.delete("all")
me@34 86 for body in self.game.bodies:
me@34 87 p = body.position
me@34 88 lt = p + Vector(-1,-1) * body.radius
me@34 89 rb = p + Vector(1,1) * body.radius
me@34 90 if isinstance(body, Tank):
me@34 91 t = body.turret * body.radius + p
me@34 92 self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, fill="green")
me@34 93 self.canvas.create_line(p.x, p.y, t.x, t.y, fill="yellow", width=2)
me@34 94 elif isinstance(body, Bullet):
me@34 95 self.canvas.create_oval(lt.x, lt.y, rb.x, rb.y, fill="red")
me@34 96
me@34 97 if __name__ == "__main__":
me@34 98 UI().root.mainloop()