forked from pytherman/pytherman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawboard.py
167 lines (137 loc) · 6.88 KB
/
drawboard.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
"""draw area of game"""
import pygame
import pytherman
from components import Bonus
from components import Health, Physics, Renderable, Treasure
from prepare_gamearea import PrepareGamearea
def draw_board(pworld, world, PPM, RESOLUTION):
"""Draw all fields on gamearea"""
width, height = RESOLUTION
wooden_field_image = pygame.image.load("assets/fields/woden_big_lines.png")
wooden_field_renderable = Renderable(image=wooden_field_image)
field_size = wooden_field_renderable.w
number_of_columns = int(width / field_size)
number_of_rows = int(height / field_size)
game_area = PrepareGamearea(number_of_rows, number_of_columns).create_table_of_game()
for i in range(1, number_of_rows - 1):
for j in range(1, number_of_columns - 1):
draw_field(world, pworld, PPM, i, j, 0)
draw_field(world, pworld, PPM, i, j, game_area[i][j])
draw_border_walls(pworld, world, PPM, RESOLUTION)
def draw_field(world, pworld, PPM, y, x, value):
"""Draw the special type of field decided by given parameter value"""
if value == 0:
draw_grass_field(world, pworld, PPM, x, y)
elif value == 1:
draw_wooden_field(world, pworld, PPM, x, y)
elif value == 2:
draw_concrete_field(world, pworld, PPM, x, y)
else:
draw_door_field(world, pworld, PPM, x, y)
def draw_grass_field(world, pworld, PPM, x, y):
"""draw grass field on given position"""
field_image = pygame.image.load("assets/fields/grass_lines.png")
field_renderable = Renderable(image=field_image)
field_size = field_renderable.w
field_size = int(field_size / PPM)
create_field(world, pworld, field_size / 2 + x * field_size,
field_size / 2 + y * field_size, field_renderable)
def draw_wooden_field(world, pworld, PPM, x, y):
"""draw wooden wall on given position"""
field_image = pygame.image.load("assets/fields/woden_big_lines.png")
field_renderable = Renderable(image=field_image)
field_size = field_renderable.w
field_size = int(field_size / PPM)
create_field_with_physics(world, pworld, x * field_size + field_size / 2, y * field_size + field_size / 2,
field_size, field_renderable)
def draw_concrete_field(world, pworld, PPM, x, y):
"""draw concrete wall on given position"""
field_image = pygame.image.load("assets/fields/concrete_drops.png")
field_renderable = Renderable(image=field_image)
field_size = field_renderable.w
field_size = int(field_size / PPM)
create_static_field(world, pworld, x * field_size + field_size / 2, y * field_size + field_size / 2,
field_size, field_renderable)
def draw_door_field(world, pworld, PPM, x, y):
"""draw wooden wall with hidden door on given position"""
field_size = int(40 / PPM)
source_pos = (x * field_size + field_size / 2, y * field_size + field_size / 2)
bonus = world.create_entity()
field_image = pygame.image.load("assets/door.png")
bonus_renderable = Renderable(image=field_image)
bonus_body = world.pworld.CreateStaticBody(
position=source_pos)
bonus_body.userData = bonus
bonus_body.fixedRotation = True
bonus_body.CreatePolygonFixture(
box=(0.01, 0.01),
density=0,
friction=0.3)
world.add_component(bonus, Bonus(on_pickup=_start_next_level))
world.add_component(bonus, bonus_renderable)
world.add_component(bonus, Physics(body=bonus_body))
field_image = pygame.image.load("assets/fields/woden_big_lines.png")
field_renderable = Renderable(image=field_image)
field_size = field_renderable.w
field_size = int(field_size / PPM)
create_field_with_physics(world, pworld, x * field_size + field_size / 2, y * field_size + field_size / 2,
field_size, field_renderable)
def _start_next_level(world, entity):
world.next_level = True
def create_field(world, pworld, position_x, position_y, field_renderable):
"""create field without physics with image from paramteres on given position"""
field = world.create_entity()
field_body = pworld.CreateStaticBody(
position=(position_x, position_y))
field_body.userData = field
world.add_component(field, Physics(body=field_body))
world.add_component(field, field_renderable)
def create_static_field(world, pworld, position_x, position_y, field_size, field_renderable):
"""draw static field with image from parameters on given position"""
field = world.create_entity()
field_body = pworld.CreateStaticBody(
position=(position_x, position_y),
userData=field)
field_body.userData = field
field_body.CreatePolygonFixture(box=(field_size / 2,
field_size / 2),
density=234,
friction=0.3)
world.add_component(field, Physics(body=field_body))
world.add_component(field, field_renderable)
def create_field_with_physics(world, pworld, position_x, position_y, field_size, field_renderable):
"""Draw field with physics with image from parameters on given position"""
field = world.create_entity()
field_body = pworld.CreateDynamicBody(
position=(position_x, position_y),
userData=field)
field_body.userData = field
field_body.fixedRotation = True
field_body.CreatePolygonFixture(box=(field_size / 2 - 0.1,
field_size / 2 - 0.1),
density=1000000,
friction=6000)
world.add_component(field, Physics(body=field_body))
world.add_component(field, field_renderable)
world.add_component(field, Health(hp=2))
world.add_component(field, Treasure(chance=0.5))
def draw_border_walls(pworld, world, PPM, RESOLUTION):
"""Draw border walls around the gamearea"""
field_image = pygame.image.load("assets/fields/hard_wall.png")
field_renderable = Renderable(image=field_image)
width, height = RESOLUTION
field_size = field_renderable.w
number_of_columns = int(width / field_size)
number_of_rows = int(height / field_size)
field_size = int(field_size / PPM)
for i in range(number_of_columns):
create_static_field(world, pworld, field_size / 2 + i * field_size, field_size / 2,
field_size, field_renderable)
create_static_field(world, pworld, field_size / 2 + i * field_size,
number_of_rows * field_size - field_size / 2, field_size,
field_renderable)
for i in range(1, number_of_rows - 1):
create_static_field(world, pworld, field_size / 2, field_size / 2 + i * field_size,
field_size, field_renderable)
create_static_field(world, pworld, number_of_columns * field_size - field_size / 2,
field_size / 2 + i * field_size, field_size, field_renderable)