repl.it
@VishnuB28/

the fishing game

Python

amelia's fishing game

fork
loading
Files
  • main.py
  • accounts.txt
  • fish.txt
  • hiscores.txt
  • rods.txt
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import random as rand
from termcolor import colored, cprint
import replit
import numpy as np

a=680
b=18

class Account:

    accountlist = []

    def __init__(self, name, pw, xp, gold, dayno, bait, rod, rarestfish, inventorycontents, myths):
        self.name = name
        self.pw = pw
        self.xp = xp
        self.level = int((a/b)*np.log(int(xp)/a + 1))+1
        self.gold = gold
        self.dayno = dayno
        self.bait = bait
        for eachrod in Rod.rodlist:
            if eachrod.name == rod:
                self.rod = eachrod
        for eachfish in Fish.fishlist:
            if eachfish.name == rarestfish:
                self.rarestfish = eachfish
        self.inventory = Inventory(inventorycontents)
        self.myths = Inventory(myths)

        Account.accountlist.append(self)

    def accinfo(self):
        if self.myths.print_inv(False) == "empty":
            mythprint = "none"
        else: 
            mythprint = self.myths.print_inv(False)
        if int(self.level)>=100:
            return "gold - {0}\nbait - {1}\nrod - {2}\nlevel - {3}\nrarest fish caught - {5}\ninventory - {4}\nmythical creatures encountered - {6}".format(self.gold, self.bait, self.rod.name,int(self.level),self.inventory.print_inv(False), self.rarestfish.name, mythprint)
        else:
            return "gold - {0}\nbait - {1}\nrod - {2}\nlevel - {3}\nrarest fish caught - {5}\ninventory - {4}\n".format(self.gold, self.bait, self.rod.name,int(self.level),self.inventory.print_inv(False), self.rarestfish.name)
            


def create_account(): 
    usernamedone = False
    while not usernamedone:
        flag = False
        cprint("enter username", "red")
        username = input()
        accountsfile = open("accounts.txt", "r")
        accounts = accountsfile.readlines()
        accountsfile.close()
        for char in username:
            if char=="-":
                print("don't use a - character; please try again")
                flag = True
                break
        for account in accounts:
            account = account.strip()
            if account.split("-")[0] == username:
                cprint("username taken; please try again", "red")
                flag = True
        if not flag:
            usernamedone = True
    cprint("enter password", "red")
    pw = input()
    accountsfile = open("accounts.txt", "a")
    accountsfile.write(username + "-" + pw + "-0-0-1-10-none-wooden-empty-empty \n")
    accountsfile.close()
    player = Account(username, pw, 0, 0, 1, 10, "wooden", "none", "empty", "empty")
    game_info(player)
    input()
    return player

def log_in():
    cprint("would you like to log in or sign up?", "red")
    aec=False
    while not aec:
        ans = input()
        if ans == "sign up":
            player = create_account()
            aec=True
        elif ans == "log in":
            loggedin = False
            while not loggedin:
                flag = False
                cprint("enter username", "red")
                username = input()
                cprint("enter password", "red")
                pw = input()
                accountsfile = open("accounts.txt","r")
                accounts = accountsfile.readlines()
                accountsfile.close()
                for account in accounts:
                    account = account.strip()
                    if username == account.split("-")[0] and pw == account.split("-")[1]:
                        loggedin = True
                        #xp = int(account.split("-")[2])
                        #gold = int(account.split("-")[3])
                        #dayno = int(account.split("-")[4])
                        #bait = int(account.split("-")[5])
                        #rarestfish = account.split("-")[6]
                        #rod = account.split("-")[7]
                        #invcontents = account.split("-")[8]
                        #mythcontents = account.split("-")[9]
                        input(colored("succesfully logged in. press enter to continue", "red"))
                        #player = Account(username, pw, xp, gold, dayno, bait, rod, rarestfish, invcontents, mythcontents)
                        player = Account(username, pw, int(account.split("-")[2]), int(account.split("-")[3]), int(account.split("-")[4]), int(account.split("-")[5]), account.split("-")[7], account.split("-")[6], account.split("-")[8], account.split("-")[9])
                if not loggedin:
                    cprint("username and password do not match; please try again", "red")
            aec=True
        else:
            cprint("please enter log in or sign up", "red")
    try:
        replit.clear()
    except:
        print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
    return player

