Ejemplo n.º 1
0
def gameLoop(gameboard):
	if len(gameboard.tiles) == 0: # if the board is empty, i.e. it's a new game...
		gameboard.spawn() #...start the game with one spawn
	running = True #by default the game is 'running' (i.e. has not been quit out of or has crashed)
	while running: #will not stop looping unless broken out of by force using a 'break'/'return' statement or a 'quit' type event has been triggered
		features = drawGameScreen(gameboard) #draw the elements of the game screen and store the properties of them inside an array called 'features'
		for event in pygame.event.get(): #for each event in the current event queue
			if event.type == pygame.QUIT: #if the type of event is a 'quit' (i.e. the red x has been clicked or an alternative quit command is entered)...
				running = False #...then flag this fact so that the loop is broken out of
			elif event.type == pygame.KEYDOWN: #if the event type is a 'key press'...
				if event.key == pygame.K_LEFT: #...and it is the left key that has been pressed...
					if gameboard.tilesLeft(): #...then move the tiles to the left...
						initSpawn(gameboard) #...and decide whether to spawn new tile(s) or to end the game
				elif event.key == pygame.K_RIGHT: #...and it is the right key that has been pressed...
					if gameboard.tilesRight(): #...then move the tiles to the right...
						initSpawn(gameboard) #...and decide whether to spawn new tile(s) or to end the game
				elif event.key == pygame.K_UP: #...and it is the up key that has been pressed...
					if gameboard.tilesUp(): #...then move the tiles up...
						initSpawn(gameboard) #...and decide whether to spawn new tile(s) or to end the game
				elif event.key == pygame.K_DOWN: #...and it is the down that has been pressed...
					if gameboard.tilesDown(): #...then move the tiles dwon...
						initSpawn(gameboard) #...and decide whether to spawn new tile(s) or to end the game
			mouse = pygame.mouse.get_pos() #get the position of the mouse
			if event.type == pygame.MOUSEBUTTONUP: #if the user clicks...
				if features[4].collidepoint(mouse): #...and the position of the mouse is within the bounds of the the 'options' button...
					optionsLoop() #...then switch to the 'options' screen

			drawGameScreen(gameboard) #draw the screen with the updated block positions and score
			pygame.display.update() #and update the screen

			for tile in gameboard.tiles: #for each tile...
				if tile.element % 11 == 0: #...if the tile is the eleventh element (works for every level using moduulus)...
					pygame.time.wait(1000) #...wait 1000ms...
					display.fill(WHITE) #...wipe the screen...
					drawLabel('Level Complete', font(50), BLACK, ((DISPLAY_WIDTH / 2), (DISPLAY_HEIGHT / 2))) #...draw 'level complete' onto the screen...
					pygame.display.update() #...update the screen with this message...
					pygame.time.wait(1000) #...wait another 1000ms...
					if gameboard.level < 11: #...if the level being completed is not the 11th...
						gameboard.level += 1 #...then increment the level...
						gameboard.state = [['Empty' for col in range(5)] for row in range(5)] #...reset the game state to an empty grid...
						gameboard.tiles = [] #...empty the arroy of tiles...
						gameboard.spawn() #...and spawn the first block for that new level
						break #break out of this 'for' loop
					else: #if the player has completed all 11 levels...
						pygame.time.wait(1000) #...wait 1000ms...
						display.fill(WHITE) #...wipe the screen...
						drawLabel('Level Complete', font(50), BLACK, ((DISPLAY_WIDTH / 2), (DISPLAY_HEIGHT / 2))) #...draw 'game complete' onto the screen...
						pygame.display.update() #...update the screen with this message...
						pygame.time.wait(1000) #...wait another 1000ms...
						leaderboardLoop() #...then go to leaderboard screen
				
		saveGameState(gameboard) #save the game state after every iteration of this 'event' loop ('autosave' feature)
		pygame.display.update() #update the screen with any new drawings
		clock.tick(120) #the game should run at a frame rate of 120 frames per second

	pygame.quit() #if the game is not 'running' anymore then quit out of pygame
Ejemplo n.º 2
0
 def display_x_values(self, win, date):
     displaystring = ("%.0f ") % (date, )
     font = drawing.font('6x10')
     green = drawing.colour('green')
     y = self.get_y() - 5
     gtk.draw_text(win, font.get_font(), green.get_colour(), 0, y,
                   displaystring)
