diff options
Diffstat (limited to 'ppgame.py')
-rwxr-xr-x | ppgame.py | 352 |
1 files changed, 352 insertions, 0 deletions
diff --git a/ppgame.py b/ppgame.py new file mode 100755 index 0000000..cc27c25 --- /dev/null +++ b/ppgame.py @@ -0,0 +1,352 @@ +#!/usr/bin/env python +# +# 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 St, Fifth Floor, Boston, MA 02110-1301 USA + +# Import section +import gtk +import pygame +import random + +# Start game music! +pygame.init() + +#end mainGame function +number_unknowns = 3 + +# scrolly bars for our little kiddies to choose their +# answers from +scales = [] + +# Dictionary that contains the variables as keys, mapped to the random numbers +variable_dictionary = {} +used_variables = [] + +# Game class (VBox to be drawn) +class ProducePuzzleGame(gtk.VBox): + + # This is a callback function, which we later will connect + # to the clicked signal of the button, which will be triggered + # when the user clicks the button with a mouse. + def on_button_clicked(self, button): + for i in range(0, number_unknowns): + if(scales[i].get_value() != variable_dictionary[used_variables[i]]): + doh_sound = pygame.mixer.Sound('sounds/doh.wav') + doh_sound.play() + break + else: + if (i == number_unknowns - 1): + clapping = pygame.mixer.Sound('sounds/clapping.wav') + clapping.play() + + # Gets us up and running! + def __init__(self, *args, **kwargs): + super(gtk.VBox, self).__init__(*args, **kwargs) + sound = pygame.mixer.music.load('sounds/background_music.wav') + pygame.mixer.music.play(100, 0) + + # A label buffer + def addBuffer(self): + buffer = gtk.Label(" ") + return buffer + + # Adding images of the fruits + def addApple(self): + imgApple = gtk.Image() + imgApple.set_from_file("graphics/apple.png") + return imgApple + + def addOrange(self): + imgOrange = gtk.Image() + imgOrange.set_from_file("graphics/orange.png") + return imgOrange + + def addBanana(self): + imgBanana = gtk.Image() + imgBanana.set_from_file("graphics/bananas.png") + return imgBanana + + def addPlum(self): + imgPlum = gtk.Image() + imgPlum.set_from_file("graphics/plum.png") + return imgPlum + + def addPear(self): + imgPear = gtk.Image() + imgPear.set_from_file("graphics/pear.png") + return imgPear + + def addMango(self): + imgMango = gtk.Image() + imgMango.set_from_file("graphics/mango.png") + return imgMango + + def addStrawberry(self): + imgStrawberry = gtk.Image() + imgStrawberry.set_from_file("graphics/strawberry.png") + return imgStrawberry + + def addKiwi(self): + imgKiwi = gtk.Image() + imgKiwi.set_from_file("graphics/kiwi.png") + return imgKiwi + + def addWatermelon(self): + imgWatermelon = gtk.Image() + imgWatermelon.set_from_file("graphics/watermelon.png") + return imgWatermelon + + # Main game function + def mainGame(self, i): + + # Lower and upper bound of the random generator. + self.lower_bound = 1 + self.upper_bound = 20 + + # Number of unknowns. + self.number_unknowns = i + + # Dictionary of variable names. + self.alphabet_dictionary = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', + 5:'f', 6:'g', 7:'h', 8:'i'} + + # Declare arrays to create grid array. + self.grid = [] + self.row = [] + + # Declare row and column sum arrays. + self.row_sum = [] + self.column_sum = [] + + # Declare array holding algebra game grid. + for i in range(0, number_unknowns): + for j in range(0, number_unknowns): + self.row.append(0) + self.grid.append(self.row) + self.row = [] + + # Clear out variable_dictionary and used_variables + while (used_variables): + used_variables.pop() + + while (variable_dictionary): + variable_dictionary.clear() + + # Load variable dictionary with a variable name as the key, mapped to a random integer. + for i in range(0, number_unknowns): + done = False + while not done: + done = True + random_variable = self.alphabet_dictionary[random.randint(0, len(self.alphabet_dictionary) - 1)] + if variable_dictionary.has_key(random_variable): + done = False + + done = False + while not done: + done = True + random_number = random.randint(self.lower_bound, self.upper_bound) + for value in variable_dictionary.itervalues(): + if value == random_number: + done = False + variable_dictionary[random_variable] = random_number + used_variables.append(random_variable) + used_variables.sort() + + # Load the algebra grid with random variables + for i in range(0, number_unknowns): + for j in range(0, number_unknowns): + random_select = random.randint(0, number_unknowns - 1) + self.grid[i][j] = used_variables[random_select] + + # Get row sums. + for i in range(0, number_unknowns): + temp_sum = 0 + for j in range(0, number_unknowns): + temp_sum = temp_sum + variable_dictionary[self.grid[i][j]] + self.row_sum.append(temp_sum) + + # Get column sums. + for i in range(0, number_unknowns): + temp_sum = 0 + for j in range(0, number_unknowns): + temp_sum = temp_sum + variable_dictionary[self.grid[j][i]] + self.column_sum.append(temp_sum) + # base game end + + # DEBUG + print "grid " + str(self.grid) + print "variable dictionary " + str(variable_dictionary) + print "row sum " + str(self.row_sum) + print "column sum " + str(self.column_sum) + + + # Create a new window + window = gtk.Window() + + def setNum(i): + window.destroy() + mainGame(i) + + # Sets the border width of the window. length depends + # on what's in the pack box + window.set_border_width(0) + + # Creates the button to check answers + button1 = gtk.Button("Check!") + + # When the button receives the "clicked" signal, it will call the + # function on_button_clicked() defined above. + button1.connect("clicked", self.on_button_clicked) + + # Creates labels with the sums + self.sumrows = [] + self.sumcols = [] + + for i in range(0, number_unknowns): + self.sumrows.append(gtk.Label(str(self.row_sum[i]))) + + for i in range(0, number_unknowns): + self.sumcols.append(gtk.Label(str(self.column_sum[i]))) + self.sumcols[i].set_width_chars(4) + + + # value, lower, upper, step_increment, page_increment, page_size + # initializes the scales to choose answers from + # first clear out the old list, so each scale[i] is a new widget + while (scales): + scales.pop() + + for i in range(0, number_unknowns): + adj1 = gtk.Adjustment(0.0, 1.0, 20.0, 1.0, 1.0, 0.0) + temp = gtk.HScale(adj1) + scales.append(temp) + scales[i].set_digits(0) + scales[i].set_draw_value(True) + scales[i].set_usize(75, 60) + + + # creates pack boxes, the formatting for the window + # note from elizabeth!! you can only add each widget ONCE! + # this means that when we are adding in images more than + # once, we actually have to create separate objects. + # so, if we want 4 apples to appear, we have to create + # 4 different objects that are the same thing! + # and also, we can't just say blah2 = blah and add blah2 + # already tried it and it doesn't work. + vbox1 = gtk.VBox(False, 10) + boxes = [] + + for i in range(0, number_unknowns): + boxes.append(gtk.HBox(False, 20)) + + for i in range(0, number_unknowns): + boxes[i].pack_start(self.addBuffer(), False, False, 0) + + for i in range(0, number_unknowns): + for j in range(0, number_unknowns): + if self.grid[i][j] == 'a': + boxes[i].pack_start(self.addApple(), False, False, 0) + elif self.grid[i][j] == 'b': + boxes[i].pack_start(self.addOrange(), False, False, 0) + elif self.grid[i][j] == 'c': + boxes[i].pack_start(self.addBanana(), False, False, 0) + elif self.grid[i][j] == 'd': + boxes[i].pack_start(self.addPlum(), False, False, 0) + elif self.grid[i][j] == 'e': + boxes[i].pack_start(self.addPear(), False, False, 0) + elif self.grid[i][j] == 'f': + boxes[i].pack_start(self.addMango(), False, False, 0) + elif self.grid[i][j] == 'g': + boxes[i].pack_start(self.addStrawberry(), False, False, 0) + elif self.grid[i][j] == 'h': + boxes[i].pack_start(self.addKiwi(), False, False, 0) + elif self.grid[i][j] == 'i': + boxes[i].pack_start(self.addWatermelon(), False, False, 0) + + # adds the sum of each of the rows + for i in range(0, number_unknowns): + boxes[i].pack_start(self.sumrows[i], False, False, 0) + boxes[i].pack_start(self.addBuffer(), False, False, 0) + + # adds the image hboxes to the grid vbox + for i in range(0, number_unknowns): + vbox1.pack_start(boxes[i], False, False, 0) + + # adds the sum of each of the column + hbox4 = gtk.HBox(False, 20) + hbox4.pack_start(self.addBuffer(), False, False, 0) + for i in range(0, number_unknowns): + hbox4.pack_start(self.sumcols[i], False, False, 0) + + # adds the scales to an hbox + hbox5 = gtk.HBox(False, 0) + hbox5.pack_start(self.addBuffer(), False, False, 5) + for i in range(0, number_unknowns): + hbox5.pack_start(scales[i], False, False, 0) + + # adds the images to an hbox + hbox6 = gtk.HBox(False, 25) + hbox6.pack_start(self.addBuffer(), False, False, 0) + for i in range(0, number_unknowns): + if used_variables[i] == 'a': + hbox6.pack_start(self.addApple(), False, False, 0) + elif used_variables[i] == 'b': + hbox6.pack_start(self.addOrange(), False, False, 0) + elif used_variables[i] == 'c': + hbox6.pack_start(self.addBanana(), False, False, 0) + elif used_variables[i] == 'd': + hbox6.pack_start(self.addPlum(), False, False, 0) + elif used_variables[i] == 'e': + hbox6.pack_start(self.addPear(), False, False, 0) + elif used_variables[i] == 'f': + hbox6.pack_start(self.addMango(), False, False, 0) + elif used_variables[i] == 'g': + hbox6.pack_start(self.addStrawberry(), False, False, 0) + elif used_variables[i] == 'h': + hbox6.pack_start(self.addKiwi(), False, False, 0) + elif used_variables[i] == 'i': + hbox6.pack_start(self.addWatermelon(), False, False, 0) + + # adds the check button + hbox7 = gtk.HBox(False, 0) + hbox7.pack_start(button1, False, False, 20) + + # adds everything to the main vbox + vbox = gtk.VBox(False, 0) + vbox.pack_start(vbox1, False, False, 15) + vbox.pack_start(hbox4, False, False, 15) + vbox.pack_start(hbox5, False, False, 15) + vbox.pack_start(hbox6, False, False, 0) + vbox.pack_start(hbox7, False, False, 20) + + # This adds the box of pack boxes to the window. + # we have one vertical box in the window because + # we are not allowed to have more than 1 widget in + # the window. a widget is pretty much everything... + # image, button, etc. but you can put whatever you + # want and however much you want in a pack box, + # so it's okay. + self.add(vbox) + + # Show the window and the button + self.show_all() + + # Run the main loop, to process events such a key presses + # and mouse movements. + gtk.main() + + pygame.mixer.quit() + + # stop gtk + gtk.main_quit()
\ No newline at end of file |