forked from zcr1/OSGCC6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.py
185 lines (155 loc) · 6 KB
/
level.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
import pygame, os
from pygame.locals import *
from platform import *
from background import *
from enemy import *
from item import *
import copy
import math
class Level():
def __init__(self, world):
self.world = world
platformpath = os.path.dirname(os.path.dirname( os.path.realpath( __file__ ) ) ) + "/OSGCC6/datafiles/level1.dat"
f = open(platformpath)
allLines = f.readlines()
self.platforms = pygame.sprite.Group()
self.drawGroup = pygame.sprite.Group() #which sprties are in view to draw
self.movePlatforms = pygame.sprite.Group()
self.bg1 = Background("background1.png", "background2.png")
self.bg2 = Background("background_2-1.png", "background_2-2.png")
self.fg = Background("foreground1.png", "foreground1.png")
for i in range (1, len(allLines)):
words = allLines[i].split(" ")
plat = Platform([(int)(words[0]),(int)(words[1])],(int)(words[2]), (words[3]), (int)(words[4]))
if plat.moveX or plat.moveY:
self.movePlatforms.add(plat)
else:
self.platforms.add(plat)
# Add enemies to level
enemy_dat_path = os.path.dirname(os.path.dirname( os.path.realpath( __file__ ) ) ) + "/OSGCC6/datafiles/enemy.dat"
f = open(enemy_dat_path)
allLines = f.readlines()
self.enemies = pygame.sprite.Group()
for i in range (1, len(allLines)):
words = allLines[i].split(" ")
x = (int)(words[0])
y = (int)(words[1])
height = (int)(words[2])
width = (int)(words[3])
enemType = (int)(words[4])
enem = Enemy([x, y], self.world, pygame.time.Clock(), enemType)
self.enemies.add(enem)
# Add items to level
item_dat_path = os.path.dirname(os.path.dirname( os.path.realpath( __file__ ) ) ) + "/OSGCC6/datafiles/item.dat"
f = open(item_dat_path)
allLines = f.readlines()
self.items = pygame.sprite.Group()
for i in range (1, len(allLines)):
words = allLines[i].split(" ")
x = (int)(words[0])
y = (int)(words[1])
ite = Item([x, y], words[2], words[3], (int)(words[4]))
self.items.add(ite)
def Update(self):
for sprite in self.enemies:
sprite.Update()
#check collisions with objects
def checkCollision(self, obj, newPos):
newrect = copy.deepcopy(obj.rect)
newrect.center = newPos
for platform in self.platforms:
newPlat = copy.deepcopy(platform.rect)
newPlat.center = platform.worldPos
#print newrect.center
if newrect.colliderect(newPlat):
return platform
return None
#check collisions with items
def checkItemCollision(self, obj, newPos):
newrect = copy.deepcopy(obj.rect)
newrect.center = newPos
for item in self.items:
newItem = copy.deepcopy(item.rect)
newItem.center = item.worldPos
if newItem.colliderect(newrect):
return item
return None
#check collisions with enemy when shooting beans
def checkCollisionEnemy(self, obj):
for enemy in self.enemies:
if enemy.rect.colliderect(obj.rect):
#if newRec.colliderect(obj.rect):
enemy.kill()
return True
return None
def reieveCheckCollisionEnemy(self, player):
playerrect = copy.deepcopy(player.rect)
playerrect.center = player.worldPos
for obj in self.world.enemyObjects:
newrect = copy.deepcopy(obj.rect)
newrect.center = obj.worldPos
if newrect.colliderect(playerrect):
obj.kill()
return True
return False
#check collision between enemies and player (player may actually be enemy)
def checkEnemyCollision(self, player, newPos):
newrect = copy.deepcopy(player.rect)
newrect.center = newPos
for enemy in self.enemies:
if enemy == player:
pass
else:
enemyrect = copy.deepcopy(enemy.rect)
enemyrect.center = enemy.worldPos
if enemyrect.colliderect(newrect):
return enemy
return None
def checkCollisionMoving(self, obj, newPos):
newrect = copy.deepcopy(obj.rect)
newrect.center = newPos
for plat in self.movePlatforms:
if plat == obj:
pass
else:
enemyrect = copy.deepcopy(plat.rect)
enemyrect.center = plat.worldPos
if enemyrect.colliderect(newrect):
return plat
return None
def Draw(self):
currentPos = copy.deepcopy(self.world.player.worldPos) #players current worldPos
self.bg1.Draw(self.world.screen)
self.bg2.Draw(self.world.screen)
self.drawGroup.empty()
#screen = pygame.Rect((currentPos[0] - 800,currentPos[1] + 450),(800,450))
for platform in self.movePlatforms:
platform.updatePos()
if (platform.worldPos[0] >= (currentPos[0] - 1600)) and (platform.worldPos[0] <= (currentPos[0] + 1600)):
platform.rect.center = [800 - (currentPos[0] - platform.worldPos[0]), 450 - (currentPos[1] - platform.worldPos[1])]
self.drawGroup.add(platform)
for platform in self.platforms:
if platform.gravyVent:
platform.updatePos()
if (platform.worldPos[0] >= (currentPos[0] - 1600)) and (platform.worldPos[0] <= (currentPos[0] + 1600)):
platform.rect.center = [800 - (currentPos[0] - platform.worldPos[0]), 450 - (currentPos[1] - platform.worldPos[1])]
#print platform.rect.center[1]
#print platform.rect.center
self.drawGroup.add(platform)
for obj in self.world.objects:
if (obj.worldPos[0] >= (currentPos[0] - 2000)) and (obj.worldPos[0] <= (currentPos[0] + 2000)):
obj.rect.center = [800 - (currentPos[0] - obj.worldPos[0]), 450 - (currentPos[1] - obj.worldPos[1])]
self.drawGroup.add(obj)
for obj in self.enemies:
if (obj.worldPos[0] >= (currentPos[0] - 2000)) and (obj.worldPos[0] <= (currentPos[0] + 2000)):
obj.rect.center = [800 - (currentPos[0] - obj.worldPos[0]), 450 - (currentPos[1] - obj.worldPos[1])]
self.drawGroup.add(obj)
for obj in self.items:
if (obj.worldPos[0] >= (currentPos[0] - 2000)) and (obj.worldPos[0] <= (currentPos[0] + 2000)):
obj.rect.center = [800 - (currentPos[0] - obj.worldPos[0]), 450 - (currentPos[1] - obj.worldPos[1])]
self.drawGroup.add(obj)
for obj in self.world.enemyObjects:
if (obj.worldPos[0] >= (currentPos[0] - 2000)) and (obj.worldPos[0] <= (currentPos[0] + 2000)):
obj.rect.center = [800 - (currentPos[0] - obj.worldPos[0]), 450 - (currentPos[1] - obj.worldPos[1])]
self.drawGroup.add(obj)
self.drawGroup.draw(self.world.screen)