def game_info(player):
    cprint("welcome, {}, to my fishing game!\nthe aim of the game is to reach level 100 as quickly as possible. catch the rarest fish you can and level up!\nyou can sell your fish in the shop. rarer fish bring in more gold. you can use this gold to purchase bait, which is needed to catch fish, and rods, which make catching rarer fish easier.\nnew types of fish are unlocked as you level up, and at higher levels, it's easier to catch fish.\nthe game saves your progress at the end of each day, so you can just log in again and pick up where you left off. good luck!\npress enter to continue.".format(player.name),"red")

class Fish:

    fishlist = []

    def __init__(self, name, level, cost, rarityno):
        self.name = name
        self.level = level
        self.cost = cost
        self.rarityno = rarityno
        if name == "none":
            self.rarity = "none"
            self.xpgain=0
        elif rarityno >=30:
            self.rarity = "common"
            self.xpgain = 5
        elif rarityno >= 20 and rarityno < 30:
            self.rarity = "uncommon"
            self.xpgain = 10
        elif rarityno >= 10 and rarityno < 20:
            self.rarity = "rare"
            self.xpgain = 15
        elif rarityno >= 6 and rarityno < 10:
            self.rarity = "super rare"
            self.xpgain = 20
        elif rarityno >= 3 and rarityno < 6:
            self.rarity = "ultra rare"
            self.xpgain = 25
        elif rarityno >=1 and rarityno < 3:
            self.rarity = "legendary"
            self.xpgain = 30
        elif rarityno == 0:
            self.rarity = "mythical"
            self.xpgain = 1000

        Fish.fishlist.append(self)

class Rod:

    rodlist = []

    def __init__(self, name, uncommonhelp, rarehelp, highrarehelp, mythhelp, cost):
        self.name = name
        self.uncommonhelp = int(uncommonhelp)
        self.rarehelp = int(rarehelp)
        self.highrarehelp = int(highrarehelp)
        self.mythhelp = int(mythhelp)
        self.cost = int(cost)

        Rod.rodlist.append(self)

rodsfile = open("rods.txt","r")
rods = [x.strip() for x in rodsfile.readlines()]
rodsfile.close()
for rod in rods:
    rod = rod.split(", ")
    Rod(rod[0],rod[1],rod[2],rod[3],rod[4],rod[5])

class Inventory:

    def __init__(self, contents):
        if contents == "empty":
            self.contents = []
        else:
            contents = contents.split("/")
            self.contents = []
            for element in contents:
                element = element.split(",")
                for fish in Fish.fishlist:
                    if fish.name == element[0]:
                        fishtoappend = fish
                self.contents.append([fishtoappend,int(element[1])])


    def add(self, obj):
        flag = False
        for fish in self.contents:
            if fish[0].name == obj.name:
                fish[1] += 1
                flag = True
        if not flag:
            self.contents.append([obj, 1])

    def takeout(self, obj, no):
        flag = False
        for fish in self.contents:
            if fish[0].name == obj.name:
                flag = True
                if fish[1] == no:
                    self.contents.remove(fish)
                    return True
                elif fish[1] > no:
                    fish[1] = fish[1] - no
                    return True
                else:
                    print("you do not have enough {} in your inventory".format(obj.name))
                    return False
        if not flag:
            print("object not in inventory")
            return False

    def print_inv(self,start):
        if not self.contents:
            if start:
                return "your inventory is empty"
            else:
                return "empty"
        else:
            if start:
                return ("in your inventory you have {0}".format(", ".join(["{0} {1}".format(fish[1], fish[0].name) for fish in self.contents])))
            else:
                return ("{0}".format(", ".join(["{0} {1}".format(fish[1], fish[0].name) for fish in self.contents])))

    def write_inv(self):
        if self.contents == []:
            return "empty" 
        else:
            return("/".join(["{0},{1}".format(x[0].name,x[1]) for x in self.contents]))

