-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscores.py
221 lines (160 loc) · 6.4 KB
/
highscores.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
import pygame, sys, random, time, cPickle
from pygame.locals import *
from random import *
try:
import android
except ImportError:
android = None
try:
import pygame.mixer as mixer
except ImportError:
import android.mixer as mixer
# Event constant.
TIMEREVENT = pygame.USEREVENT
# The FPS the game runs at.
FPS = 35
# Screen constants
SCREEN_WIDTH, SCREEN_HEIGHT = 480, 800
SCREENRECT = Rect(0, 0, 480, 800)
def load_image(file_name):
'''load images with exception handling'''
try:
image = pygame.image.load(file_name)
except pygame.error, message:
print "Cannot open image: " + file_name
raise SystemExit, message
return image.convert_alpha()
class Backgrounds1 (pygame.sprite.Sprite):
def __init__(self, screen, speed, image_file):
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.scrolling = False
self.image = load_image(image_file)
self.rect = self.image.get_rect()
self.image_w, self.image_h = self.image.get_size()
self.dy = speed
self.rect.bottom = SCREEN_HEIGHT
def draw(self):
self.screen.blit(self.image, self.rect)
class Balloons1 (pygame.sprite.Sprite):
def __init__(self, screen, init_x, init_y, dx, dy, image_file, image2_file):
'''constructor for balloon class, initialize the balloon'''
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.x = init_x
self.y = init_y
self.dx = dx
self.dy = dy
self.image = pygame.transform.scale (load_image(image_file), (200, 300))
self.image2 = pygame.transform.scale (load_image(image2_file), (200, 300))
self.rect = self.image.get_rect()
self.rect.move(self.x, self.y)
self.rect.topleft = (self.x, self.y)
self.image_w, self.image_h = self.image.get_size()
self.rect.bottomright = (self.x + self.image_w, self.y + self.image_h)
self.active = False
def update(self):
'''update the balloon and check to make sure it hasn't moved off the screen'''
pass
def draw(self):
'''draw the balloon in its current position'''
if self.active:
self.screen.blit(self.image2, (self.x, self.y))
else:
self.screen.blit(self.image, (self.x, self.y))
def scores(screen):
"""Score function. Takes in screen (surface to blit to). Returns 0 if player clicks Menu button."""
SCREEN_WIDTH, SCREEN_HEIGHT = screen.get_size()[0], screen.get_size()[1]
pygame.init()
window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Sky High')
screen = pygame.display.get_surface()
clock = pygame.time.Clock()
scroll_speed = 3
################3333
if android:
filename = "highscores.txt"
else:
filename = "highscores.txt"
highscores = [("",0),("",0), ("",0), ("",0), ("",0), ("",0), ("",0), ("",0) ]
try: # If there is already a highscores file, try opening it
f = open(filename, "r+")
except IOError: # If there is no highscores file, create one and add 0's to it
f = open(filename, "w+")
cPickle.dump(highscores, f)
f.seek(0)
highscores = cPickle.load(f)
f.close()
#highscores.sort()
#highscores.reverse()
# Fonts
font = pygame.font.Font("BRLNSDB.TTF", 30)
score_text = []
for i in range(len(highscores)):
a = str(highscores[i][1]) + " by " +(highscores[i][0])
score_text.append(font.render(a, 1, (100, 20, 0)))
score_height = score_text[0].get_rect()[3]
score_rect = []
for i in range(len(highscores)):
score_rect.append(score_text[0].get_rect(topleft = (60, 30 + score_height * i)))
##################33333
menu_image = "assets/highscores.png"
play_image = "assets/play.png"
played_image = "assets/play_pressed.png"
highscore_image = "assets/highscore.png"
highscored_image = "assets/highscore_pressed.png"
exit_image = "assets/exit.png"
exited_image = "assets/exit_pressed.png"
font = pygame.font.Font("assets/freesansbold.ttf", 30)
try:
mixer.music.load("assets/Scores.ogg")
mixer.music.play(-1)
except pygame.error:
print "Couldn't find file."
background = (0, 0, 0)
sky = Backgrounds1(screen, scroll_speed, menu_image)
# Map the back button to the escape key.
if android:
android.init()
android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)
android.accelerometer_enable(True)
# Use a timer to control FPS.
pygame.time.set_timer(TIMEREVENT, 3000)
pygame.time.set_timer(USEREVENT + 1, 1000)
pygame.time.set_timer(USEREVENT + 2, 2000)
pygame.key.set_repeat(FPS, FPS) # set key repeat on
getout = Balloons1(screen, SCREEN_WIDTH / 2 +30, SCREEN_HEIGHT/2 +10, 0, 0, exit_image, exited_image)
while True:
#game loop
time_passed = clock.tick(FPS)
if android:
if android.check_pause():
android.wait_for_resume()
init_x = randint(0, SCREEN_WIDTH)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if getout.rect.collidepoint(event.pos):
getout.active = True
screen.fill(background)
sky.draw()
getout.draw()
pygame.display.flip()
elif event.type == pygame.MOUSEBUTTONUP:
getout.active = False
if event.button == 1:
if getout.rect.collidepoint(event.pos):
return 0
screen.fill(background)
sky.update()
sky.draw()
getout.draw()
for i in range(len(highscores)):
screen.blit(score_text[i], score_rect[i])
pygame.display.flip()
if __name__ == "__main__":
main()