Skip to content

Commit

Permalink
Moves most hard-coded values (except keys) to globals.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
BertrandBordage committed Apr 11, 2013
1 parent 62940d6 commit a5e6a98
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 116 deletions.
126 changes: 114 additions & 12 deletions globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,136 @@
# Third-party packages
from pyglet.resource import get_settings_path


APP_NAME = 'pyCraftr' # should I stay or should I go?


#
# Game modes
#

SURVIVAL_MODE = 'survival'
CREATIVE_MODE = 'creative'
GAME_MODE_CHOICES = (SURVIVAL_MODE, CREATIVE_MODE)
GAME_MODE = CREATIVE_MODE


#
# Saves
#

DISABLE_SAVE = True
SAVE_FILENAME = None

PICKLE_SAVE_MODE = 'pickle'
PICKLE_COMPRESSED_SAVE_MODE = 'pickle_compressed'
FLATFILE_SAVE_MODE = 'flatfile'
SAVE_MODES = (
PICKLE_SAVE_MODE, PICKLE_COMPRESSED_SAVE_MODE, FLATFILE_SAVE_MODE
)
SAVE_MODE = FLATFILE_SAVE_MODE


#
# Game engine
#

SECTOR_SIZE = 8
TILESET_SIZE = 16 # The tileset therefore contains TILESET_SIZE ** 2 tiles.


#
# Game logic
#

TIME_RATE = 240 * 10 # Rate of change (steps per hour).
SPREADING_MUTATION_DELAY = 4 # in seconds


#
# Terrain generation
#

TERRAIN_CHOICES = {
'plains': {
'hill_height': '2',
'max_trees': '700',
},
'desert': {
'hill_height': '5',
'max_trees': '50',
},
'island': {
'hill_height': '8',
'max_trees': '700',
},
'mountains': {
'hill_height': '12',
'max_trees': '4000',
},
'snow': {
'hill_height': '4',
'max_trees': '1500',
}
}
DEFAULT_TERRAIN_CHOICE = 'plains'


#
# Graphical rendering
#

WINDOW_WIDTH = 850 # Screen width (in pixels)
WINDOW_HEIGHT = 480 # Screen height (in pixels)

MAX_FPS = 60 # Maximum frames per second.

VISIBLE_SECTORS_RADIUS = 8
DRAW_DISTANCE = 60.0

DRAW_DISTANCE_CHOICES = {
'short': 60.0,
'medium': 60.0 * 1.5,
'long': 60.0 * 2.0
}
DEFAULT_DRAW_DISTANCE_CHOICE = 'short'
DRAW_DISTANCE = DRAW_DISTANCE_CHOICES[DEFAULT_DRAW_DISTANCE_CHOICE]

FOV = 65.0 # TODO: add menu option to change FOV
NEAR_CLIP_DISTANCE = 0.1 # TODO: make min and max clip distance dynamic
FAR_CLIP_DISTANCE = 200.0 # Maximum render distance,
# ignoring effects of sector_size and fog
DISABLE_SAVE = True
TIME_RATE = 240 * 10 # Rate of change (steps per hour).
MAX_FPS = 60 # Maximum frames per second.
DEG_RAD = pi / 180.0
HALF_PI = pi / 2.0 # 90 degrees
SPREADING_MUTATION_DELAY = 4 # in seconds
TILESET_SIZE = 16 # The tileset therefore contains TILESET_SIZE ** 2 tiles.
GAME_MODE = 'creative'

EFFECT_VOLUME = 1
MOTION_BLUR = False

SAVE_FILENAME = None

# Tool type
#
# Sound
#

EFFECT_VOLUME = 1


#
# Tool types
#

WOODEN_TOOL, STONE_TOOL, IRON_TOOL, DIAMOND_TOOL, GOLDEN_TOOL = range(5)
PICKAXE, AXE, SHOVEL, HOE, SWORD = range(5)
HELMET, CHESTPLATE, LEGGINGS, BOOTS = range(4)


#
# Static aliases
#

DEG_RAD = pi / 180.0
HALF_PI = pi / 2.0 # 90 degrees


#
# Global files & directories
#

config = ConfigParser()
LAUNCH_OPTIONS = argparse.Namespace()

Expand Down
49 changes: 22 additions & 27 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@
from savingsystem import *
from controllers import *

terrain_options = {
'plains': ('0', '2', '700'), # type, hill_height, max_trees
'mountains': ('5', '12', '4000'),
'desert': ('2', '5', '50'),
'island': ('3', '8', '700'),
'snow': ('6', '4', '1500')
}

config_file = os.path.join(globals.game_dir, 'game.cfg')
if not os.path.lexists(config_file):
type, hill_height, max_trees = terrain_options['plains']
globals.config.add_section('World')
globals.config.set('World', 'type', str(type)) # 0=plains,1=dirt,2=desert,3=islands,4=sand,5=stone,6=snow
globals.config.set('World', 'hill_height', str(hill_height)) # height of the hills

