From af5f53e85bad24f315488238381661deed316754 Mon Sep 17 00:00:00 2001 From: "Goncalo Lourenco (dacostag)" Date: Thu, 30 Apr 2020 19:38:33 +0100 Subject: [PATCH] Screen Class created Screen now follows the player and the map created can be as big as necessary. --- __pycache__/tylemap.cpython-38.pyc | Bin 0 -> 1412 bytes main.py | 57 ++++++++++++++--------------- map2.txt | 16 ++++++++ new_main.py | 0 tylemap.py | 32 ++++++++++++++++ 5 files changed, 75 insertions(+), 30 deletions(-) create mode 100644 __pycache__/tylemap.cpython-38.pyc create mode 100644 map2.txt delete mode 100644 new_main.py create mode 100644 tylemap.py diff --git a/__pycache__/tylemap.cpython-38.pyc b/__pycache__/tylemap.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b2911ba9ad0f79d4209885225879a463f7450ed1 GIT binary patch literal 1412 zcmZ`(OOG2x5UzgAcy{9@n_FM&pe(a7EQ+KlZ|_(5rLL*!>gxWw>Z^IQv$IVYqu2lZ{!9_QWaI3> z8r{ZhZb29_qN2S0id?{!m3&A)kx@oJC8JMdu3#l5g_Xk6Mm(Ti=M|!<-xEQ6J7o3j zkT7mzHWwi*@v*ry!pNtTE29vugSuPRL*BR<8p;;tK4vq87}JUh@?s^7IFZjkKb9+b zhkn~VRx6s)sa&ZSLeS$&ztBy(NB8Mh^3h}DRXP!f)vX=%1TosD7x~N%^kl7Ph5$$P zbLqSjxV5(xv?+ZV^rE)&kd`$*CaB8J^DRHA?30P{2l+Ou2X-<(@F8QpnH<@WL}y4_ z2VSWgTa7|C!iIS{vq6l4t|oK)H@Wk;?9Gq+-whr$)-{8JdT_@!fA~c`sNG^}hkh`( zPl_?32KNq2SI#}+u1DK1D?2Oe8}%||MKPIAd{J~bZMzU8QqdKexFUA(?TVu=GJRvS ztq)L$VK7Azwu@r6FozZE-J*DWSXQmaUBE#R;u*oGJEl*H*>LSL5$*pcz&F_RZvrs0 zz@x~iJTVnc^GZcn073avK5wFo@LO1b>!U{S%t|*@v^w^0!mOef0tk7W$ojWJy zdebYf_?_!!aaW-S@mI`Y5X+d?pk|Y~yTrQ~ZG2=;uixfNH@4n=$&RmBa3i#WQx?+0 z8vSRlL2oh$PWztP7Dqel$F}}{FLitHIXYtU@3|dU literal 0 HcmV?d00001 diff --git a/main.py b/main.py index 6b31a17..baad6c6 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,11 @@ import pygame import random import math +import os.path from pygame import mixer -from settings import * from sprites import * -import os.path +from settings import * +from tylemap import * # music: Licensed CC-BY. Attribute to Avgvst. @@ -28,20 +29,12 @@ class Game: self.load_data() def load_data(self): - self.dir = os.path.dirname(__file__) - img_dir = os.path.join(self.dir, 'img') - self.spritesheet1 = SpriteSheet(os.path.join(img_dir, spritesheet1)) - self.spritesheet2 = SpriteSheet(os.path.join(img_dir, spritesheet2)) - - # load soundself. - self.sound_dir = os.path.join(self.dir, 'sound') - self.jump_sound = pygame.mixer.Sound(os.path.join(self.sound_dir, 'Jump13.wav')) - + self.spritesheet1 = SpriteSheet(os.path.join(img_folder, spritesheet1)) + self.spritesheet2 = SpriteSheet(os.path.join(img_folder, spritesheet2)) + # load jump sound + self.jump_sound = pygame.mixer.Sound(os.path.join(sound_folder, 'Jump13.wav')) # load map - self.map_data = [] - with open(os.path.join(self.dir, 'map.txt'), 'rt') as f: - for line in f: - self.map_data.append(line) + self.map = Map(os.path.join(game_folder, 'map2.txt')) def new(self): # starts a new game not a new program @@ -51,15 +44,15 @@ class Game: self.tile = pygame.sprite.Group() # spawn walls from map - for row, tiles in enumerate(self.map_data): + for row, tiles in enumerate(self.map.data): for col, tile in enumerate(tiles): if tile == '1': Tile(self, col, row) if tile == 'P': - self.player = Player(self, col, row) #spawns player at 'P' map location + self.player = Player(self, col, row) # spawns player at 'P' map location self.all_sprites.add(self.player) - - pygame.mixer.music.load(os.path.join(self.sound_dir, '04 - Sanctuary.ogg')) + self.camera = Camera(self.map.width, self.map.height) + pygame.mixer.music.load(os.path.join(sound_folder, '04 - Sanctuary.ogg')) self.run() def run(self): @@ -76,17 +69,13 @@ class Game: def update(self): # Game loop - Update self.all_sprites.update() - # wrap around the screen - if self.player.pos.x > width: - self.player.pos.x = 0 - if self.player.pos.x < 0: - self.player.pos.x = width + self.camera.update(self.player) hits = pygame.sprite.spritecollide(self.player, self.tile, False) if hits: lowest = max(hits, key=lambda x: x.rect.bottom) highest = min(hits, key=lambda x: x.rect.bottom) - if self.player.y < lowest.y: # if falling on block - player remains on top of it + if self.player.y < lowest.y + 0.5: # if falling on block - player remains on top of it self.player.y = lowest.y - 1.3 self.player.vel.y = 0 self.player.jumping = False @@ -94,7 +83,15 @@ class Game: if self.player.y > lowest.y - 0.5: self.player.y = lowest.y + 1 self.player.vel.y = 0 - + # hitting blocks from the side + for hit in range(0, len(hits)): + if self.player.y > hits[hit].y: + if self.player.x < hits[hit].x: + self.player.x = hits[hit].x - 1 + self.player.vel.x = 0 + if self.player.x > hits[hit].x: + self.player.x = hits[hit].x + 1 + self.player.vel.x = 0 # Die, by falling of the screen if self.player.rect.bottom > height + 60: @@ -124,8 +121,8 @@ class Game: # Game loop - draw/render self.screen.fill((146, 244, 255)) self.draw_grid() - self.all_sprites.draw(self.screen) - self.screen.blit(self.player.image, self.player.rect) + for sprite in self.all_sprites: + self.screen.blit(sprite.image, self.camera.apply(sprite)) self.draw_text("Score: " + str(self.score), 26, (255, 255, 255), 40, 15) # after everything is drawn "flip the board" @@ -133,7 +130,7 @@ class Game: def show_start_screen(self): # game start screen - pygame.mixer.music.load(os.path.join(self.sound_dir, '01 - Opening.ogg')) + pygame.mixer.music.load(os.path.join(sound_folder, '01 - Opening.ogg')) pygame.mixer.music.play(loops=-1) self.screen.fill((209, 206, 29)) self.draw_text(title, 48, (255, 255, 255), width // 2, height // 4 + 50) @@ -145,7 +142,7 @@ class Game: def show_go_screen(self): # game over screen - pygame.mixer.music.load(os.path.join(self.sound_dir, '20 - Game Over.ogg')) + pygame.mixer.music.load(os.path.join(sound_folder, '20 - Game Over.ogg')) pygame.mixer.music.play(loops=-1) if not self.running: return diff --git a/map2.txt b/map2.txt new file mode 100644 index 0000000..176c03c --- /dev/null +++ b/map2.txt @@ -0,0 +1,16 @@ +............................................................ +....P.....1................................................. +............................................................ +............................................................ +...............11........................................... +............................................................ +....111..................................................... +...................111...................................... +...........111.............................................. +................111.............1111111111.................. +.................................................11111111... +.....111............11111111................................ +1..........11111............................................ +1............................1.............................. +111111111111111111111111111111.............................. +..............................111111111111111111111111111111 \ No newline at end of file diff --git a/new_main.py b/new_main.py deleted file mode 100644 index e69de29..0000000 diff --git a/tylemap.py b/tylemap.py new file mode 100644 index 0000000..9a725c1 --- /dev/null +++ b/tylemap.py @@ -0,0 +1,32 @@ +import pygame +import os +from settings import * + + +class Map: + def __init__(self, filename): + self.data = [] + with open(filename, 'rt') as f: + for line in f: + self.data.append(line) + self.tilewidth = len(self.data[0]) + self.tileheight = len(self.data) + self.width = self.tilewidth * tilesize + self.height = self.tileheight * tilesize + + +class Camera: + def __init__(self, width, height): + self.camera = pygame.Rect(0, 0, width, height) + self.width, self.height = width, height + + def apply(self, entity): + return entity.rect.move(self.camera.topleft) + + def update(self, target): + x = -target.rect.x + width // 2 + y = -target.rect.y + height // 2 + + # limit scrolling + x = min(0,x) #left + self.camera = pygame.Rect(x, y, self.width, self.height)