forked from sd18spring/InteractiveProgramming
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_level.py
More file actions
Latest commit
155 lines (129 loc) · 5.36 KB
/
Copy pathmultiple_level.py
File metadata and controls
155 lines (129 loc) · 5.36 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
importpygame, sys
frompygame.localsimport*
importtime
importos
fromrandomimport*
importrandomasrandom
youwin0=pygame.image.load('win.jpg')
youwin=pygame.transform.scale(youwin0, (1280, 960))
gameover0=pygame.image.load('gameover.jpg')
gameover=pygame.transform.scale(gameover0, (1280, 960))
background=pygame.image.load('underwater.jpg')
mainfish0=pygame.image.load('mainfish.png')
mainfish0.set_colorkey((255,255,255))
trash0=pygame.image.load('trash2.png')
food0=pygame.image.load('target.png')
classPyGameWindowView(object):
"""This class initializes the background, sets the screen, and converts image formats for spped"""
def__init__(self, target, size):
self.screen=pygame.display.set_mode(size)
youwin=youwin0.convert_alpha()
mainfish=mainfish0.convert_alpha()
trash=trash0.convert_alpha()
food=food0.convert_alpha()
self.target=target
self.background=pygame.Surface(self.screen.get_size())
self.background.fill((255, 255, 255))
classTarget(object):
"""Initializing the "goal" for the player to drag the fish to
"""
def__init__(self, size):
self.image=pygame.Surface((100,100))
self.pos= (1230,480)
self.height=70
self.width=70
self.x=1140
self.y=800
def__str__(self):
return"Target height=%f, width=%f, x=%f, y=%f"% (self.height,
self.width,
self.x,
self.y)
classObstacle(object):
"""Initializing an obstacle for collison.
"""
def__init__(self):
self.image=pygame.Surface((90, 90))
self.image.fill((150, 60, 10))
self.image.set_colorkey((150,60,10))
self.pos=pygame.math.Vector2(random.randrange(1230),
random.randrange(910))
self.vel=pygame.math.Vector2(random.uniform(-10, 10),
random.uniform(-10, 10))
def__str__(self):
return"Obstacle height=%f, width=%f, x=%f, y=%f"% (self.height,
self.width,
self.x,
self.y)
defupdate(self):
#prevents obstacle from leaving pygame window
self.pos+=self.vel
ifself.pos[0] <50:
self.vel= (random.randint(1, 5), random.randint(1,5))
ifself.pos[0] >1230:
self.vel= (random.randint(-5, -1), random.randint(-5,-1))
ifself.pos[1] <50:
self.vel= (random.randint(1, 5), random.randint(1,5))
ifself.pos[1] >960:
self.vel= (random.randint(-5, -1), random.randint(-5,-1))
defcreate_newLevel(PyGameWindowView, Target, background, size):
"""This function creates a whole new background with different
obstacles for the next level."""
obstacles= []
foriinrange(random.randint(1,7)):
obstacle=Obstacle()
obstacles.append(obstacle)
nextLevel=PyGameWindowView(target,size)
nextLevel.screen.blit(background, (0,0))
forobstacleinobstacles:
obstacle.update()
nextLevel.screen.blit(obstacle.image, obstacle.pos)
pygame.display.update()
returnnextLevel, obstacles
if__name__=='__main__':
pygame.init()
size= (1280,960)
target=Target(size)
obstacles= []
obstacle=Obstacle()
obstacles.append(obstacle)
view=PyGameWindowView(target,size)
view.background=background
score=0
running=True
view.screen.blit(view.background,(0,0))
whilerunning:
foreventinpygame.event.get():
ifevent.type==QUIT:
running=False
mousex, mousey=pygame.mouse.get_pos()
pygame.mouse.set_visible(False)
fishposx=range(int(mousex-5), int(mousex+5))
fishposy=range(int(mousey-5), int(mousey+5))
view.screen.blit(view.background,(0,0))
view.screen.blit(mainfish0, (mousex-100, mousey-100))
view.screen.blit(food0, (1280-257,960-269) )
forobstacleinobstacles:
obstacle.update()
view.screen.blit(obstacle.image, obstacle.pos)
view.screen.blit(trash0,obstacle.pos)
### Collisions ###
forobstacleinobstacles:
ifabs(obstacle.pos[0]-mousex) <=40:
ifabs(obstacle.pos[1]-mousey) <=65:
view.screen.blit(gameover, (0,0))
time.sleep(1)
running=False
###Leveling Up ###
ifmousexinrange(target.x, target.x+target.width) andmouseyinrange(target.y, target.y+target.height):
levelTwo, obstacles=create_newLevel(PyGameWindowView, target, background, size)
score+=1
pygame.display.update()
pygame.mouse.set_pos((0,200)) #resetting mouse position to the left most part of the screen
ifscore==5:
view.screen.blit(youwin, (0,0))
time.sleep(1)
running=False
pygame.display.update()
time.sleep(.001)
pygame.quit()