summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas Schodet2011-10-16 15:26:22 +0200
committerNicolas Schodet2011-10-16 15:26:22 +0200
commita0beeed07493245d7048390625bfca7087abfe1b (patch)
tree2db165b3e768266dfca10e269cf2ebfebfc56415
parent99dd816f4859c8d17ea2c3df9435cbbbcce595d8 (diff)
host/simu: add eurobot 2012 table
-rw-r--r--host/simu/model/table_eurobot2012.py93
-rw-r--r--host/simu/view/table_eurobot2012.py189
2 files changed, 282 insertions, 0 deletions
diff --git a/host/simu/model/table_eurobot2012.py b/host/simu/model/table_eurobot2012.py
new file mode 100644
index 00000000..38960513
--- /dev/null
+++ b/host/simu/model/table_eurobot2012.py
@@ -0,0 +1,93 @@
+# simu - Robot simulation. {{{
+#
+# Copyright (C) 2011 Nicolas Schodet
+#
+# APBTeam:
+# Web: http://apbteam.org/
+# Email: team AT apbteam DOT org
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# }}}
+"""Table model for Eurobot 2012."""
+import simu.model.table
+from simu.model.round_obstacle import RoundObstacle
+from simu.model.rectangular_obstacle import RectangularObstacle
+from math import pi
+import math
+import random
+
+class Table (simu.model.table.Table):
+
+ def __init__ (self, cards = None):
+ simu.model.table.Table.__init__ (self)
+ # Well, this is a boring write only code which create every elements.
+ # Add coins.
+ self.coins = [ ]
+ def add_coin (pos, angle, level = 1):
+ coin = RoundObstacle (60, level)
+ coin.pos = pos
+ coin.angle = angle
+ coin.value = 1
+ self.coins.append (coin)
+ def add_coin_circle (center, radius, start, step, n, level = 1):
+ angle = start
+ for i in xrange (n):
+ pos = (center[0] + radius * math.cos (angle),
+ center[1] + radius * math.sin (angle))
+ add_coin (pos, angle, level)
+ angle += step
+ add_coin ((1000, 1500), 0)
+ add_coin ((2000, 1500), pi)
+ add_coin ((450, 300), pi)
+ add_coin ((3000 - 450, 300), 0)
+ add_coin_circle ((1500, 300), 90, 0, pi / 2, 4)
+ add_coin_circle ((1500 - 400, 1000), 300 - 60, pi / 4, pi / 4, 7)
+ add_coin_circle ((1500 + 400, 1000), 300 - 60, pi + pi / 4, pi / 4, 7)
+ add_coin_circle ((1500 - 400, 1000), 115, pi / 4, pi / 2, 4)
+ add_coin_circle ((1500 + 400, 1000), 115, pi / 4, pi / 2, 4)
+ add_coin_circle ((1500 - 400, 1000), 105, pi / 4, pi / 2, 4, 3)
+ add_coin_circle ((1500 + 400, 1000), 105, pi / 4, pi / 2, 4, 3)
+ # Add gold bars.
+ self.gold_bars = [ ]
+ def add_gold_bar (pos, angle, level = 1):
+ gold_bar = RectangularObstacle ((150, 70), level)
+ gold_bar.pos = pos
+ gold_bar.angle = angle
+ gold_bar.value = 3
+ self.gold_bars.append (gold_bar)
+ add_gold_bar ((1500, 647), 0)
+ add_gold_bar ((1500 - 400, 1000 + 125 - 35), 0, 2)
+ add_gold_bar ((1500 + 400, 1000 + 125 - 35), 0, 2)
+ add_gold_bar ((1500 - 400, 1000 - 125 + 35), 0, 2)
+ add_gold_bar ((1500 + 400, 1000 - 125 + 35), 0, 2)
+ ba = pi / 2 - 0.04995839
+ cba = math.cos (ba)
+ sba = math.sin (ba)
+ gbx = 400 - (285 + 75) * cba + 35
+ gby = 1500 - (285 + 75) * sba
+ add_gold_bar ((gbx, gby), ba)
+ add_gold_bar ((3000 - gbx, gby), pi - ba)
+ # Set random black coins.
+ nblack = 0
+ while nblack < 4:
+ coin = random.choice (self.coins[2:])
+ if coin.value:
+ coin.value = 0
+ nblack += 1
+ # Add everything to obstacles.
+ self.obstacles += self.coins
+ self.obstacles += self.gold_bars
+
diff --git a/host/simu/view/table_eurobot2012.py b/host/simu/view/table_eurobot2012.py
new file mode 100644
index 00000000..89ada042
--- /dev/null
+++ b/host/simu/view/table_eurobot2012.py
@@ -0,0 +1,189 @@
+# simu - Robot simulation. {{{
+#
+# Copyright (C) 2011 Nicolas Schodet
+#
+# APBTeam:
+# Web: http://apbteam.org/
+# Email: team AT apbteam DOT org
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# }}}
+"""Eurobot 2012 table."""
+from simu.inter.drawable import Drawable
+import math
+from math import pi
+
+PURPLE = '#a737ff'
+RED = '#fe4543'
+BLUE = '#01c3ff'
+GREEN = '#81ff00'
+BLACK = '#1f1b1d'
+YELLOW = '#f7ff00'
+WHITE = '#f5fef2'
+TRANS = '#c0c2b5'
+BROWN = '#9d4e01'
+
+def draw_coin (d, coin):
+ d.trans_push ()
+ color = WHITE if coin.value else BLACK
+ d.draw_circle ((0, 0), coin.radius, fill = color)
+ d.draw_circle ((0, 0), 7.5, fill = color)
+ d.trans_translate ((40.5, 0))
+ d.draw_rectangle ((-9, -9), (9, 9), fill = color, outline = TRANS)
+ d.trans_pop ()
+
+class Coin (Drawable):
+
+ def __init__ (self, onto, model):
+ Drawable.__init__ (self, onto)
+ self.model = model
+ self.model.register (self.__notified)
+ self.__notified ()
+
+ def __notified (self):
+ self.pos = self.model.pos
+ self.angle = self.model.angle
+ self.update ()
+
+ def draw (self):
+ self.reset ()
+ if self.pos:
+ self.trans_translate (self.pos)
+ self.trans_rotate (self.angle)
+ draw_coin (self, self.model)
+ Drawable.draw (self)
+
+def draw_gold_bar (d, gold_bar):
+ d.draw_rectangle ((-gold_bar.dim[0] / 2, -gold_bar.dim[1] / 2),
+ (gold_bar.dim[0] / 2, gold_bar.dim[1] / 2), fill = YELLOW)
+ d.draw_rectangle ((-gold_bar.dim[0] / 2 + 13, -gold_bar.dim[1] / 2 + 13),
+ (gold_bar.dim[0] / 2 - 13, gold_bar.dim[1] / 2 - 13), fill = YELLOW)
+
+class GoldBar (Drawable):
+
+ def __init__ (self, onto, model):
+ Drawable.__init__ (self, onto)
+ self.model = model
+ self.model.register (self.__notified)
+ self.__notified ()
+
+ def __notified (self):
+ self.pos = self.model.pos
+ self.angle = self.model.angle
+ self.update ()
+
+ def draw (self):
+ self.reset ()
+ if self.pos:
+ self.trans_translate (self.pos)
+ self.trans_rotate (self.angle)
+ draw_gold_bar (self, self.model)
+ Drawable.draw (self)
+
+class Table (Drawable):
+ """The table and its elements."""
+
+ def __init__ (self, onto, model):
+ Drawable.__init__ (self, onto)
+ self.model = model
+ for e in self.model.coins:
+ if e.level <= 2:
+ Coin (self, e)
+ for e in self.model.gold_bars:
+ GoldBar (self, e)
+ for e in self.model.coins:
+ if e.level > 2:
+ Coin (self, e)
+
+ def draw_both (self, primitive, *args, **kargs):
+ """Draw a primitive on both sides."""
+ primitive (*args, **kargs)
+ primitive (*((3000 - x, y) for x, y in args), **kargs)
+
+ def draw (self):
+ # Redraw.
+ self.reset ()
+ # Table.
+ self.draw_rectangle ((-22, -22), (3000 + 22, 2000 + 22), fill = BLUE)
+ self.draw_rectangle ((0, 0), (3000, 2000), fill = BLUE)
+ # Start zones.
+ self.draw_rectangle ((0, 2000 - 500), (500, 2000), fill = PURPLE)
+ self.draw_rectangle ((3000, 2000 - 500), (3000 - 500, 2000), fill = RED)
+ # Black lines.
+ pup = 2000 - 500 + 50
+ self.draw_both (self.draw_polygon, (500, pup), (650, pup), (650, 0), (630, 0),
+ (630, pup - 20), (500, pup - 20), fill = BLACK, outline = 'black')
+ # Ships.
+ ba = 0.04995839
+ self.draw_both (self.draw_polygon, (0, 2000 - 500), (400, 2000 - 500), (325, 0), (0, 0),
+ fill = BROWN, outline = 'black')
+ self.draw_both (self.draw_rectangle, (0, 2000 - 500), (400, 2000 - 500 - 18), fill = BROWN)
+ cba = 750 * math.cos (ba)
+ sba = 750 * math.sin (ba)
+ self.draw_both (self.draw_polygon, (325, 0), (325 + sba, cba), (325 + sba + 18, cba), (325 + 18, 0),
+ fill = BROWN, outline = 'black')
+ self.draw_both (self.draw_rectangle, (-22, -22), (340, 610 - 22), fill = TRANS)
+ self.draw_both (self.draw_rectangle, (-4, -4), (340 - 18, 610 - 22 - 18), fill = TRANS)
+ # Peanut island.
+ self.draw_both (self.draw_circle, (1500 - 400, 1000), r = 300, fill = YELLOW)
+ self.draw_rectangle ((1500 - 400 + 141, 1000 + 265), (1500 + 400 - 141, 1000 - 265), fill = YELLOW, outline = '')
+ self.draw_arc ((1500, 1000 + 750), 550, start = pi + 1, extent = pi - 2, fill = BLUE, outline = '')
+ self.draw_arc ((1500, 1000 + 750), 550, start = pi + 1.08083, extent = pi - 2 * 1.08083, style = 'arc')
+ self.draw_arc ((1500, 1000 - 750), 550, start = 1, extent = pi - 2, fill = BLUE, outline = '')
+ self.draw_arc ((1500, 1000 - 750), 550, start = 1.08083, extent = pi - 2 * 1.08083, style = 'arc')
+ self.draw_both (self.draw_circle, (1500 - 400, 1000), r = 200, fill = GREEN)
+ self.draw_circle ((1500, 1000), r = 75, fill = GREEN)
+ self.draw_both (self.draw_rectangle, (1500 - 400 - 125, 1000 - 125), (1500 - 400 + 125, 1000 + 125), fill = BROWN)
+ # Map island.
+ self.draw_arc ((1500, 2000), 400, start = pi, extent = pi, fill = YELLOW)
+ self.draw_arc ((1500, 2000), 300, start = pi, extent = pi, fill = GREEN)
+ # Map.
+ self.draw_rectangle ((1500 - 200 - 22, 2000 + 148), (1500 + 200 + 22, 2000 + 140), fill = BLUE)
+ self.draw_rectangle ((1500 - 200, 2000 + 140), (1500 + 200, 2000 - 8), fill = BLUE)
+ self.draw_both (self.draw_rectangle, (1500 - 200 - 22, 2000 + 140), (1500 - 200, 2000 + 125), fill = BLUE)
+ self.draw_both (self.draw_rectangle, (1500 - 200 - 22, 2000 + 125), (1500 - 200, 2000 - 23), fill = BLUE)
+ self.draw_rectangle ((1500 - 22, 2000 + 140), (1500 + 22, 2000 + 125), fill = BLUE)
+ self.draw_rectangle ((1500 - 22, 2000 + 125), (1500 + 22, 2000 + 115), fill = BLUE)
+ self.draw_rectangle ((1500 - 22, 2000 + 2), (1500 + 22, 2000 - 13), fill = BLUE)
+ self.draw_rectangle ((1500 - 22, 2000 - 13), (1500 + 22, 2000 - 23), fill = BLUE)
+ # Bottles.
+ def draw_bottle (x, color):
+ self.draw_rectangle ((x - 100, 0), (x + 100, -44), fill = color)
+ self.draw_rectangle ((x - 100, -44), (x + 100, -66), fill = color)
+ self.draw_rectangle ((x - 11, 44), (x + 11, 0), fill = color)
+ self.draw_rectangle ((x - 50, 22), (x + 100, -44), fill = color)
+ self.draw_polygon ((x - 50, 22), (x - 80, 0), (x - 100, 0), (x - 100, -22), (x - 80, -22), (x - 50, -44),
+ fill = color, outline = 'black')
+ draw_bottle (640, PURPLE)
+ draw_bottle (640 + 477, RED)
+ draw_bottle (3000 - 640, RED)
+ draw_bottle (3000 - 640 - 477, PURPLE)
+ # Axes.
+ self.draw_line ((0, 200), (0, 0), (200, 0), arrow = 'both')
+ # Beacons.
+ self.draw_both (self.draw_rectangle, (-22, 2000 + 22), (-22 - 80, 2000 + 22 + 80), fill = BLACK)
+ self.draw_both (self.draw_rectangle, (-22, 1000 - 40), (-22 - 80, 1000 + 40), fill = BLACK)
+ self.draw_both (self.draw_rectangle, (-22, -80 - 22), (-22 - 80, -22), fill = BLACK)
+ # Children.
+ Drawable.draw (self)
+
+if __name__ == '__main__':
+ from simu.inter.inter import Inter
+ import simu.model.table_eurobot2012 as model
+ app = Inter ()
+ m = model.Table ()
+ Table (app.table_view, m)
+ app.mainloop ()