]> Skullheadx's Git Forge - word-hunt.git/commitdiff
tile, board, font, word making
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 21 Nov 2022 20:41:24 +0000 (15:41 -0500)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 21 Nov 2022 20:41:24 +0000 (15:41 -0500)
12 files changed:
.idea/misc.xml
.idea/word-hunt.iml
Main/__pycache__/board.cpython-310.pyc [new file with mode: 0644]
Main/__pycache__/game.cpython-310.pyc [new file with mode: 0644]
Main/__pycache__/setup.cpython-310.pyc
Main/__pycache__/tile.cpython-310.pyc [new file with mode: 0644]
Main/board.py [new file with mode: 0644]
Main/font/Silkscreen-Regular.ttf [new file with mode: 0644]
Main/game.py [new file with mode: 0644]
Main/main.py
Main/setup.py
Main/tile.py [new file with mode: 0644]

index a4652f355e26c43a58fbff0f10cd661868d00976..d8a3dd862ab749b9d47a3524c1bf032024a561e9 100644 (file)
@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (venv)" project-jdk-type="Python SDK" />
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (word-hunt)" project-jdk-type="Python SDK" />
 </project>
\ No newline at end of file
index 68d1b4db39dbbdccd861fb8c83731c7d46ba2147..bfd0b6b900b3bf282452a6aed344f7ab5757429f 100644 (file)
@@ -3,8 +3,9 @@
   <component name="NewModuleRootManager">
     <content url="file://$MODULE_DIR$">
       <excludeFolder url="file://$MODULE_DIR$/Main/venv" />
+      <excludeFolder url="file://$MODULE_DIR$/venv" />
     </content>
-    <orderEntry type="jdk" jdkName="Python 3.10 (venv)" jdkType="Python SDK" />
+    <orderEntry type="jdk" jdkName="Python 3.10 (word-hunt)" jdkType="Python SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
 </module>
\ No newline at end of file
diff --git a/Main/__pycache__/board.cpython-310.pyc b/Main/__pycache__/board.cpython-310.pyc
new file mode 100644 (file)
index 0000000..67a9d2c
Binary files /dev/null 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
new file mode 100644 (file)
index 0000000..2b5d001
Binary files /dev/null and b/Main/__pycache__/game.cpython-310.pyc differ
index f71793c2e6dad9fa53ffd32c863f970689d1c7db..68b60281cdfb6fb1f9f8404bc312e1f339d996b5 100644 (file)
Binary files a/Main/__pycache__/setup.cpython-310.pyc and b/Main/__pycache__/setup.cpython-310.pyc differ
diff --git a/Main/__pycache__/tile.cpython-310.pyc b/Main/__pycache__/tile.cpython-310.pyc
new file mode 100644 (file)
index 0000000..cb06862
Binary files /dev/null and b/Main/__pycache__/tile.cpython-310.pyc differ
diff --git a/Main/board.py b/Main/board.py
new file mode 100644 (file)
index 0000000..1c2c042
--- /dev/null
@@ -0,0 +1,49 @@
+import pygame
+
+from setup import *
+from tile import Tile
+
+
+class Board:
+
+    def __init__(self,position, length, width):
+        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.tiles = []
+        for i in range(self.length):
+            for j in range(self.width):
+                self.tiles.append(self.board[i][j])
+        self.last_selected = None
+
+    def update(self, delta):
+        letter = ""
+        for tile in self.tiles:
+            if self.last_selected is None:
+
+                l = tile.update(delta,tile.neighbors)
+            else:
+                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
+            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)
+
diff --git a/Main/font/Silkscreen-Regular.ttf b/Main/font/Silkscreen-Regular.ttf
new file mode 100644 (file)
index 0000000..e087e17
Binary files /dev/null and b/Main/font/Silkscreen-Regular.ttf differ
diff --git a/Main/game.py b/Main/game.py
new file mode 100644 (file)
index 0000000..2f3b535
--- /dev/null
@@ -0,0 +1,25 @@
+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)
+        self.word = ""
+        self.word_display = self.font.render(self.word,True,Colour.BLACK,Colour.LIGHT_GRAY)
+
+    def update(self, delta):
+        self.word += self.board.update(delta)
+        if not pygame.mouse.get_pressed(3)[0]: # mouse not down
+            self.word = ""
+
+        self.word_display = self.font.render(self.word,True,Colour.BLACK,Colour.LIGHT_GRAY)
+
+
+    def draw(self, surf):
+        self.board.draw(surf)
+        surf.blit(self.word_display, self.word_display.get_rect(center=(center.x,SCREEN_HEIGHT/5)))
index 8bc2939d797b84ab696aab139390c3a11a74da25..beee3c603060af13944e4d993db02f355066952c 100644 (file)
@@ -1,13 +1,22 @@
+import pygame.time
+
 from setup import *
