-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworld.py
More file actions
133 lines (112 loc) · 3.88 KB
/
world.py
File metadata and controls
133 lines (112 loc) · 3.88 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
""" world/play field game entity """
import xml.etree.ElementTree as ET
from tilengine import Tilemap, Spriteset, Sequence
from effect import Effect
from score import Score
from eagle import Eagle
from opossum import Opossum
import game
class Tiles:
""" types of tiles for sprite-terrain collision detection """
Empty, Floor, Gem, Wall, SlopeUp, SlopeDown, InnerSlopeUp, InnerSlopeDown = range(8)
class Medium:
""" types of environments """
Floor, Air, Ladder, Water = range(4)
class Item(object):
""" Generic item declared in tilemap object layer awaiting to spawn """
Opossum, Eagle, Frog = range(3)
def __init__(self, item_type, x, y):
self.type = item_type
self.x = x
self.y = y
self.alive = False
def try_spawn(self, x):
""" Tries to spawn an active game object depending on screen position and item type """
if self.alive is False and x < self.x < x + game.WIDTH:
self.alive = True
if self.type is Item.Eagle:
Eagle(self, self.x, self.y - Eagle.size[1])
elif self.type is Item.Opossum:
Opossum(self, self.x, self.y - Opossum.size[1])
def load_objects(file_name, layer_name, first_gid):
""" loads tiles in object layer from a tmx file.
Returns list of Item objects """
tree = ET.parse(file_name)
root = tree.getroot()
for child in root.findall("objectgroup"):
name = child.get("name")
if name == layer_name:
item_list = list()
for item in child.findall("object"):
gid = item.get("gid")
if gid is not None:
x = item.get("x")
y = item.get("y")
item_list.append(
Item(int(gid) - first_gid, int(x), int(y)))
return item_list
return None
class World(object):
""" world/play field entity """
def __init__(self):
self.foreground = game.engine.layers[1]
self.background = game.engine.layers[2]
self.clouds = 0.0
self.foreground.setup(Tilemap.fromfile("layer_foreground.tmx"))
self.background.setup(Tilemap.fromfile("layer_background.tmx"))
self.spriteset_vanish = Spriteset.fromfile("effect_vanish")
self.seq_vanish = Sequence.create_sprite_sequence(self.spriteset_vanish, "vanish", 4)
self.x = 0
self.x_max = self.foreground.width - game.WIDTH
self.objects = load_objects(game.ASSETS_PATH +
"/layer_foreground.tmx", "Capa de Objetos 1", 973)
game.engine.set_background_color(self.background.tilemap)
game.actors.append(self)
def start(self):
self.time = 30
self.add_timer(0)
game.ui.update_time(self.time)
def pick_gem(self, tiles_list):
""" updates tilemap when player picks a gem """
for tile_info in tiles_list:
if tile_info.type is Tiles.Gem:
self.foreground.tilemap.set_tile(
tile_info.row, tile_info.col, None)
Effect(tile_info.col*16, tile_info.row*16,
self.spriteset_vanish, self.seq_vanish)
game.sounds.play("pickup", 1)
self.add_timer(1)
Score(1, tile_info.col*16, tile_info.row*16)
break
def add_timer(self, amount):
""" increases/decreases timer timer """
self.due_time = game.window.get_ticks() + 1000
if amount >= 0:
self.time += amount
else:
amount = -amount
if self.time >= amount:
self.time -= amount
else:
self.time = 0
game.ui.update_time(self.time)
def update(self):
""" updates world state once per frame """
oldx = self.x
if game.player.x < 240:
self.x = 0
else:
self.x = int(game.player.x - 240)
if self.x > self.x_max:
self.x = self.x_max
self.clouds += 0.1
if self.x is not oldx:
self.foreground.set_position(self.x, 0)
self.background.set_position(self.x/8, 0)
# spawn new entities from object list
for item in self.objects:
item.try_spawn(self.x)
now = game.window.get_ticks()
if now > self.due_time:
self.add_timer(-1)
return True