Package pyzzle
[hide private]
[frames] | no frames]

Source Code for Package pyzzle

  1  import pygame, sys, sqlite3 
  2   
  3  import pyzzle 
  4  import DB 
  5  from Slide import Slide 
  6  from Hotspot import Hotspot 
  7  from Switch import Switch 
  8  from Item import Item 
  9  from Panel import Panel 
 10  from Text import Text 
 11  from standard import * 
 12  import os 
 13   
 14  panel=Panel() 
 15  cursor=pygame.sprite.Sprite() 
 16  cursor.rect=Rect(0,0,0,0) 
 17  cursor.image=pygame.Surface((0,0)) 
 18  screen=None 
 19  framerate=30 
 20  datafile=None 
 21  globals=DB.Table('globals', (object,), {}) 
 22  stages=DB.Table('stages', (object,), {}) 
 23  design=False 
 24  zip=True 
 25  menu=sys.exit 
 26  history=[] 
 27   
28 -def init(screensize=(800,600), name='Pyzzle', iconfile=None, fullscreen=False):
29 """Initializes the screen. Must call before anything else. 30 @type screensize: (int,int) 31 @param screensize: Dimensions of the screen. 32 @type name: string 33 @param name: Text to display in the window's title bar 34 @param iconfile: name of the image file to display as the window's icon 35 """ 36 pygame.init() 37 pyzzle.screen=pygame.display.set_mode(screensize, 38 pygame.FULLSCREEN if fullscreen else 0) 39 pyzzle.panel.rect=pyzzle.screen.get_rect() 40 if iconfile: 41 icon=pygame.image.load(iconfile).convert_alpha() 42 pygame.display.set_icon(icon) 43 pygame.mouse.set_visible(False) 44 pygame.display.set_caption(name)
45 -def load(datafilename):
46 """Loads game data from an SQLite database file. 47 @param datafilename: name of the SQLite database file to load 48 """ 49 pyzzle.datafile=DB.DB(datafilename) 50 51 stages.rows=datafile.load(DB.Row,'Stage') 52 datafile.load(Slide) 53 for slide in Slide: slide._loadRefs() 54 datafile.load(Hotspot) 55 datafile.load(Item) 56 datafile.load(Switch)
57
58 -def save(datafilename=None):
59 """Saves game data to an SQLite database file. 60 @param datafilename: name of the SQLite database file to load, 61 or the currently loaded file, if none specified 62 """ 63 if datafilename: 64 pyzzle.datafile=DB.DB(datafilename) 65 datafile.save(Slide) 66 datafile.save(Hotspot)
67
68 -def cleanup():
69 """Corrects capitalization of any image files mentioned in 70 the loaded SQLite database file. 71 Useful to prepare for distribution using dynamically downloaded content, 72 but takes a while to run.""" 73 for stage in stages: 74 directory=os.path.join('pictures',stage.folder) 75 print directory 76 for file in os.listdir(directory): 77 print file 78 datafile.query('update Slide set image = ? where lower(image)=lower(?)', 79 (file,file))
80 -def play():
81 """The main game loop. Call this function only after you have finished 82 scripting and other initialization.""" 83 clock = pygame.time.Clock() 84 while True: 85 #update/draw the game 86 clock.tick(150) 87 pyzzle.framerate=clock.get_fps() 88 89 pyzzle.beginDraw() 90 pyzzle.drawCursor(pyzzle.panel.highlight()) 91 pyzzle.endDraw() 92 93 #process user input - MUST COME AFTER DRAW 94 #(cursor pos needs to be set when calling pyzzle.panel.click()) 95 for event in pygame.event.get(): 96 if event.type == QUIT: 97 pyzzle.datafile.close() 98 sys.exit() 99 if event.type == KEYDOWN: 100 if event.key == K_ESCAPE: 101 pyzzle.menu() 102 if pyzzle.design and pygame.key.get_mods() & KMOD_CTRL: 103 if event.key == K_z: 104 if pyzzle.history: 105 oldslide, newslide=pyzzle.history.pop(-1) 106 if oldslide: 107 pyzzle.transition(newslide, oldslide, 0) 108 pyzzle.history=pyzzle.history[0:-1] 109 elif event.key==K_g: 110 slidename = pyzzle.promptText('Enter slide to jump to:') 111 if slidename in Slide.rows: 112 pyzzle.panel.sprites.empty() 113 pyzzle.panel.add(Slide[slidename]) 114 elif event.key==K_s: 115 pyzzle.save() 116 pyzzle.prompt('Game saved') 117 if event.type == MOUSEBUTTONDOWN: 118 pyzzle.panel.click(design=(pygame.mouse.get_pressed()[2] and pyzzle.design))
119