Skip to content

Commit

Permalink
🥶 - Player added (sheeeeeesh)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrokx committed May 8, 2021
1 parent fbe2507 commit 83a727e
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Assets/IceTileset.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="IceTileset" tilewidth="16" tileheight="16" tilecount="720" columns="40">
<image source="IceTileset.png" trans="000000" width="640" height="288"/>
</tileset>
File renamed without changes
4 changes: 4 additions & 0 deletions Assets/RPG Nature Tileset Autumn.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="RPG Nature Tileset Autumn" tilewidth="16" tileheight="16" tilecount="540" columns="30">
<image source="RPG Nature Tileset Autumn.png" trans="000000" width="480" height="288"/>
</tileset>
4 changes: 4 additions & 0 deletions Assets/RPG Nature Tileset.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="RPG Nature Tileset" tilewidth="16" tileheight="16" tilecount="720" columns="40">
<image source="RPG Nature Tileset.png" trans="000000" width="641" height="288"/>
</tileset>
47 changes: 45 additions & 2 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,59 @@

import pygame

import pytmx

import pyscroll

from src.Player import Player


class Game:
def __init__(self):
super().__init__()
self.screen = pygame.display.set_mode((1080,720))
self.screen = pygame.display.set_mode((1080, 720))
pygame.display.set_caption("Desert-island-adventure")

# Map data
tmx_data = pytmx.util_pygame.load_pygame('main_map.tmx')
map_data = pyscroll.data.TiledMapData(tmx_data)
self.map_layer = pyscroll.orthographic.BufferedRenderer(map_data, self.screen.get_size())
self.map_layer.zoom = 1.8

# Player spawn
self.player_spawn = tmx_data.get_object_by_name("spawn")

# 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.add(self.player)

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

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

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

while running:

self.handle()
self.group.update()
self.group.draw(self.screen)
self.group.center(self.player.rect.center)
# Refresh window
pygame.display.flip()

Expand All @@ -24,3 +65,5 @@ def run(self):
print("✅ - Window closes")
sys.exit(0)
running = False

clock.tick(60)
209 changes: 209 additions & 0 deletions main_map.tmx

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src/Player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pygame


class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.sprite_sheet = pygame.image.load("Assets/Player.png")
self.image = self.get_image(0, 0)
self.rect = self.image.get_rect()
self.position = [x, y]
self.speed = 2
self.images = {
'down': self.get_image(0, 0),
'up': self.get_image(0, 96),
'left': self.get_image(0, 32),
'right': self.get_image(0, 64)
}

def update(self):
self.rect.topleft = self.position

def get_image(self, x, y):
image = pygame.Surface([32, 32])
image.blit(self.sprite_sheet, (0, 0), (x, y, 32, 32))
image.set_colorkey([0, 0, 0])
return image

def change_image(self, name):
self.image = self.images[name]

def move_up(self):
self.position[1] -= self.speed

def move_down(self):
self.position[1] += self.speed

def move_left(self):
self.position[0] -= self.speed

def move_right(self):
self.position[0] += self.speed

0 comments on commit 83a727e

Please sign in to comment.