-
Notifications
You must be signed in to change notification settings - Fork 94
/
startScreen.py
267 lines (225 loc) · 8.76 KB
/
startScreen.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
import pygame
import os
import tkinter as tk
from tkinter import messagebox
import sys
pygame.init()
win = pygame.display.set_mode((1080, 600))
title = pygame.image.load(os.path.join('img', 'title.png'))
back = pygame.image.load(os.path.join('img', 'back.png'))
course = pygame.image.load(os.path.join('img', 'course1.png'))
course1 = pygame.transform.scale(course, (200, 200))
font = pygame.font.SysFont('comicsansms', 24)
buttons = [[1080/2 - course1.get_width()/2, 260, course1.get_width(), course1.get_height(), 'Grassy Land']]
shopButton = []
ballObjects = []
surfaces = []
class ball():
def __init__(self, color, locked, org):
self.color = color
self.locked = locked
self.original = org
self.price = 10
self.equipped = False
self.font = pygame.font.SysFont('comicsansms', 22)
def unlock(self):
file = open('scores.txt', 'r')
f = file.readlines()
file.close()
file = open('scores.txt', 'w')
for line in f:
if line.find(self.original) != -1:
file.write(self.original + '-' + 'True\n')
else:
file.write(line)
self.locked = False
def getLocked(self):
return self.locked
def equip(self):
self.equipped = True
def getEquip(self):
return self.equipped
def getSurf(self, hover=False):
surf = pygame.Surface((160, 125), pygame.SRCALPHA, 32)
surf = surf.convert_alpha()
#surf.fill((255,255,255))
pygame.draw.circle(surf, (0,0,0), (round(surf.get_width()/2), 25), 22)
pygame.draw.circle(surf, self.color, (round(surf.get_width()/2), 25), 20)
if self.locked == True:
label = self.font.render('Price: 10', 1, (0,0,0))
if hover:
buy = self.font.render('Purchase?', 1, (64,64,64))
else:
buy = self.font.render('Purchase?', 1, (0,0,0))
surf.blit(label, (round(surf.get_width()/2 - label.get_width()/2), 50))
surf.blit(buy, (round(surf.get_width()/2 - label.get_width()/2), 80))
else:
label = self.font.render('Unlocked', 1, (0,0,0))
if self.equipped == False:
buy = self.font.render('Equip', 1, (0,0,0))
surf.blit(buy, (round(surf.get_width() / 2 - buy.get_width() / 2), 80))
else:
buy = self.font.render('Equipped', 1, (0,0,0))
surf.blit(buy, (round(surf.get_width() / 2 - buy.get_width() / 2), 80))
surf.blit(label, (round(surf.get_width()/2 - label.get_width()/2), 50))
pygame.display.update()
return surf
def getBest():
file = open('scores.txt', 'r')
for line in file:
l = line.split()
if l[0] == 'score':
file.close()
return l[1].strip()
return 0
file.close()
def getCoins():
file = open('scores.txt', 'r')
for line in file:
l = line.split()
if l[0] == 'coins':
file.close()
return l[1].strip()
def drawShop(pos=None, click=False):
global ballObjects
pygame.time.delay(20)
if pos != None:
c = 0
for i in surfaces:
if pos[0] > i[0] and pos[0] < i[0] + i[2]:
if pos[1] > i[1] + 80 and pos[1] < i[1] + i[3]:
if click == True:
root = tk.Tk()
root.attributes("-topmost", True)
root.withdraw()
if ballObjects[c].locked == True:
if messagebox.askyesno('Confirm Purchase?', 'Are you sure you would like to purchase this new ball for 10 coins?'):
if int(getCoins()) >= 10:
ballObjects[c].unlock()
oldCoins = int(getCoins())
file = open('scores.txt', 'r')
f = file.readlines()
file = open('scores.txt', 'w')
for line in f:
l = line.split()
if l[0] == 'coins':
file.write('coins ' + str(oldCoins - 10)+ '\n')
else:
file.write(line)
file.close()
else:
messagebox.showerror('Not enough coins!', 'You do not have enough coins to purchase this item!')
try:
root.destroy()
break
except:
break
else:
break
else:
for balls in ballObjects:
balls.equipped = False
ballObjects[c].equip()
ballObjects[c].equipped = True
c = c + 1
surf = pygame.Surface((1080, 600))
surf.blit(back,(0,0))
backButton = font.render('<-- Back', 1, (135,206,250))
surf.blit(backButton, (10, 560))
text = font.render('Coins: ' + getCoins(), 1, (51,51,153))
surf.blit(text, (10, 10))
count = 0
c = 0
xVal = 0
file = open('scores.txt', 'r')
for line in file:
if line.find('True') != -1 or line.find('False') != -1:
count += 1
l = line.split('-')
color = l[0]
color = color.split(',')
newList = []
for num in color:
newList.append(int(num))
if len(ballObjects) <= 15:
if l[1].strip() == 'True':
obj = ball(tuple(newList), False, l[0])
else:
obj = ball(tuple(newList), True, l[0])
if len(ballObjects) == 0:
obj.equip()
else:
obj = ballObjects[c]
s = obj.getSurf()
surf.blit(s, ((200 * count) - 150, 50 + (xVal * 160)))
surfaces.append([(200 * count) - 150, 50 + (xVal * 160), 160, 125])
ballObjects.append(obj)
if count % 5 == 0:
xVal = xVal + 1
count = 0
c = c + 1
file.close()
pygame.display.update()
return surf
def getBallColor():
global ballObjects
for balls in ballObjects:
if balls.equipped == True:
return balls.color
return None
def mainScreen(hover=False):
global shopButton
surf = pygame.Surface((1080, 600))
w = title.get_width()
h = title.get_height()
surf.blit(back, (0,0))
surf.blit(title, ((1080/2 - (w/2)), 50))
# For Shop Button
if hover == True:
text = font.render('Ball Shop', 1,(0, 0, 0))
else:
text = font.render('Ball Shop', 1, (51, 51, 153))
surf.blit(text, (960, 12))
shopButton = text.get_rect()
shopButton[0] = 960
shopButton[1] = 12
# For course Button
i = buttons[0]
surf.blit(course1, (i[0], i[1]))
text = font.render(i[4], 1, (51,51,153))
surf.blit(text, (i[0] + ((i[3] - text.get_width())/2), i[1] + i[3] + 10))
text = font.render('Best: ' + getBest(), 1, (51, 51, 153))
surf.blit(text, (i[0] + ((i[3] - text.get_width())/2), i[1] + i[3] + 40))
text = font.render('Coins: ' + getCoins(), 1, (51,51,153))
surf.blit(text, (10, 10))
win.blit(surf, (0,0))
pygame.display.update()
def mouseOver(larger=False):
global course1
if larger:
buttons[0][0] = 415
buttons[0][1] = 220
buttons[0][2] = 250
buttons[0][3] = 250
course1 = pygame.transform.scale(course, (250, 250))
else:
buttons[0][1] = 240
buttons[0][0] = 440
buttons[0][2] = 200
buttons[0][3] = 200
course1 = pygame.transform.scale(course, (200, 200))
mainScreen()
def shopClick(pos):
global shopButton
i = shopButton
if pos[0] > i[0] and pos[0] < i[0] + i[2]:
if pos[1] > i[1] and pos[1] < i[1] + i[3]:
return True
return False
def click(pos):
for i in buttons:
if pos[0] > i[0] and pos[0] < i[0] + i[2]:
if pos[1] > i[1] and pos[1] < i[1] + i[3]:
return i[4]
break
return None