Ejemplo n.º 3
0
def usernameSubmitted(newGame, username):
	found = False #be default, assume that the name will not be found within the 'game saves' folder
	for fileName in os.listdir('/home/lewis/Documents/School/Fusion/External Files/Game Saves'): #for each file in the 'game saves' folder
		if fileName == '{}.pickle'.format(username): #if a currently unfinished game is being stored using that username...
			found = True #...then flag this fact
	if found: #if the name was found within the folder 
		if newGame: #...and it's a new game then the user will not be able to pick this name
			drawLabel('This username is currently being used.', font(25), BLACK, (250, 450)) #...so draw a message saying so...
			drawLabel('Please use another one.', font(25), BLACK, (250, 480))
			pygame.display.update() #...display it...
			pygame.time.wait(500) #...for a brief moment...
			drawNameInputScreen(newGame, username) #...and then draw the screen again, covering/hiding the message
		else: #if the user is loading a game and the name exists...
			gameboard = openGameSave(username) #...then reload the gameboard attributes from that file into a new gameboard object...
			gameLoop(gameboard) #...and start the game from the point that user left off
	else: #if the name is not found...
		if newGame: #...and the user is starting a new game
			 gameboard = Gameboard(username = username, state = [['Empty' for col in range(5)] for row in range(5)], level = 1, tiles = [], score = 0) #... then instantiate a new gameboard with the username entered...
			 gameLoop(gameboard) #...and start a new game
		else: #if the user is trying to load a game with a name that does not exist within the 'game saves' folder...
			drawLabel('This username is not currently being used', font(25), BLACK, (250, 450)) #...then draw a message saying so...
			pygame.display.update() #...display it...
			pygame.time.wait(500) #...for a brief moment...
			drawNameInputScreen(newGame, username) #...and then draw the screen again, covering/hiding the message
Ejemplo n.º 4
0
 def display_values(self, win, date, x, y, dataset):
     displaystring = ("%.0f ") % (date, )
     for d in self.drawables.keys():
         dr = self.drawables[d]
         drvalue = dr.get_value_at_x(x, dataset, date)
         # dataseries can have a null value at a given point
         if drvalue == None:
             continue
         for l in drvalue:
             # 0 is the title, 1 is the value.
             displaystring += ("%s:%.2f ") % (l[0], l[1])
     y = self.get_y() - 5
     font = drawing.font('6x10')
     green = drawing.colour('green')
     gtk.draw_text(win, font.get_font(), green.get_colour(), 0, y,
                   displaystring)
     del displaystring
Ejemplo n.º 5
0
def draw(ymax, ymin, scale_x1, x1, y1, height, \
         width, yscale, lowest, \
         scale_area, horizontal_grid_lines, minpix=20):
    interval = _calc_interval(ymax, ymin, height, minpix, yscale)
    drawline = drawing.drawline
    drawtext = drawing.drawtext
    drawpoints = drawing.drawpoints
    grey = drawing.colour('grey')
    drawline(grey, scale_x1+3, y1, scale_x1+3, y1 + height)
    ystart = _cal_ystart(interval, ymin)
    font = drawing.font('6x10')
    y = ystart
    gridpoints = []
    while (y < ymax):
        yv = y1 + height - ((y - lowest) * yscale)
        drawline(grey, scale_x1, int(yv), scale_x1+3, int(yv))
        drawtext(font, grey, scale_x1+7, int(yv)+5,
                 ("%.2f") % float(str(y)))
        if horizontal_grid_lines:
            for x in range(x1, width+1, 10):
                gridpoints.append((x,yv))
        y += interval
    if horizontal_grid_lines:
        drawpoints(grey, gridpoints)
Ejemplo n.º 6
0
 def _set_defaults(self):
     self.font = drawing.font('6x10')
     self.grey = drawing.colour('grey')
Ejemplo n.º 7
0
#    chase 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 chase; see the file COPYING. If not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import YScale
import utils
import drawing
import gtk
import drawable
from Scale import *

defaultFont = drawing.font('6x10')


class Subchart:
    def __init__(self, title, container):
        self.drawables = {}
        self.title = title
        # grid lines are off by default
        self.horizontal_grid_lines = 0
        self.vertical_grid_lines = 0

        self.x = -1
        self.y = -1
        self.width = -1
        self.height = -1
