/
main.py
141 lines (122 loc) · 5.18 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
import os
import sys
import kivy
kivy.require('1.8.0')
from kivy.app import App
from kivy.core.window import Window
from kivy.config import Config
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
import glo
from gameboard import GameBoard
from player import Player
from tiles import *
class FilGame(GridLayout):
def __init__(self, **kwargs):
super(FilGame, self).__init__(**kwargs)
# define tile types
self.tiles = [] # holds all tile widgets
self.types = {
#'arrow': {'id': 1, 'rot': True, 'mov': True},
#'start': {'id': 2, 'rot': False, 'mov': False},
'movchair': {'id': 1, 'rot': False, 'mov': True},
'rotchair': {'id': 2, 'rot': True, 'mov': True},
'couch3m': {'id': 4, 'rot': False, 'mov': False},
}
self.images = self.setup_images()
# set background image
self.bg = Image(source=os.path.join('assets', 'bg.png'), pos=(0,0),
width=glo.const.WIDTH, height=glo.const.HEIGHT)
self.bg.allow_stretch = True
self.bg.keep_ratio = False
self.add_widget(self.bg)
# set up game board
self.board = GameBoard()
self.board.load()
# place tiles for starting level
self.setup()
def setup_images(self):
images = {}
# setup player
images['player'] = []
for i in range(4): # 0, 1, 2, 3
images['player'].append(os.path.join('assets', 'player_'+str(i)+'.png'))
# setup tiles
for key in self.types:
images[key] = []
for i in range(4): # 0, 1, 2, 3
images[key].append(os.path.join('assets', key+'_'+str(i)+'.png'))
return images
def setup(self):
# i[0] = row number
# i[1] = column number
# i[2] = tile number and rotation
# i[2][0] = tile number
# i[2][1] = rotation (0=up,1=right,2=down,3=left)
# place the tiles
for i in self.board.boarditer():
r, c = (int(i[0]), int(i[1]))
tile_type = int(i[2][0])
rotation = int(i[2][1])
for t in self.types:
maketile = False # flag; gets set to True when matching tile type found
if tile_type == 0: # blank space
break
if tile_type == int(self.types[t]['id']):
maketile = True
# tile can be rotated and moved pass
if self.types[t]['rot'] and self.types[t]['mov']:
newtile = MovRotTile(self.images[t], rotation,
source=self.images[t][rotation],
width=glo.const.TILE_SIZE, height=glo.const.TILE_SIZE)
elif self.types[t]['rot']: # tile can only be rotated
newtile = RotTile(self.images[t], rotation,
source=self.images[t][rotation],
width=glo.const.TILE_SIZE, height=glo.const.TILE_SIZE)
elif self.types[t]['mov']: # tile can only be moved
newtile = MovTile(self.images[t], rotation,
source=self.images[t][rotation],
width=glo.const.TILE_SIZE, height=glo.const.TILE_SIZE)
else: # tile can't be moved or rotated
newtile = Tile(source=self.images[t][rotation],
width=glo.const.TILE_SIZE, height=glo.const.TILE_SIZE)
else: #! should never get here
pass
if maketile:
newtile.pos = glo.coord2pos(r, c)
self.tiles.append(newtile)
self.add_widget(newtile)
# place the player tile
self.ptile = PlayerTile(self.images['player'], self.board.player.get_rot(),
width=glo.const.TILE_SIZE, height=glo.const.TILE_SIZE)
self.ptile.pos = glo.coord2pos(self.board.entrance)
self.ptile.set_last = (self.board.entrance)
glo.const.pcoord = self.board.entrance
self.add_widget(self.ptile)
# delete all widgets before loading the next level's tiles
def clear(self):
for tile in self.tiles:
self.remove_widget(tile)
print('* Tiles cleared')
class FilApp(App):
def build(self):
self.set_sizes()
self.title = "Floor is Lava"
Config.set('graphics', 'resizable', 0)
#Config.set('graphics', 'height', str(glo.const.HEIGHT))
#Config.set('graphics', 'width', str(glo.const.WIDTH))
return FilGame()
def set_sizes(self):
#Window.toggle_fullscreen()
glo.const.HEIGHT = Window.height
glo.const.WIDTH = Window.width
sizeh = glo.const.HEIGHT / glo.const.ROWS
sizew = glo.const.WIDTH / glo.const.COLS
if sizeh < sizew:
glo.const.TILE_SIZE = sizeh
else:
glo.const.TILE_SIZE = sizew
glo.const.MARGIN = glo.const.HEIGHT\
- (glo.const.TILE_SIZE * glo.const.ROWS)
if __name__ == '__main__':
FilApp().run()