forked from ChristianD37/YoutubeTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7be46b
commit 36b3e98
Showing
6 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from tiles import * | ||
from spritesheet import Spritesheet | ||
|
||
|
||
################################# LOAD UP A BASIC WINDOW AND CLOCK ################################# | ||
pygame.init() | ||
DISPLAY_W, DISPLAY_H = 480, 270 | ||
canvas = pygame.Surface((DISPLAY_W,DISPLAY_H)) | ||
window = pygame.display.set_mode(((DISPLAY_W,DISPLAY_H))) | ||
running = True | ||
clock = pygame.time.Clock() | ||
|
||
################################# LOAD PLAYER AND SPRITESHEET################################### | ||
spritesheet = Spritesheet('spritesheet.png') | ||
player_img = spritesheet.parse_sprite('chick.png') | ||
player_rect = player_img.get_rect() | ||
|
||
#################################### LOAD THE LEVEL ####################################### | ||
map = TileMap('test_level.csv', spritesheet ) | ||
player_rect.x, player_rect.y = map.start_x, map.start_y | ||
|
||
################################# GAME LOOP ########################## | ||
while running: | ||
clock.tick(60) | ||
################################# CHECK PLAYER INPUT ################################# | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
running = False | ||
if event.type == pygame.KEYDOWN: | ||
pass | ||
# if event.key == pygame.K_LEFT: | ||
# player.LEFT_KEY, player.FACING_LEFT = True, True | ||
# elif event.key == pygame.K_RIGHT: | ||
# player.RIGHT_KEY, player.FACING_LEFT = True, False | ||
# | ||
# if event.type == pygame.KEYUP: | ||
# if event.key == pygame.K_LEFT: | ||
# player.LEFT_KEY = False | ||
# elif event.key == pygame.K_RIGHT: | ||
# player.RIGHT_KEY = False | ||
|
||
################################# UPDATE/ Animate SPRITE ################################# | ||
|
||
################################# UPDATE WINDOW AND DISPLAY ################################# | ||
canvas.fill((0, 180, 240)) # Fills the entire screen with light blue | ||
map.draw_map(canvas) | ||
canvas.blit(player_img, player_rect) | ||
window.blit(canvas, (0,0)) | ||
pygame.display.update() | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{"frames": { | ||
|
||
"chick.png": | ||
{ | ||
"frame": {"x":0,"y":0,"w":16,"h":16}, | ||
"rotated": false, | ||
"trimmed": false, | ||
"spriteSourceSize": {"x":0,"y":0,"w":16,"h":16}, | ||
"sourceSize": {"w":16,"h":16} | ||
}, | ||
"grass.png": | ||
{ | ||
"frame": {"x":16,"y":0,"w":16,"h":16}, | ||
"rotated": false, | ||
"trimmed": false, | ||
"spriteSourceSize": {"x":0,"y":0,"w":16,"h":16}, | ||
"sourceSize": {"w":16,"h":16} | ||
}, | ||
"grass2.png": | ||
{ | ||
"frame": {"x":32,"y":0,"w":16,"h":16}, | ||
"rotated": false, | ||
"trimmed": false, | ||
"spriteSourceSize": {"x":0,"y":0,"w":16,"h":16}, | ||
"sourceSize": {"w":16,"h":16} | ||
}}, | ||
"meta": { | ||
"app": "https://www.codeandweb.com/texturepacker", | ||
"version": "1.0", | ||
"image": "test.png", | ||
"format": "RGBA8888", | ||
"size": {"w":48,"h":16}, | ||
"scale": "1", | ||
"smartupdate": "$TexturePacker:SmartUpdate:a66d329312f98accd6de9452ce22c2dc:dc1a8a27030eb8549f6ad949e95565fe:02ab132358d6d8b512e80119463a8329$" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pygame | ||
import json | ||
|
||
|
||
class Spritesheet: | ||
def __init__(self, filename): | ||
self.filename = filename | ||
self.sprite_sheet = pygame.image.load(filename).convert() | ||
self.meta_data = self.filename.replace('png', 'json') | ||
with open(self.meta_data) as f: | ||
self.data = json.load(f) | ||
f.close() | ||
|
||
|
||
|
||
def get_sprite(self, x, y, w, h): | ||
sprite = pygame.Surface((w, h)) | ||
sprite.set_colorkey((0,0,0)) | ||
sprite.blit(self.sprite_sheet,(0, 0),(x, y, w, h)) | ||
return sprite | ||
|
||
def parse_sprite(self, name): | ||
sprite = self.data['frames'][name]['frame'] | ||
x, y, w, h = sprite["x"], sprite["y"], sprite["w"], sprite["h"] | ||
image = self.get_sprite(x, y, w, h) | ||
return image | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 | ||
-1,-1,-1,-1,-1,-1,1,1,1,1,1,-1,-1,-1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-1,-1,2,2,2,2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import pygame, csv, os | ||
|
||
class Tile(pygame.sprite.Sprite): | ||
def __init__(self, image, x, y, spritesheet): | ||
pygame.sprite.Sprite.__init__(self) | ||
self.image = spritesheet.parse_sprite(image) | ||
# Manual load in: self.image = pygame.image.load(image) | ||
self.rect = self.image.get_rect() | ||
self.rect.x, self.rect.y = x, y | ||
|
||
def draw(self, surface): | ||
surface.blit(self.image, (self.rect.x, self.rect.y)) | ||
|
||
class TileMap(): | ||
def __init__(self, filename, spritesheet): | ||
self.tile_size = 16 | ||
self.start_x, self.start_y = 0, 0 | ||
self.spritesheet = spritesheet | ||
self.tiles = self.load_tiles(filename) | ||
self.map_surface = pygame.Surface((self.map_w, self.map_h)) | ||
self.map_surface.set_colorkey((0, 0, 0)) | ||
self.load_map() | ||
|
||
def draw_map(self, surface): | ||
surface.blit(self.map_surface, (0, 0)) | ||
|
||
def load_map(self): | ||
for tile in self.tiles: | ||
tile.draw(self.map_surface) | ||
|
||
def read_csv(self, filename): | ||
map = [] | ||
with open(os.path.join(filename)) as data: | ||
data = csv.reader(data, delimiter=',') | ||
for row in data: | ||
map.append(list(row)) | ||
return map | ||
|
||
def load_tiles(self, filename): | ||
tiles = [] | ||
map = self.read_csv(filename) | ||
x, y = 0, 0 | ||
for row in map: | ||
x = 0 | ||
for tile in row: | ||
if tile == '0': | ||
self.start_x, self.start_y = x * self.tile_size, y * self.tile_size | ||
elif tile == '1': | ||
tiles.append(Tile('grass.png', x * self.tile_size, y * self.tile_size, self.spritesheet)) | ||
elif tile == '2': | ||
tiles.append(Tile('grass2.png', x * self.tile_size, y * self.tile_size, self.spritesheet)) | ||
# Move to next tile in current row | ||
x += 1 | ||
|
||
# Move to next row | ||
y += 1 | ||
# Store the size of the tile map | ||
self.map_w, self.map_h = x * self.tile_size, y * self.tile_size | ||
return tiles | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|