-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoemoflife.py
356 lines (309 loc) · 12.7 KB
/
poemoflife.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import pygame
import random
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Poem of Life')
clock = pygame.time.Clock()
# some Variables
crashed = False
pause = True
size = 25
roll = False
DONE1 = False
# colors used
back = (246, 249, 229)
forg = (250, 227, 227)
darkback = (136, 57, 85)
boardclr = (217, 166, 183)
boardclr2 = (194, 112, 139)
pla1clr = (4, 150, 255)
pla2clr = (216, 17, 89)
def generate_poem(words_list):
# Define the number of words for each line
lines_lengths = [4, 5, 6]
# Initialize an index to keep track of the current position in the words_list
index = 0
# Create a list to hold the poem's lines
poem_lines = []
# Iterate over the desired lengths for each line
for length in lines_lengths:
# Check if there are enough words remaining in the words_list
if index + length <= len(words_list):
# Select the words for the current line and join them
line = " ".join(words_list[index:index+length])
# Add the line to the poem_lines list
poem_lines.append(line)
# Update the index to skip the words already used
index += length
else:
# If there are not enough words left, break the loop
break
# Join the poem_lines to form the complete poem
poem = "\n".join(poem_lines)
return poem
# after Win
def WINNER():
global pla1_poem, pla2_poem
# Generate poems for both players
pla1_poem = generate_poem(pla1.collected_words)
pla2_poem = generate_poem(pla2.collected_words)
# Determine the winner based on the turn
if turn == 1:
winner_name = "Player 2"
winner_poem = pla2_poem
winner_clr = pla2.clr
elif turn == 2:
winner_name = "Player 1"
winner_poem = pla1_poem
winner_clr = pla1.clr
# Display the winner and their poem on the screen
pygame.draw.rect(gameDisplay, (back), (95, 95, 610, 410))
pygame.draw.rect(gameDisplay, (darkback), (100, 100, 600, 400))
pygame.draw.rect(gameDisplay, (back), (105, 105, 590, 390))
smallText = pygame.font.SysFont("comicsansms", 90)
textSurf, textRect = text_objects("GAME OVER", smallText, darkback)
textRect.center = ((display_width // 2), (display_height // 2 - 100))
gameDisplay.blit(textSurf, textRect)
smallText = pygame.font.SysFont("comicsansms", 50)
textSurf, textRect = text_objects(f"WINNER: {winner_name}", smallText, darkback)
textRect.center = ((display_width // 2), (display_height // 2))
gameDisplay.blit(textSurf, textRect)
# Display the winner's poem
smallText = pygame.font.SysFont("comicsansms", 20)
lines = winner_poem.split('\n')
for i, line in enumerate(lines):
textSurf, textRect = text_objects(line, smallText, winner_clr)
textRect.center = ((display_width // 2), (display_height // 2 + i * 30 + 50))
gameDisplay.blit(textSurf, textRect)
smallText1 = pygame.font.SysFont("comicsansms", 20)
textSurf, textRect = text_objects("Hit Space to Play Again", smallText1, darkback)
textRect.center = ((700), (580))
gameDisplay.blit(textSurf, textRect)
temp = True
while temp:
global crashed, DONE1
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
temp = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
gameDisplay.fill(back)
b.__init__()
pla1.__init__(b, pla1clr)
pla2.__init__(b, pla2clr)
temp = False
DONE1 = False
pygame.display.update()
# game Board Class
class board:
def __init__(self):
# board List
self.boardarr = []
count = 1
self.words = self.load_words() # Load words from the text file
# Inside the __init__ method of the board class
for i in range(0, 10):
temp = []
for j in range(0, 10):
x = j * 60
y = i * 60
if random.randint(1, 100) <= 5: # 5% chance for a wildcard
word = "WILDCARD"
else:
word = random.choice(self.words)
temp.append((x, y, count, word))
count += 1
self.boardarr.append(temp)
def load_words(self):
with open('words.txt', 'r') as f:
words = f.readlines()
return [word.strip() for word in words]
def draw(self):
# Inside the draw method of the board class
for i in self.boardarr:
for j in i:
if int(j[2]) % 2 == 0:
colorb = boardclr
else:
colorb = boardclr2
pygame.draw.rect(gameDisplay, (colorb), (j[0], j[1], 59, 59))
# Adjust font size based on word length
if len(j[3]) > 8:
smallText = pygame.font.SysFont("comicsansms", 8)
display_word = j[3][:7] + "..."
else:
smallText = pygame.font.SysFont("comicsansms", 10)
display_word = j[3]
textSurf, textRect = text_objects(display_word, smallText, darkback)
textRect.center = ((j[0] + (60 // 2)), (j[1] + (60 // 2)))
gameDisplay.blit(textSurf, textRect)
# player class
class player:
def __init__(self, B, clr):
self.load_words()
self.val = 100
self.xpos = None
self.ypos = None
self.barr = B.boardarr
self.clr = clr
self.size = random.randint(15, 25)
self.collected_words = [] # Initialize an empty list to store collected words
for x in self.barr:
for y in x:
if self.val == y[2]:
self.xpos = y[0]
self.ypos = y[1]
def load_words(self):
with open('words.txt', 'r') as f:
self.words = f.readlines()
self.words = [word.strip() for word in self.words]
def move(self, no):
if self.val - no > 0:
self.val -= no
else:
print("You can not move")
for x in self.barr:
for y in x:
if self.val == y[2]:
self.xpos = y[0]
self.ypos = y[1]
if y[3] == "WILDCARD":
action = random.choice(["ADD", "REMOVE"])
if action == "ADD":
added_words = []
for _ in range(3):
word = random.choice(self.words)
self.collected_words.append(word)
added_words.append(word)
# Display message for added words
self.display_message("WILDCARD: Extra words added")
elif action == "REMOVE" and self.collected_words:
if len(self.collected_words) >= 1:
self.collected_words.pop() # Remove the last collected word
self.display_message("WILDCARD: Word removed")
else:
self.collected_words.append(y[3])# Add the word of the current tile to collected_words
print(f"Appended word: {y[3]}")
if self.val == 1:
print("+=+" * 10 + " YOU WIN " + "+=+" * 10)
global DONE1
DONE1 = True
def display_message(self, message):
# Display the message at the bottom right corner
smallText = pygame.font.SysFont("comicsansms", 15)
textSurf, textRect = text_objects(message, smallText, darkback)
textRect.bottomright = (display_width - 10, display_height - 10)
gameDisplay.blit(textSurf, textRect)
pygame.display.update()
def draw(self):
pygame.draw.circle(gameDisplay, (self.clr), (self.xpos + 30, self.ypos + 30), self.size)
def text_objects(text, font, clr, *kward):
if len(kward) > 0:
textSurface = font.render(text, True, clr, kward[0])
else:
textSurface = font.render(text, True, clr)
return textSurface, textSurface.get_rect()
# functions for dice
def one():
x, y, w, h = 605, 50, 190, 190
pygame.draw.rect(gameDisplay, darkback, (x, y, w, h))
pygame.draw.circle(gameDisplay, forg, ((x + (w // 2)), (y + (h // 2))), size)
def two():
x, y, w, h = 605, 50, 190, 190
pygame.draw.rect(gameDisplay, darkback, (x, y, w, h))
pygame.draw.circle(gameDisplay, forg, ((x + (w // 4)), (y + (h // 2))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (3 * w // 4)), (y + (h // 2))), size)
def three():
x, y, w, h = 605, 50, 190, 190
pygame.draw.rect(gameDisplay, darkback, (x, y, w, h))
pygame.draw.circle(gameDisplay, forg, ((x + (w // 4)), (y + (3 * h // 4))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (w // 2)), (y + (h // 2))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (3 * w // 4)), (y + (h // 4))), size)
def four():
x, y, w, h = 605, 50, 190, 190
pygame.draw.rect(gameDisplay, darkback, (x, y, w, h))
pygame.draw.circle(gameDisplay, forg, ((x + (w // 4)), (y + (h // 4))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (w // 4)), (y + (3 * h // 4))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (3 * w // 4)), (y + (h // 4))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (3 * w // 4)), (y + (3 * h // 4))), size)
def five():
x, y, w, h = 605, 50, 190, 190
pygame.draw.rect(gameDisplay, darkback, (x, y, w, h))
pygame.draw.circle(gameDisplay, forg, ((x + (w // 2)), (y + (h // 2))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (w // 4)), (y + (h // 4))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (w // 4)), (y + (3 * h // 4))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (3 * w // 4)), (y + (h // 4))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (3 * w // 4)), (y + (3 * h // 4))), size)
def six():
x, y, w, h = 605, 50, 190, 190
pygame.draw.rect(gameDisplay, darkback, (x, y, w, h))
pygame.draw.circle(gameDisplay, forg, ((x + (w // 4)), (y + (h // 2))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (3 * w // 4)), (y + (h // 2))), size)
pygame.draw.circle(gameDisplay, forg, ((x + (w // 4)), (y + (h // 4)) - 10), size)
pygame.draw.circle(gameDisplay, forg, ((x + (w // 4)), (y + (3 * h // 4)) + 10), size)
pygame.draw.circle(gameDisplay, forg, ((x + (3 * w // 4)), (y + (h // 4)) - 10), size)
pygame.draw.circle(gameDisplay, forg, ((x + (3 * w // 4)), (y + (3 * h // 4)) + 10), size)
# pygame Start
pygame.init()
b = board()
pla1 = player(b, (4, 150, 255))
pla2 = player(b, (216, 17, 89))
turn = 1
gameDisplay.fill(back)
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if roll == True:
roll = False
else:
roll = True
if not DONE1:
b.draw()
pla1.draw()
pla2.draw()
smallText = pygame.font.SysFont("comicsansms", 40)
textSurf, textRect = text_objects(str("Turn"), smallText, darkback, back)
textRect.center = ((700), (300))
gameDisplay.blit(textSurf, textRect)
smallText1 = pygame.font.SysFont("comicsansms", 20)
textSurf, textRect = text_objects("Hit Space to Play", smallText1, darkback, back)
textRect.center = ((700), (500))
gameDisplay.blit(textSurf, textRect)
if roll:
time = random.randint(5, 25)
for i in range(time):
no = random.randint(1, 6)
if no == 1:
one()
elif no == 2:
two()
elif no == 3:
three()
elif no == 4:
four()
elif no == 5:
five()
elif no == 6:
six()
pygame.time.wait(100)
pygame.display.update()
roll = False
if turn == 1:
pla1.move(no)
turn = 2
pygame.draw.circle(gameDisplay, pla2.clr, (700, 400), 50)
elif turn == 2:
pla2.move(no)
turn = 1
pygame.draw.circle(gameDisplay, pla1.clr, (700, 400), 50)
else:
WINNER()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()