Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import pygame
import os.path
from pygame import mixer
from sprites import *
from settings import *
from tylemap import *
# music: Licensed CC-BY. Attribute to Avgvst.
# https://opengameart.org/content/generic-8-bit-jrpg-soundtrack
class Game:
def __init__(self):
# initialize game window, etc
pygame.init()
pygame.mixer.init()
self.screen = pygame.display.set_mode(((width, height)))
# Title and Icon
pygame.display.set_caption(title)
icon = pygame.image.load('img/icon.png')
pygame.display.set_icon(icon)
self.font_name = pygame.font.match_font(basic_font)
self.clock = pygame.time.Clock()
self.running = True
self.load_data()
def load_data(self):
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 = Map(os.path.join(game_folder, 'map2.txt'))
def new(self):
# starts a new game not a new program
self.score = 0
self.all_sprites = pygame.sprite.Group()
self.platforms = pygame.sprite.Group()
self.tile = pygame.sprite.Group()
# spawn walls from map
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.all_sprites.add(self.player)
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):
# Game loop
pygame.mixer.music.play(loops=-1)
self.playing = True
while self.playing:
self.dt = self.clock.tick(fps) / 1000
self.events()
self.update()
self.draw()
pygame.mixer.music.fadeout(1000)
def update(self):
# Game loop - Update
self.all_sprites.update()
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 + 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
# Die, by falling of the screen
if self.player.rect.bottom > height + 60:
self.playing = False
def events(self):
# Game loop - events
for event in pygame.event.get():
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.player.jump()
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
self.player.jump_cut()
def draw_grid(self):
for x in range(0, width, tilesize):
pygame.draw.line(self.screen, (169, 169, 169), (x, 0), (x, height))
for y in range(0, height, tilesize):
pygame.draw.line(self.screen, (169, 169, 169), (0, y), (width, y))
def draw(self):
# Game loop - draw/render
self.screen.fill((146, 244, 255))
self.draw_grid()
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(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)
self.draw_text("Arrows to move, Z to shoot", 22, (255, 255, 255), width // 2, height // 2)
self.draw_text("Press a key to play", 22, (255, 255, 255), width // 2, height // 2 + 40)
pygame.display.flip()
self.wait_for_key()
pygame.mixer.music.fadeout(1000)
def show_go_screen(self):
# game over screen
pygame.mixer.music.load(os.path.join(sound_folder, '20 - Game Over.ogg'))
pygame.mixer.music.play(loops=-1)
if not self.running:
return
self.screen.fill(BG_colour)
self.draw_text("GAME OVER", 48, (255, 255, 255), width // 2, height // 4 + 50)
self.draw_text("Final Score: " + str(self.score), 22, (255, 255, 255), width / 2, height // 2)
self.draw_text("Press a key to play again", 22, (255, 255, 255), width // 2, height // 2 + 40)
pygame.display.flip()
self.wait_for_key()
pygame.mixer.music.fadeout(1000)
def wait_for_key(self):
waiting = True
while waiting:
self.clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
waiting = False
self.running = False
if event.type == pygame.KEYUP:
waiting = False
def draw_text(self, text, size, color, x, y):
font = pygame.font.Font(self.font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
self.screen.blit(text_surface, text_rect)
g = Game()
g.show_start_screen()
while g.running:
g.new()
g.show_go_screen() # go stands for Game Over
pygame.quit()