Skip to content

Instantly share code, notes, and snippets.

@EricsonWillians
Last active November 6, 2015 18:30
Show Gist options
  • Save EricsonWillians/3f0bfbe617cba3291869 to your computer and use it in GitHub Desktop.
Save EricsonWillians/3f0bfbe617cba3291869 to your computer and use it in GitHub Desktop.
Pyglet Game App Template
# main.py
#
# Copyright 2015 Ericson Willians (Rederick Deathwill) <EricsonWRP@ERICSONWRP-PC>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import os
import pyglet
import json
class Serializable:
def __init__(self):
self.file = None
def serialize(self, path, mode):
try:
self.file = open(path, mode)
except:
raise FileNotFoundError()
if self.file:
return self.file
class App(Serializable, pyglet.window.Window):
def __init__(self):
Serializable.__init__(self)
if os.path.isfile("config.txt"):
self.data = self.load("config.txt")
else:
self.data = {
"SCREEN_WIDTH": 1024,
"SCREEN_HEIGHT": 768,
"SQUARE_SIZE": 32,
"GAME_SPEED": 0.1,
"FULLSCREEN": False,
"LOCKED_MOUSE": True}
self.write("config.txt")
pyglet.window.Window.__init__(self, self.data["SCREEN_WIDTH"],
self.data["SCREEN_HEIGHT"],
fullscreen=self.data["FULLSCREEN"])
if self.data["LOCKED_MOUSE"]:
self.set_exclusive_mouse()
def write(self, path):
self.serialize(path, "w").write(json.dumps(self.data))
def load(self, path):
json_data = open(path, "r")
self.data = json.load(json_data)
json_data.close()
return self.data
if __name__ == '__main__':
app = App()
label = pyglet.text.Label("Hello World!",
font_name = "Times New Roman",
font_size=100,
x = app.width // 2, y = app.height // 2,
anchor_x = "center", anchor_y = "center")
@app.event
def on_draw():
app.clear()
label.draw()
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment