repl.it
@Inevitable/

CashPanda

Pygame

One Man's Trash is a Trash Panda's Cash

fork
loading
Files
  • main.py
  • APC.png
  • api.py
  • arrow_small.png
  • arrow.png
  • BAN.png
  • BAT.png
  • bin.png
  • blogpost.py
  • blue_carpet.png
  • bookshelf.png
  • BTC.png
  • calendar.png
  • carpet_tile_green.png
  • cat.py
  • CFC.png
  • chaching.ogg
  • chart_bg.png
  • chart.png
  • chef.png
  • choice_panel.png
  • coin.png
  • cork_board.png
  • cursor_long.png
  • cursor_main.png
  • cursor_small.png
  • cursor.py
  • desk.png
  • dog.png
  • FBO.png
  • gamestate.py
  • Graph-35-pix.ttf
  • keyboard.ogg
  • market.py
  • menus.py
  • NEW.png
  • nohup.out
  • notification.ogg
  • PBG.png
  • POP.png
  • retrieved.ogg
  • RZA.png
  • SFPixelate.ttf
  • shadow.png
  • shine.png
  • SOC.png
  • star.png
  • submit.png
  • SYR.png
  • tilemap.py
  • trash_panda_back.png
  • trash_panda_front.png
  • trash_panda_right.png
  • trophy.png
  • tutorial_panel.png
  • ui_elements.py
  • utils.py
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import pygame
import pygame.locals
import string
from cat import Cat
from tilemap import Map
from cursor import *
from menus import *
from ui_elements import *
from market import item_order, Account
from blogpost import PromotePanel
from gamestate import GameState
from api import *
import time

# pygame.mixer.init()
pygame.init()


