-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
344 lines (265 loc) · 9.31 KB
/
main.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
import pygame
import random
from math import ceil
# Game constants
width = 800
height = 600
# Initializing Pygame
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("GameSTONK TO THE MOON Simulator")
# Loading images
icon = pygame.image.load('gamestonk.png')
pygame.display.set_icon(icon)
screen.fill((255,255,255))
#
runInstance = True
run_menu = True
run_game = False
click = False
# Initializing some variables
# Determining general trend of stonks
stonks_go_up = True
chance_of_crashing = 1200
each_frame_increase = 4
# Stock price
stock_price = 10
stocks_owned = 0
cash = 100000
# Slow down increase
stock_inc_counter = 0
# Setting up font
myfont = pygame.font.SysFont("monospace", 50)
gameStopFont = pygame.font.SysFont("Impact", 50)
#Loading images
gameBG = pygame.image.load('gameBG.png')
gameBGPositionX = 0
gameBGPositionY = 0
gameOver = pygame.image.load('gameOver.png')
gameOverPositionX = 0
gameOverPositionY = 0
wsbImg = pygame.image.load('wallstreetbets.png')
wsbPositionX = -50
wsbPositionY = 250
tylerImg = pygame.image.load('tyler.png')
tylerPositionX = wsbPositionX
tylerPositionY = wsbPositionY
start_buttonX = 312
start_buttonY = 315
buttonWidth = 200
buttonHeight = 75
start_button = pygame.transform.scale(pygame.image.load('start.png'), (buttonWidth, buttonHeight))
exit_button = pygame.transform.scale(pygame.image.load('exit.png'), (buttonWidth,buttonHeight))
exit_buttonX = 312
exit_buttonY = 425
menu = pygame.image.load('main_menu.png')
menuPositionX = 0
menuPositionY = 0
restart_button = pygame.image.load('restart.png')
restartX = 312
restartY = 315
# music player
pygame.mixer.music.load('CoffinDance.mp3')
pygame.mixer.music.play(-1)
explosion = False
# For hitmarker sound
hitloaded = False
# For color of text
increasing = True
# Random quote
listOfQuotes = ["HOLD THE LINE",
"GME TO THE MOON", "DIAMOND HANDS", "IF U/DFV HOLDS I HOLD", "I LIKE THIS STOCK", "IF HE'S IN IM IN!"]
selectedQuote = random.choice(listOfQuotes)
# Sounds
hit_channel = 0
hit_sound = pygame.mixer.Sound('hitmarker.ogg')
luck_factor = random.randint(600, 900)
# Resetting game
def reset_game():
pass
#Defining images
def menuImages():
screen.blit(menu, (menuPositionX, menuPositionY))
screen.blit(start_button, (start_buttonX, start_buttonY))
screen.blit(exit_button, (exit_buttonX, exit_buttonY))
def gameImages():
screen.blit(gameBG, (gameBGPositionX, gameBGPositionY))
screen.blit(wsbImg, (wsbPositionX, wsbPositionY))
def gameOverScreen():
global cash, explosion, stocks_owned
screen.blit(gameOver, (gameOverPositionX, gameOverPositionY))
stock_color = (0, 255, 0) if cash >= 1000 else (255, 0, 0)
shareCounter = myfont.render(f'SHARES: {stocks_owned:.2f}', 1, stock_color)
moneyCounter = myfont.render(f'BUYING POWER: {cash:.2f}', 1, stock_color)
share_rect = shareCounter.get_rect(center=((width / 2), (250)))
money_rect = moneyCounter.get_rect(center=((width / 2), (height / 2)))
screen.blit(shareCounter, share_rect)
screen.blit(moneyCounter, money_rect)
if not explosion:
explosion = True
pygame.mixer.music.load('explosion.mp3')
pygame.mixer.music.play()
# For stock graph
stock_history = []
time = 0
# Graph boundaries
graph_x = 310
graph_y = 50
# Maybe if time passes 800, then win
graph_width = 490
graph_height = 550
def scale_price(highest, current):
global graph_y, graph_height
return int(graph_y + (graph_height - ((current / highest) * graph_height)))
def scale_time(highest, current):
global graph_x, graph_width
return round(graph_x + ((current / highest) * graph_width))
def drawGraph():
global screen, stock_history, graph_height, time
step = ceil(len(stock_history) / 300)
for i in range(0, len(stock_history) - step, step):
# Draw line between this point and the next point
stock_color = (0, 255, 0) if increasing else (255, 0, 0)
high = max(stock_history, key=lambda x: x[1])[1]
if high < 100:
high = 100
high_x = time
if high_x < 800:
high_x = 800
x = scale_time(high_x, stock_history[i][0])
x_2 = scale_time(high_x, stock_history[i + step][0])
price = scale_price(high, stock_history[i][1])
price2 = scale_price(high, stock_history[i + step][1])
pygame.draw.line(screen, stock_color, (x, price), (x_2, price2), 3)
playing_tyler = False
def play_tyler():
global playing_tyler, hitloaded
if not playing_tyler:
playing_tyler = True
pygame.mixer.music.load('tyler1 scream.mp3')
hitloaded = False
pygame.mixer.music.play(start = 4.5, loops=-1)
def buy_stock():
global cash, stocks_owned, stock_price
if cash >= stock_price and stock_price > 0:
cash -= stock_price
stocks_owned += 1
def sell_stock():
global cash, stocks_owned, stock_price
if stocks_owned >= 1 and stock_price > 0:
cash += stock_price
stocks_owned -= 1
gameMusicPlaying = False
def gameMusic():
global gameMusicPlaying
if not gameMusicPlaying:
gameMusicPlaying = True
pygame.mixer.music.load('gameMusic.mp3')
pygame.mixer.music.play(-1, start = 48)
def game():
# Calculating stock price
global stock_price, stonks_go_up, stock_inc_counter, screen, increasing, time, stock_history, selectedQuote, luck_factor
gameMusic()
stock_history.append((time, stock_price))
time += 1
# Every half a second
if random.randint(1,luck_factor) == 1:
stonks_go_up = False
# Calling my diamond hand wsb
gameImages()
if stonks_go_up:
if stock_inc_counter == 10:
temp = stock_price
stock_price += each_frame_increase + random.randint(-800, 800)/100
if stock_price > temp:
increasing = True
else:
increasing = False
stock_inc_counter = 0
stock_inc_counter += 1
if stonks_go_up == False:
increasing = False
stock_price -= random.randint(10,40)
if not playing_tyler:
play_tyler()
screen.blit(tylerImg, (tylerPositionX, tylerPositionY))
drawGraph()
if stock_price <= 0:
stock_price = 0
# Show game over screen here
gameOverScreen()
pygame.display.update()
#Graphics
# Display text
# stock_price <= 0 means Game Over
if stock_price > 0:
stock_color = (0, 255, 0) if increasing else (255, 0, 0)
label = myfont.render(f'GME PRICE: {stock_price:.2f}', 1, stock_color)
screen.blit(label, (0, 50))
quotes = myfont.render(f'{selectedQuote}', 1, (0,255,0))
quotes_rect = quotes.get_rect(center=((width / 2), (25)))
screen.blit(quotes, quotes_rect)
stockCounter = myfont.render(f'SHARES: {stocks_owned}', 1, (255,255,255))
screen.blit(stockCounter, (0,100))
moneyCounter = myfont.render(f'BUYING POWER: {cash:.2f}', 1, (255,255,255))
screen.blit(moneyCounter, (0,150))
pygame.display.update()
#Main menu
def menu_run():
global click, runInstance, run_game, run_menu, mx, my, start_button, exit_button, buttonWidth, buttonHeight, hit_channel
# Check main menu displaying
menuImages()
start = pygame.Rect(start_buttonX, start_buttonY, buttonWidth, buttonHeight)
exitb = pygame.Rect(exit_buttonX, exit_buttonY, buttonWidth, buttonHeight)
pygame.display.update()
if start.collidepoint ((mx, my)):
if click:
run_game = True
run_menu = False
pygame.mixer.music.fadeout(1500)
if exitb.collidepoint ((mx, my)):
if click:
pygame.mixer.Channel(hit_channel).play(pygame.mixer.Sound('hitmarker.ogg'))
pygame.mixer.Channel(hit_channel).stop()
runInstance = False
while runInstance:
# 20 Frames per second (1000ms / 50ms == 20)
pygame.time.delay(50)
# Handling events
for event in pygame.event.get():
if event.type == pygame.QUIT:
runInstance = False
if event.type == pygame.MOUSEBUTTONDOWN:
click = True
if event.type == pygame.MOUSEBUTTONUP:
click = False
# Getting mouse position
mx, my = pygame.mouse.get_pos()
# Handling key presses
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
# Buying stonks
if stock_price > 0:
pygame.mixer.Channel(hit_channel).play(hit_sound)
# if not hitloaded and not playing_tyler:
# pygame.mixer.Channel(hit_channel).load('hitmarker.mp3')
# hitloaded = True
# if not playing_tyler:
# pygame.mixer.Channel(hit_channel).play(0)
buy_stock()
if keys[pygame.K_DOWN]:
# Selling stonks
if stock_price > 0:
pygame.mixer.Channel(hit_channel).play(hit_sound)
# if not hitloaded and not playing_tyler:
# pygame.mixer.Channel(hit_channel).play(pygame.mixer.Sound('hitmarker.mp3'))
# hitloaded = True
# if not playing_tyler:
# pygame.mixer.Channel(hit_channel).play(0)
sell_stock()
# Deciding to run menu or game
if run_menu:
menu_run()
elif run_game:
game()
pygame.quit()