+from game import Game
+
 
+scene = Game()
 
 is_running = True
+delta = 1000/fps
 
 while is_running:
     screen.fill(Colour.WHITE)
 
-
     if pygame.event.peek(pygame.QUIT):
         is_running = False
 
+    scene.update(delta)
+    scene.draw(screen)
+
     pygame.display.update()
+    delta = clock.tick(fps)
\ No newline at end of file
index dc7b6549c8ebf1ca9a803563855b03708934dcbd..a04d5c7db8cd178fb39ac555eb57b8ae2686b6fa 100644 (file)
@@ -1,4 +1,6 @@
 import json
+import string
+
 import pygame
 import random
 
@@ -9,13 +11,22 @@ with open("pruned_words.json", 'r') as f:
 
 
 pygame.init()
-SCREEN_WIDTH, SCREEN_HEIGHT = 720, 720
-dimensions = pygame.Vector2(SCREEN_WIDTH, SCREEN_HEIGHT)
-center = pygame.Vector2(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
-screen = pygame.display.set_mode((0,0), pygame.RESIZABLE)
+SCREEN_WIDTH, SCREEN_HEIGHT = 720, 720
+dimensions = pygame.Vector2(SCREEN_WIDTH, SCREEN_HEIGHT)
+center = pygame.Vector2(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
+screen = pygame.display.set_mode(dimensions, pygame.RESIZABLE)
 
 pygame.display.set_caption("Word Hunt")
+clock = pygame.time.Clock()
+fps = 60
 
+alphabet = string.ascii_lowercase
 
 class Colour:
     WHITE = (255,255,255)
+    GRAY = (255/2,255/2,255/2)
+    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)
diff --git a/Main/tile.py b/Main/tile.py
new file mode 100644 (file)
index 0000000..82cd3de
--- /dev/null
@@ -0,0 +1,47 @@
+import pygame.mouse
+
+from setup import *
+
+class Tile:
+    side_length = 80
+    bezel = 5
+    cutoff = 7.5
+    font = pygame.font.Font("font/Silkscreen-Regular.ttf", 20)
+
+    def __init__(self, position):
+        self.position = pygame.Vector2(position)
+        self.selected = False
+        self.letter = random.choice(alphabet)
+        self.text = self.font.render(self.letter,True, Colour.BLACK)
+        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, j * self.side_length))
+
+    def update(self, delta, neighbors):
+        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
+        return ""
+
+    def reset(self):
+        self.selected = False
+
+    def get_rect(self):
+        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))
+
+    def draw(self, surf):
+        if self.selected:
+            pygame.draw.rect(surf, Colour.LIGHT_GRAY, self.get_rect())
+        else:
+            pygame.draw.rect(surf, Colour.GRAY, self.get_rect())
+        pygame.draw.rect(surf,Colour.BLACK,self.get_rect(),self.bezel)
+
+        # debug
+        # pygame.draw.rect(surf, Colour.RED, self.get_collision_rect())
+
+        # letter
+        surf.blit(self.text,self.text.get_rect(center = (self.position.x + self.side_length/2, self.position.y + self.side_length/2)))