This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
forked from michal-molas/minecraft2D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.py
192 lines (158 loc) · 8.93 KB
/
Player.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
186
187
188
189
190
191
192
import config
import pygame
import Textures
import Inventory
from blocks.Blocks import *
class Player:
screen_pos_x = config.screen_width
screen_pos_y = config.screen_height
player_png = Textures.loadTxt("player", "player")
can_jump = True
jump_count = 44
already_digged = False
can_dig_left = False
can_dig_right = False
clicked_mouse = False
is_falling = False
left_wall = False
right_wall = False
POS_ON_SCREEN_X = 32 * config.screen_width // 64 - 16
POS_ON_SCREEN_Y = 32 * config.screen_height // 64
def __init__(self, terrain):
self.player_name = "SKR"
self.position = [terrain.world_size_x * 32 // 2 + 16, 64 * 32 + 32]
self.block_x = self.position[0] // 32
self.block_y = self.position[1] // 32 - 1
def drawNickname(self, window):
Inventory.Inventory.gui_handler.drawText(window, config.screen_width//2 - (len(self.player_name) * 4),
config.screen_height//2 - 16, self.player_name)
def update_current_block(self):
if self.position[0] % 32 != 0:
self.block_x = self.position[0] // 32
if self.position[1] % 32 != 0:
self.block_y = self.position[1] // 32
def clicked_block(self):
mouse_pos = pygame.mouse.get_pos()
dif_x = mouse_pos[0] - (self.POS_ON_SCREEN_X + 16)
dif_y = mouse_pos[1] - (self.POS_ON_SCREEN_Y + 32)
if not self.right_wall:
clicked_block_x = self.block_x + (dif_x + self.position[0] % 32) // 32
else:
clicked_block_x = self.block_x + dif_x // 32 + 1
if self.position[1] % 32 != 0:
clicked_block_y = self.block_y + (dif_y + self.position[1] % 32) // 32
else:
clicked_block_y = self.block_y + dif_y // 32 + 1
return clicked_block_x, clicked_block_y
def jump(self):
if self.jump_count > 0:
self.jump_count -= 2
self.position[1] -= 2
else:
self.is_falling = True
self.can_jump = True
self.jump_count = 44
def start_fall(self, terrain):
if self.position[1] % 32 == 0 and terrain.terrain[self.block_y + 1][self.block_x].transparent:
self.is_falling = True
def fall(self, terrain):
if self.is_falling:
self.position[1] += 2
if self.position[1] % 32 == 0:
self.is_falling = False
self.start_fall(terrain)
def dig(self, terrain, events, eq):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
clicked_block = self.clicked_block()
if clicked_block is not None:
rel_blocks_x = clicked_block[0] - self.block_x
rel_blocks_y = clicked_block[1] - self.block_y
if terrain.terrain[clicked_block[1]][clicked_block[0]].breakable:
if (rel_blocks_x == 0 and (rel_blocks_y == 1 or rel_blocks_y == -1)) \
or (rel_blocks_y == 0 and (rel_blocks_x == 1 or rel_blocks_x == -1)) \
or (rel_blocks_y == -1 and rel_blocks_x == -1
and (terrain.terrain[clicked_block[1] + 1][clicked_block[0]].transparent
or terrain.terrain[clicked_block[1]][clicked_block[0] + 1].transparent)) \
or (rel_blocks_y == -1 and rel_blocks_x == 1
and (terrain.terrain[clicked_block[1] + 1][clicked_block[0]].transparent
or terrain.terrain[clicked_block[1]][clicked_block[0] - 1].transparent)) \
or (rel_blocks_y == 1 and rel_blocks_x == -1
and (terrain.terrain[clicked_block[1] - 1][clicked_block[0]].transparent
or terrain.terrain[clicked_block[1]][clicked_block[0] + 1].transparent)) \
or (rel_blocks_y == 1 and rel_blocks_x == 1
and (terrain.terrain[clicked_block[1] - 1][clicked_block[0]].transparent
or terrain.terrain[clicked_block[1]][clicked_block[0] - 1].transparent)):
if terrain.terrain[clicked_block[1]][clicked_block[0]].collectable:
# eq.set_slot(terrain.terrain[clicked_block[1]][clicked_block[0]].type)
eq.bar.container.addItem(terrain.terrain[clicked_block[1]][clicked_block[0]].type, 1,
(0, 0))
terrain.terrain[clicked_block[1]][clicked_block[0]] = Sky()
def place_block(self, terrain, events, eq):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3 and eq.bar.container.content[eq.picked_slot].item != "empty":
clicked_block = self.clicked_block()
if clicked_block is not None:
rel_blocks_x = clicked_block[0] - self.block_x
rel_blocks_y = clicked_block[1] - self.block_y
if terrain.terrain[clicked_block[1]][clicked_block[0]].transparent:
if (rel_blocks_x == 0 and (rel_blocks_y == 1 or rel_blocks_y == -1)) \
or (rel_blocks_y == 0 and (rel_blocks_x == 1 or rel_blocks_x == -1)) \
or (rel_blocks_y == -1 and rel_blocks_x == -1
and (terrain.terrain[clicked_block[1] + 1][clicked_block[0]].transparent
or terrain.terrain[clicked_block[1]][clicked_block[0] + 1].transparent)) \
or (rel_blocks_y == -1 and rel_blocks_x == 1
and (terrain.terrain[clicked_block[1] + 1][clicked_block[0]].transparent
or terrain.terrain[clicked_block[1]][clicked_block[0] - 1].transparent)) \
or (rel_blocks_y == 1 and rel_blocks_x == -1
and (terrain.terrain[clicked_block[1] - 1][clicked_block[0]].transparent
or terrain.terrain[clicked_block[1]][clicked_block[0] + 1].transparent)) \
or (rel_blocks_y == 1 and rel_blocks_x == 1
and (terrain.terrain[clicked_block[1] - 1][clicked_block[0]].transparent
or terrain.terrain[clicked_block[1]][clicked_block[0] - 1].transparent)):
terrain.terrain[clicked_block[1]][clicked_block[0]] = \
blocks[eq.bar.container.content[eq.picked_slot].item]()
eq.bar.container.takeItem(1, (0, eq.picked_slot))
def move(self, events, terrain, eq):
#handling toggleable keys
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
eq.eq_opened = not eq.eq_opened
if event.key == pygame.K_c:
eq.crafting_opened = not eq.crafting_opened
#handling keys held continuously
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
if (self.position[0] % 32 != 0 or terrain.terrain[self.block_y][self.block_x - 1].transparent
or self.right_wall and terrain.terrain[self.block_y][self.block_x].transparent
and not (self.left_wall and keys[pygame.K_d])):
self.position[0] -= 1
elif not self.right_wall:
self.left_wall = True
if keys[pygame.K_d]:
if (self.position[0] % 32 != 0 or terrain.terrain[self.block_y][self.block_x + 1].transparent
or (self.left_wall and not keys[pygame.K_a])) \
and terrain.terrain[self.block_y][self.block_x].transparent:
self.position[0] += 1
elif not self.left_wall:
self.right_wall = True
if (keys[pygame.K_w] or keys[pygame.K_SPACE]) and self.can_jump and not self.is_falling:
if terrain.terrain[self.block_y - 1][self.block_x].transparent:
self.can_jump = False
#some other stuff idk not my code lmao
if not self.can_jump:
self.jump()
if self.position[0] % 32 != 0:
self.left_wall = False
self.right_wall = False
self.start_fall(terrain)
self.fall(terrain)
def update(self, events, terrain, eq):
self.move(events, terrain, eq)
self.update_current_block()
self.dig(terrain, events, eq)
self.place_block(terrain, events, eq)
def draw(self, window):
window.blit(self.player_png, (self.POS_ON_SCREEN_X, self.POS_ON_SCREEN_Y))
self.drawNickname(window)