Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/double rainbow #266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pelita/scripts/pelita_tkviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def geometry_string(s):
' LOGFILE (default \'stderr\')',
metavar='LOGFILE', const='-', nargs='?')


def main():
args = parser.parse_args()
if args.version:
Expand Down
39 changes: 27 additions & 12 deletions pelita/ui/tk_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from ..game import next_round_turn
from ..team import _ensure_list_tuples
from .tk_sprites import BotSprite, Food, Wall, Arrow, RED, BLUE, YELLOW, GREY, BROWN, LIGHT_BLUE, LIGHT_RED, STRONG_BLUE, STRONG_RED
from .tk_sprites import BotSprite, Food, Wall, Arrow, RED, BLUE, YELLOW, GREY, BROWN, LIGHT_BLUE, LIGHT_RED, STRONG_BLUE, STRONG_RED, col
from .tk_utils import wm_delete_window_handler
from .. import layout

Expand Down Expand Up @@ -141,7 +141,7 @@ class UI:

class TkApplication:
def __init__(self, window, controller_address=None,
geometry=None, delay=1, stop_after=None):
geometry=None, delay=1, stop_after=None, rainbow=False):
self.window = window
self.window.configure(background="white")

Expand All @@ -166,6 +166,7 @@ def __init__(self, window, controller_address=None,
self.size_changed = True

self._grid_enabled = False
self._rainbow = rainbow

self._times = []
self._fps = None
Expand Down Expand Up @@ -399,6 +400,13 @@ def update(self, game_state=None):
!= (self.ui.game_canvas.winfo_width(), self.ui.game_canvas.winfo_height())):
self.size_changed = True

if self._rainbow:
import random
r = random.randint(0, 15)
g = random.randint(0, 15)
b = random.randint(0, 15)
self.ui.game_canvas.configure(background=col(r*16, g*16, b*16))

self.mesh_graph.screen_width = self.ui.game_canvas.winfo_width()
self.mesh_graph.screen_height = self.ui.game_canvas.winfo_height()

Expand Down Expand Up @@ -788,7 +796,7 @@ def clear(self):
self.ui.game_canvas.delete(tkinter.ALL)

def draw_food(self, game_state):
if not self.size_changed:
if not self.size_changed and not self._rainbow:
return
self.ui.game_canvas.delete("food")
self.food_items = {}
Expand All @@ -799,24 +807,31 @@ def draw_food(self, game_state):
self.food_items[position] = food_item

def draw_maze(self, game_state):
if not self.size_changed:
if not self.size_changed and not self._rainbow:
return
self.ui.game_canvas.delete("wall")
# we keep all wall items stored in a list
# some versions of Python seem to forget about drawing
# them otherwise
self.wall_items = []
num = 0
self.t = getattr(self, "t", 0) + 1
if self._rainbow:
self.t = getattr(self, "t", 0) + 1
for wall in game_state['walls']:
model_x, model_y = wall
wall_neighbors = [(dx, dy)
for dx in [-1, 0, 1]
for dy in [-1, 0, 1]
if (model_x + dx, model_y + dy) in game_state['walls']]
wall_item = Wall(self.mesh_graph, wall_neighbors=wall_neighbors, position=(model_x, model_y))
wall_item.draw(self.ui.game_canvas)
self.wall_items.append(wall_item)
num += 1
if wall:
wall_neighbors = [(dx, dy)
for dx in [-1, 0, 1]
for dy in [-1, 0, 1]
if (model_x + dx, model_y + dy) in game_state['walls']]
wall_item = Wall(self.mesh_graph, wall_neighbors=wall_neighbors, position=(model_x, model_y))
if self._rainbow:
wall_item.draw(self.ui.game_canvas, self.t)
else:
wall_item.draw(self.ui.game_canvas)
self.wall_items.append(wall_item)
num += 1

def init_bot_sprites(self, bot_positions):
for sprite in self.bot_sprites.values():
Expand Down
31 changes: 28 additions & 3 deletions pelita/ui/tk_sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,40 @@ def __init__(self, mesh, wall_neighbors=None, **kwargs):


def draw(self, canvas, game_state=None):
scale = (self.mesh.half_scale_x + self.mesh.half_scale_y) * 0.6
scale = (self.mesh.half_scale_x + self.mesh.half_scale_y) * 0.5
if game_state:
wall_col = col(48, 26, 22)

import random
r = random.randint(0, 15)
g = random.randint(0, 15)
b = random.randint(0, 15)
wall_col = col(r*16, g*16, b*16)

def rb_col(idx, width, phase=0):
import math
freq = math.pi * 2 / width
r = math.sin(freq * idx + 2 + phase) * 127 + 128
g = math.sin(freq * idx + 0 + phase) * 127 + 128
b = math.sin(freq * idx + 4 + phase) * 127 + 128
return (int(r), int(g), int(b))

x, y = self.position
if x == 0 or y == 0:
wall_col = col(*rb_col(x + y, 16, game_state))
elif x == self.mesh.mesh_width - 1 or y == self.mesh.mesh_height - 1:
wall_col = col(*rb_col(- x - y, 16, game_state))
else:
wall_col = col(48, 26, 22)

if not ((0, 1) in self.wall_neighbors or
(1, 0) in self.wall_neighbors or
(0, -1) in self.wall_neighbors or
(-1, 0) in self.wall_neighbors):
# if there is no direct neighbour, we can’t connect.
# draw only a small dot.
# TODO add diagonal lines
canvas.create_line(self.screen((-0.3, 0)), self.screen((+0.3, 0)), fill=BROWN,
canvas.create_line(self.screen((-0.3, 0)), self.screen((+0.3, 0)), fill=wall_col,
width=scale, tag=(self.tag, "wall"), capstyle="round")
else:
neighbours = [(-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0)]
Expand All @@ -282,7 +307,7 @@ def draw(self, canvas, game_state=None):
neighbours[(index - 1) % len(neighbours)] in self.wall_neighbors):
pass
else:
canvas.create_line(self.screen((0, 0)), self.screen((2*dx, 2*dy)), fill=BROWN,
canvas.create_line(self.screen((0, 0)), self.screen((2*dx, 2*dy)), fill=wall_col,
width=scale, tag=(self.tag, "wall"), capstyle="round")

# if we are drawing a closed square, fill in the internal part
Expand Down
8 changes: 7 additions & 1 deletion pelita/ui/tk_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def __init__(self, address, controller_address=None, geometry=None, delay=1, sto

self._delay = 2

if os.getenv("PELITA_DOUBLE_RAINBOW", None):
self.rainbow = True
else:
self.rainbow = False

def run(self):
try:
self.root = tkinter.Tk()
Expand All @@ -109,7 +114,8 @@ def run(self):
controller_address=self.controller_address,
geometry=self.geometry,
delay=self.delay,
stop_after=self.stop_after)
stop_after=self.stop_after,
rainbow=self.rainbow)
# schedule next read
self.root.after_idle(self.read_queue)
try:
Expand Down
Loading