Add user configuration for graphics

This commit is contained in:
Nguyễn Gia Phong 2018-02-02 23:33:22 +07:00
parent 1b2dc5e169
commit dbe0ae4c01
4 changed files with 40 additions and 11 deletions

View File

@ -18,7 +18,14 @@
# Copyright (C) 2017 Nguyễn Gia Phong
from collections import deque
try:
from configparser import ConfigParser # Python 3
except ImportError:
from ConfigParser import ConfigParser # Python 2
from os.path import join
from appdirs import AppDirs
from pkg_resources import resource_filename
import pygame
from pygame.locals import *
@ -29,21 +36,36 @@ from .misc import some
def main():
"""Start game and main loop."""
# Read configuration file
dirs = AppDirs(appname='brutalmaze')
config = ConfigParser()
if not config.read(join(dirs.user_config_dir, 'settings.ini')):
if not config.read(join(dirs.site_config_dir, 'settings.ini')):
config.read(resource_filename('brutalmaze', 'settings.ini'))
scrtype = RESIZABLE
if config.getboolean('Graphics', 'OpenGL'):
surftype |= OPENGL | DOUBLEBUF
fps = config.getfloat('Graphics', 'Maximum FPS')
# Initialization
pygame.mixer.pre_init(frequency=44100)
pygame.init()
pygame.mixer.music.load(MUSIC)
pygame.mixer.music.play(-1)
pygame.display.set_icon(ICON)
pygame.fastevent.init()
maze, clock = Maze(SIZE, INIT_FPS), pygame.time.Clock()
fps, flash_time, going = INIT_FPS, deque(), True
maze = Maze((config.getint('Graphics', 'Screen width'),
config.getint('Graphics', 'Screen height')), scrtype, fps)
clock, flash_time, going = pygame.time.Clock(), deque(), True
# Main loop
while going:
events = pygame.fastevent.get()
for event in events:
if event.type == QUIT:
going = False
elif event.type == VIDEORESIZE:
maze.resize(event.w, event.h)
maze.resize((event.w, event.h), scrtype)
elif event.type == KEYDOWN:
if event.key == K_F2: # new game
maze.__init__((maze.w, maze.h), fps)

View File

@ -81,10 +81,10 @@ class Maze:
sfx_shot (Sound): sound effect indicating an enemy get shot
sfx_lose (Sound): sound effect to be played when you lose
"""
def __init__(self, size, fps):
def __init__(self, size, scrtype, fps):
self.w, self.h = size
self.fps = fps
self.surface = pygame.display.set_mode(size, RESIZABLE)
self.surface = pygame.display.set_mode(size, scrtype)
self.distance = (self.w * self.h / 416) ** 0.5
self.x, self.y = self.w // 2, self.h // 2
self.centerx, self.centery = self.w / 2.0, self.h / 2.0
@ -339,10 +339,10 @@ class Maze:
self.vy += y * accel
if abs(self.vy) > velocity: self.vy = y * velocity
def resize(self, w, h):
def resize(self, size, scrtype):
"""Resize the maze."""
size = self.w, self.h = w, h
self.surface = pygame.display.set_mode(size, RESIZABLE)
self.w, self.h = size
self.surface = pygame.display.set_mode(size, scrtype)
self.hero.resize()
offsetx = (self.centerx-self.x) / self.distance

7
brutalmaze/settings.ini Normal file
View File

@ -0,0 +1,7 @@
[Graphics]
Screen width: 640
Screen height: 480
# OpenGL should be supported on all machines with hardware acceleration
OpenGL: no
# FPS should not be greater than refresh rate
Maximum FPS: 60

View File

@ -7,7 +7,7 @@ with open('README.rst') as f:
setup(
name='brutalmaze',
version='0.4.2',
version='0.5.0',
description='A hash and slash game with fast-paced action and a minimalist art style',
long_description=long_description,
url='https://github.com/McSinyx/brutalmaze',
@ -27,6 +27,6 @@ setup(
'Topic :: Games/Entertainment :: Arcade'],
keywords='pygame action-game arcade-game maze',
packages=['brutalmaze'],
install_requires=['pygame>=1.9'],
package_data={'brutalmaze': ['icon.png', 'soundfx/*.ogg']},
install_requires=['appdirs', 'pygame>=1.9'],
package_data={'brutalmaze': ['icon.png', 'soundfx/*.ogg', 'settings.ini']},
entry_points={'gui_scripts': ['brutalmaze = brutalmaze:main']})