-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmastermind.py
More file actions
220 lines (153 loc) · 7.52 KB
/
mastermind.py
File metadata and controls
220 lines (153 loc) · 7.52 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
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
from sys import exit
import pygame
import pickle
import datetime
from buttonhandle import button
pygame.init()
#-----setup stuff-----#
class rownum(pygame.sprite.Sprite): #Class for the info numbers that are displayed on the side.
def __init__(self, row : int, nums : list):
self.row = row
self.nums = nums
pygame.sprite.Sprite.__init__(self)
def update(self, window, font):
textct = font.render(self.nums[0], True, (10, 10, 10))
textinp = font.render(self.nums[1], True, (255, 50, 50))
window.blit(textct, pygame.rect.Rect(410, 40 * self.row + 10, 20, 20))
window.blit(textinp, pygame.rect.Rect(220, 40 * self.row + 10, 20, 20))
#from stackoverflow, https://stackoverflow.com/a/58541111/17493432
def fadeout(screen : pygame.display, clock : pygame.time.Clock):
fadeout = pygame.Surface((640, 480))
fadeout = fadeout.convert()
fadeout.fill((56, 56, 56))
for i in range(60):
clock.tick(60)
fadeout.set_alpha(i)
screen.blit(fadeout, (0, 0))
pygame.display.update()
class dot(pygame.sprite.Sprite):
def __init__(self, position, colorset, window):
self.color = 0
self.position = position
self.colorset = colorset
self.window = window
pygame.sprite.Sprite.__init__(self)
self.boundrect = pygame.draw.circle(window, self.colorset[self.color], (40 * self.position[0] + 260, 40 * self.position[1] + 20), 10)
def update(self):
pygame.draw.circle(self.window, (0, 0, 0), (40 * self.position[0] + 260, 40 * self.position[1] + 20), 12, width=2)
pygame.draw.circle(self.window, self.colorset[self.color], (40 * self.position[0] + 260, 40 * self.position[1] + 20), 10)
def clicked(self, colorind):
self.color = colorind #We make the game itself calculate the color index to simplify events call structure
def mainmaster(screen: pygame.display, clock: pygame.time.Clock, rowmax: int, combo : list, colorset : tuple):
'''The main base function for the game
screen is the screen things will be displayed on, clock is a pygame clock for FPS limiting,
rowmax is the num of rows with max of 11(to make it harder),
combo is the games final combo (set in main for possible future networking),
and colorset is the colors loaded from the settings.conf file'''
dots = pygame.sprite.Group()
rownums = pygame.sprite.Group()
gamefont = pygame.font.Font(None, 40)
time = datetime.datetime.now() #used in game saving
row = 0
running = True
guess = [0, 0, 0, 0]
mbu = False
win = False
loss = False
#these just create static screen elements
for i in range(0, rowmax):
for x in range(0, 4):
dots.add(dot((x, i), colorset, screen))
guessbutton = button((480, 400, 130, 50), (0, 80, 0), "Guess", 0, (502, 410))
mainbutton = button((235, 215, 170, 50), (252, 252, 80), "Main Menu", 0, (245, 225))
#-----setup stuff-----#
while running:
#-----mainloop-----#
clock.tick(60)
mouse = pygame.mouse.get_pos()
for i in pygame.event.get(): #Process all events one by one. Each event has a type which we do things with
#Each type has attributes, which we can furthur use to our advantage
match i.type:
case pygame.QUIT:
running = False
pygame.quit
exit()
case pygame.MOUSEBUTTONUP:
mbu = True
for j in dots:
#this just creates the hitboxes for the dots then checks if they were clicked
if j.boundrect.collidepoint(mouse) and (j.position[1]) == row:
x = j.position[0]
if i.button == 1 or i.button == 4: #Checks if it was right click or scroll
if guess[x] != 7:
guess[x] += 1
else:
guess[x] = 0
else:
if guess[x] != 0:
guess[x] -= 1
else:
guess[x] = 7
j.clicked(guess[x])
#time to draw stuff on the screen!
screen.fill((56, 56, 56))
pygame.draw.rect(screen, (10, 10, 10), (240, 0, 160, 480))
dots.update()
rownums.update(screen, gamefont)
if not win and not loss:
if guessbutton.update(screen, mouse, mbu, gamefont):
#this deals with if the player presses the guess button
#so here there is a bug where the game wants to say you have all 4 colors on the row if you put one color and that color happens to be in the combo
#im getting around this by replacing the already "gone to" list items with an impossible (to the game at least) number, 8. if i did list.remove it just broke
#thats what combocopy is for
#aside from that weird fix this is really just simple code for checking if an item is in a list
colorthere = 0
inpos = 0
combocopy = combo.copy()
for i in range(0, 4):
if guess[i] in combo:
colorthere += 1
comboind = combo.index(guess[i])
if combo[comboind] == guess[comboind]:
inpos += 1
combo[comboind] = 8
combo = combocopy.copy()
strct = str(colorthere) #convert to a string for display
strinp = str(inpos)
classnums = [strct, strinp]
rownums.add(rownum(row, classnums))
if guess == combo:
fadeout(screen, clock)
win = True
if row >= rowmax - 1:
fadeout(screen, clock)
loss = True
row += 1
guess = [0, 0, 0, 0]
if win:
screen.fill((56, 56, 56))
wintext = gamefont.render("You did it!", True, (10, 10, 10))
wintextpos = wintext.get_rect(centerx=screen.get_width() / 2, y=10)
screen.blit(wintext, wintextpos)
elif loss and not win:
screen.fill((56, 56, 56))
losetext = gamefont.render("You lost", True, (10, 10, 10))
losetextpos = losetext.get_rect(centerx=screen.get_width() / 2, y=10)
screen.blit(losetext, losetextpos)
if (win or loss) and mainbutton.update(screen, mouse, mbu, gamefont): #check if main menu button was pressed and then exit
running = False
with open("pastgames.dat", "ab") as file:
data = []
tempdata = []
for i in rownums:
tempdata.append(i.nums)
data.append(tempdata) #adding to the array which will contain all our data for this game
tempdata = []
for x in dots:
tempdata.append(x.color)
data.append(tempdata)
data.append(combo)
data.append((time.year, time.month, time.day, time.hour, time.minute))
pickle.dump(data, file) #Pickle allows us to use plain binary data for the file
mbu = False
pygame.display.update()