Skip to content

Commit

Permalink
Fixes global variables overrides and rewrites parameter game_mode.
Browse files Browse the repository at this point in the history
Never use "from  globals import *" again!
  • Loading branch information
BertrandBordage committed Apr 11, 2013
1 parent 961a1ed commit cc1de58
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 227 deletions.
40 changes: 0 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,46 +86,6 @@ For git:

- **ESC** / **Q**: release mouse, then close window

### Command-line arguments
usage: main.py [-h] [-width WIDTH] [-height HEIGHT]
[-terrain {plains,island,desert,snow,mountains}]
[-hillheight HILLHEIGHT] [-worldsize WORLDSIZE]
[-maxtrees MAXTREES] [-seed SEED] [--flat] [--hide-fog]
[--show-gui] [--disable-auto-save]
[-draw-distance {short,medium,long}] [-save SAVE]
[--disable-save] [--fast] [--save-config] [-fullscreen]
[-nocompression] [-gamemode GAMEMODE]

Play a Python made Minecraft clone.

optional arguments:
-h, --help show this help message and exit
-width WIDTH Set the default Widht.
-height HEIGHT Set the default Height.
-terrain {plains,island,desert,snow,mountains}
Different terains. Choose grass, island,
mountains,desert, plains
-hillheight HILLHEIGHT
How high the hills are.
-worldsize WORLDSIZE The width size of the world.
-maxtrees MAXTREES How many trees and cacti should be made.
-seed SEED
--flat Generate a flat world.
--hide-fog Hides the fog, see the whole landscape.
--show-gui Enabled by default.
--disable-auto-save Do not save world on exit.
-draw-distance {short,medium,long}
How far to draw the map. Choose short, medium or long.
-save SAVE Type a name for the world to be saved as.
--disable-save Disables saving.
--fast Makes time progress faster then normal.
--save-config Saves the choices as the default config.
-fullscreen Runs the game in fullscreen. Press 'Q' to exit the
game.
-nocompression Disables compression for a smaller save file.
-gamemode GAMEMODE Set the Gamemode for player. 0 = Creative, 1 =
Survival

## Better performance using Cython

For optimal performance, you can compile the code to C using Cython.
Expand Down
49 changes: 27 additions & 22 deletions blocks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# coding: utf-8

# Future imports
from __future__ import unicode_literals
from globals import *
import sounds
# Python packages
import os
# Third-party packages
from pyglet.gl import *
from pyglet.image.atlas import TextureAtlas
# Modules from this project
import globals
import sounds


def get_texture_coordinates(x, y, tileset_size=TILESET_SIZE):
def get_texture_coordinates(x, y, tileset_size=globals.TILESET_SIZE):
if x == -1 and y == -1:
return ()
m = 1.0 / tileset_size
Expand Down Expand Up @@ -153,7 +158,7 @@ class AirBlock(Block):

class WoodBlock(Block):
break_sound = sounds.wood_break
digging_tool = AXE
digging_tool = globals.AXE


class HardBlock(Block):
Expand All @@ -167,7 +172,7 @@ class StoneBlock(HardBlock):
hardness = 1.5
id = 1
name = "Stone"
digging_tool = PICKAXE
digging_tool = globals.PICKAXE

def __init__(self):
super(StoneBlock, self).__init__()
Expand All @@ -183,7 +188,7 @@ class GrassBlock(Block):
id = 2
break_sound = sounds.dirt_break
name = 'Grass'
digging_tool = SHOVEL
digging_tool = globals.SHOVEL

def __init__(self):
super(GrassBlock, self).__init__()
Expand All @@ -198,7 +203,7 @@ class DirtBlock(Block):
hardness = 0.5
id = 3
name = "Dirt"
digging_tool = SHOVEL
digging_tool = globals.SHOVEL
break_sound = sounds.dirt_break

class SnowBlock(Block):
Expand All @@ -221,7 +226,7 @@ class SandBlock(Block):
amount_label_color = 0, 0, 0, 255
id = 12
name = "Sand"
digging_tool = SHOVEL
digging_tool = globals.SHOVEL
break_sound = sounds.sand_break


Expand All @@ -232,7 +237,7 @@ class GoldOreBlock(HardBlock):
texture_name = "oreGold",
hardness = 3
id = 14
digging_tool = PICKAXE
digging_tool = globals.PICKAXE
name = "Gold Ore"


Expand All @@ -243,7 +248,7 @@ class IronOreBlock(HardBlock):
texture_name = "oreIron",
hardness = 3
id = 15
digging_tool = PICKAXE
digging_tool = globals.PICKAXE
name = "Iron Ore"


Expand All @@ -254,7 +259,7 @@ class DiamondOreBlock(HardBlock):
texture_name = "oreDiamond",
hardness = 3
id = 56
digging_tool = PICKAXE
digging_tool = globals.PICKAXE
def __init__(self):
super(DiamondOreBlock, self).__init__()
self.drop_id = 264
Expand All @@ -268,7 +273,7 @@ class CoalOreBlock(HardBlock):
texture_name = "oreCoal",
hardness = 3
id = 16
digging_tool = PICKAXE
digging_tool = globals.PICKAXE
def __init__(self):
super(CoalOreBlock, self).__init__()
self.drop_id = 263
Expand All @@ -282,7 +287,7 @@ class BrickBlock(HardBlock):
texture_name = "brick",
hardness = 2
id = 45
digging_tool = PICKAXE
digging_tool = globals.PICKAXE
name = "Bricks"