fishfile = open("fish.txt", "r")
fishes = [x.strip() for x in fishfile.readlines()]
fishfile.close()
for fish in fishes:
    fish = fish.split(", ")
    Fish(fish[0], int(fish[1]), int(fish[2]), int(fish[3]))


def go_fishing():

    if player.bait == 0:
        print("you have no bait! go to the shop to buy some")
        go_shopping()
    else:
        input("press enter to roll the dice to see how many attempts to catch fish you can make today")
        if player.bait < 6:
            max = player.bait
        else:
            max = 6
        dice = rand.randint(1,max)

        if dice>1:
            attemptstr = "attempts"
        else:
            attemptstr = "attempt"

        print("you can make {0} {1}".format(colored(str(dice),"yellow"), attemptstr))


        for x in range(int(dice)):

            input(colored("attempt {} ...".format(x+1),"yellow"))

            player.bait -= 1

            distfishlist = []
            for fish in Fish.fishlist:
                if fish.level <= int(player.level):
                    timestoappend = fish.rarityno
                    if fish.rarity == "uncommon":
                        timestoappend = timestoappend + player.rod.uncommonhelp
                    elif fish.rarity == "rare":
                        timestoappend = timestoappend + player.rod.rarehelp
                    elif fish.rarity == "super rare" or fish.rarity == "ultra rare" or fish.rarity == "legendary":
                        timestoappend = timestoappend + player.rod.highrarehelp
                    elif fish.rarity == "mythical":
                        timestoappend = timestoappend + player.rod.mythhelp
                    for x in range(timestoappend):
                        distfishlist.append(fish)
            percchance = player.level +99
            resultchance = rand.randint(1,200)
            if resultchance < percchance:

                fish = rand.choice(distfishlist)

                if fish.rarity == "mythical":
                    print("wow, you encountered a {0}! a {1} is a {2} creature that can't be sold".format(colored(fish.name, "cyan"), fish.name, fish.rarity))
                    player.myths.add(fish)
                else:
                    print("you caught a {0}! a {1} is a {2} fish that can be sold for {3} gold".format(colored(fish.name, "cyan"), fish.name, fish.rarity, fish.cost))
                    player.inventory.add(fish)
                oldlevel = int(player.level)
                player.xp = player.xp + fish.xpgain
                player.level = int((a/b)*np.log(int(player.xp)/a + 1))+1
                if ((fish.rarityno<=player.rarestfish.rarityno and fish.name != player.rarestfish.name) or player.rarestfish.rarityno==0) and fish.rarity != "mythical":
                    if fish.rarityno==player.rarestfish.rarityno:
                        if fish.level>player.rarestfish.level:
                            cprint("this is the rarest fish you've ever caught!", "cyan")
                            player.rarestfish = fish
                    else:
                        cprint("this is the rarest fish you've ever caught!", "cyan")
                        player.rarestfish = fish
                if int(player.level) > oldlevel:
                    if int(player.level) == 100:
                        reached_lvl100() 
                    else:
                        cprint("congratulations, you reached level {0}".format(int(player.level)),"magenta")
                        for fish in Fish.fishlist:
                            if fish.level == int(player.level):
                                if fish.rarity == "mythical":
                                    print("you can now encounter {0}s. a {0} is a {1} creature that can't be sold".format(fish.name, fish.rarity))
                                else:
                                    print("you can now catch {0}. a {0} is a {1} fish that can be sold for {2} gold".format(fish.name, fish.rarity, fish.cost))
            else:
                print("the fishing line came up empty.")

