-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword.py
More file actions
80 lines (67 loc) · 3.24 KB
/
word.py
File metadata and controls
80 lines (67 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import pygame
import json
import random
from game_settings import GameSettings
class WordSpell():
"""A class to represent a word for player to spell."""
def __init__(self, game_session):
"""Initialize a random word (model) and establish its position inside colored squares."""
self.screen = game_session.screen
self.screen_rect = game_session.screen.get_rect()
self.settings = game_session.settings
def get_random_word(self):
with open('word_pull.json', encoding='utf-8') as input_data:
text_in_file = json.load(input_data)
word_length = self.settings.number_letters
possible_words = text_in_file[str(word_length)]
word_to_spell = random.choice(possible_words).upper()
return word_to_spell
def get_letters(self, word):
letters = []
for letter in word:
letters.append(letter)
print(letters)
return letters
def create_word_pane(self, letter_index, starting_x):
# Drawing border
color = self.settings.border_color
square_y = self.settings.starting_y
square_x = 20 + starting_x*(letter_index)
dimension = self.settings.square_dimension
border = pygame.Rect(square_x, square_y, dimension, dimension)
pygame.draw.rect(self.screen, color, border)
# Drawing squares
color = self.settings.square_color
square_y = self.settings.starting_y + self.settings.border
square_x = 20 + starting_x*(letter_index) + self.settings.border
dimension = self.settings.square_dimension - 2*self.settings.border
self.word_square = pygame.Rect(square_x, square_y, dimension, dimension)
pygame.draw.rect(self.screen, color, self.word_square)
def draw_letter_in_square(self, word, letter_index):
# Drawing each letter inside a square
text_color = self.settings.text_color
self.font = pygame.font.SysFont('Arial', 50)
self.letter_image = self.font.render(word[letter_index], True, text_color)
self.letter_image_rect = self.letter_image.get_rect()
self.letter_image_rect.center = self.word_square.center
self.screen.blit(self.letter_image, self.letter_image_rect)
def create_empty_word_pane(self, letter_index):
# Drawing border
color = self.settings.border_color
square_y = self.settings.starting_y
dimension = self.settings.square_dimension
square_x = self.screen_rect.width - 80 - (dimension + 2)*(self.settings.number_letters - letter_index)
border = pygame.Rect(square_x, square_y, dimension, dimension)
pygame.draw.rect(self.screen, color, border)
# Drawing squares
color = self.settings.square_color
square_y = self.settings.starting_y + self.settings.border
square_x = square_x + self.settings.border
dimension = dimension - 2*self.settings.border
self.word_square = pygame.Rect(square_x, square_y, dimension, dimension)
pygame.draw.rect(self.screen, color, self.word_square)
def create_placeholder_word(self):
player_word_placeholder = []
for i in range(self.settings.number_letters):
player_word_placeholder.append('*')
return player_word_placeholder