[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] OcempGUI help neede on container



Hi all,

I busy with a word search game as my first game project and I found OcempGUI to be easy to start with.

I have trouble in understanding how to use the container in my game. It sounds like the right thing to use because it can hold more than one widget.
I have in my game a main playing field where the scrampled letters is supposed to be. On the right of that there is he game control buttons.
At the bottom of the main playing field, is he list of words that is in the puzzle.
I attached my code so that you can see what I am doing.


The next big steps is to put the words in my main playing field and to hide the words. I am not sure what to use to populate the field with the letters.
I thought of a image for each letter and the main field to be about 15 letters down and 15 across.


Any ideas and comments on my code and how I can use OcempGUI to do what I want?

TIA,

Johan
# Imports------------------------------------------------------------
import sys, os
from ocempgui.widgets import *
from ocempgui.widgets.Constants import *
from ocempgui.draw import Draw

#--------------------------------------------------------------------
# Get vents from mouse and keyboard--------------
def _get_type(eventtype):
    if eventtype == SIG_MOUSEDOWN:
        return "SIG_MOUSEDOWN"
    elif eventtype == SIG_MOUSEUP:
        return "SIG_MOUSEUP"
    elif eventtype == SIG_MOUSEMOVE:
        return "SIG_MOUSEMOVE"
    else:
        return "Unknown signal"
    
#--------------------------------------------------------------------
# We must know where the mouse is----------------
def _got_mouseevent(event, imagemap, labels):
    labels[0].text = "Signal: %s" % _get_type (imagemap.last_event.signal)
    if imagemap.last_event.signal != SIG_MOUSEMOVE:
        labels[1].text = "Button: %d" % imagemap.last_event.data.button
    else:
        labels[1].text = "Button: None"
    labels[2].text = "Event pos: %s" % str (imagemap.last_event.data.pos)
    labels[3].text = "Rel. pos: %s" % str (imagemap.relative_position)
    
#--------------------------------------------------------------------
# Ceate a frame----------------------------------
def _create_game_frame(text):
    frame = VFrame(Label(text))
    frame.spacing = 5
    frame.align = ALIGN_LEFT
    return frame
    
#--------------------------------------------------------------------
# Create stuff inside the frame-------------------
def create_game_map_view():
    frm_map = _create_game_frame("PLAY HERE")
    
    imagemap = ImageMap("game_field.png")     #use this to change the background of the play area
    lbl_desc = Label("FIND THE HIDDEN WORDS")
    lbl_desc.multiline = False
    
    #--------------------------------------------------
    # Use the next section just to see where the mouse is and what button is clicked, will not be here
    # when game is played. 
    # We need to know in the game where a selection was made by mouse clicked.
    lbl_results = [Label("Signal:"), Label("Button:"), Label("Event pos:"),
                   Label("Rel. pos:")]
    for label in lbl_results:
        print "Line 91 label: ", label
        label.get_style()["fgcolor"][STATE_NORMAL] = (255, 0, 0)
        
    #--------------------------------------------------
    imagemap.connect_signal(SIG_MOUSEDOWN, _got_mouseevent, imagemap,
                             lbl_results)
    imagemap.connect_signal(SIG_MOUSEMOVE, _got_mouseevent, imagemap,
                             lbl_results)
    imagemap.connect_signal(SIG_MOUSEUP, _got_mouseevent, imagemap,
                             lbl_results)
    
    frm_map.add_child(imagemap, lbl_desc, *lbl_results)
    return frm_map
    
#--------------------------------------------------------------------
# Button clicked to exit----------------------------------
def btn5_clicked():
    sys.exit(1)
    
#--------------------------------------------------------------------
# Ceate a frame----------------------------------
def _create_control_frame(text):
    frame = VFrame(Label(text))
    frame.spacing = 20
    frame.align = ALIGN_LEFT
    return frame
    
#--------------------------------------------------------------------
# Create stuff inside the frame-------------------
def create_control_view():
    
    states = ("#NEW_GAME", "#SAVE_GAMEL", "#LOAD_GAME", 
             "#END_GAME", "#QUIT_GAME")
              
    table = Table(1, 1) # Rows, Columns
    
    table.position = 500, 5
    frm_states = _create_control_frame("CONTROLS")
    
    btn1 = Button(states[0])
    btn1.state = STATE_TYPES[0]
    frm_states.add_child(btn1)
    
    btn2 = Button(states[1])
    btn2.state = STATE_TYPES[0]
    frm_states.add_child(btn2)
    
    btn3 = Button(states[2])
    btn3.state = STATE_TYPES[0]
    frm_states.add_child(btn3)
    
    btn4 = Button(states[3])
    btn4.state = STATE_TYPES[0]
    frm_states.add_child(btn4)
    
    btn5 = Button(states[4])
    btn5.state = STATE_TYPES[0]
    btn5.connect_signal (SIG_CLICKED, btn5_clicked)
    frm_states.add_child(btn5)
    
    entry = Entry("PLAYER NAME")  # This needs to go into a list or file to retrive it next time the game is started.
    entry.state = STATE_TYPES[0]
    frm_states.add_child(entry)
    
    table.add_child(0, 0, frm_states)
    table.set_align(0, 0, ALIGN_TOP)
    
 #   frm_map.add_child(imagemap, lbl_desc, *lbl_results)
    return table
    
#--------------------------------------------------------------------
# Ceate words frame------------------------------
def _create_word_frame(text):
    frame = VFrame(Label(text))
    frame.spacing = 5
    frame.align = ALIGN_LEFT
    return frame
    
#--------------------------------------------------------------------
# Create content of words frame------------------
def create_word_view():
    
    table = Table(1, 1) # Rows, Columns
    table.position = 5, 575
    table.spacing = 2
    frm_words = _create_word_frame("WORD LIST")
    
    words = ("CRICKET    BARBEQUE    KITTY    LADDER",
             "PLAYERS      HOUSE   BICYCLE    DOGBOX")
              
    
    for i in range(2):
        lbl = Label(words[i])
        frm_words.add_child(lbl)
    table.add_child(0, 0, frm_words)
    
    return table
    

#--------------------------------------------------------------------

if __name__ == '__main__':
   
   # Initialize the drawing window
   re = Renderer()
   re.create_screen(650, 650)
   re.title = "Word Search Game"
   re.color = (234, 228, 223)
   
   re.add_widget(create_game_map_view())
   re.add_widget(create_control_view())
   re.add_widget(create_word_view())
   
   # Start the main rendering loop.
   re.start ()