world_type = globals.DEFAULT_TERRAIN_CHOICE # FIXME: Unify names!
globals.config.set('World', 'type', world_type)
terrain = globals.TERRAIN_CHOICES[world_type]
for k, v in terrain.items():
globals.config.set('World', k, v)

globals.config.set('World', 'flat', 'false') # dont make mountains, make a flat world
globals.config.set('World', 'size', '64')
globals.config.set('World', 'show_fog', 'true')
globals.config.set('World', 'max_trees', str(max_trees))

globals.config.add_section('Controls')
globals.config.set('Controls', 'move_forward', str(key.W))
Expand Down Expand Up @@ -119,16 +115,15 @@ def main(options):
for name, val in options._get_kwargs():
setattr(globals.LAUNCH_OPTIONS, name, val)

if options.draw_distance == 'medium':
globals.DRAW_DISTANCE = 60.0 * 1.5
elif options.draw_distance == 'long':
globals.DRAW_DISTANCE = 60.0 * 2.0
if options.draw_distance:
globals.DRAW_DISTANCE = globals.DRAW_DISTANCE_CHOICES[options.draw_distance]

if options.terrain:
type, hill_height, max_trees = terrain_options[options.terrain]
globals.config.set('World', 'type', type)
globals.config.set('World', 'hill_height', hill_height)
globals.config.set('World', 'max_trees', max_trees)
world_type = options.terrain # FIXME: Unify names!
globals.config.set('World', 'type', world_type)
terrain = globals.TERRAIN_CHOICES[world_type]
for k, v in terrain.items():
globals.config.set('World', k, v)

if options.flat:
globals.config.set('World', 'flat', 'true')
Expand Down Expand Up @@ -177,26 +172,26 @@ def main(options):

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Play a Python made Minecraft clone.')

display_group = parser.add_argument_group('Display options')
display_group.add_argument("--width", type=int, default=850, help="Set the window width.")
display_group.add_argument("--height", type=int, default=480, help="Set the window height.")
display_group.add_argument("--width", type=int, default=globals.WINDOW_WIDTH, help="Set the window width.")
display_group.add_argument("--height", type=int, default=globals.WINDOW_HEIGHT, help="Set the window height.")
display_group.add_argument("--show-gui", action="store_true", default=True, help="Enabled by default.")
display_group.add_argument("--draw-distance", choices=['short', 'medium', 'long'], default='short', help=" How far to draw the map. Choose short, medium or long.")
display_group.add_argument("--draw-distance", choices=globals.DRAW_DISTANCE_CHOICES, default=globals.DEFAULT_DRAW_DISTANCE_CHOICE, help="How far to draw the map.")
display_group.add_argument("--fullscreen", action="store_true", default=False, help="Runs the game in fullscreen. Press 'Q' to exit the game.")

game_group = parser.add_argument_group('Game options')
game_group.add_argument("--terrain", choices=terrain_options.keys(), help="Different terrains. Choose grass, island, mountains,desert, plains")
game_group.add_argument("--terrain", choices=globals.TERRAIN_CHOICES, default=globals.DEFAULT_TERRAIN_CHOICE)
game_group.add_argument("--flat", action="store_true", default=False, help="Generate a flat world.")
game_group.add_argument("--fast", action="store_true", default=False, help="Makes time progress faster then normal.")
game_group.add_argument("--game-mode", choices=('survival', 'creative'), default=globals.GAME_MODE, help="Sets the game mode.")
game_group.add_argument("--game-mode", choices=globals.GAME_MODE_CHOICES, default=globals.GAME_MODE)

save_group = parser.add_argument_group('Save options')
save_group.add_argument("--disable-auto-save", action="store_false", default=True, help="Do not save world on exit.")
save_group.add_argument("--save", type=unicode, default=globals.SAVE_FILENAME, help="Type a name for the world to be saved as.")
save_group.add_argument("--save", default=globals.SAVE_FILENAME, help="Type a name for the world to be saved as.")
save_group.add_argument("--disable-save", action="store_false", default=True, help="Disables saving.")
save_group.add_argument("--save-config", action="store_true", default=False, help="Saves the choices as the default config.")
save_group.add_argument("--save-mode", type=int, default=2, help="0 = Uncompressed Pickle, 1 = Compressed Pickle, 2 = Flatfile Struct (smallest, fastest)")
save_group.add_argument("--save-mode", choices=globals.SAVE_MODES, default=globals.SAVE_MODE, help="Flatfile Struct (flatfile) is the smallest and fastest")

parser.add_argument("--seed", default=None)
parser.add_argument("--motion-blur", action="store_true", default=False)
Expand Down
94 changes: 30 additions & 64 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, initialize=True):

def initialize(self):
world_size = globals.config.getint('World', 'size')
world_type = globals.config.getint('World', 'type')
world_type = globals.config.get('World', 'type')
hill_height = globals.config.getint('World', 'hill_height')
flat_world = globals.config.getboolean('World', 'flat')
self.max_trees = globals.config.getint('World', 'max_trees')
Expand All @@ -29,58 +29,28 @@ def initialize(self):
s = 1
y = 0

