Skip to content
Permalink
7607ac804a
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
184 lines (160 sloc) 6.97 KB
import pygame
import random
import math
from pygame import mixer
from settings import *
from sprites import *
import os.path
# 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.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 sound
self.sound_dir = os.path.join(self.dir, 'sound')
self.jump_sound = pygame.mixer.Sound(os.path.join(self.sound_dir, 'Jump13.wav'))
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.player = Player(self)
self.all_sprites.add(self.player)
for plat in platform_list:
p = Platform(self, *plat)
self.all_sprites.add(p)
self.platforms.add(p)
pygame.mixer.music.load(os.path.join(self.sound_dir, '04 - Sanctuary.ogg'))
self.run()
def run(self):
# Game loop
pygame.mixer.music.play(loops=-1)
self.playing = True
while self.playing:
self.clock.tick(fps)
self.events()
self.update()
self.draw()
pygame.mixer.music.fadeout(1000)
def update(self):
# Game loop - Update
self.all_sprites.update()
# check if player hits platform
hits = pygame.sprite.spritecollide(self.player, self.platforms, False)
if hits:
lowest = max(hits, key=lambda x: x.rect.bottom)
highest = min(hits, key=lambda x: x.rect.bottom)
#print("Player pos top:", str(self.player.rect.left), "y", str(self.player.pos.x), "bottom",
#str(self.player.rect.right))
if self.player.pos.y < lowest.rect.centery - 5: # if falling on block - player remains on top of it
self.player.pos.y = lowest.rect.top
self.player.vel.y = 0
self.player.jumping = False
# if player hits block from the side
if self.player.rect.right > highest.rect.left or self.player.rect.left < highest.rect.right:
if self.player.pos.y > highest.rect.centery:
if self.player.rect.right > highest.rect.left:
self.player.pos.x = highest.rect.left - 18 # 18 is 1 pixel more then half the width of the player sprite
if self.player.rect.left < highest.rect.right:
self.player.pos.x = highest.rect.right + 18
self.player.vel.x = 0
self.player.vel.y = 2
if self.player.pos.y > lowest.rect.bottom: # if jumping from below - player hits his head on the platform
self.player.rect.top = lowest.rect.bottom + 5
self.player.vel.y = 0
# if player reaches 3/5 of width of the screen
if self.player.rect.right >= (width * 0.6):
self.player.pos.x -= abs(self.player.vel.x)
for plat in self.platforms:
plat.rect.x -= abs(self.player.vel.x)
# if player reaches 1/5 of the left side of the screen move screen
if self.player.rect.left <= (width * 0.2):
self.player.pos.x += abs(self.player.vel.x)
for plat in self.platforms:
plat.rect.x += abs(self.player.vel.x)
# 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(self):
# Game loop - draw/render
self.screen.fill((146, 244, 255))
self.all_sprites.draw(self.screen)
self.screen.blit(self.player.image, self.player.rect)
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.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(self.sound_dir, '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()