-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathfinal_game.py
More file actions
192 lines (155 loc) · 6.8 KB
/
final_game.py
File metadata and controls
192 lines (155 loc) · 6.8 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
import pygame, sys
from pygame.locals import *
import time
import os
from random import *
import random as random
from pygame import mixer
youwin0 = pygame.image.load('win.png')
gameover0 = pygame.image.load('lose.png')
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')
class PyGameWindowView(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()
gameover = gameover0.convert_alpha()
self.target = target
self.background1 = pygame.Surface(self.screen.get_size())
self.background1.fill((255, 255, 255))
class Target(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)
class Obstacle(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)
def update(self, stop):
#prevents obstacle from leaving pygame window
self.pos += self.vel
if self.pos[0] < 50:
self.vel = (random.randint(1, 5), random.randint(1,5))
if self.pos[0] > 1230:
self.vel = (random.randint(-5, -1), random.randint(-5,-1))
if self.pos[1] < 50:
self.vel = (random.randint(1, 5), random.randint(1,5))
if self.pos[1] > 960:
self.vel = (random.randint(-5, -1), random.randint(-5,-1))
if stop == True:
self.vel = (0,0)
def create_newLevel(PyGameWindowView, Target, background, size):
"""This function creates a whole new background with different
obstacles for the next level."""
obstacles = []
for i in range(random.randint(1,7)):
obstacle = Obstacle()
obstacles.append(obstacle)
nextLevel = PyGameWindowView(target,size)
nextLevel.screen.blit(background, (0,0))
for obstacle in obstacles:
obstacle.update(stop = False)
nextLevel.screen.blit(obstacle.image, obstacle.pos)
pygame.display.update()
return nextLevel, obstacles
if __name__ == '__main__':
pygame.init()
pygame.mixer.init()
pygame.font.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))
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.MOUSEMOTION:
mousex, mousey = pygame.mouse.get_pos()
pygame.mouse.set_visible(False)
view.screen.blit(view.background,(0,0))
view.screen.blit(mainfish0, (mousex-100, mousey-100))
view.screen.blit(food0, (1280-257,960-269) )
for obstacle in obstacles:
obstacle.update(stop = False)
view.screen.blit(obstacle.image, obstacle.pos)
view.screen.blit(trash0,obstacle.pos)
### Collisions ###
for obstacle in obstacles:
if abs(obstacle.pos[0]-mousex) <= 40:
if abs(obstacle.pos[1]-mousey) <= 65:
obstacle.update(stop=True)
view.screen.blit(gameover0, (0,0))
pygame.event.set_blocked(pygame.MOUSEMOTION)
pygame.event.set_allowed(pygame.KEYDOWN)
pygame.mixer.music.load('smack.mp3')
pygame.mixer.music.play()
###Leveling Up ###
if mousex in range(target.x, target.x+target.width) and mousey in range(target.y, target.y+target.height):
pygame.mixer.music.load('bubble.mp3')
pygame.mixer.music.play()
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
if score == 5:
view.screen.blit(youwin0, (0,0))
pygame.event.set_blocked(pygame.MOUSEMOTION)
time.sleep(3)
#running = False
## You Lost Screen Options###
if event.type == KEYDOWN:
if event.key == K_TAB:
print('pressed')
running = False
if event.key == K_SPACE:
print('pressed')
pygame.event.set_allowed(MOUSEMOTION)
event.type == MOUSEMOTION
levelTwo, obstacles = create_newLevel(PyGameWindowView, target, background, size)
pygame.mouse.set_pos((0,200)) #resetting mouse position to the left most part of the screen
mousex, mousey = pygame.mouse.get_pos()
pygame.mouse.set_visible(False)
view.screen.blit(view.background,(0,0))
view.screen.blit(mainfish0, (mousex-100, mousey-100))
view.screen.blit(food0, (1280-257,960-269) )
pygame.display.update()
pygame.display.update()
time.sleep(.001)
pygame.quit()