worldtypes_grounds = (
dirt_block,
dirt_block,
(sand_block,) * 15 + (sandstone_block,) * 4,
(water_block,) * 30 + (clay_block,) * 4,
dirt_block,
(dirt_block,) * 15 + (dirt_block,) * 3 + (stone_block,),
(snowgrass_block,) * 10 + (snow_block,) * 4 + (ice_block,) * 8,
)

world_type_trees = (
(OakTree, BirchTree, WaterMelon, Pumpkin, YFlowers, Potato),
(OakTree, WaterMelon, YFlowers, Rose, Carrot),
(Cactus, TallCactus, Rose),
(OakTree, JungleTree, BirchTree, Cactus, TallCactus, WaterMelon, YFlowers, Reed),
(Cactus, BirchTree, TallCactus, YFlowers, Reed, Carrot),
(OakTree, BirchTree, Pumpkin, YFlowers, Potato, Carrot),
(OakTree, BirchTree, WaterMelon, YFlowers, Potato, Rose,),
)

ore_type_blocks = (
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
gravel_block, gravel_block, gravel_block, gravel_block, gravel_block,
gravel_block, gravel_block, gravel_block, gravel_block, gravel_block,
coalore_block, coalore_block, coalore_block, coalore_block, coalore_block,
ironore_block, ironore_block, ironore_block, ironore_block,
goldore_block, goldore_block, goldore_block,
diamondore_block, diamondore_block,
emeraldore_block, emeraldore_block,
rubyore_block, rubyore_block,
sapphireore_block, sapphireore_block,
lapisore_block, lapisore_block,
quartz_block, quartz_block, quartz_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
stone_block, stone_block, stone_block, stone_block, stone_block,
)

#ore_type_blocks = (
#(stone_block,) * 50 + (gravel_block,) * 10 + (coalore_block,) * 5 + (ironore_block,) * 4 + (goldore_block,) * 3 + (diamondore_block,) + (stone_block,) * 15,
#)
worldtypes_grounds = {
'plains': dirt_block,
'desert': (sand_block,) * 15 + (sandstone_block,) * 4,
'island': (water_block,) * 30 + (clay_block,) * 4,
'mountains': (dirt_block,) * 15 + (dirt_block,) * 3 + (stone_block,),
'snow': (snowgrass_block,) * 10 + (snow_block,) * 4 + (ice_block,) * 8,
}

world_type_trees = {
'plains': (OakTree, BirchTree, WaterMelon, Pumpkin, YFlowers, Potato),
'desert': (Cactus, TallCactus, Rose),
'island': (OakTree, JungleTree, BirchTree, Cactus, TallCactus, WaterMelon, YFlowers, Reed),
'mountains': (OakTree, BirchTree, Pumpkin, YFlowers, Potato, Carrot),
'snow': (OakTree, BirchTree, WaterMelon, YFlowers, Potato, Rose,),
}

ore_type_blocks = (stone_block,) * 75 + (gravel_block,) * 10 \
+ (coalore_block,) * 5 + (ironore_block,) * 5 \
+ (goldore_block,) * 3 + (diamondore_block,) * 2 \
+ (emeraldore_block,) * 2 + (rubyore_block,) * 2 \
+ (sapphireore_block,) * 2 + (lapisore_block,) * 8 \
+ (quartz_block,) * 3

for x in xrange(-n, n + 1, s):
for z in xrange(-n, n + 1, s):
Expand All @@ -91,8 +61,6 @@ def initialize(self):
self.init_block((x, y + dy, z), stone_block)
continue



# Generation of the ground

block = worldtypes_grounds[world_type]
Expand Down Expand Up @@ -122,15 +90,13 @@ def initialize(self):

o = n - 10 + hill_height - 6

world_type_blocks = (
dirt_block,
(dirt_block, sandstone_block),
sand_block,
(dirt_block, sand_block),
(dirt_block,) * 2 + (sand_block,),
stone_block,
snowgrass_block,
)
world_type_blocks = {
'plains': dirt_block,
'desert': sand_block,
'island': (dirt_block, sand_block),
'mountains': stone_block,
'snow': snowgrass_block,
}

# Hills generation
# FIXME: This generation in two phases (ground then hills), leads to
Expand Down
4 changes: 2 additions & 2 deletions player.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def on_key_press(self, symbol, modifiers):
elif symbol == key.LSHIFT or symbol == key.RSHIFT:
if self.flying:
self.dy = -0.045 # inversed jump speed
elif symbol == key.TAB and self.game_mode == 'creative':
elif symbol == key.TAB and self.game_mode == globals.CREATIVE_MODE:
self.dy = 0
self.flying = not self.flying

Expand Down Expand Up @@ -160,7 +160,7 @@ def collide(self, parent, position, height):
p[i] -= (d - pad) * face[i]
if face == (0, -1, 0) or face == (0, 1, 0):
# jump damage
if self.game_mode == 'survival':
if self.game_mode == globals.SURVIVAL_MODE:
damage = self.dy * -1000.0
damage = 3.0 * damage / 22.0
damage -= 2.0
Expand Down
Loading

1 comment on commit a5e6a98

@BertrandBordage
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant "keyboard keys".

Please sign in to comment.