boggle.py

Created by 1epauletteshark

Created on December 06, 2023

1.88 KB


from kandinsky import *
from random import *
from time import *
from ion import *

SCREEN_W=320
SCREEN_H=222
FONT_W=10
FONT_H=16
start=0

CELL_SIZE=int((SCREEN_H-22)/4)
gray=color(225,225,225)

DIE=[
  ["S","A","F","P","F","K"],
  ["bB","O","A","B","J","O"],
  ["R","L","Z","H","N","N"],
  ["I","S","S","O","T","E"],
  ["bG","E","E","N","A","A"],
  ["W","E","bG","N","H","E"],
  ["T","bE","R","W","V","H"],
  ["Y","R","D","V","E","L"],
  ["bL","R","E","I","D","X"],
  ["S","H","A","P","O","C"],
  ["Qu","U","M","H","I","N"],
  ["T","Y","E","L","R","T"],
  ["T","I","T","S","Y","D"],
  ["T","bO","A","T","W","O"],
  ["S","U","E","E","I","N"],
  ["T","O","I","M","C","U"]]

def cell(text,x,y,color):
  fill_rect(x,y,CELL_SIZE,CELL_SIZE,"black")
  fill_rect(x+1,y+1,CELL_SIZE-2,CELL_SIZE-2,color)
  draw_string(text,int(x+(CELL_SIZE-FONT_W*len(text))/2),
    int(y+(CELL_SIZE-FONT_H)/2),"black",color)

def shuffle(arr):
  n=len(arr)
  for i in range(n-1,0,-1):
    j=randint(0,i)
    arr[i],arr[j]=arr[j],arr[i]

def board(x,y):
  dice_order=list(range(16))
  shuffle(dice_order)
  for a in range(4):
    for b in range(4):
      dice_index=dice_order.pop(0)
      letter=choice(DIE[dice_index])
      if letter[0]=="b":
        color="green"
        letter=letter[1:]
      else:
        color=gray
      cell(letter,x+CELL_SIZE*b,y+CELL_SIZE*a,color)

def timer(x,y,w,h):
  start=monotonic()
  fill_rect(x,y,w,h,"black")
  fill_rect(x+1,y+1,w-2,h-2,color(255,233,0))
  while monotonic()-start<180:
    fill_rect(x+1,y+1,w-2,int(h/180*(monotonic()-start)),"white")
  fill_rect(x,y,w,h,"black")
  fill_rect(x+1,y+1,w-2,h-2,"red")

def boggle():
  board(int((SCREEN_W-CELL_SIZE*4)/1.25),int((SCREEN_H-CELL_SIZE*4)/2))
  timer(int(((SCREEN_W-CELL_SIZE*4)/1.25)/2-30),int((SCREEN_H-CELL_SIZE*4)/2),
    60,CELL_SIZE*4)
  while monotonic()-start<10:
    pass
  while not keydown(KEY_OK):
    pass
  boggle()

boggle()