forked from matheus-1618/OneBit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilemap_codigo.py
More file actions
87 lines (69 loc) · 3 KB
/
tilemap_codigo.py
File metadata and controls
87 lines (69 loc) · 3 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
import pygame
import pytmx
from config import *
#Definindo colisão entre dois objetos
def collide_hit_rect(one, two):
return one.hit_rect.colliderect(two.rect)
#Colisão com objetos
def collide_with_ilhas(sprite, group, dir):
if dir == 'x':
hits = pygame.sprite.spritecollide(sprite, group, "", collide_hit_rect)
if hits:
if hits[0].rect.centerx > sprite.hit_rect.centerx:
sprite.pos.x = hits[0].rect.left - sprite.hit_rect.width / 2
if hits[0].rect.centerx < sprite.hit_rect.centerx:
sprite.pos.x = hits[0].rect.right + sprite.hit_rect.width / 2
sprite.vel.x = 0
sprite.hit_rect.centerx = sprite.pos.x
if dir == 'y':
hits = pygame.sprite.spritecollide(sprite, group, "", collide_hit_rect)
if hits:
if hits[0].rect.centery > sprite.hit_rect.centery:
sprite.pos.y = hits[0].rect.top - sprite.hit_rect.height / 2
if hits[0].rect.centery < sprite.hit_rect.centery:
sprite.pos.y = hits[0].rect.bottom + sprite.hit_rect.height / 2
sprite.vel.y = 0
sprite.hit_rect.centery = sprite.pos.y
class TiledMap:
#Configurações gerais da classe:
def __init__(self, filename):
tm = pytmx.load_pygame(filename, pixelalpha=True)
self.width = tm.width * tm.tilewidth
self.height = tm.height * tm.tileheight
self.tmxdata = tm
#Renderizando o mapa
def render(self, surface):
ti = self.tmxdata.get_tile_image_by_gid
for layer in self.tmxdata.visible_layers:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, gid, in layer:
tile = ti(gid)
if tile:
surface.blit(tile, (x * self.tmxdata.tilewidth,
y * self.tmxdata.tileheight))
def make_map(self):
#Criando superfície onde o mapa será desenhado:
temp_surface = pygame.Surface((self.width, self.height))
self.render(temp_surface)
return temp_surface
class Camera:
#Configurações gerais da classe:
def __init__(self, width, height):
#Fazendo retângulo:
self.camera = pygame.Rect(0, 0, width, height)
self.width = width
self.height = height
def apply(self, entity):
return entity.rect.move(self.camera.topleft)
def apply_rect(self, rect):
return rect.move(self.camera.topleft)
def update(self, target):
#Posicionando câmera com alvo (jogador):
x = -target.rect.centerx + int(WIDTH / 2)
y = -target.rect.centery + int(HEIGHT / 2)
# Limite de scrolling do mapa:
x = min(0, x) # Esquerda
y = min(0, y) # Topo
x = max(-(self.width - WIDTH), x) # Direita
y = max(-(self.height - HEIGHT), y) # Base
self.camera = pygame.Rect(x, y, self.width, self.height)