Expand Down Expand Up @@ -319,7 +324,7 @@ class GravelBlock(Block):
amount_label_color = 0, 0, 0, 255
id = 13
name = "Gravel"
digging_tool = SHOVEL
digging_tool = globals.SHOVEL
break_sound = sounds.gravel_break


Expand Down Expand Up @@ -402,7 +407,7 @@ class QuartzBlock(HardBlock):
hardness = 2
name = "Quartz"
amount_label_color = 0, 0, 0, 255
digging_tool = PICKAXE
digging_tool = globals.PICKAXE


class StonebrickBlock(HardBlock):
Expand Down Expand Up @@ -898,7 +903,7 @@ class DiamondBlock(HardBlock):
side_texture = 11, 0
hardness = 5
id = 57
digging_tool = PICKAXE
digging_tool = globals.PICKAXE
name = "Diamond Block"

class GoldBlock(HardBlock):
Expand All @@ -907,7 +912,7 @@ class GoldBlock(HardBlock):
side_texture = 11, 1
hardness = 4
id = 41
digging_tool = PICKAXE
digging_tool = globals.PICKAXE
name = "Gold Block"

class IronBlock(HardBlock):
Expand All @@ -916,7 +921,7 @@ class IronBlock(HardBlock):
side_texture = 11, 2
hardness = 4
id = 42
digging_tool = PICKAXE
digging_tool = globals.PICKAXE
name = "Iron Block"

class StonebrickBlock(HardBlock):
Expand Down Expand Up @@ -952,7 +957,7 @@ class QuartzBlock(HardBlock):
hardness = 2
name = "Quartz"
amount_label_color = 0, 0, 0, 255
digging_tool = PICKAXE
digging_tool = globals.PICKAXE

class ColumnQuartzBlock(HardBlock):
top_texture = 3, 2
Expand All @@ -962,7 +967,7 @@ class ColumnQuartzBlock(HardBlock):
hardness = 2
name = "Column Quartz"
amount_label_color = 0, 0, 0, 255
digging_tool = PICKAXE
digging_tool = globals.PICKAXE

class ChisledQuartzBlock(HardBlock):
top_texture = 3, 2
Expand All @@ -972,7 +977,7 @@ class ChisledQuartzBlock(HardBlock):
hardness = 2
name = "Chisled Quartz"
amount_label_color = 0, 0, 0, 255
digging_tool = PICKAXE
digging_tool = globals.PICKAXE

class IceBlock(Block):
top_texture = 8, 7
Expand All @@ -991,7 +996,7 @@ class MossyStoneBlock(HardBlock):
hardness = 1.5
id = 48
name = "Mossy Stone"
digging_tool = PICKAXE
digging_tool = globals.PICKAXE
max_stack_size = 64

