-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
216 lines (167 loc) · 7.61 KB
/
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
import random, os, pygame, sys
from pygame.locals import *
from Snoopy import Snoopy
from Woodstock import Woodstock
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'
# Pygame Keys
Q = pygame.K_q
R = pygame.K_r
ESCAPE = pygame.K_ESCAPE
# Colors
COLOR_BLACK = (0,0,0)
# Events
TIME_UP = pygame.USEREVENT + 1
class Game(object):
def __init__(self, width, height):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode([width, height])
self.re_init()
def re_init(self):
self.score = 0
self.totalTime = 10
highscore = open("highscore.txt", "r")
currentHighScore = highscore.readline()
self.score = 0
self.totalTime = 10
# Start a timer to figure out when the game ends (60 seconds)
pygame.time.set_timer(TIME_UP,10000)
self.startTime = pygame.time.get_ticks()
# font = pygame.font.Font(os.path.join("assets/fonts", "peanuts.tff"), 28)
self.fontRegular = pygame.font.Font(os.path.join("assets/fonts", "cella.otf"), 28)
self.fontSmall = pygame.font.Font(os.path.join("assets/fonts", "cella.otf"), 20)
# Render the text with Anti-aliasing
self.scoreText = self.fontRegular.render("Score: " + str(self.score), True, COLOR_BLACK)
self.scorepos = self.scoreText.get_rect(left = 40, top = 20)
self.timerText = self.fontRegular.render("Time Left: " + str(self.time_left()), True, COLOR_BLACK)
self.timerpos = self.timerText.get_rect(left = 40, top = 50)
self.highscoreText = self.font.render("High Score: " + currentHighScore, True, COLOR_BLACK)
self.highscorepos = self.highscoreText.get_rect(left = self.width-300, top = 20)
# Initialize snoopy and the woodstocks
self.snoopy = Snoopy(self)
self.woodstocks = self.createWoodstocks()
pygame.joystick.init()
def time_left(self):
return self.totalTime - self.current_time()
# Returns time since startTime in seconds
def current_time(self):
return (pygame.time.get_ticks() - self.startTime)/1000
def run(self):
runGame = True
while runGame:
joyStick = pygame.joystick.Joystick(0)
joyStick.init()
# Define colors
black = (0,0,0)
end = True
self.gameOver = False
timerIsRunning = True
while not self.gameOver:
self.resetDeadWoodstocks()
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if timerIsRunning and event.type == TIME_UP or event.type == QUIT or keys and keys[Q]:
self.gameOver = True
highscore = open("highscore.txt", "r")
currentHighScore = int(highscore.readline())
#Write the highscore
if currentHighScore < self.score:
highscore = open("highscore.txt", "w")
highscore.write(str(self.score))
#highscore.write(str(self.score))
break
self.moveWoodstocks()
self.snoopy.move(joyStick, keys)
self.draw()
pygame.display.update()
# Game End State
self.snoopy.stop()
while end:
#print "drawing the end"
self.drawTheEnd()
keys = pygame.key.get_pressed()
if joyStick.get_button(0):
runGame = True
self.re_init()
end = False
break
for event in pygame.event.get():
if event.type == QUIT or keys and keys[Q]:
return
if keys and keys[R]: #Restart game
runGame = True
self.re_init()
end = False
break
pygame.display.update()
def createWoodstocks(self):
# There is one woodstock for each speed
# The first one is vertical speed. Second one is the rotational speed
speeds = [(4,10), (8,20), (12,30), (16, 40)]
woodstocks = []
for speedPair in speeds:
speed, rspeed = speedPair
w = Woodstock(self)
w.setVSpeed(speed)
w.setRSpeed(rspeed)
woodstocks += [w]
return woodstocks
# Checks if any of the woodstocks have collided with snoopy
def resetDeadWoodstocks(self):
for w in self.woodstocks:
# Woodstock is dead if he hits the bottom or if Snoopy catches him
if w.isAtBottomEdge() or w.hasCollidedWith(self.snoopy):
if(w.hasCollidedWith(self.snoopy)):
self.score += 1
vspeed = w.vspeed
rspeed = w.rspeed
self.woodstocks.remove(w)
w = Woodstock(self)
w.setVSpeed(vspeed)
w.setRSpeed(rspeed)
self.woodstocks.append(w)
def moveWoodstocks(self):
for w in self.woodstocks:
w.move()
def draw(self):
# Redraw the background and the game characters
self.drawBackground()
for w in self.woodstocks:
self.screen.blit(w.image, w.rect)
self.screen.blit(self.snoopy.image, self.snoopy.rect)
highscore = open("highscore.txt", "r")
currentHighScore = highscore.readline()
self.scoreText = self.font.render("Score: " + str(self.score), True, COLOR_BLACK)
self.screen.blit(self.scoreText, self.scorepos)
self.timerText = self.fontRegular.render("Time Left: " + str(self.time_left()), True, COLOR_BLACK)
self.screen.blit(self.timerText, self.timerpos)
self.highscoreText = self.font.render("High Score: " + currentHighScore, True, COLOR_BLACK)
self.screen.blit(self.highscoreText, self.highscorepos)
pygame.display.update()
def drawBackground(self):
self.drawSky()
def drawSky(self):
skyimage = pygame.image.load(os.path.join("assets/images/sky", "sky1.jpg"))
skyimage = pygame.transform.scale(skyimage, (self.width, self.height))
skyrect = skyimage.get_rect()
self.screen.blit(skyimage, skyrect)
def drawTheEnd(self):
# Draw the background
self.drawBackground()
# Draw the final score
self.scoreText = self.fontRegular.render("Score: " + str(self.score), True, COLOR_BLACK)
self.scorepos = self.scoreText.get_rect(centerx = self.width/2, centery = self.height/2 - 50)
self.screen.blit(self.scoreText, self.scorepos)
# Draw "The End"
theEnd = self.fontRegular.render("The End", True, COLOR_BLACK)
theEndpos = theEnd.get_rect(centerx = self.width/2, centery = self.height/2)
self.screen.blit(theEnd, theEndpos)
# Draw Instructions Text
self.instructions = self.fontSmall.render("[R] to start a new game, [ESC] to quit", True, COLOR_BLACK)
self.instructionsPos = instructions.get_rect(centerx = self.width/2, centery = self.height/2 + 50)
self.screen.blit(self.instructions, self.instructionsPos)
pygame.display.update()
game = Game(1250, 750)
game.run()