-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfish.py
More file actions
275 lines (219 loc) · 9.19 KB
/
fish.py
File metadata and controls
275 lines (219 loc) · 9.19 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
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
# This is a sample Python script.
import random
#import inventory
import audioManager
import pygame
winwidth, winheight = 900, 700
win = pygame.display.set_mode((winwidth, winheight))
fps = 30
clock = pygame.time.Clock()
water = pygame.transform.scale(pygame.image.load(r'water.jpg'), (winwidth, winheight)).convert()
grass = pygame.transform.scale(pygame.image.load(r'grass.jpg'), (winwidth, winheight*.2)).convert_alpha()
grassBorder = winheight*.8
commonFish = [" Salmon", " Bass", " Tuna", " Cod", " Catfish"]
rareFish = [" Glow Fish", " Jelly Fish", " Peanut Butter Fish", " Dogfish", " Star Fish", " Puffer Fish", " Flounder", " Squid"]
mythicFish = [" Missing Link", " Supernova Fish", " Sea Bear", "n Electric Bass", " Hammerhead Shark", " Screw Driver Shark",
"n Electric Eel", " Fire Eel", "n Ice Eel", "n Earth Eel", " Shroom Fish", " Sword Fish"]
anomalyFish = [" Black Hole Fish", "n Anti-Eel", " Fishfish", " Nessie", " Katana Fish"]
pygame.mixer.init()
class Fishy():
def __init__(self, name):
self.name = name
self.width = 30
self.height = 60
self.xpos = 10
self.ypos = 10
self.left = self.xpos
self.right = self.xpos + self.width
self.top = self.ypos
self.bottom = self.ypos + self.height
self.maxXVel = 2
self.maxYVel = 1
self.xvel = self.maxXVel
self.yvel = self.maxYVel
self.xCoolDown = 9*fps
self.yCoolDown = 13*fps
self.xDirChangeTime = self.xCoolDown
self.yDirChangeTime = self.yCoolDown
self.isCaught = 0
self.coolDown = 3 # num seconds til respawn
self.image = pygame.transform.scale(pygame.image.load(r'fishy.png'), (self.width, self.height)).convert_alpha()
def calculate_movement_coords(self, window):
if self.isCaught:
return
self.xpos += self.xvel
self.ypos += self.yvel
if self.xpos >= window.get_width() or self.xpos <= 0:
self.xvel *= -1
self.xDirChangeTime = self.xCoolDown # reset change direction time
if self.ypos >= grassBorder or self.ypos <= 0:
self.yvel *= -1
self.yDirChangeTime = self.yCoolDown # reset change direction time
if self.xDirChangeTime == 0:
print(self.xDirChangeTime)
self.xDirChangeTime = self.xCoolDown # reset cool down
print(self.xDirChangeTime)
if self.xvel == 0:
self.xvel = self.maxXVel # get up to normal moving speed
elif random.random() < .3: # 50% time will decide to stand still
self.xvel = 0
else:
self.xvel *= -1 # stand still
else:
self.xDirChangeTime -= 1
if self.yDirChangeTime == 0:
self.yDirChangeTime = self.yCoolDown # reset cool down
if self.yvel == 0:
self.yvel = self.maxYVel # get up to normal moving speed
elif random.random() < .5: # 50% time will decide to stand still
self.yvel = 0
else:
self.yvel *= -1 # stand still
else:
self.yDirChangeTime -= 1
def draw(self, window): # this is the character's drawing function that will draw them and their rod on screen
win.blit(self.image, (self.xpos, self.ypos))
class player():
def __init__(self, topBorder):
self.topBorder = topBorder # top border designates the y limitation for character movement, not ideal
#dimensions
self.width = 80
self.height = 80
#positioning
self.xpos = 0
self.ypos = topBorder
self.baitx = -1 # x position of the bait
self.baity = -1 # y position of the bait
self.left = self.xpos
self.right = self.xpos + self.width
self.top = self.ypos
self.bottom = self.ypos + self.height
self.vel = 5 # character walking speed
self.image = pygame.transform.scale(pygame.image.load(r'fisherman.png'), (self.width, self.height))
self.bobber = pygame.transform.scale(pygame.image.load(r'bobber.png'), (10, 10))
self.rodRarity = 4
self.rodIsCast = 0
self.waitTime = 0
self.potentialItem = "None"
# update character position value for later calculation such as touching a border
def updateLeftRight(self):
self.left = self.xpos
self.right = self.xpos + self.width
def updateTopBottom(self):
self.top = self.ypos
self.bottom = self.ypos + self.height
# basic movement of character, calls updaters after
def moveLeft(self, window):
self.xpos -= self.vel
if self.left <= 0:
self.xpos = 0
self.updateLeftRight()
def moveRight(self, window):
self.xpos += self.vel
if self.right >= window.get_width():
self.xpos = window.get_width() - self.width
self.updateLeftRight()
def moveDown(self, window):
self.ypos += self.vel
if self.bottom >= window.get_height():
self.ypos = window.get_height() - self.height
self.updateTopBottom()
def moveUp(self, window):
self.ypos -= self.vel
if self.top <= self.topBorder:
self.ypos = self.topBorder
self.updateTopBottom()
def drawLine(self, window):
pygame.draw.line(window, (0, 0, 0), (self.right, self.top), (self.baitx, self.baity), 2)
window.blit(self.bobber, (self.baitx-5, self.baity))
def draw(self, window): # this is the character's drawing function that will draw them and their rod on screen
win.blit(self.image, (self.xpos, self.ypos))
if self.rodIsCast:
self.drawLine(window)
def cast(self):
self.rodIsCast = 1
#timeToCatch = fps * random.randint(3, 5 - 3 * self.rodRarity)
timeToCatch = 3*fps
item = self.determineItem()
return timeToCatch, item
def handleClick(self):
pos = pygame.mouse.get_pos()
if pos[1] <= grassBorder - 40: # check if clicked on actual water, else we clicked on grass so do nothing
if self.rodIsCast: # if rod is cast, uncast it
self.rodIsCast = 0 # set uncast variable to 0 so self.draw() can check it later and for future logic when actually catching a fish
else:
self.rodIsCast = 1
self.baitx = pos[0]
self.baity = pos[1]
self.waitTime, self.potentialItem = self.cast()
def handleEvent(self, event): # handles character movement event only
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.moveLeft(win)
if keys[pygame.K_RIGHT]:
self.moveRight(win)
if keys[pygame.K_DOWN]:
self.moveDown(win)
if keys[pygame.K_UP]:
self.moveUp(win)
def determineItem(self): # logically determines item recieved based on percentages, doesnt handle timing # rarity = 0, 1, 2, 3
firstDiv = .01 + self.rodRarity/25
secondDiv = firstDiv + .05
thirdDiv = secondDiv + .2
fourthDiv = thirdDiv + .45
r = random.random()
if r < firstDiv:
return anomalyFish[int(random.random()*len(anomalyFish))]
elif r < secondDiv:
return mythicFish[int(random.random()*len(mythicFish))]
elif r < thirdDiv:
return rareFish[int(random.random()*len(rareFish))]
elif r < fourthDiv:
return commonFish[int(random.random()*len(commonFish))]
else:
return " piece of trash"
def main():
run = True
pygame.init()
font = pygame.font.Font('freesansbold.ttf', 32)
textDuration = 0
text = font.render((""), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (10, winheight*.95)
tune = audioManager.Song('clam waters')
tune.loop()
character = player(grassBorder) # pass in grass border for players y limitation
character.image = character.image.convert_alpha()
character.bobber = character.bobber.convert_alpha()
fishy = Fishy("No name")
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONUP:
character.handleClick()
#handle movement
character.handleEvent(event)
if character.rodIsCast:
if character.waitTime==0:
#print("You caught a" + character.potentialItem)
#text = ("You caught a" + character.potentialItem)
text = font.render(("You caught a" + character.potentialItem), True, (0, 0, 0))
textDuration = 2.5*fps
character.rodIsCast = 0
else:
character.waitTime -= 1
fishy.calculate_movement_coords(win)
win.blit(water, (0,0))
win.blit(grass, (0,grassBorder))
character.draw(win)
fishy.draw(win)
if textDuration>=0:
#, (10, winheight*.95)
win.blit(text, textRect)
textDuration-=1
pygame.display.update()
clock.tick(fps)
if __name__ == '__main__':
main()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/