-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNibbie_Game.py
252 lines (212 loc) · 9.33 KB
/
Nibbie_Game.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
import pygame, sys, os
from pygame.math import Vector2
import random
class Fruit:
def __init__(self):
self.randomize()
self.treat1 = pygame.image.load('Nibbie_Graphics/treat_1.png').convert_alpha()
self.treat2 = pygame.image.load('Nibbie_Graphics/treat_2.png').convert_alpha()
self.treat3 = pygame.image.load('Nibbie_Graphics/treat_3.png').convert_alpha()
self.random_treat()
def random_treat(self):
self.treat = random.choice([self.treat1, self.treat2, self.treat3])
def draw_fruit(self):
fruit_rect = pygame.Rect(self.pos.x * cell_size, self.pos.y * cell_size, cell_size, cell_size)
screen.blit(self.treat, fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class Nibbie:
def __init__(self):
self.body = [Vector2(5, 5), Vector2(4, 5)]
self.direction = Vector2(0, 0)
self.poop_pos = []
self.update_poop = False
self.poop_time = 100
self.head_up = pygame.image.load('Nibbie_Graphics/nibbie_up.png').convert_alpha()
self.head_down = pygame.image.load('Nibbie_Graphics/nibbie_down.png').convert_alpha()
self.head_right = pygame.image.load('Nibbie_Graphics/nibbie_right.png').convert_alpha()
self.head_left = pygame.image.load('Nibbie_Graphics/nibbie_left.png').convert_alpha()
self.tail_up = pygame.image.load('Nibbie_Graphics/nibbie_db.png').convert_alpha()
self.tail_down = pygame.image.load('Nibbie_Graphics/nibbie_ub.png').convert_alpha()
self.tail_right = pygame.image.load('Nibbie_Graphics/nibbie_rb.png').convert_alpha()
self.tail_left = pygame.image.load('Nibbie_Graphics/nibbie_lb.png').convert_alpha()
self.poop_up = pygame.image.load('Nibbie_Graphics/nibbie_db_poop.png').convert_alpha()
self.poop_down = pygame.image.load('Nibbie_Graphics/nibbie_ub_poop.png').convert_alpha()
self.poop_right = pygame.image.load('Nibbie_Graphics/nibbie_rb_poop.png').convert_alpha()
self.poop_left = pygame.image.load('Nibbie_Graphics/nibbie_lb_poop.png').convert_alpha()
self.poop = pygame.image.load('Nibbie_Graphics/poop.png').convert_alpha()
def draw_nibbie(self):
self.update_head_graphics()
self.update_tail_graphics()
if self.direction != Vector2(0, 0):
self.update_poop_graphics()
for index, block in enumerate(self.body):
x_pos = int(block.x * cell_size)
y_pos = int(block.y * cell_size)
block_rect = pygame.Rect(x_pos, y_pos, cell_size, cell_size)
if index == 0:
screen.blit(self.head, block_rect)
elif index == 1:
screen.blit(self.tail, block_rect)
def update_head_graphics(self):
head_relation = self.body[1] - self.body[0]
if head_relation == Vector2(1, 0):
self.head = self.head_left
elif head_relation == Vector2(-1, 0):
self.head = self.head_right
elif head_relation == Vector2(0, 1):
self.head = self.head_up
elif head_relation == Vector2(0, -1):
self.head = self.head_down
def update_tail_graphics(self):
tail_relation = self.body[0] - self.body[1]
if tail_relation == Vector2(1, 0):
self.tail = self.tail_right
elif tail_relation == Vector2(-1, 0):
self.tail = self.tail_left
elif tail_relation == Vector2(0, 1):
self.tail = self.tail_up
elif tail_relation == Vector2(0, -1):
self.tail = self.tail_down
def update_poop_graphics(self):
if self.update_poop:
if self.poop_time > 0:
tail_relation = self.body[0] - self.body[1]
if tail_relation == Vector2(1, 0):
self.tail = self.poop_right
elif tail_relation == Vector2(-1, 0):
self.tail = self.poop_left
elif tail_relation == Vector2(0, 1):
self.tail = self.poop_up
elif tail_relation == Vector2(0, -1):
self.tail = self.poop_down
self.poop_time -= 1
elif self.poop_time == 0:
self.poop_time = 100
self.update_poop = False
def move_nibbie(self):
if self.direction == Vector2(0, 0):
self.body = [Vector2(5, 5), Vector2(4, 5)]
else:
self.body[0] += self.direction
self.body[1] = self.body[0] - self.direction
def draw_nibbie_poop(self):
if self.direction != Vector2(0, 0):
for rect in self.poop_pos:
x_poop = int(rect.x * cell_size)
y_poop = int(rect.y * cell_size)
poop_rect = pygame.Rect(x_poop, y_poop, cell_size, cell_size)
screen.blit(self.poop, poop_rect)
else:
pass
def spawn_nibbie_poop(self):
self.poop_pos.append(Vector2(self.body[1].x, self.body[1].y))
class Main():
def __init__(self):
self.nibbie = Nibbie()
self.fruit = Fruit()
pygame.mixer.init()
self.play_background_music()
self.score = 0
self.crunch_sound = pygame.mixer.Sound('Sound/crunch.wav')
self.speed = 200
self.reset_poop = False
def draw_elements(self):
self.fruit.draw_fruit()
self.nibbie.draw_nibbie()
self.draw_score()
self.nibbie.draw_nibbie_poop()
def check_collision(self):
if self.nibbie.body[0] == self.fruit.pos:
self.fruit.random_treat()
self.crunch_sound.play()
self.score += 1
self.speed -= 10
self.fruit.randomize()
self.check_poop_treat_location()
def check_poop_treat_location(self):
for i in self.nibbie.poop_pos:
if self.fruit.pos == i:
self.fruit.randomize()
def update(self):
self.nibbie.move_nibbie()
self.reset()
self.check_collision()
def play_background_music(self):
pygame.mixer.music.load('Sound/Nibbie Song.mp3')
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1, 0)
def draw_score(self):
score_surface = game_font.render(str(self.score), True, (56, 74, 12))
score_x = int(cell_size * cell_number - 30)
score_y = int(cell_size * cell_number - 30)
score_rect = score_surface.get_rect(center=(score_x, score_y))
apple_rect = self.fruit.treat.get_rect(midright=(score_rect.left, score_rect.centery))
bg_rect = pygame.Rect(apple_rect.left, apple_rect.top, apple_rect.width + score_rect.width + 6,
apple_rect.height)
pygame.draw.rect(screen, BISQUE, bg_rect, 4)
screen.blit(score_surface, score_rect)
screen.blit(self.fruit.treat, apple_rect)
pygame.draw.rect(screen, (56, 74, 12), bg_rect, 2)
def reset(self):
if not 0 <= self.nibbie.body[0].x < cell_number or not 0 <= self.nibbie.body[0].y < cell_number:
self.reset_it()
else:
for i in self.nibbie.poop_pos:
if self.nibbie.body[0] == i:
self.reset_it()
def reset_it(self):
self.nibbie.body = [Vector2(5, 5), Vector2(4, 5)]
self.nibbie.direction = Vector2(0, 0)
self.score = 0
self.nibbie.poop_pos = []
self.fruit.randomize()
self.reset_poop = False
pygame.mixer.pre_init(44100,-16,2,512)
pygame.init()
clock = pygame.time.Clock()
cell_number = 10
cell_size = 40
screen = pygame.display.set_mode((cell_size * cell_number, cell_size * cell_number))
game_font = pygame.font.Font('Font/PoetsenOne-Regular.ttf', 25)
BISQUE = pygame.Color('bisque')
main_game = Main()
SCREEN_UPDATE = pygame.USEREVENT
NIBBIE_POOP = pygame.USEREVENT + 1
pygame.time.set_timer(SCREEN_UPDATE, main_game.speed)
pygame.time.set_timer(NIBBIE_POOP, main_game.speed * 20)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == SCREEN_UPDATE:
main_game.update()
if event.type == NIBBIE_POOP:
main_game.nibbie.spawn_nibbie_poop()
main_game.nibbie.update_poop = True
if event.type == pygame.KEYDOWN:
# main_game.reset_poop = True
if event.key == pygame.K_UP:
if main_game.nibbie.direction.y != 1:
main_game.nibbie.direction = Vector2(0, -1)
if event.key == pygame.K_RIGHT:
if main_game.nibbie.direction.x != -1:
main_game.nibbie.direction = Vector2(1, 0)
if event.key == pygame.K_DOWN:
if main_game.nibbie.direction.y != -1:
main_game.nibbie.direction = Vector2(0, 1)
if event.key == pygame.K_LEFT:
if main_game.nibbie.direction.x != 1:
main_game.nibbie.direction = Vector2(-1, 0)
# if main_game.reset_poop:
# main_game.poop_speed = main_game.speed * 10
# pygame.time.set_timer(NIBBIE_POOP, main_game.poop_speed)
# if not main_game.reset_poop:
# pygame.time.set_timer(NIBBIE_POOP, 0)
screen.fill(BISQUE)
main_game.draw_elements()
pygame.display.update()
clock.tick(60)