forked from emocallaghan/FinalProjectRobotLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtuailWorldIterationOne.py
More file actions
367 lines (241 loc) · 10.1 KB
/
virtuailWorldIterationOne.py
File metadata and controls
367 lines (241 loc) · 10.1 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 2 13:54:39 2014
@author: eocallaghan
"""
import pygame
from pygame.locals import *
import time
from abc import ABCMeta, abstractmethod
class Model:
""" Encodes the game state of Galaga and keeps track of all attributes
contains a fighter, a list of bullets fired by my fighter, a list of enemies
and a list of bullets fired by the enemy"
"""
def __init__(self, size):
""" contructor for the GalagaModel class"""
self.y = size[0]
self.x = size[1]
self.robot = Robot()
self.myWalls = []
self.brick = 50
self.newWall(100,100,self.brick,100)
self.newWall(0,0, self.x, self.brick)
self.newWall(0,0, self.brick,self.y)
self.newWall(self.y-self.brick,0,self.x,self.brick)
self.newWall(0,self.x-self.brick,self.brick, self.y)
def newWall(self, x,y, height, width):
"""this function creates a new bullet fired from fighter at the passed in locations, x and y"""
color = (0,0,0)
self.myWalls.append(Wall(color,x,y,height,width))
def update(self):
"""updates fighter, all bullets, and all enemies"""
self.robot.update()
class Collidable(object):
__metaclass__ = ABCMeta
@abstractmethod
def collide(self, collidable):
pass
@abstractmethod
def perimeter(self):
pass
class Robot():
"""fighter class contains a contruction and update method"""
def __init__(self):
"""constructor for fighter sets location based ppppp given values, number of lives, pixle height
and width inital speed as zero and loads the image of a fighter"""
self.x = 400
self.y = 600
self.vx = 0
self.vy = 0
self.radius = 40
self.color = pygame.Color(170,50,255)
self.front = (self.x,self.y-self.radius)
self.direction = 0
self.directionBeforeCollision = 0
self.canForward = True
self.collided = False
self.previousX = self.x
self.previousY = self.y
def collide(self,collidable):
pass
def update(self):
"""moves position based on velocity unless at either side of screen and attempting to move
off screen where it is stopped"""
"""print("In update")
print(self.canForward)
print(self.direction)
print self.directionBeforeCollision"""
if(not (self.x == self.previousX and self.y == self.previousY)):
self.collided = False
if(self.canForward):
self.move()
elif(self.direction == self.directionBeforeCollision):
self.stop()
else:
self.canForward = True
self.move()
def move(self):
self.x += self.vx
self.y += self.vy
if(self.direction == 0):
self.front = (self.x, self.y - self.radius)
if(self.direction == 90):
self.front = (self.x - self.radius, self.y)
if(self.direction == 180):
self.front = (self.x, self.y+self.radius)
if(self.direction == 270):
self.front = (self.x + self.radius, self.y)
def stop(self):
self.vx = 0
self.vy = 0
def foward(self):
if(self.direction == 0):
self.vy = -1
self.vx = 0
if(self.direction == 90):
self.vy = 0
self.vx = -1
if(self.direction == 180):
self.vy = 1
self.vx = 0
if(self.direction == 270):
self.vy = 0
self.vx = 1
def turnLeft(self):
self.stop()
if(self.direction == 0):
self.front = (self.x - self.radius, self.y)
self.direction = 90
elif(self.direction == 90):
self.front = (self.x, self.y+self.radius)
self.direction = 180
elif(self.direction == 180):
self.front = (self.x + self.radius, self.y)
self.direction = 270
elif(self.direction == 270):
self.front = (self.x, self.y - self.radius)
self.direction = 0
def turnRight(self):
self.stop()
if(self.direction == 0):
self.front = (self.x + self.radius, self.y)
self.direction = 270
elif(self.direction == 270):
self.front = (self.x, self.y+self.radius)
self.direction = 180
elif(self.direction == 180):
self.front = (self.x - self.radius, self.y)
self.direction = 90
elif(self.direction == 90):
self.front = (self.x, self.y - self.radius)
self.direction = 0
def setDirectionBeforeCollision(self, direction):
self.directionBeforeCollision = direction
class Wall:
"""bullet class contains a constructor and update"""
def __init__(self,color,x, y,height,width):
"""sets color, height, width, position, and velocity based on passed in parameters"""
self.color = color
self.height = height
self.width = width
self.x = x
self.y = y
class PyGameWindowView:
""" A view of Galaga rendered in a Pygame window """
def __init__(self,model,screen):
self.model = model
self.screen = screen
def draw(self):
"""draws all of the elements on the screen uses a series of subfunctions
to draw fighters, bullets, and enemy"""
self.screen.fill(pygame.Color(255,255,255))
for wall in self.model.myWalls:
self.drawWall(wall)
self.drawRobot(self.model.robot)
pygame.display.update()
def drawWall(self, wall):
"""draws a rectangle for a bullet based on passed in bullet and its parameters"""
rectangle = pygame.Rect(wall.x,wall.y,wall.width,wall.height)
pygame.draw.rect(self.screen, wall.color, rectangle)
def drawRobot(self, robot):
"""draws a fighter from what is passed in"""
pygame.draw.circle(self.screen, robot.color, (robot.x, robot.y), robot.radius, 0)
pygame.draw.circle(self.screen, (0,0,0), robot.front, 5)
class PyGameKeyboardController:
""" Handles keyboard input for galaga"""
def __init__(self,model):
"""contructor just sets the model to model"""
self.model = model
def handle_keyboard_event(self,event):
"""sets how the fighter moves and if the user shoots a bullet"""
if event.type == KEYDOWN:
"""if the a key is pushed and held down the fighter moves to the left if the d
key is pushed and held down the fighter moves to the right if the space bar is hit
a bullet is created"""
robot = self.model.robot
if event.key == pygame.K_a:
robot.turnLeft()
if event.key == pygame.K_d:
robot.turnRight()
if event.key == pygame.K_w:
robot.foward()
if event.type == KEYUP:
"""if the a key is lifted up and the player is traveling to the left the player will stop.
if the d key is lifted and the player is traveling to the right the player will stop."""
if event.key == pygame.K_w:
self.model.robot.stop()
class CollisionController:
"""tests if any two objects colloide"""
def __init__(self, model):
self.model = model
def checkCollisions(self):
for wall in self.model.myWalls:
robot = self.model.robot
x1 = robot.x-robot.radius
y1 = robot.y - robot.radius
width1 = robot.radius*2
height1 = robot.radius*2
x2 = wall.x
y2 = wall.y
width2 = wall.width
height2 = wall.height
if (self.sameSpace(x1, y1, width1, height1, x2, y2, width2, height2)):
"""checks if the fighter is in the same place as any of the basic enemies and removes the enemy and takes away a life from the
fighter if this is the case. if the fighter has no more lifes closes window and prints that the player
lost and prints the score"""
if(not self.model.robot.collided):
self.model.robot.setDirectionBeforeCollision(self.model.robot.direction)
self.model.robot.canForward = False
print("In Collision")
print(self.model.robot.direction)
print(self.model.robot.directionBeforeCollision)
self.model.robot.collided = True
print (self.model.robot.collided)
self.model.robot.previousX = self.model.robot.x
self.model.robot.previousY = self.model.robot.y
def sameSpace(self, x1, y1, width1, height1, x2, y2, width2, height2):
"""function checks if thetwo objects occupy the same space."""
return (x2<= x1+width1 and x2+width2>= x1 and y2 <= y1+height2 and y2+height2 >= y1)
if __name__ == '__main__':
pygame.init()
size = (1000,700)
screen = pygame.display.set_mode(size)
model = Model(size)
view = PyGameWindowView(model,screen)
KeyBoardcontroller = PyGameKeyboardController(model)
collisionController = CollisionController(model)
running = True
startTime = time.time()
xTime = time.time()
while running:
collisionController.checkCollisions()
for event in pygame.event.get():
if event.type == QUIT:
running = False
KeyBoardcontroller.handle_keyboard_event(event)
collisionController.checkCollisions()
model.update()
view.draw()
time.sleep(.001)
pygame.quit()