-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
308 lines (201 loc) · 8.09 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
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
299
300
301
302
303
304
305
306
307
308
import pygame as PG, time, random, sys, datetime
import actors, constants, lists, toolsV2, controls
import loadImages, dialogs
scaledDir = (1, 1)
addDir = (1, 1)
#don't forget this!sdd
#from constants import *
print 'start2'
##----------------------------------------------------------------------
#def getScreen():
#"""returns the screen object """
#return screen
def init():
'''initializes screen'''
global screen, background_surface
#initializes pygame
PG.init()
#starts the screen
screen = PG.display.set_mode((constants.WIDTH, constants.HEIGHT))
#set Icon, may not work off windows
icon = loadImages.loadImages(r'./art/man/', 'png')
icon = icon[7]
PG.display.set_icon(icon)
#fills the screen with a color
screen.fill(constants.BACKGROUND)
#loads the background image
background_surface = loadImages.loadImages(r'./art/backgrounds/',
r'pond.png',
(screen.get_rect().w,
screen.get_rect().h))
background_surface = background_surface.convert()
screen.blit(background_surface, (0, 0))
#loads joyticks to gamePad list
controls.initJoysticks()
global imageList
imageList = loadImages.loadImages(r'./art/man/', 'png')
#create a first Anything
actors.spawnAnything(screen, imageList, (400, 400))
actors.Shooter(lists.ANYTHINGs[0])
PG.display.flip()
return screen
#----------------------------------------------------------------------
def gameLogic():
"""handles movement and AI"""
for anything in lists.ANYTHINGs:
#update, right now only checking to see if facing == direction
anything.update()
for bullet in lists.BULLETs:
bullet.update()
pass
#----------------------------------------------------------------------
def drawUI():
"""basic ui info, name health kills xp"""
#first text box
plrHP = lists.ANYTHINGs[0].components['shooter'].curHP
plrKls = lists.ANYTHINGs[0].components['shooter'].kills
text1 = 'Heath: {hp}\nKills: {kills}'.format(hp = plrHP, kills = plrKls)
popupbox1 = dialogs.popUp(str(text1), background=(12, 214, 0))
screen.blit(popupbox1, (0, 0))
#second text bos
text2 = 'will be a target\'s name'
popupbox2 = dialogs.popUp(str(text2), background=(12, 214, 0))
screen.blit(popupbox2, (popupbox1.get_rect().w, 0))
#----------------------------------------------------------------------
def angleDrawer():
"""draws lines that represent angle between mouse and player dir"""
plr = lists.ANYTHINGs[0]
#player pos
pos = plr.pos()
#direction 25 spaces away
dir = toolsV2.vectors.scale(plr.direction, 250)
dir = toolsV2.vectors.add(dir, pos)
#mouse pos
mPos = PG.mouse.get_pos()
for lines in ((pos, dir), (pos, mPos)):
PG.draw.line(screen, constants.BLUE, lines[0], lines[1], 2)
#----------------------------------------------------------------------
def drawItems(functions):
"""calls all functions in list"""
for func in functions:
eval('{}()'.format(func))
#----------------------------------------------------------------------
def drawing():
"""draws objects to screen"""
try:
try:
#paint background
screen.blit(background_surface, (0, 0))
drawUI()
drawItems(lists.FUNCs)
except IOError:
print 'IO ERROR'
#draw anythings
for thing in lists.ANYTHINGs:
thing.draw(screen)
#draw lines to represent angles, if lines is true
lines = 1
if lines:
addDir = toolsV2.vectors.linkScaleVector(lists.ANYTHINGs[0].pos(),
lists.ANYTHINGs[0].direction,
100)
points = [lists.ANYTHINGs[0].pos(), PG.mouse.get_pos(), addDir]
PG.draw.lines(screen, constants.BLACK, True, points, 1)
#draw line to represent players direction
adjDir = toolsV2.vectors.scale(lists.ANYTHINGs[0].direction, 25)
adjDir = toolsV2.vectors.add(lists.ANYTHINGs[0].pos(), adjDir)
PG.draw.line(screen, constants.GREEN,
lists.ANYTHINGs[0].pos(), addDir)
#-----
for bullet in lists.BULLETs:
bullet.move()
bullet.draw(screen)
##drawing grid where player is
#try:
##pos_list = []
##pos_tiles = []
#for tile in lists.all_tiles:
#for thing in lists.ANYTHINGs:
#pos = thing.pos()
#if tile.range_x[0] <= pos[0] <= tile.range_x[1]:
#if tile.range_y[0] <= pos[1] <= tile.range_y[1]:
##pos_tiles.append(tile)
##screen.blit(tile.draw(), tile.rect.topleft)
#pass
##print len(pos_tiles)
#except Exception as e:
#print e
#pass
#flip display to monitor
PG.display.flip()
except PG.error, message:
print '\nGame Exited:'
print message, '\n'
#except UnboundLocalError:
#screen.bllit(lists.flames[1], (100, 100))
#flameNum = 0
pass
##----------------------------------------------------------------------
#def drawFlames(surface):
#"""draw the flames that switch frames 3 times per second"""
##draw the flames that switch frames 3 times per second
#flame = lists.flames[constants.flameNum]
#if constants.frameNum % 15 == 0:
#if constants.flameNum >= len(lists.flames) - 1:
#constants.flameNum = 0
#else:
#constants.flameNum += 1
#pass
##blit to surface
#surface.blit(flame, (100, 100))
#----------------------------------------------------------------------
def mainloop():
"""main game loop"""
global oldFrames
#create a timer for FPS
fpsTimer = PG.time.Clock()
#init of fps counter
#t = 0.0
dt = 1 / float(constants.FRAMERATE)
currentTime = float(PG.time.get_ticks())
oldFrames = time.clock()
#frame counter
constants.frameNum = 0
print 'starting main loop'
global gameRunning, delta
gameRunning = True
while gameRunning:
#fps
newTime = float(PG.time.get_ticks())
frameTime = float(newTime) - float(currentTime)
currentTime = newTime
#if quit event in in queue, quit
if PG.event.peek(PG.QUIT):
gameRunning = False
break
#handle input
controls.inputHandler(screen)
#while frameTime > 0.0:
##print frameTime
#deltaTime = min(frameTime, dt)
##run logic
#gameLogic()
##frameTime -= deltaTime
##t += deltaTime
#draw to screen
drawing()
##limit framerate and find the delta time
diff = fpsTimer.tick(constants.FRAMERATE)
#print delta, constants.FRAMERATE
#constants.frameNum += 1
#every second, do:
if constants.frameNum % constants.FRAMERATE == 0:
pass
else: print 'else exit loop'
print 'mainloop is over, thanks for playing'
if __name__ == '__main__':
"""Run the following if module is top module"""
screen = init()
mainloop()
else:
print __name__, 'wasnt ran as mainfile,', __package__