Ejemplo n.º 8
0
def nameInputLoop(newGame):
	running = True #by default the game is 'running' (i.e. has not been quit out of or has crashed)
	username = [] #initialise the username to an empty list of characters
	while running: #will not stop looping unless broken out of by force using a 'break'/'return' statement or a 'quit' type event has been triggered
		features = drawNameInputScreen(newGame, username) #draw the elements of the 'game' screen and store the properties of them inside an array called 'features'
		for event in pygame.event.get(): #for each event in the current event queue
			if event.type == pygame.QUIT: #if the type of event is a 'quit' (i.e. the red x has been clicked or an alternative quit command is entered)...
				running = False #...then flag this fact so that the loop is broken out of
			mouse = pygame.mouse.get_pos() #get the position of the mouse
			if event.type == pygame.MOUSEBUTTONUP: #if the user clicks...
				if features[2].collidepoint(mouse): #...and the position of the mouse is within the bounds of the the input box...
					clickedInBox = True #...then flag this fact...
					while clickedInBox and running: #...and loop whilst they haven't clicked outside the box and haven't quit
						pygame.draw.rect(display, (255, 0, 0), [150, 310, 200, 30], 2) #change the border of the input box to red to indicate that the box has been clicked on
						for event in pygame.event.get(): #for each event in the event queue (an internal event queue loop is needed as we have now entered a secondary while loop)
							mouse = pygame.mouse.get_pos() #variable that holds the current coordinate of the mouse
							if event.type == pygame.QUIT: #if the type of event is a 'quit' (i.e. the red x has been clicked or an alternative quit command is entered)...
								running = False #...then flag this fact so that the loop is broken out of
							elif event.type == pygame.MOUSEBUTTONUP: #if the user clicks...
								if not features[2].collidepoint(mouse): #...outwith the input box...
									if features[3].collidepoint(mouse): #...and within the 'enter' button (or enter key is pressed)...
										if len(list(username)) >= 3: #...and username has the minimum number of characters...
											usernameSubmitted(newGame, username) #...then submit that username
										else: #if the username is not long enough...
											drawLabel('Username needs to be at least 3 characters!', font(25), BLACK, (250, 450)) #...draw a message saying so...
											pygame.display.update() #...display it...
											pygame.time.wait(500) #...for a brief moment...
											drawNameInputScreen(newGame, username) #...and then draw the screen again, covering/hiding the message 
									if features[4].collidepoint(mouse): #if the user clicks outwith the input box and within the 'Back' button then...
										return #...return to main menu
									else: #a click anywhere else and we will...
										clickedInBox = False #...flag this fact...
										break #...and break out of this loop, returning to the 'not clicked in box' loop
							elif event.type == pygame.KEYDOWN: #if the user presses the...
								if event.key == pygame.K_RETURN: #...return key...
									if len(list(username)) >= 3: #...then check username has the minimum number of characters...
										usernameSubmitted(newGame, username) #...then either load a game using that username or start a new one
									else:
										drawLabel('Username needs to be at least 3 characters!', font(25), BLACK, (250, 450)) #...draw a message saying so...
										pygame.display.update() #...display it...
										pygame.time.wait(500) #...for a brief moment...
										drawNameInputScreen(newGame, username) #...and then draw the screen again, covering/hiding the message 
								elif event.key == pygame.K_BACKSPACE: #...backspace...
									if len(username) > 0: #...check that the current username has characters to prevent an 'out of bounds' exception being raised on the username array
										username = list(username) #convert the username into a list...
										del username[-1] #...remove the last char from the list...
										username = ''.join(username) #...join that list back into a string...
										drawNameInputScreen(newGame, username) #...and draw the updated username inside the input box
								elif len(username) < 8: #if the press another key then first check we don't end up putting more than 8 chars into the string
									if re.match('[a-z0-9]', chr(event.key)): #if they press a key between a-z or 0-9 (regex)...
										username = list(username) #...convert the username into a list
										username.append(chr(event.key)) #...add that character to the list...
										username = ''.join(username) #...join that list back into a string
										drawNameInputScreen(newGame, username) #...and draw the updated username inside the input box
									else: #if they press any other key...
										drawLabel('Invalid character!', font(25), BLACK, (250, 450)) #...draw a message saying so...
										pygame.display.update() #...display it...
										pygame.time.wait(500) #...for a brief moment...
										username = ''.join(username) #...join the username list into a string...
										drawNameInputScreen(newGame, username) #...and then draw the screen again, covering/hiding the message
								else: #if they press another key but the string already has 8 chars then...
									drawLabel('Only allowed up to 8 characters!', font(25), BLACK, (250, 450)) #...draw a message saying so...
									pygame.display.update() #...display it...
									pygame.time.wait(500) #...for a brief moment...
									drawNameInputScreen(newGame, username) #...and then draw the screen again, covering/hiding the message 
						pygame.display.update() #update the display with anything that has been drawn (new chars inside the input box)
						clock.tick(120)  #the game should run at a frame rate of 120 frames per second
				elif features[3].collidepoint(mouse) and len(list(username)) >= 3 and len(list(username)) <= 8: #if the start/continue button is clicked and username is valid...
					usernameSubmitted(newGame, username) #...submit it
				elif features[3].collidepoint(mouse) and len(list(username)) < 3: #if the start/continue button is clicked and the username is too short...
					drawLabel('Username needs to be at least 3 characters!', font(25), BLACK, (250, 450)) #...draw a message saying so...
					pygame.display.update() #...display it...
					pygame.time.wait(500) #...for a brief moment...
					drawNameInputScreen(newGame, username) #...and then draw the screen again, covering/hiding the message 
				elif features[3].collidepoint(mouse) and len(list(username)) > 8: #if the start/continue button is clicked and the username is too long...
					drawLabel('Only allowed up to 8 characters!', font(25), BLACK, (250, 450)) #...draw a message saying so...
					pygame.display.update() #...display it...
					pygame.time.wait(500) #...for a brief moment...
					drawNameInputScreen(newGame, username) #...and then draw the screen again, covering/hiding the message 
				elif features[4].collidepoint(mouse): #if the 'back' button is clicked...
					return #...then switch back to the 'main menu' screen by returning nothing and breaking out of the 'name input' loop, back into the 'main menu' loop
		pygame.display.update() #update the screen
		clock.tick(120) #the game should run at a frame rate of 120 frames per second
	pygame.quit() #if the game is not 'running' anymore then quit out of pygame