-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
212 lines (183 loc) · 6.78 KB
/
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
import pygame
import sys
import random
from pygame.locals import *
from sprites import *
from Realtime_PyAudio_FFT.stream_analyzer import Stream_Analyzer
import level1, level3, level2, level4
pygame.init()
vec = pygame.math.Vector2
HEIGHT = 1080
WIDTH = 1920
FPS = 30
FramesPecSec = pygame.time.Clock()
ear = Stream_Analyzer(rate = None, # Audio samplerate, None uses the default source settings
FFT_window_size_ms = 60, # Window size used for the FFT transform
updates_per_second = 1000, # How often to read the audio stream for new data
smoothing_length_ms = 50)
displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Charmer")
restingSprite = pygame.image.load("sprites/RestingSnek2.png")
movingSprite = pygame.image.load("sprites/MovingSnek.png")
movingSprite2 =pygame.image.load("sprites/MovingSnek2.png")
restingSprite2 = pygame.image.load("sprites/RestingSnek.png")
restingSpriteF = pygame.transform.flip(restingSprite, True, False)
movingSpriteF = pygame.transform.flip(movingSprite, True, False)
movingSprite2F = pygame.transform.flip(movingSprite2, True, False)
restingSprite2F = pygame.transform.flip(restingSprite2, True, False)
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.surf = restingSprite
self.rect = self.surf.get_rect(center= (10,420))
self.pos = vec(level1.position)
self.vel = vec(0,0)
self.flipped = False
self.moveToggle = False
def animateMove(self):
if self.moveToggle:
self.surf = movingSpriteF if self.flipped else movingSprite
self.moveToggle = False
else:
self.surf = movingSprite2F if self.flipped else movingSprite2
self.moveToggle = True
def move(self, note, volume):
on_floor = pygame.sprite.spritecollide(PLAYER, FLOORS, False)
on_tree = pygame.sprite.spritecollide(PLAYER, TREES, False)
on_sign = pygame.sprite.spritecollide(PLAYER, signs, False)
on_goal = pygame.sprite.spritecollide(PLAYER, [GOAL], False)
DIR = 'None'
if (volume > 500000):
if (note == 'C' or note == 'B'):
DIR = 'LEFT'
elif (note == 'A' or note == 'A#' or note == 'G#'):
DIR = 'RIGHT'
elif (note == 'F' or note == 'F#'):
DIR = 'UP'
elif (note == 'E' or note == 'C#'):
DIR = 'DOWN'
if DIR == 'LEFT' and on_floor:
self.vel = (-8,0)
self.animateMove()
self.flipped = True
elif DIR == 'RIGHT' and on_floor:
self.vel = (8,0)
self.animateMove()
self.flipped = False
elif DIR == 'UP' and (on_tree or on_floor):
self.vel = (0,-8)
self.surf = restingSpriteF if self.flipped else restingSprite
self.flipped = not self.flipped
elif DIR == 'DOWN' and on_tree:
if on_tree:
self.vel = (0,8)
self.surf = restingSpriteF if self.flipped else restingSprite
self.flipped = not self.flipped
else:
self.vel = (0,0)
self.moveToggle = False
if random.randint(0, 100) == 0:
self.surf = restingSprite2F if self.flipped else restingSprite2
else:
self.surf = restingSpriteF if self.flipped else restingSprite
if not on_floor and not on_tree:
self.vel = (0, 8)
if on_sign:
SIGN_OVERLAY.change_text(on_sign[0].text)
SIGN_OVERLAY.visible = True
else:
SIGN_OVERLAY.visible = False
self.pos += self.vel
self.rect.midbottom = self.pos
if (level == 2):
if pygame.sprite.spritecollide(PLAYER, [level2.KNIGHT], False):
FADER.FADEOUT = True
FADER.LOSE = True
return
elif (level == 3):
if pygame.sprite.spritecollide(PLAYER, list(map(lambda x: x.child, level3.SPAWNERS)), False):
FADER.FADEOUT = True
FADER.LOSE = True
if (self.pos.y > 1200):
FADER.FADEOUT = True
FADER.LOSE = True
elif (on_goal):
if (not FADER.FADEOUT and not FADER.FADEIN):
FADER.FADEOUT = True
FADER.WIN = True
PLAYER = Player()
SIGN_OVERLAY = Sign_Overlay()
TIME_OVERLAY = Time_Overlay()
LEVEL_OVERLAY = Level_Overlay()
VOLUME_OVERLAY = Volume_Overlay()
NOTES_OVERLAY = Notes_Overlay()
FADER = Fader()
all_sprites = level1.create_sprite_group(PLAYER)
FLOORS = level1.FLOORS
TREES = level1.TREES
signs = level1.signs
GOAL = level1.GOAL
all_sprites.add(LEVEL_OVERLAY)
level = 0 # will increment to one, set this to zero
current_level = level1
def start_next_level():
global level, current_level
level += 1
if level == 1:
current_level = level1
elif level == 2:
current_level = level2
elif level == 3:
current_level = level3
# elif level == 4:
# current_level = level4
else:
level = 1
current_level = level1
TIME_OVERLAY.reset()
LEVEL_OVERLAY.update(level)
global all_sprites
global FLOORS
global TREES
global signs
global GOAL
all_sprites = current_level.create_sprite_group(PLAYER)
all_sprites.add(LEVEL_OVERLAY)
FLOORS = current_level.FLOORS
TREES = current_level.TREES
signs = current_level.signs
GOAL = current_level.GOAL
PLAYER.pos = vec(current_level.position)
start_next_level()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
displaysurface.fill((100,100,200))
audio_data = ear.get_audio_features()
PLAYER.move(audio_data['note'], audio_data['vol'])
if level == 2:
level2.KNIGHT.update(PLAYER.pos.x, FLOORS)
elif level == 3:
for entity in level3.SPAWNERS:
entity.update(displaysurface)
for entity in all_sprites:
displaysurface.blit(entity.surf, entity.rect)
if SIGN_OVERLAY.visible:
for entity in SIGN_OVERLAY.entities:
displaysurface.blit(entity["surf"], entity["rect"])
TIME_OVERLAY.display(displaysurface)
NOTES_OVERLAY.display(audio_data['note'], audio_data['vol'], displaysurface)
VOLUME_OVERLAY.display(audio_data['vol'], displaysurface)
if FADER.FADEOUT or FADER.FADEIN:
FADER.display(displaysurface)
FADER.update()
if (FADER.NEXT and FADER.LOSE):
PLAYER.pos = vec(current_level.position)
if (level == 2):
level2.reset_knight()
if (FADER.NEXT and FADER.WIN):
start_next_level()
pygame.display.update()
FramesPecSec.tick(FPS)