Skip to content
Permalink
Browse files
Screen Class created
Screen now follows the player and the map created can be as big as necessary.
  • Loading branch information
dacostag committed Apr 30, 2020
1 parent ab485d1 commit af5f53e85bad24f315488238381661deed316754
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 30 deletions.
Binary file not shown.
57 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,25 +69,29 @@ 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
# collision when the player hits the block from below
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,16 +121,16 @@ 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"
pygame.display.flip()

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
@@ -0,0 +1,16 @@
............................................................
....P.....1.................................................
............................................................
............................................................
...............11...........................................
............................................................
....111.....................................................
...................111......................................
...........111..............................................
................111.............1111111111..................
.................................................11111111...
.....111............11111111................................
1..........11111............................................
1............................1..............................
111111111111111111111111111111..............................
..............................111111111111111111111111111111
Empty file.
@@ -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)

0 comments on commit af5f53e

Please sign in to comment.