Skip to content

Commit

Permalink
🍫 - Collision system added (sheeeeeesh)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrokx committed May 8, 2021
1 parent 83a727e commit 1d41155
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Assets/house.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.5" tiledversion="1.6.0" name="house" tilewidth="16" tileheight="16" tilecount="775" columns="31">
<image source="house.png" trans="000000" width="500" height="400"/>
</tileset>
28 changes: 21 additions & 7 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,46 @@ def __init__(self):
# Player instence
self.player = Player(self.player_spawn.x, self.player_spawn.y)
# group with map
self.group = pyscroll.PyscrollGroup(map_layer=self.map_layer, default_layer=4)
self.group = pyscroll.PyscrollGroup(map_layer=self.map_layer, default_layer=3)
self.group.add(self.player)

# Collision avec les mur
self.objects_in_map = []
for obj in tmx_data.objects:
if obj.type == "collid":
self.objects_in_map.append(pygame.Rect(obj.x, obj.y, obj.width, obj.height))

def handle(self):
keys = pygame.key.get_pressed()

if keys[pygame.K_UP]:
if keys[pygame.K_UP] or keys[pygame.K_z]:
self.player.move_up()
self.player.change_image('up')
elif keys[pygame.K_DOWN]:
elif keys[pygame.K_DOWN] or keys[pygame.K_s]:
self.player.move_down()
self.player.change_image('down')
elif keys[pygame.K_LEFT]:
elif keys[pygame.K_LEFT] or keys[pygame.K_q]:
self.player.move_left()
self.player.change_image('left')
elif keys[pygame.K_RIGHT]:
elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.player.move_right()
self.player.change_image('right')

def update(self):
self.group.update()

for sprite in self.group.sprites():
if sprite.feet.collidelist(self.objects_in_map) > -1:
sprite.move_back()

def run(self):
running = True
clock = pygame.time.Clock()

while running:
self.player.save_location()
self.handle()
self.group.update()
self.update()
self.group.draw(self.screen)
self.group.center(self.player.rect.center)
# Refresh window
Expand All @@ -66,4 +80,4 @@ def run(self):
sys.exit(0)
running = False

clock.tick(60)
clock.tick(60)
Loading

0 comments on commit 1d41155

Please sign in to comment.