-
Notifications
You must be signed in to change notification settings - Fork 0
/
newgame.py
299 lines (247 loc) · 8.06 KB
/
newgame.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python
import random, os.path
#import basic pygame modules
import pygame
from pygame.locals import *
#game constants
BACKGROUNDCOLOR = [255, 255, 0]
MAX_SHOTS = 2 #most player bullets onscreen
BOMB_ODDS = 60 #chances a new bomb will drop
SCREENRECT = Rect(0, 0, 1024, 1024)
SCORE = 0
walls = []
main_dir = os.path.split(os.path.abspath(__file__))[0]
def load_image(file):
"loads an image, prepares it for play"
file = os.path.join(main_dir, 'assets', file)
try:
surface = pygame.image.load(file)
surfaceBig = pygame.transform.scale(surface, (64, 64))
surfaceBig.set_colorkey((255, 0, 255))
except pygame.error:
raise SystemExit('Could not load image "%s" %s'%(file, pygame.get_error()))
return surfaceBig.convert()
def load_images(*files):
imgs = []
for file in files:
imgs.append(load_image(file))
return imgs
winstyle = 0
# Initialize pygame
pygame.init()
# Set the display mode
winstyle = 0 # |FULLSCREEN
bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32)
screen = pygame.display.set_mode(SCREENRECT.size, winstyle, bestdepth)
#decorate the game window
#create the background, tile the bgd image
background = pygame.Surface(SCREENRECT.size)
screen.blit(background, (0,0))
screen.fill(BACKGROUNDCOLOR)
pygame.display.flip()
#see if we can load more than standard BMP
if not pygame.image.get_extended():
raise SystemExit("Sorry, extended image module required")
# each type of game object gets an init and an
# update function. the update function is called
# once per frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
# the keyboard
class wall(pygame.sprite.Sprite):
images = []
def __init__(self, pos):
walls.append(self)
self.image = self.images[0]
self.rect = self.image.get_rect(center=pos)
# self.rect = pygame.Rect(pos[0], pos[1], 64, 64)
class player(pygame.sprite.Sprite):
# speed = 10
bounce = 24
gun_offset = -11
frontImages = []
sideXImages = []
sideYImages = []
backImages = []
def __init__(self, pos, speed=5):
pygame.sprite.Sprite.__init__(self, self.containers)
self.speed = speed
self.xDir = 0
self.yDir = 0
self.curentImages = self.frontImages
self.image = self.frontImages[0]
self.rect = self.image.get_rect(center=pos)
self.rect.size = (64, 64)
self.reloading = 0
self.origtop = self.rect.top
self.facing = -1
def move(self, xDir, yDir):
for wall in walls:
if self.rect.colliderect(wall.rect):
if xDir < 0:
# self.rect.left = wall.rect.right +
self.rect.left = wall.rect.right + 5
return
if xDir > 0:
self.rect.right = wall.rect.left - 5
return
if yDir > 0:
self.rect.bottom = wall.rect.top - 5
return
if yDir < 0:
self.rect.top = wall.rect.bottom + 5
return
if xDir:
self.facingX = xDir
self.rect.move_ip(xDir*self.speed, 0)
self.rect = self.rect.clamp(SCREENRECT)
self.xDir = xDir
if yDir:
self.facingY = yDir
self.rect.move_ip(0, yDir*self.speed)
self.rect = self.rect.clamp(SCREENRECT)
self.yDir = yDir
if yDir > 0:
self.curentImages = self.frontImages
elif yDir < 0:
self.curentImages = self.backImages
if xDir > 0:
self.curentImages = self.sideYImages
self.image = self.curentImages[0]
elif xDir < 0:
self.curentImages = self.sideXImages
self.image = self.curentImages[0]
self.image = self.curentImages[0]
# self.rect.top = self.origtop - (self.rect.left//self.bounce%2)
def gunpos(self):
pos = self.facing*self.gun_offset + self.rect.centerx
return pos, self.rect.top
class shot(pygame.sprite.Sprite):
speed = -11
images = []
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images
class Score(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.Font(None, 20)
self.font.set_italic(1)
self.color = Color('white')
self.lastscore = -1
self.update()
self.rect = self.image.get_rect().move(10, 32)
def update(self):
if SCORE != self.lastscore:
self.lastscore = SCORE
msg = "Score: %d" % SCORE
self.image = self.font.render(msg, 0, self.color)
#Load images, assign to sprite classes
#(do this before the classes are used, after screen setup)
img1 = load_image('player1Front1.gif')
img2 = load_image('player1Front2.gif')
player.frontImages = [img1, img2]
img3 = load_image('player1Side1.gif')
img4 = load_image('player1Side2.gif')
player.sideXImages = [img3, img4]
player.sideYImages = [pygame.transform.flip(img3, 1, 0), pygame.transform.flip(img4, 1, 0)]
img5 = load_image('player1Back1.gif')
img6 = load_image('player1Back2.gif')
player.backImages = [img5, img6]
img7 = load_image('floor.gif')
wall.images = [img7]
# Initialize Game Groups
all = pygame.sprite.RenderUpdates()
#assign default groups to each sprite class
player.containers = all
wall.containers = all
Score.containers = all
#Create Some Starting Values
global score
kills = 0
clock = pygame.time.Clock()
#initialize our starting sprites
#global SCORE
player1 = player((512, 512))
if pygame.font:
all.add(Score())
level = [
"WWWWWWWWWWWWWWWW",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"WWWWWWWWWWWWWWWW"
]
x = y = 0
for row in level:
for col in row:
if col == "W":
wall((x,y))
x +=64
y += 64
x = 0
while player1.alive():
screen.fill(BACKGROUNDCOLOR)
#get input
for event in pygame.event.get():
if event.type == QUIT or \
(event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
keystate = pygame.key.get_pressed()
# clear/erase the last drawn sprites
all.clear(screen, background)
#update all the sprites
all.update()
xDir = player1.xDir
yDir = player1.yDir
for event in pygame.event.get():
if event.type == QUIT :
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
# if snake.
# if .key == pygame.K_UP :
# yDir = -1
# if event.key == pygame.K_DOWN :
# yDir = 1
# if event.key == pygame.K_LEFT :
# xDir = -1
# if event.key == pygame.K_RIGHT :
# xDir = 1
xDir = keystate[K_RIGHT] - keystate[K_LEFT]
yDir = keystate[K_DOWN] - keystate[K_UP]
#handle player input
# if keystate
# direction = keystate[K_RIGHT] - keystate[K_LEFT]
player1.move(xDir, yDir)
# firing = keystate[K_SPACE]
# player1.reloading = firing
#draw the scene
dirty = all.draw(screen)
pygame.display.update(dirty)
#cap the framerate
clock.tick(60)
# for wall in walls:
pygame.draw.rect(screen, (255, 255, 255), wall.rect)
pygame.display.flip()
if pygame.mixer:
pygame.mixer.music.fadeout(1000)
pygame.time.wait(1000)
pygame.quit()
#call the "main" function if running this script
#if __name__ == '__main__': main()