Skip to content
Permalink
b52c4dbd1a
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
172 lines (150 sloc) 6.15 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 - only if falling
if self.player.vel.y > 0:
hits = pygame.sprite.spritecollide(self.player, self.platforms, False)
if hits:
lowest = hits[0]
for hit in hits:
if hit.rect.bottom > lowest.rect.bottom:
lowest = hit
#if self.player.pos.x < lowest.rect.right +20 and self.player.pos.x > lowest.rect.left + 20:
if self.player.pos.y < lowest.rect.centery:
self.player.pos.y = lowest.rect.top
self.player.vel.y = 0
self.player.jumping = False
# if player reaches 3/4 of width of the screen
if self.player.rect.x >= (width * 0.6):
self.player.pos.x -= abs(self.player.vel.x)
for plat in self.platforms:
plat.rect.x -= self.player.vel.x
# if player reaches 1/5 of the left side of the screen move screen
if self.player.rect.x <= (width * 0.2):
self.player.pos.x += abs(self.player.vel.x)
for plat in self.platforms:
plat.rect.x -= 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(BG_colour)
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(BG_colour)
self.draw_text(title, 48, (255, 255, 255), width / 2, int(height / 4) + 50)
self.draw_text("Arrows to move, Z to shoot", 22, (255, 255, 255), width / 2, int(height / 2))
self.draw_text("Press a key to play", 22, (255, 255, 255), width / 2, int(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, int(height / 4) + 50)
self.draw_text("Final Score: " + str(self.score), 22, (255, 255, 255), width / 2, int(height / 2))
self.draw_text("Press a key to play again", 22, (255, 255, 255), width / 2, int(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()