def reached_lvl100():
    cprint("CONGRATULATIONS! YOU REACHED LEVEL 100!","magenta", "on_white", attrs=["blink"])
    cprint("you reached level 100 in {0} days".format(player.dayno),"magenta")
    cprint("these are the high score rankings for how many days other players have reached level 100 in","magenta")
    hiscoresfile = open("hiscores.txt","r")
    hiscores = [x.strip() for x in hiscoresfile.readlines()]
    hiscoresfile.close()
    for hiscore in hiscores:
        hiscore = hiscore.split("-")
        Hiscorer(hiscore[0],int(hiscore[1]))
    #highscorelist = []
    #oldHiscorerlist = Hiscorer.hiscorers[:]
    Hiscorer(player.name,player.dayno)

    orderedlist=[]

    orderedlist.append([1,Hiscorer.hiscorers[0]])
    for highscorer in Hiscorer.hiscorers[1:]:
        inlist=False
        for orderedhighscorer in orderedlist:
            if highscorer.days<orderedhighscorer[1].days:
                orderedlist.insert(orderedlist.index(orderedhighscorer),[orderedhighscorer[0],highscorer])
                for neworderedhighscorer in orderedlist[orderedlist.index(orderedhighscorer):]:
                    neworderedhighscorer[0]+=1
                inlist=True
            elif highscorer.days==orderedhighscorer[1].days:
                orderedlist.insert(orderedlist.index(orderedhighscorer),[orderedhighscorer[0],highscorer])
                for neworderedhighscorer in orderedlist[(orderedlist.index(orderedhighscorer)+1):]:
                    neworderedhighscorer[0]+=1
                inlist=True
            if inlist:
                break
        if not inlist:
            orderedlist.append([len(orderedlist)+1,highscorer])
        
    for printinghi in orderedlist:
        cprint("{0}. {1} - {2} days".format(printinghi[0], printinghi[1].name, printinghi[1].days), "magenta")



    # playerdone = False
    # for hiscorer in oldHiscorerlist:
    #     if playerhi.days > hiscorer.days:
    #         highscorelist.append(hiscorer)
    #     elif playerhi.days == hiscorer.days:
    #         if not playerdone:
    #             highscorelist.append(playerhi)
    #             playerdone = True
    #         highscorelist.append(hiscorer)
    #     elif playerhi.days < hiscorer.days:
    #         if not playerdone:
    #             highscorelist.append(playerhi)
    #             playerdone = True 
    #         highscorelist.append(hiscorer)
    # if not playerdone:
    #     highscorelist.append(playerhi)
    # place = 1
    # for highscore in highscorelist:
    #     cprint("{0}. {1} - {2} days".format(place,highscore.name,highscore.days),"magenta")
    #     place += 1
    hiscoresfile = open("hiscores.txt","w")
    for hi in orderedlist:
        hiscoresfile.write("{0}-{1}\n".format(hi[1].name, hi[1].days))
    # for towritehiscore in highscorelist:
    #     hiscoresfile.write("{0}-{1}\n".format(towritehiscore.name,towritehiscore.days))
    hiscoresfile.close()


    cprint("now that you've reached level 100, you have the ability to encounter mythical creatures. these are extremely rare and can only be found with the moonstone rod, which is now unlocked in the shop. they can't be sold, but encountering them gives you a lot of experience!","magenta")
    for fish in Fish.fishlist:
        if fish.level == int(player.level):
            if fish.rarity == "mythical":
                print("you can now encounter {0}s. a {0} is a {1} creature that can't be sold".format(fish.name, fish.rarity))
            else:
                print("you can now catch {0}. a {0} is a {1} fish that can be sold for {2} gold".format(fish.name, fish.rarity, fish.cost))
        
class Hiscorer:

    hiscorers = []

    def __init__(self, name, days):
        self.name = name
        self.days = days

        Hiscorer.hiscorers.append(self)


def go_shopping():
    stay = True
    while stay:
        print("would you like to buy, sell or leave?")
        ans = input()
        if ans == "sell":
            sell_fish()
        elif ans == "buy":
            buy()
        elif ans == "leave":
            stay = False


