-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
126 lines (115 loc) · 3.44 KB
/
classes.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
#!/usr/bin/python2
# coding: utf-8
class Niveau:
"""
2 methods :
- Generate the level from the given file dans un attribut structure
- Print the level structure \o/
"""
def generate_level(self, fichier):
"""
-Generate the level structure from the given file
"""
fp = open("levels/"+fichier, 'r').read()
list_level = fp.split('\n')
# Remove the last \n
list_level.pop()
self.list_level = list_level
def print_level(self, list_level, window, begin, wall, end):
"""
Method which aim is to print the level structure into the window
- It will parse the given structure
"d" is departure box
"m" is the wall
"a" the end (the banana :D)
position_x = num_case_x * sprite's length in pixels
position_y = num_case_y * sprite's length in pixels
"""
for y in range(0,15):
for x in range(0,15):
if list_level[y][x] == 'd':
position_x = x * 30
position_y = y * 30
window.blit(begin, (position_x,position_y))
elif list_level[y][x] == 'm':
position_x = x * 30
position_y = y * 30
window.blit(wall, (position_x,position_y))
elif list_level[y][x] == 'a':
position_x = x * 30
position_y = y * 30
window.blit(end, (position_x,position_y))
else: # it's a 0
continue
class Perso:
"""
Create a character, and define;
* position ( in both boxes and pixels)
* image (default image is down)
"""
def __init__(self, dk, list_level):
class Found(Exception): pass
try:
for y in range(0,15):
for x in range(0,15):
if list_level[y][x]=='d':
raise Found
except Found:
self.position = (x*30, y*30)
self.position_case = (x,y)
# Display dk down (default value)
self.position_rect = dk[0].get_rect()
self.position_rect.left= self.position[0]
self.position_rect.top = self.position[1]
# default value
self.last_movement="down"
self.win=0
def move_perso(self, position_rect, list_level, movement):
"""
Method which aim is to move the character:
The "m" represents the wall, we (unfortunately ) are not allowed to walk into walls :d
"""
#pixels
pos_x = position_rect.left
pos_y = position_rect.top
#boxes
x = (pos_x / 30)
y = (pos_y / 30)
self.last_movement = movement
#Uncomment these lines, only for debugging purposes
#print x, y
#print list_level[y][x-1]
#left
if movement == "left" and (x-1)>=0:
if list_level[y][x-1]=='0' or list_level[y][x-1]=='d':
self.position_rect = position_rect.move(-30, 0)
elif list_level[y][x-1]=='a':
self.position_rect = position_rect.move(-30, 0)
self.win=1
#print "You win!"
#right
elif movement == "right" and (x+1)<15:
if list_level[y][x+1]=='0' or list_level[y][x+1]=='d':
self.position_rect = position_rect.move(30,0)
elif list_level[y][x+1]=='a':
self.position_rect = position_rect.move(30,0)
self.win=1
#print "You win!"
#down
elif movement == "down" and (y+1)<15:
if list_level[y+1][x] == '0' or list_level[y+1][x]=='d':
self.position_rect = position_rect.move(0,30)
elif list_level[y+1][x]=='a':
self.position_rect = position_rect.move(0,30)
self.win=1
#print "You win!"
#up
elif movement == "up" and (y-1)>=0:
if list_level[y-1][x] == '0' or list_level[y-1][x]=='d':
self.position_rect = position_rect.move(0,-30)
elif list_level[y-1][x]=='a':
self.position_rect = position_rect.move(0,-30)
self.win=1
#print "You win!"
#else:
# print "owned, nothin"