class Game:
    DISPLAY_WIDTH = 800
    DISPLAY_HEIGHT = 600
    GROUND_COLOUR = (110, 95, 88)
    MS_PER_CYCLE = 5000
    STATE_CYCLE = 50
    MS_BETWEEN_SAVES = 80000

    def __init__(self):
        self.clock = pygame.time.Clock()
        self.gameDisplay = pygame.display.set_mode((Game.DISPLAY_WIDTH, Game.DISPLAY_HEIGHT))
        pygame.display.set_caption('Trash Tycoon')
        self.logged_in = False

        self.char_sprites = pygame.sprite.Group()
        self.cat = Cat()
        self.BIG_FONT = pygame.font.Font('SFPixelate.ttf', 60)
        self.authentication = None

        self.char_sprites.add(self.cat)
        self.bg_sprites = pygame.sprite.Group()
        self.map_ = Map(self.bg_sprites)
        # self.keyboard_sound = pygame.mixer.Sound('keyboard.ogg')

        self.game_state = GameState()

        self.ui_sprites = pygame.sprite.Group()
        self.main_cursor = MainCursor()

        self.announcements = Announcements()
        self.selected = None
        self.new_game()

    def new_game(self):
        self.cycle = 0
        self.state_cycle = 0
        self.main_menu = True
        self.sub_menu = False
        self.buy_sell_menu = False
        self.going_up = False
        self.going_down = False
        self.going_up_ctr = 0
        self.going_down_ctr = 0
        self.gamestate_in_progress = False
        self.action_in_progress = None
        self.last_divable = False
        self.last_refreshed_blog = False

        self.INVALID_CHAR = set(string.punctuation
            .replace("_", "")
            .replace("-", "")
            .replace("\"", "")
            .replace("'", "")
            .replace("?", "")
            .replace("!", ""))

        self.MarketMenu = MarketMenu()
        self.account = Account(0.0, self.MarketMenu.market)
        self.account_view = AccountView(self.account, self.MarketMenu.market)
        self.tutorial = Tutorial(self.account)

        self.BlogMenu = BlogMenu(self.MarketMenu.market)
        self.blog_display = BlogDisplay()
        self.promote_panel = PromotePanel()

        self.BuySellMenu = BuySellMenu(self.account, self.MarketMenu.market, self.announcements, self.tutorial, self.BlogMenu)
        self.graph = Chart()

        self.TrashMenu = TrashMenu(self.account, self.tutorial, self.MarketMenu.market, self.announcements)

        self.ui_sprites.add(self.BlogMenu)
        self.ui_sprites.add(self.promote_panel)
        self.ui_sprites.add(self.MarketMenu)

        self.set_trigger_states()

    def start(self):
        print(chr(27) + "[2J")
        self.playing = True
        # game loop
        while self.playing:
            self.dt = self.clock.tick(60) / 1000
            self.get_input()
            self.update()
            self.draw()

    def set_trigger_states(self):
        self.first_dive_complete = False
        self.first_opened_market = False
        self.first_ordered_sale = False
        self.first_posted_blog = False
        self.first_sale_finished = False

    def get_input(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.playing = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    self.go_left()
                elif event.key == pygame.K_RIGHT:
                    self.go_right()
                if event.key == pygame.K_UP:
                    self.go_up()
                elif event.key == pygame.K_DOWN:
                    self.go_down()
                if event.key == pygame.K_SPACE:
                    self.select()
                if event.key == pygame.K_ESCAPE:
                    self.escape()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_RIGHT:
                    self.going_up = False
                if event.key == pygame.K_DOWN or event.key == pygame.K_LEFT:
                    self.going_down = False

    def update(self):
        self.check_trigger_states()
        ticks = pygame.time.get_ticks()
        self.tutorial.update()

        self.MarketMenu.update()
        item_idx = item_order[self.MarketMenu.scroll_pos]
        self.graph.set_item(item_idx, self.MarketMenu.market.items[item_idx].history)
        self.graph.update()
        self.account_view.set_item(item_idx)
        self.account_view.update()
        if 'hunger' in self.game_state.controller['states']:
            if self.game_state.controller['states']['hunger'] > 2001. and self.account.has_trash():
                self.eat_trash()

        if ticks > self.cycle * Game.STATE_CYCLE:
            self.cycle = ticks // Game.STATE_CYCLE + 1
            self.gamestate_update()
            if self.cycle % (Game.MS_PER_CYCLE // Game.STATE_CYCLE) == 0:
                self.periodic_update()
            if self.cycle % (Game.MS_BETWEEN_SAVES // Game.STATE_CYCLE) == 10:
                self.save_game_data()

        if self.MarketMenu.updated:
            self.MarketMenu.updated = False
            # self.announcements.announce("Market prices updated.")

        self.map_.update(self.game_state.controller['unlocked'])
        if not self.tutorial.tutorial_mode:
            self.cat.update(self.dt)
            self.main_cursor.update()
            self.promote_panel.update()
            if self.going_up:
                if self.going_up_ctr == 0 or self.going_up_ctr > 10:
                    self.BuySellMenu.scroll(1)
                self.going_up_ctr += 1
            else:
                self.going_down_ctr = 0
            if self.going_down:
                if self.going_down_ctr == 0 or self.going_down_ctr > 10:
                    self.BuySellMenu.scroll(-1)
                self.going_down_ctr += 1
            else:
                self.going_down_ctr = 0
            self.announcements.update()

        self.TrashMenu.update()
        if self.TrashMenu.divable and not self.last_divable:
            self.bg_sprites.add(self.map_.bin_shine)
        elif not self.TrashMenu.divable and self.last_divable:
            self.bg_sprites.remove(self.map_.bin_shine)

        self.last_divable = self.TrashMenu.divable

        self.BuySellMenu.update()
        self.BuySellMenu.set_item(item_idx)
        self.BuySellMenu.check_max_val()

        headline = self.BlogMenu.headline_topics[self.BlogMenu.scroll_pos]
        title = self.BlogMenu.text_list[self.BlogMenu.scroll_pos]
        self.BuySellMenu.set_blogpost(headline, title)

        self.BlogMenu.update()
        refresh_blog = self.BlogMenu.check_refresh_blogs()
        if refresh_blog and not self.last_refreshed_blog:
            self.bg_sprites.add(self.map_.submit)
        self.last_refreshed_blog = refresh_blog
        self.blog_display.update()

    def periodic_update(self):
        self.MarketMenu.check_new_prices()
        self.blog_display.get_blogposts()

    def gamestate_update(self):
        if self.gamestate_in_progress:
            return
        if not self.gamestate_in_progress and self.action_in_progress is not None:
            if self.action_in_progress[1] == 'T' or self.action_in_progress[0] in ['SALE_complete', 'STARTING']:
                self.game_state.update_tutorial(self.action_in_progress[0])
            if self.action_in_progress[1] == 'C':
                self.update_state(self.action_in_progress[0], self.action_in_progress[2])
            self.action_in_progress = None
        todo = self.game_state.update_gamestate()
        if todo is not None:
            action_name, action = todo
            if action is not None:
                action_type, value = action
                if action_type == 'T':
                    self.tutorial.start_tutorial(value)
                elif action_type == 'C':
                    if action_name == 'protection':
                        if self.account.cash > 2000.:
                            value = (value[0], value[1] + (("Hire him as guard", [2000., "self.game_state.controller['unlocked']['dog'] = True; del self.game_state.controller['states']['protection']"], "'Oh, aight. How d'ya know I need a job?'"),))
                    self.tutorial.start_choice(value)
                self.action_in_progress = (action_name, action_type, value)
                self.gamestate_in_progress = True

    def update_state(self, state_name, choices):
        choice_pos = self.tutorial.choice_pos
        choice = choices[1][choice_pos]
        for action in choice[1]:
            if type(action) == int or type(action) == float:
                self.account.cash -= action
                if action > 0. and state_name in self.game_state.controller['states']:
                    self.game_state.controller['states'][state_name] = -action * 100 - 2000
                self.announcements.announce("You spent ${}".format(action))
            elif type(action) == str:
                exec(action)

    def check_trigger_states(self):
        if len(self.game_state.tutorial_list) > 0:
            # After first time trash diving, suggest selling
            if not self.first_dive_complete and self.TrashMenu.have_dived:
                self.game_state.update_tutorial('FIRST_TIME_sell')
                self.first_dive_complete = True
            # Trigger when opening market, tutorial
            if not self.first_opened_market and self.sub_menu and self.selected == self.MarketMenu:
                self.game_state.update_tutorial('FIRST_TIME_market')
                self.first_opened_market = True
            # Trigger after first sell order, suggest blog
            if not self.first_ordered_sale and self.BuySellMenu.sold:
                self.game_state.update_tutorial('FIRST_TIME_blog')
                self.first_ordered_sale = True
            # After first blogpost, tutorial about reading posts
            if not self.first_posted_blog and self.BuySellMenu.blog_posted:
                self.game_state.update_tutorial('POST_complete')
                if len(self.BuySellMenu.orders) > 0:
                    self.BuySellMenu.orders[0]['delivery_time'] = 0
                self.first_posted_blog = True
            # After making first sale, suggest buy
            if not self.first_sale_finished and self.BuySellMenu.sale_complete:
                self.game_state.update_tutorial('SALE_complete')
                self.first_sale_finished = True

    def draw(self):
        self.gameDisplay.fill(Game.GROUND_COLOUR)
        self.bg_sprites.draw(self.gameDisplay)
        self.char_sprites.draw(self.gameDisplay)
        self.main_cursor.draw(self.gameDisplay)
        self.ui_sprites.draw(self.gameDisplay)
        self.MarketMenu.draw_text(self.gameDisplay)
        self.graph.draw(self.gameDisplay)
        self.account_view.draw(self.gameDisplay)
        self.BlogMenu.draw_text(self.gameDisplay)
        self.blog_display.draw(self.gameDisplay)
        self.BuySellMenu.draw(self.gameDisplay)
        if self.sub_menu and self.selected == self.MarketMenu:
            self.MarketMenu.draw(self.gameDisplay)
        self.announcements.draw(self.gameDisplay)
        self.tutorial.draw(self.gameDisplay)
        pygame.display.update()

    def go_left(self):
        if self.tutorial.tutorial_mode: return
        if self.buy_sell_menu:
            self.going_down = True
        elif self.sub_menu:
            if self.selected == self.MarketMenu:
                self.MarketMenu.toggle_buy_sell()
        elif self.main_menu:
            self.cat.move_x = -1
            self.cat.move_y = 0
            self.main_cursor.selectTrash()
            self.selected = self.TrashMenu

    def go_right(self):
        if self.tutorial.tutorial_mode: return
        if self.buy_sell_menu:
            self.going_up = True
        elif self.sub_menu:
            if self.selected == self.MarketMenu:
                self.MarketMenu.toggle_buy_sell()
        elif self.main_menu:
            self.cat.move_x = 1
            self.cat.move_y = 0
            self.main_cursor.selectNotepad()
            self.selected = self.MarketMenu

    def go_up(self):
        if self.tutorial.tutorial_mode: return
        if self.tutorial.choice_mode:
            self.tutorial.scroll(-1)
            return
        if self.main_menu:
            self.cat.move_y = -1
            self.cat.move_x = 0
            self.main_cursor.selectDesktop()
            self.selected = self.BlogMenu
        elif self.sub_menu:
            self.selected.scroll(-1)
            self.BuySellMenu.fix_value()
        elif self.buy_sell_menu:
            self.going_up = True

    def go_down(self):
        if self.tutorial.tutorial_mode: return
        if self.tutorial.choice_mode:
            self.tutorial.scroll(1)
            return
        if self.main_menu:
            self.cat.move_y = 1
            self.cat.move_x = 0
            self.main_cursor.selectNone()
            self.selected = None
        elif self.sub_menu:
            self.selected.scroll(1)
            self.BuySellMenu.fix_value()
        elif self.buy_sell_menu:
            self.going_down = True

    def select(self):
        if self.tutorial.tutorial_mode:
            self.tutorial.end_tutorial()
            self.gamestate_in_progress = False
            return
        if self.tutorial.choice_mode:
            self.tutorial.choose()
            self.gamestate_in_progress = False
            return
        if self.buy_sell_menu:
            order = self.BuySellMenu.execute_order()
            self.buy_sell_menu = False
            self.BuySellMenu.hide()
            self.sub_menu = True
            if self.BuySellMenu.mode == 'promote':
                self.BlogMenu.hide()
                self.promote_panel.hide()
                self.blog_display.show()
                self.BlogMenu.last_blogged = time.time()
                self.BlogMenu.posting = False
                self.bg_sprites.remove(self.map_.submit)
            if self.BuySellMenu.value == 0 and self.BuySellMenu.mode != 'promote':
                return
            self.announcements.announce(order)
        elif self.sub_menu:
            if self.selected == self.BlogMenu and self.BlogMenu.posting:
                self.buy_sell_menu = True
                self.sub_menu = False
                self.BuySellMenu.show()
                self.BuySellMenu.mode = 'promote'
            elif self.selected == self.MarketMenu:
                if self.MarketMenu.buy:
                    self.buy()
                else:
                    self.sell()
        elif self.main_menu and self.selected:
            self.main_menu = False
            self.sub_menu = True
            if self.selected == self.MarketMenu:
                self.selected.show()
                self.graph.show()
                self.account_view.show()
            elif self.selected == self.BlogMenu:
                if 'internet' in self.game_state.controller['states']and self.game_state.controller['states']['internet'] > 4000.:
                    self.tutorial.start_tutorial("Your internet connection is down.")
                    self.main_menu = True
                    self.sub_menu = False
                    return
                if self.BlogMenu.posting:
                    self.selected.show()
                    self.promote_panel.show()
                    # self.keyboard_sound.play()
                else:
                    self.blog_display.show()
            # try:
            #     self.selected.new_headlines()
            # except:
            #     pass
            elif self.selected == self.TrashMenu:
                self.selected.show()
                self.sub_menu = False
                self.main_menu = True

    def escape(self):
        if self.tutorial.tutorial_mode:
            self.tutorial.end_tutorial()
            self.gamestate_in_progress = False
            return
        if self.tutorial.choice_mode:
            return
        if self.buy_sell_menu:
            self.buy_sell_menu = False
            self.BuySellMenu.hide()
            self.sub_menu = True
            self.buying = self.selling = False
        elif self.sub_menu:
            self.main_menu = True
            self.sub_menu = False
            self.selected.hide()
            if self.selected == self.MarketMenu:
                self.graph.hide()
                self.account_view.hide()
            elif self.selected == self.BlogMenu:
                self.promote_panel.hide()
                self.blog_display.hide()

    def buy(self):
        if self.sub_menu and self.selected == self.MarketMenu:
            self.buy_sell_menu = True
            self.BuySellMenu.show()
            self.BuySellMenu.mode = "buy"
            self.sub_menu = False

    def sell(self):
        if self.sub_menu and self.selected == self.MarketMenu:
            self.buy_sell_menu = True
            self.BuySellMenu.show()
            self.BuySellMenu.mode = "sell"
            self.sub_menu = False
    
    def auth(self):
        self.logged_in = False
        print(">>> SIGN UP / LOG IN HERE <<<")
        print("\033[1;32;31m [DISCLAIMER] I'll be honest, this game does not have the best security. Don't use your regular passwords.\033[0m")
        while not self.logged_in:
            username = (input("Username: ")).strip()
            valid, reason = self.check_valid(username)
            if not valid:
                print("Invalid username: " + reason + ". Try again.")
                continue
            if does_username_exist(username):
                print("This username exists. If it's not yours, type \'change name\'. If this is you, enter your password.")        
                pw = input("Password: ")
            else:
                print("This is a new username. If you misspelled your username, enter \'change name\'. If you are making a new account, enter your preferred password.")
                pw = input("Password: ")
            if pw == 'change name':
                continue
            valid, reason = self.check_valid(pw)
            if not valid:
                print("Invalid password: " + reason + ". Try again.")
                continue
            login_info = log_in(username, pw)
            if login_info == False:
                print("Wrong password. Try again.")
            else:
                self.logged_in = True
                self.authentication = (username, pw)
                if 'info' in login_info:
                    self.load_game_data(login_info['info'])
                self.announcements.announce("Logged in successfully. Please resume playing.")

    def check_valid(self, phrase):
        # return (valid (bool), reason(string))
        if type(phrase) != str:
            return (False, "Invalid string")
        if len(phrase) > 20:
            return (False, "String too long")
        if any([char in self.INVALID_CHAR for char in phrase]):
            return (False, "Contains invalid character")
        return (True, "")

    def save_game_data(self):
        if not self.logged_in or self.authentication is None:
            return
        info = {}
        info['cash'] = self.account.cash
        info['owned'] = self.account.owned
        info['bought_values'] = self.account.bought_value
        info['gamestate'] = self.game_state.controller
        self.announcements.announce("Your game data is being saved...")
        save_game(self.authentication[0], self.authentication[1], info)

    def load_game_data(self, info):
        if 'cash' in info:
            self.account.cash = info['cash']
        if 'owned' in info:
            self.account.owned = info['owned']
        if 'bought_values' in info:
            self.account.bought_value = info['bought_values']
        if 'gamestate' in info:
            self.game_state.controller = info['gamestate']
    
    def unlock(self, item_name):
        self.game_state.controller['unlocked'][item_name] = True

    def eat_trash(self):
        import random
        item = random.choice([item for item in self.account.owned if self.account.owned[item] > 0])
        self.account.owned[item] -= 1
        self.account.bought_value[item].pop(0)
        self.tutorial.start_tutorial("Starving, you broke down and ate 1 {} from your trash pile.".format(self.MarketMenu.market.items[item].name))
        self.game_state.controller['states']['hunger'] = 300.
    
    def get_robbed(self):
        if self.account.cash > 0:
            loss = self.account.cash / 2
            self.account.cash -= loss
            self.tutorial.start_tutorial("The dog took half your money.")
            self.announcements.announce("You lost ${}".format(round(loss, 2)))
            self.game_state.controller['states']['protection'] = 15000.


game = Game()
game.new_game()
game.start()