def buy():
    print("on the shelves you see bait and a selection of rods. what would you like to buy?")
    ans = input()
    if ans == "bait":
        print("bait costs 1 gold each. how much would you like to buy?")
        ans = int(input())
        if player.gold >= ans:
            player.gold = player.gold - ans
            player.bait = player.bait + ans
            print("bought successfully. you now have {0} bait and {1} gold".format(player.bait,player.gold))
        else:
            print("you do not have enough gold")
    elif ans == "rod":
        for rod in Rod.rodlist:
            if not (rod.name=="moonstone" and player.level <100):
                print("the {} rod costs {} gold".format(rod.name,rod.cost))
            else:
                print("the {} rod is currently unavailable".format(rod.name))

        print("which rod would you like to buy? enter none if you do not want to buy one")
        ans = input()
        if ans != "none":
            rodbought = False
            for rod in Rod.rodlist:
                if ans == rod.name:
                    if not (rod.name=="moonstone" and player.level<100):
                        if player.gold - rod.cost >=0:
                            rodbought = True
                            player.rod = rod
                            player.gold = player.gold - rod.cost
                            print("bought succesfully. you now have the {} rod and {} gold".format(player.rod.name, player.gold))
                        else:
                            rodbought=True
                            print("you don't have enough gold for that rod")
            if not rodbought:
                print("that rod is not available")

def sell_fish():
    if player.inventory.print_inv(False) == "empty":
        pass
    else:
        print("would you like to sell all your fish or some?")
        ans = input()
        if ans == "all":
            sell_all_fish()
        elif ans == "some":
            sell_ind_fish()

def sell_all_fish():
    pastcontents = player.inventory.print_inv(False)
    oldinv = player.inventory.contents[:]
    oldgold = player.gold
    for fish in oldinv:
        player.inventory.takeout(fish[0],fish[1])
        player.gold = player.gold + (fish[0].cost * fish[1])
    print("the {0} were succesfully sold for {1} gold. you now have {2} gold".format(pastcontents, player.gold-oldgold, player.gold))


def sell_ind_fish():
    print("enter the quantity and name of the fish you would like to sell. separate the number and name with a \"-\". seperate multiple fish with a \", \"")
    aec=False
    while not aec:
        oldgold=player.gold
        try:
            ans = input().split(", ")
            fishestosell=[]
            for fis in ans:
                minilist=fis.split("-")
                fishestosell.append(minilist)
            for fishy in fishestosell:
                for fish in Fish.fishlist:
                    if fishy[1] == fish.name:
                        fishtosell = fish
                flag=player.inventory.takeout(fishtosell,int(fishy[0]))
                if flag:
                    player.gold = player.gold + (fishtosell.cost * int(fishy[0]))
            print("you sold the fish for {0} gold. you now have {1} gold".format(player.gold-oldgold, player.gold))
            aec=True
        except:
            print("make sure to enter the fish correctly")

def do_stuff(dayno):
    play = True
    cprint("day {}".format(dayno), "green")
    print(player.accinfo())
    print("would you like to a. go fishing, b. go to the shop or c. log out?")
    aec = False
    while not aec:
        ans = input()
        if ans == "b":
            go_shopping()
            aec=True
            input("it's the end of the day. press enter to save and continue")
        elif ans == "a":
            go_fishing()
            aec=True
            input("it's the end of the day. press enter to save and continue")
        elif ans == "c":
            play = False
            aec=True
        else:
            print("please enter a, b or c")
    return play

def save():
    accountsfile = open("accounts.txt", "r")
    accounts = [x.strip() for x in accountsfile.readlines()]
    accountsfile.close()
    for account in accounts:
        if account.split("-")[0] == player.name:
            accounts.remove(account)
    accountsfile = open("accounts.txt", "w")
    for account in accounts:
        account = account.split("-")
        #print(account[0])
        accountsfile.write("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}-{8}-{9}\n".format(account[0],account[1],account[2],account[3], account[4],account[5],account[6],account[7],account[8],account[9]))
    accountsfile.write("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}-{8}-{9}\n".format(player.name,player.pw,player.xp,player.gold,player.dayno,player.bait,player.rarestfish.name,player.rod.name,player.inventory.write_inv(),player.myths.write_inv()))
    accountsfile.close()

player = log_in()

play = True

player.dayno

while play:
    play = do_stuff(player.dayno)
    try:
        replit.clear()
    except:
        print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
    save()
    player.dayno += 1

print("your progress was saved")
print("thanks for playing!")