Permalink
Cannot retrieve contributors at this time
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?
Quarantine-game/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
185 lines (158 sloc)
6.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 soundself. | |
self.sound_dir = os.path.join(self.dir, 'sound') | |
self.jump_sound = pygame.mixer.Sound(os.path.join(self.sound_dir, '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) | |
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) | |
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.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() | |
# 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 | |
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 | |
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 | |
# 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() | |
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() |