-import pygame
-
from setup import *
from tile import Tile
class Board:
- def __init__(self,position, length, width):
+ def __init__(self, position, length, width, letters):
self.position = pygame.Vector2(position)
self.length = length
self.width = width
- self.board = [[Tile((self.position.x + i * Tile.side_length,self.position.y + j * Tile.side_length)) for j in range(self.width)][:] for i in range(self.length)]
+ self.board = [[Tile((self.position.x + col * Tile.side_length, self.position.y + row * Tile.side_length),
+ letters[row * self.width + col]) for col in range(self.width)][:] for row in range(self.length)]
self.tiles = []
for i in range(self.length):
letter = ""
for tile in self.tiles:
if self.last_selected is None:
-
- l = tile.update(delta,tile.neighbors)
+ l = tile.update(delta, tile.neighbors)
else:
- l = tile.update(delta,self.last_selected.neighbors)
+ l = tile.update(delta, self.last_selected.neighbors)
if l != "":
letter += l
self.last_selected = tile
-
- if not pygame.mouse.get_pressed(3)[0]: # mouse not down
+ if not pygame.mouse.get_pressed(3)[0]: # mouse not down
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)))
+ 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)
-
-import pygame
-
from setup import *
from tile import Tile
from board import Board
+
class Game:
font = pygame.font.Font("font/Silkscreen-Regular.ttf", 40)
- def __init__(self):
- self.board = Board(center - pygame.Vector2(2 * Tile.side_length),4,4)
+ def __init__(self, imported_file_name=None):
+ if imported_file_name is None:
+ length, width = 4,4
+ self.letters = [random.choice(alphabet) for i in range(length * width)]
+ else:
+ with open(imported_file_name,"r") as f:
+ file_contents = f.read().split("\n")
+ length, width = tuple(map(int,file_contents[0].split()))
+ self.letters = file_contents[1]
+
+ 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.word_display = self.font.render(self.word, True, Colour.BLACK, Colour.LIGHT_GRAY)
self.seen = set()
def update(self, delta):
self.word += self.board.update(delta)
- if not pygame.mouse.get_pressed(3)[0]: # mouse not down
+ if not pygame.mouse.get_pressed(3)[0]: # mouse not down
if self.word in word_list:
self.seen.add(self.word)
self.word = ""
elif self.word in word_list:
bg_colour = Colour.GREEN
- self.word_display = self.font.render(self.word,True,Colour.BLACK,bg_colour)
-
+ self.word_display = self.font.render(self.word, True, Colour.BLACK, bg_colour)
def draw(self, surf):
self.board.draw(surf)
- surf.blit(self.word_display, self.word_display.get_rect(center=(center.x,SCREEN_HEIGHT/5)))
+ surf.blit(self.word_display, self.word_display.get_rect(center=(center.x, SCREEN_HEIGHT / 5)))
from game import Game
-scene = Game()
+scene = Game("puzzles/templates/puzzle.txt")
is_running = True
delta = 1000/fps
--- /dev/null
+4 4
+abcdefghijklmnop
\ No newline at end of file
class Tile:
side_length = 80
bezel = 5
- cutoff = 7.5
+ cutoff = 8
font = pygame.font.Font("font/Silkscreen-Regular.ttf", 20)
- def __init__(self, position):
+ def __init__(self, position, letter):
self.position = pygame.Vector2(position)
self.selected = False
- self.letter = random.choice(alphabet)
+ self.letter = letter #random.choice(alphabet)
self.text = self.font.render(self.letter,True, Colour.BLACK)
self.neighbors = []
for i in range(-1,2):