From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Mon, 21 Nov 2022 21:10:04 +0000 (-0500) Subject: import puzzle X-Git-Url: http://git.skullheadx.com/tech/index.html?a=commitdiff_plain;h=43ef67be161766d85300e4fbcdfbb54bffb43ebe;p=word-hunt.git import puzzle --- diff --git a/Main/__pycache__/board.cpython-310.pyc b/Main/__pycache__/board.cpython-310.pyc index 67a9d2c..f25d6f4 100644 Binary files a/Main/__pycache__/board.cpython-310.pyc and b/Main/__pycache__/board.cpython-310.pyc differ diff --git a/Main/__pycache__/game.cpython-310.pyc b/Main/__pycache__/game.cpython-310.pyc index 260740c..fc0e4e0 100644 Binary files a/Main/__pycache__/game.cpython-310.pyc and b/Main/__pycache__/game.cpython-310.pyc differ diff --git a/Main/__pycache__/tile.cpython-310.pyc b/Main/__pycache__/tile.cpython-310.pyc index cb06862..450e962 100644 Binary files a/Main/__pycache__/tile.cpython-310.pyc and b/Main/__pycache__/tile.cpython-310.pyc differ diff --git a/Main/board.py b/Main/board.py index 1c2c042..89f5f8b 100644 --- a/Main/board.py +++ b/Main/board.py @@ -1,16 +1,15 @@ -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): @@ -22,16 +21,14 @@ class Board: 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 @@ -39,11 +36,11 @@ class Board: 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) - diff --git a/Main/game.py b/Main/game.py index 9f8e11e..712ab32 100644 --- a/Main/game.py +++ b/Main/game.py @@ -1,21 +1,30 @@ -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 = "" @@ -26,9 +35,8 @@ class Game: 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))) diff --git a/Main/main.py b/Main/main.py index beee3c6..3c0238c 100644 --- a/Main/main.py +++ b/Main/main.py @@ -4,7 +4,7 @@ from setup import * from game import Game -scene = Game() +scene = Game("puzzles/templates/puzzle.txt") is_running = True delta = 1000/fps diff --git a/Main/puzzle_generator.py b/Main/puzzle_generator.py new file mode 100644 index 0000000..e69de29 diff --git a/Main/puzzles/templates/puzzle.txt b/Main/puzzles/templates/puzzle.txt new file mode 100644 index 0000000..14c84b0 --- /dev/null +++ b/Main/puzzles/templates/puzzle.txt @@ -0,0 +1,2 @@ +4 4 +abcdefghijklmnop \ No newline at end of file diff --git a/Main/tile.py b/Main/tile.py index 82cd3de..fb21c0a 100644 --- a/Main/tile.py +++ b/Main/tile.py @@ -5,13 +5,13 @@ from setup import * 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):