CRACK_LEVELS = 10
Expand Down
58 changes: 29 additions & 29 deletions controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# Third-party packages
from pyglet.window import key
from pyglet.text import Label
from pyglet.gl import *
# Modules from this project
from cameras import *
import globals
from globals import *
from gui import *
from model import *
from player import *
Expand Down Expand Up @@ -68,7 +68,7 @@ def __init__(self, window, show_gui=True):
self.start_game = Button(0, 0, 160, 50, image=button_image, caption="Start game", batch=self.batch, group=self.group, label_group=self.labels_group, on_click=self.start_game_func, font_name='ChunkFive Roman')
self.exit_game = Button(0, 0, 160, 50, image=button_image, caption="Exit game", batch=self.batch, group=self.group, label_group=self.labels_group, on_click=self.exit_game_func, font_name='ChunkFive Roman')
self.buttons = [self.start_game, self.exit_game]
self.label = Label(APP_NAME, font_name='ChunkFive Roman', font_size=50, x=window.width/2, y=self.frame.y + self.frame.height,
self.label = Label(globals.APP_NAME, font_name='ChunkFive Roman', font_size=50, x=window.width/2, y=self.frame.y + self.frame.height,
anchor_x='center', anchor_y='top', color=(255, 255, 255, 255), batch=self.batch,
group=self.labels_group)

Expand Down Expand Up @@ -136,12 +136,12 @@ def __init__(self, window, show_gui=True):
self.block_damage = 0
self.crack = None
self.mouse_pressed = False
self.show_fog = config.getboolean('World', 'show_fog')
self.show_fog = globals.config.getboolean('World', 'show_fog')
self.last_key = None
self.sorted = False
self.key_inventory = config.getint('Controls', 'inventory')
self.key_sound_up = config.getint('Controls', 'sound_up')
self.key_sound_down = config.getint('Controls', 'sound_down')
self.key_inventory = globals.config.getint('Controls', 'inventory')
self.key_sound_up = globals.config.getint('Controls', 'sound_up')
self.key_sound_down = globals.config.getint('Controls', 'sound_down')

def update(self, dt):
sector = sectorize(self.player.position)
Expand Down Expand Up @@ -209,7 +209,7 @@ def setup(self):
glFogi(GL_FOG_MODE, GL_LINEAR)
glFogf(GL_FOG_DENSITY, 0.35)
glFogf(GL_FOG_START, 20.0)
glFogf(GL_FOG_END, DRAW_DISTANCE) # 80)
glFogf(GL_FOG_END, globals.DRAW_DISTANCE) # 80)

self.window.set_exclusive_mouse(True)
self.focus_block = Block(width=1.05, height=1.05)
Expand All @@ -218,15 +218,14 @@ def setup(self):
self.ambient = vec(1.0, 1.0, 1.0, 1.0)
self.polished = GLfloat(100.0)
self.crack_batch = pyglet.graphics.Batch()
if DISABLE_SAVE and world_exists(game_dir, SAVE_FILENAME):
open_world(self, game_dir, SAVE_FILENAME)
if globals.DISABLE_SAVE \
and world_exists(globals.game_dir, globals.SAVE_FILENAME):
open_world(self, globals.game_dir, globals.SAVE_FILENAME)
else:
self.model = Model()
self.player = Player((0, 0, 0), (-20, 0), game_mode=GAMEMODE)
if self.player.game_mode == 0:
print('Game mode: Creative')
if self.player.game_mode == 1:
print('Game mode: Survival')
self.player = Player((0, 0, 0), (-20, 0),
game_mode=globals.GAME_MODE)
print('Game mode: ' + self.player.game_mode)
self.item_list = ItemSelector(self, self.player, self.model)
self.inventory_list = InventorySelector(self, self.player, self.model)
self.item_list.on_resize(self.window.width, self.window.height)
Expand All @@ -241,7 +240,8 @@ def setup(self):
self.label = pyglet.text.Label(
'', font_name='Arial', font_size=8, x=10, y=self.window.height - 10,
anchor_x='left', anchor_y='top', color=(255, 255, 255, 255))
pyglet.clock.schedule_interval_soft(self.model.process_queue, 1.0 / MAX_FPS)
pyglet.clock.schedule_interval_soft(self.model.process_queue,
1.0 / globals.MAX_FPS)

def update_time(self):
"""
Expand All @@ -258,21 +258,23 @@ def update_time(self):
else 24.0 - self.time_of_day

if time_of_day <= 2.5:
self.time_of_day += 1.0 / TIME_RATE
time_of_day += 1.0 / TIME_RATE
self.time_of_day += 1.0 / globals.TIME_RATE
time_of_day += 1.0 / globals.TIME_RATE
self.count += 1
else:
self.time_of_day += 20.0 / TIME_RATE
time_of_day += 20.0 / TIME_RATE
self.time_of_day += 20.0 / globals.TIME_RATE
time_of_day += 20.0 / globals.TIME_RATE
self.count += 1.0 / 20.0
if self.time_of_day > 24.0:
self.time_of_day = 0.0
time_of_day = 0.0

side = len(self.model.sectors) * 2.0

self.light_y = 2.0 * side * sin(time_of_day * self.hour_deg * DEG_RAD)
self.light_z = 2.0 * side * cos(time_of_day * self.hour_deg * DEG_RAD)
self.light_y = 2.0 * side * sin(time_of_day * self.hour_deg
* globals.DEG_RAD)
self.light_z = 2.0 * side * cos(time_of_day * self.hour_deg
* globals.DEG_RAD)
if time_of_day <= 2.5:
ambient_value = 1.0
else:
Expand All @@ -285,7 +287,7 @@ def update_time(self):
self.bg_green = 0.9 * sin_t
self.bg_blue = min(sin_t + 0.4, 0.8)

if fmod(self.count / 2, TIME_RATE) == 0:
if fmod(self.count / 2, globals.TIME_RATE) == 0:
if self.clock == 18:
self.clock = 6
else:
Expand All @@ -299,8 +301,8 @@ def set_highlighted_block(self, block):
self.crack = None

def save_to_file(self):
if DISABLE_SAVE:
save_world(self, game_dir, SAVE_FILENAME)
if globals.DISABLE_SAVE:
save_world(self, globals.game_dir, globals.SAVE_FILENAME)

def on_mouse_press(self, x, y, button, modifiers):
if self.window.exclusive:
Expand Down Expand Up @@ -401,11 +403,9 @@ def set_3d(self):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
if width != float(height):
gluPerspective(FOV, width / float(height), NEAR_CLIP_DISTANCE,
FAR_CLIP_DISTANCE)
else:
gluPerspective(FOV, 1, NEAR_CLIP_DISTANCE, FAR_CLIP_DISTANCE)
gluPerspective(globals.FOV, width / float(height),
globals.NEAR_CLIP_DISTANCE,
globals.FAR_CLIP_DISTANCE)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
self.camera.transform()
Expand Down
Loading

0 comments on commit cc1de58

Please sign in to comment.