from tile import Tile
-class Board:
+class Grid:
- def __init__(self, position, length, width, letters):
+ def __init__(self, position, length, height, letters):
self.position = pygame.Vector2(position)
self.length = length
- self.width = width
- self.board = [[Tile((self.position.x + col * (Tile.side_length+Tile.separation_distance), self.position.y + row * (Tile.side_length+Tile.separation_distance)),
- letters[row * self.width + col]) for col in range(self.width)][:] for row in range(self.length)]
+ self.height = height
+ self.board = [[Tile((self.position.x + col * (Tile.side_length + Tile.separation_distance),
+ self.position.y + row * (Tile.side_length + Tile.separation_distance)),
+ letters[row * self.height + col]) for col in range(self.height)][:] for row in
+ range(self.length)]
self.tiles = []
for i in range(self.length):
- for j in range(self.width):
+ for j in range(self.height):
self.tiles.append(self.board[i][j])
self.last_selected = None
- def update(self, delta):
+ def update(self, delta, colour):
letter = ""
for tile in self.tiles:
if self.last_selected is None:
- l = tile.update(delta, tile.neighbors)
+ l = tile.update(delta, tile.neighbors, colour)
else:
- l = tile.update(delta, self.last_selected.neighbors)
+ l = tile.update(delta, self.last_selected.neighbors, colour)
if l != "":
letter += l
self.last_selected = tile
for tile in self.tiles:
tile.reset()
self.last_selected = None
-
return letter
def draw(self, surf):
- # pygame.draw.rect(surf, Colour.BLACK, pygame.Rect(self.position - pygame.Vector2(Tile.bezel / 2), (
- # self.length * Tile.side_length + Tile.bezel, self.width * Tile.side_length + Tile.bezel)))
for tile in self.tiles:
tile.draw(surf)
# debug
# if self.last_selected is not None:
# for i in self.last_selected.neighbors:
# pygame.draw.rect(surf,Colour.BLUE,pygame.Rect(i,(Tile.side_length,Tile.side_length)),3)
+
+
+class Board:
+ text_font = pygame.font.Font("font/Silkscreen-Regular.ttf", 20)
+
+ inset = 15
+ edge_radius = 25
+ border_width = 8
+
+ def __init__(self, position, length, height, letters):
+ self.points = 0
+ self.points_display = self.text_font.render(f"Score: {self.points}", True, Colour.BLACK)
+ # position is the center of the grid then calculated to find topleft
+ self.length = length * Tile.side_length + (length - 1) * Tile.separation_distance
+ self.height = height * Tile.side_length + (
+ height - 1) * Tile.separation_distance + self.points_display.get_rect().height
+ self.position = pygame.Vector2(position) - pygame.Vector2(self.length / 2, self.height / 2)
+
+ self.grid = Grid(self.position + pygame.Vector2(0, self.points_display.get_rect().height), length,
+ height, letters)
+
+ def update(self, delta, colour, points):
+ letter = self.grid.update(delta, colour)
+
+ self.points = points
+ self.points_display = self.text_font.render(f"Score: {self.points}", True, Colour.BLACK)
+
+ return letter
+
+ def get_rect(self):
+ return pygame.Rect(round(self.position.x - self.inset),
+ round(self.position.y - self.inset),
+ round(self.inset * 2 + self.length),
+ round(self.inset * 2 + self.height))
+
+ def draw(self, surf):
+ r = self.get_rect()
+
+ pygame.draw.rect(surf, Palette.primary_shade2, r, border_radius=self.edge_radius)
+ pygame.draw.rect(surf, Palette.secondary_shade4, r, border_radius=self.edge_radius, width=self.border_width)
+
+ self.grid.draw(surf)
+
+ surf.blit(self.points_display, self.points_display.get_rect(
+ bottomleft=self.grid.position + pygame.Vector2(0, -Tile.separation_distance)))
+
+ # print(self.position+pygame.Vector2(2 * Tile.side_length + Tile.separation_distance * 3/ 2), center)
+ #
+ # pygame.draw.circle(surf, Colour.RED, self.grid.position, 3)
+ # pygame.draw.circle(surf, Colour.RED,
+ # self.grid.position + pygame.Vector2(2 * Tile.side_length + Tile.separation_distance * 3 / 2),
+ # 3)
+ # pygame.draw.circle(surf, Colour.BLUE, self.get_rect().topleft, 4)
+ # pygame.draw.circle(surf, Colour.BLUE, self.get_rect().center, 4)
--- /dev/null
+import pygame
+
+from setup import *
+from tile import Tile
+
+class WordConnector:
+ edge_radius= 4
+ thickness = 15
+
+ # hl = pygame.Rect(0,0,Tile.separation_distance + Tile.side_length,thickness)
+ # surface = pygame.Surface(hl.size)
+ # pygame.draw.rect(surface,Colour.RED,hl,border_radius=edge_radius)
+ # horizontal_line = surface.copy()
+ #
+ # vl= pygame.Rect(0,0,thickness,Tile.separation_distance + Tile.side_length)
+ # surface = pygame.Surface(vl.size)
+ # pygame.draw.rect(surface,Colour.RED,vl,border_radius=edge_radius)
+ # vertical_line = surface.copy()
+ #
+ # dl = pygame.Rect(0,0,math.sqrt(2 * (Tile.separation_distance + Tile.side_length) ** 2),thickness)
+ # surface = pygame.Surface(dl.size)
+ # pygame.draw.rect(surface,Colour.RED,dl,border_radius=edge_radius)
+ # diagonal_line = pygame.transform.rotate(surface,45)
+
+
+ def __init__(self):
+ self.order = []
+ self.colour = Colour.RED
+
+ def reset(self):
+ self.order = []
+ self.colour = Colour.RED
+ def update(self, tile, status):
+ if tile is not None and tile.position + pygame.Vector2(tile.side_length/2) not in self.order:
+ self.order.append(tile.position + pygame.Vector2(tile.side_length/2))
+ if status:
+ self.colour = Palette.primary_shade3
+ else:
+ self.colour = Colour.GRAY
+
+ def draw(self, surf):
+ for i,pos in enumerate(self.order):
+ if i == len(self.order) - 1:
+ break
+
+ difference = pygame.Vector2(self.order[i+1])- pygame.Vector2(pos)
+ # difference.x /= Tile.side_length
+ # difference.y /= Tile.side_length
+
+ if difference.y == 0:
+ l = Tile.separation_distance + Tile.side_length + 2 * self.edge_radius
+ angle= 0
+ elif difference.x == 0:
+ l = Tile.separation_distance + Tile.side_length+ 2 * self.edge_radius
+ angle= 90
+ else:
+ l=math.sqrt(2 * (Tile.separation_distance + Tile.side_length) ** 2)+ 2 * self.edge_radius
+ angle = math.degrees(math.atan(difference.y/difference.x)) + 90
+
+ line = pygame.Rect(0,0,l,self.thickness)
+ surface = pygame.Surface(line.size).convert_alpha()
+ surface.fill((0,0,0,0))
+ pygame.draw.rect(surface,self.colour,line,border_radius=self.edge_radius)
+ line = pygame.transform.rotate(surface,angle)
+ line.set_alpha(150)
+ surf.blit(line,line.get_rect(center = pygame.Vector2(pos) - 0.5 * (pygame.Vector2(pos) - pygame.Vector2(self.order[i+1]))))
+
-import pygame
+import pygame.draw
from setup import *
from tile import Tile
from board import Board
+from effects import WordConnector
class Game:
font = pygame.font.Font("font/Silkscreen-Regular.ttf", 40)
- text_font = pygame.font.Font("font/Silkscreen-Regular.ttf", 20)
title_font = pygame.font.Font("font/Silkscreen-Regular.ttf", 75)
- inset = 15
- edge_radius = 25
- border_width = 8
+
+ correct_colour = Colour.GREEN
+ found_colour = Colour.YELLOW
+ wrong_colour = Colour.LIGHT_GRAY
+
+ colour_converter = {correct_colour: True,
+ found_colour: False,
+ wrong_colour: False}
+
def __init__(self, imported_file_name=None):
if imported_file_name is None:
length, width = 4, 4
if "" in self.word_list:
self.word_list.remove("")
print(self.word_list)
-
- self.board = Board(center - pygame.Vector2(2 * Tile.side_length), length, width, self.letters)
-
- self.word = ""
- self.word_display = self.font.render(self.word, True, Colour.BLACK, Colour.LIGHT_GRAY)
self.seen = set()
self.points = 0
- self.points_display = self.text_font.render(f"Score: {self.points}", True, Colour.BLACK)
self.title = self.title_font.render("Word Hunt", True, Colour.BLACK)
+ self.board = Board(pygame.Vector2(SCREEN_WIDTH / 3, SCREEN_HEIGHT / 2), length, width, self.letters)
+ self.word = ""
+ self.word_display = self.font.render(self.word, True, Colour.BLACK, Colour.LIGHT_GRAY)
+
+ self.word_connector = WordConnector()
+ self.bg_colour = Colour.LIGHT_GRAY
def update(self, delta):
- self.word += self.board.update(delta)
+ self.word += self.board.update(delta, self.bg_colour, self.points)
+ self.word_connector.update(self.board.grid.last_selected, self.colour_converter[self.bg_colour])
if not pygame.mouse.get_pressed(3)[0]: # mouse not down
if self.word in word_list and self.word not in self.seen:
self.seen.add(self.word)
- self.points += 10 ** (len(self.word)-2)
- self.points_display = self.text_font.render(f"Score: {self.points}", True, Colour.BLACK)
-
+ # self.points += 10 ** (len(self.word) - 2)
+ if len(self.word) == 3:
+ self.points += 100
+ else:
+ self.points += 100 * 2 ** (len(self.word) - 2)
+ # self.points_display = self.text_font.render(f"Score: {self.points}", True, Colour.BLACK)
self.word = ""
- bg_colour = Colour.LIGHT_GRAY
+ self.word_connector.reset()
+ self.bg_colour = self.wrong_colour
if self.word in self.seen:
- bg_colour = Colour.YELLOW
+ self.bg_colour = self.found_colour
elif self.word in word_list:
- bg_colour = Colour.GREEN
+ self.bg_colour = self.correct_colour
- self.word_display = self.font.render(self.word, True, Colour.BLACK, bg_colour)
+ self.word_display = self.font.render(self.word, True, Colour.BLACK, self.bg_colour)
def draw(self, surf):
surf.fill(Palette.primary_shade1)
- pygame.draw.rect(surf, Palette.primary_shade2, pygame.Rect((self.board.position.x - self.inset, self.board.position.y - self.points_display.get_rect().height - self.inset), (self.inset *2+ self.board.length * (Tile.side_length + Tile.separation_distance) - Tile.separation_distance, self.inset*2 +self.points_display.get_rect().height+ self.board.width * (Tile.side_length + Tile.separation_distance)- Tile.separation_distance)),border_radius=self.edge_radius)
- pygame.draw.rect(surf, Palette.secondary_shade0, pygame.Rect((self.board.position.x - self.inset, self.board.position.y - self.points_display.get_rect().height - self.inset), (self.inset *2+ self.board.length * (Tile.side_length + Tile.separation_distance)- Tile.separation_distance, self.inset*2 +self.points_display.get_rect().height+ self.board.width * (Tile.side_length + Tile.separation_distance)- Tile.separation_distance)),border_radius=self.edge_radius, width=self.border_width)
self.board.draw(surf)
surf.blit(self.word_display, self.word_display.get_rect(center=(center.x, SCREEN_HEIGHT / 5)))
surf.blit(self.title, self.title.get_rect(center=(center.x, SCREEN_HEIGHT / 10)))
- surf.blit(self.points_display, self.points_display.get_rect(bottomleft=self.board.position + pygame.Vector2(0,-Tile.separation_distance)))
+
+ self.word_connector.draw(surf)
+
+ # pygame.draw.circle(surf, Colour.WHITE,r.center,5)
+ # pygame.draw.line(surf, Colour.BLACK, (0, SCREEN_HEIGHT / 2), (SCREEN_WIDTH, SCREEN_HEIGHT / 2))
+ # pygame.draw.line(surf, Colour.BLACK, (SCREEN_WIDTH / 2, 0), (SCREEN_WIDTH / 2, SCREEN_HEIGHT))
+ # for i, shade in enumerate(Palette.shades):
+ # pygame.draw.rect(surf,shade,pygame.Rect(0,i*50,50,50))
import pygame
import random
+import math
# word list from https://github.com/dwyl/english-words
with open("pruned_words.json", 'r') as f:
class Colour:
WHITE = (255, 255, 255)
GRAY = (255 / 2, 255 / 2, 255 / 2)
+ DARK_GRAY = (255 / 3, 255 / 3, 255 / 3)
LIGHT_GRAY = (255 * 2 / 3, 255 * 2 / 3, 255 * 2 / 3)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
- YELLOW = (255 / 2, 255 / 2, 0)
+ YELLOW = (255,255,77)
class Palette:
tertiary_shade2 = (176, 88, 131)
tertiary_shade3 = (106, 18, 61)
tertiary_shade4 = (88, 5, 46)
+
+ shades = [primary_shade1,primary_shade2,primary_shade0,primary_shade3,primary_shade4,
+ secondary_shade1,secondary_shade2,secondary_shade0,secondary_shade3,secondary_shade4,
+ tertiary_shade1,tertiary_shade2,tertiary_shade0,tertiary_shade3,tertiary_shade4]
edge_radius = 15
separation_distance = 7.5
- font = pygame.font.Font("font/Silkscreen-Regular.ttf", 20)
+ font = pygame.font.Font("font/Silkscreen-Regular.ttf", 40)
+
+ shrink_amount =2.5
def __init__(self, position, letter):
self.selected = False
self.letter = letter #random.choice(alphabet)
self.text = self.font.render(self.letter,True, Colour.BLACK)
+ self.colour = Palette.secondary_shade2
self.neighbors = []
for i in range(-1,2):
for j in range(-1,2):
self.neighbors.append(self.position + pygame.Vector2(i * (self.side_length + self.separation_distance), j * (self.side_length+ self.separation_distance)))
- def update(self, delta, neighbors):
+ def update(self, delta, neighbors, colour):
if not self.selected and pygame.mouse.get_pressed()[0] and self.get_collision_rect().collidepoint(pygame.mouse.get_pos()) and self.position in neighbors:
self.selected = True
return self.letter
+ if self.selected:
+ self.colour = colour
return ""
def reset(self):
self.selected = False
+ self.colour = Palette.secondary_shade2
def get_rect(self):
- return pygame.Rect(self.position,(self.side_length, self.side_length))
+ if self.selected:
+ return pygame.Rect(self.position + pygame.Vector2(self.shrink_amount),(self.side_length - 2 * self.shrink_amount, self.side_length - 2 * self.shrink_amount))
+ else:
+ return pygame.Rect(self.position,(self.side_length, self.side_length))
def get_collision_rect(self):
- return pygame.Rect(self.position + pygame.Vector2(self.cutoff), pygame.Vector2(self.side_length) - pygame.Vector2(2 * self.cutoff))
+ if self.selected:
+ return pygame.Rect(self.position + pygame.Vector2(self.cutoff) + pygame.Vector2(self.shrink_amount), pygame.Vector2(self.side_length) - pygame.Vector2(2 * self.cutoff) - pygame.Vector2(2 * self.shrink_amount))
+ else:
+ return pygame.Rect(self.position + pygame.Vector2(self.cutoff), pygame.Vector2(self.side_length) - pygame.Vector2(2 * self.cutoff))
def draw(self, surf):
if self.selected:
- pygame.draw.rect(surf, Palette.secondary_shade2, self.get_rect(),border_radius=self.edge_radius)
+ pygame.draw.rect(surf, self.colour, self.get_rect(),border_radius=self.edge_radius)
else:
- pygame.draw.rect(surf, Palette.secondary_shade0, self.get_rect(),border_radius=self.edge_radius)
- pygame.draw.rect(surf,Palette.tertiary_shade4,self.get_rect(),self.bezel,border_radius=self.edge_radius)
+ pygame.draw.rect(surf, Palette.secondary_shade1, self.get_rect(),border_radius=self.edge_radius)
+ pygame.draw.rect(surf,Palette.secondary_shade3,self.get_rect(),self.bezel,border_radius=self.edge_radius)
# debug
- # pygame.draw.rect(surf, Colour.RED, self.get_collision_rect())
+ # pygame.draw.rect(surf, Colour.RED, self.get_collision_rect(), width=5)
# letter
surf.blit(self.text,self.text.get_rect(center = (self.position.x + self.side_length/2, self.position.y + self.side_length/2)))